Cleanup: remove accidentally committed debug code.
[blender.git] / build_files / buildbot / slave_compile.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20
21 import os
22 import subprocess
23 import sys
24 import shutil
25
26 # get builder name
27 if len(sys.argv) < 2:
28     sys.stderr.write("Not enough arguments, expecting builder name\n")
29     sys.exit(1)
30
31 builder = sys.argv[1]
32
33 # we run from build/ directory
34 blender_dir = os.path.join('..', 'blender.git')
35
36
37 def parse_header_file(filename, define):
38     import re
39     regex = re.compile("^#\s*define\s+%s\s+(.*)" % define)
40     with open(filename, "r") as file:
41         for l in file:
42             match = regex.match(l)
43             if match:
44                 return match.group(1)
45     return None
46
47 if 'cmake' in builder:
48     # cmake
49
50     # Some fine-tuning configuration
51     blender_dir = os.path.abspath(blender_dir)
52     build_dir = os.path.abspath(os.path.join('..', 'build', builder))
53     install_dir = os.path.abspath(os.path.join('..', 'install', builder))
54     targets = ['blender']
55
56     chroot_name = None  # If not None command will be delegated to that chroot
57     cuda_chroot_name = None  # If not None cuda compilationcommand will be delegated to that chroot
58     build_cubins = True  # Whether to build Cycles CUDA kernels
59     bits = 64
60
61     # Config file to be used (relative to blender's sources root)
62     cmake_config_file = "build_files/cmake/config/blender_release.cmake"
63     cmake_player_config_file = None
64     cmake_cuda_config_file = None
65
66     # Set build options.
67     cmake_options = []
68     cmake_extra_options = ['-DCMAKE_BUILD_TYPE:STRING=Release']
69     cuda_cmake_options = []
70
71     if builder.startswith('mac'):
72         # Set up OSX architecture
73         if builder.endswith('x86_64_10_9_cmake'):
74             cmake_extra_options.append('-DCMAKE_OSX_ARCHITECTURES:STRING=x86_64')
75         cmake_extra_options.append('-DCMAKE_OSX_DEPLOYMENT_TARGET=10.9')
76
77     elif builder.startswith('win'):
78         if builder.endswith('_vs2017'):
79             if builder.startswith('win64'):
80                 cmake_options.extend(['-G', 'Visual Studio 15 2017 Win64'])
81             elif builder.startswith('win32'):
82                 bits = 32
83                 cmake_options.extend(['-G', 'Visual Studio 15 2017'])
84         elif builder.endswith('_vc2015'):
85             if builder.startswith('win64'):
86                 cmake_options.extend(['-G', 'Visual Studio 14 2015 Win64'])
87             elif builder.startswith('win32'):
88                 bits = 32
89                 cmake_options.extend(['-G', 'Visual Studio 14 2015'])
90             cmake_extra_options.append('-DCUDA_NVCC_FLAGS=--cl-version;2013;' +
91                 '--compiler-bindir;C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin')
92         else:
93             if builder.startswith('win64'):
94                 cmake_options.extend(['-G', 'Visual Studio 12 2013 Win64'])
95             elif builder.startswith('win32'):
96                 bits = 32
97                 cmake_options.extend(['-G', 'Visual Studio 12 2013'])
98
99     elif builder.startswith('linux'):
100         tokens = builder.split("_")
101         glibc = tokens[1]
102         if glibc == 'glibc224':
103             deb_name = "stretch"
104         elif glibc == 'glibc219':
105             deb_name = "jessie"
106         elif glibc == 'glibc211':
107             deb_name = "squeeze"
108         cmake_config_file = "build_files/buildbot/config/blender_linux.cmake"
109         cmake_player_config_file = "build_files/buildbot/config/blender_linux_player.cmake"
110         if builder.endswith('x86_64_cmake'):
111             chroot_name = 'buildbot_' + deb_name + '_x86_64'
112             targets = ['player', 'blender']
113         elif builder.endswith('i686_cmake'):
114             bits = 32
115             chroot_name = 'buildbot_' + deb_name + '_i686'
116             targets = ['player', 'blender']
117         if deb_name != "stretch":
118             cmake_extra_options.extend(["-DCMAKE_C_COMPILER=/usr/bin/gcc-7",
119                                         "-DCMAKE_CXX_COMPILER=/usr/bin/g++-7"])
120
121     # Workaround to build only sm_7x kernels with CUDA 10, until
122     # older kernels work well with this version.
123     if builder.startswith('win'):
124         cmake_extra_options.append('-DCUDA_VERSION=9.1')
125         cmake_extra_options.append('-DCUDA_TOOLKIT_INCLUDE:PATH=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.1/include')
126         cmake_extra_options.append('-DCUDA_TOOLKIT_ROOT_DIR:PATH=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.1')
127         cmake_extra_options.append('-DCUDA_NVCC_EXECUTABLE:FILEPATH=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.1/bin/nvcc.exe')
128         cmake_extra_options.append('-DCUDA10_NVCC_EXECUTABLE:FILEPATH=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0/bin/nvcc.exe')
129         cmake_extra_options.append('-DCUDA10_TOOLKIT_ROOT_DIR:PATH=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0')
130     elif builder.startswith('linux'):
131         cmake_extra_options.append('-DCUDA_VERSION=9.1')
132         cmake_extra_options.append('-DCUDA_TOOLKIT_INCLDUE:PATH=/usr/local/cuda-9.1/include')
133         cmake_extra_options.append('-DCUDA_TOOLKIT_ROOT_DIR:PATH=/usr/local/cuda-9.1')
134         cmake_extra_options.append('-DCUDA_NVCC_EXECUTABLE:FILEPATH=/usr/local/cuda-9.1/bin/nvcc')
135         cmake_extra_options.append('-DCUDA10_NVCC_EXECUTABLE:FILEPATH=/usr/local/cuda-10.0/bin/nvcc')
136         cmake_extra_options.append('-DCUDA10_TOOLKIT_ROOT_DIR:PATH=/usr/local/cuda-10.0')
137
138     cmake_options.append("-C" + os.path.join(blender_dir, cmake_config_file))
139
140     # Prepare CMake options needed to configure cuda binaries compilation, 64bit only.
141     if bits == 64:
142         cuda_cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=%s" % ('ON' if build_cubins else 'OFF'))
143         if build_cubins or 'cuda' in targets:
144             cuda_cmake_options.append("-DCUDA_64_BIT_DEVICE_CODE=ON")
145
146         # Only modify common cmake options if cuda doesn't require separate target.
147         if 'cuda' not in targets:
148             cmake_options += cuda_cmake_options
149     else:
150         cuda_cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=OFF")
151
152     cmake_options.append("-DCMAKE_INSTALL_PREFIX=%s" % (install_dir))
153
154     cmake_options += cmake_extra_options
155
156     # Prepare chroot command prefix if needed
157     if chroot_name:
158         chroot_prefix = ['schroot', '-c', chroot_name, '--']
159     else:
160         chroot_prefix = []
161     if cuda_chroot_name:
162         cuda_chroot_prefix = ['schroot', '-c', cuda_chroot_name, '--']
163     else:
164         cuda_chroot_prefix = chroot_prefix[:]
165
166     # Make sure no garbage remained from the previous run
167     if os.path.isdir(install_dir):
168         shutil.rmtree(install_dir)
169
170     for target in targets:
171         print("Building target %s" % (target))
172         # Construct build directory name based on the target
173         target_build_dir = build_dir
174         target_chroot_prefix = chroot_prefix[:]
175         if target != 'blender':
176             target_build_dir += '_' + target
177         target_name = 'install'
178         # Tweaking CMake options to respect the target
179         target_cmake_options = cmake_options[:]
180         if target == 'player':
181             target_cmake_options.append("-C" + os.path.join(blender_dir, cmake_player_config_file))
182         elif target == 'cuda':
183             target_cmake_options += cuda_cmake_options
184             target_chroot_prefix = cuda_chroot_prefix[:]
185             target_name = 'cycles_kernel_cuda'
186         # If cuda binaries are compiled as a separate target, make sure
187         # other targets don't compile cuda binaries.
188         if 'cuda' in targets and target != 'cuda':
189             target_cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=OFF")
190         # Do extra git fetch because not all platform/git/buildbot combinations
191         # update the origin remote, causing buildinfo to detect local changes.
192         os.chdir(blender_dir)
193         print("Fetching remotes")
194         command = ['git', 'fetch', '--all']
195         print(command)
196         retcode = subprocess.call(target_chroot_prefix + command)
197         if retcode != 0:
198             sys.exit(retcode)
199         # Make sure build directory exists and enter it
200         if not os.path.isdir(target_build_dir):
201             os.mkdir(target_build_dir)
202         os.chdir(target_build_dir)
203         # Configure the build
204         print("CMake options:")
205         print(target_cmake_options)
206         if os.path.exists('CMakeCache.txt'):
207             print("Removing CMake cache")
208             os.remove('CMakeCache.txt')
209         retcode = subprocess.call(target_chroot_prefix + ['cmake', blender_dir] + target_cmake_options)
210         if retcode != 0:
211             print('Configuration FAILED!')
212             sys.exit(retcode)
213
214         if 'win32' in builder or 'win64' in builder:
215             command = ['cmake', '--build', '.', '--target', target_name, '--config', 'Release']
216         else:
217             command = ['make', '-s', '-j2', target_name]
218
219         print("Executing command:")
220         print(command)
221         retcode = subprocess.call(target_chroot_prefix + command)
222
223         if retcode != 0:
224             sys.exit(retcode)
225
226         if builder.startswith('linux') and target == 'cuda':
227             blender_h = os.path.join(blender_dir, "source", "blender", "blenkernel", "BKE_blender_version.h")
228             blender_version = int(parse_header_file(blender_h, 'BLENDER_VERSION'))
229             blender_version = "%d.%d" % (blender_version // 100, blender_version % 100)
230             kernels = os.path.join(target_build_dir, 'intern', 'cycles', 'kernel')
231             install_kernels = os.path.join(install_dir, blender_version, 'scripts', 'addons', 'cycles', 'lib')
232             os.mkdir(install_kernels)
233             print("Copying cuda binaries from %s to %s" % (kernels, install_kernels))
234             os.system('cp %s/*.cubin %s' % (kernels, install_kernels))
235
236 else:
237     print("Unknown building system")
238     sys.exit(1)