Merge branch 'master' into blender2.8
[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_full.cmake"
63     cmake_cuda_config_file = None
64
65     # Set build options.
66     cmake_options = []
67     cmake_extra_options = ['-DCMAKE_BUILD_TYPE:STRING=Release']
68     cuda_cmake_options = []
69
70     if builder.startswith('mac'):
71         # Set up OSX architecture
72         if builder.endswith('x86_64_10_9_cmake'):
73             cmake_extra_options.append('-DCMAKE_OSX_ARCHITECTURES:STRING=x86_64')
74         cmake_extra_options.append('-DCMAKE_OSX_DEPLOYMENT_TARGET=10.9')
75         # Used to trick CUDFA to see CLang as an older version.
76         # cmake_extra_options.append('-DCUDA_HOST_COMPILER=/usr/local/cuda-hack/clang')
77         # cmake_extra_options.append('-DCUDA_NVCC_EXECUTABLE=/usr/local/cuda-hack/nvcc')
78
79     elif builder.startswith('win'):
80         if builder.endswith('_vs2017'):
81             if builder.startswith('win64'):
82                 cmake_options.extend(['-G', 'Visual Studio 15 2017 Win64'])
83             elif builder.startswith('win32'):
84                 bits = 32
85                 cmake_options.extend(['-G', 'Visual Studio 15 2017'])
86         elif builder.endswith('_vc2015'):
87             if builder.startswith('win64'):
88                 cmake_options.extend(['-G', 'Visual Studio 14 2015 Win64'])
89             elif builder.startswith('win32'):
90                 bits = 32
91                 cmake_options.extend(['-G', 'Visual Studio 14 2015'])
92             cmake_extra_options.append('-DCUDA_NVCC_FLAGS=--cl-version;2013;' +
93                 '--compiler-bindir;C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin')
94         else:
95             if builder.startswith('win64'):
96                 cmake_options.extend(['-G', 'Visual Studio 12 2013 Win64'])
97             elif builder.startswith('win32'):
98                 bits = 32
99                 cmake_options.extend(['-G', 'Visual Studio 12 2013'])
100
101     elif builder.startswith('linux'):
102         tokens = builder.split("_")
103         glibc = tokens[1]
104         if glibc == 'glibc224':
105             deb_name = "stretch"
106         elif glibc == 'glibc219':
107             deb_name = "jessie"
108         elif glibc == 'glibc211':
109             deb_name = "squeeze"
110         cmake_config_file = "build_files/buildbot/config/blender_linux.cmake"
111         if builder.endswith('x86_64_cmake'):
112             chroot_name = 'buildbot_' + deb_name + '_x86_64'
113             targets = ['blender']
114         elif builder.endswith('i686_cmake'):
115             bits = 32
116             chroot_name = 'buildbot_' + deb_name + '_i686'
117             targets = ['blender']
118         if deb_name != "stretch":
119             cmake_extra_options.extend(["-DCMAKE_C_COMPILER=/usr/bin/gcc-7",
120                                         "-DCMAKE_CXX_COMPILER=/usr/bin/g++-7"])
121
122     cmake_options.append("-C" + os.path.join(blender_dir, cmake_config_file))
123
124     # Prepare CMake options needed to configure cuda binaries compilation, 64bit only.
125     if bits == 64:
126         cuda_cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=%s" % ('ON' if build_cubins else 'OFF'))
127         cuda_cmake_options.append("-DCYCLES_CUDA_BINARIES_ARCH=sm_30;sm_35;sm_37;sm_50;sm_52;sm_60;sm_61;sm_70")
128         if build_cubins or 'cuda' in targets:
129             cuda_cmake_options.append("-DCUDA_64_BIT_DEVICE_CODE=ON")
130
131         # Only modify common cmake options if cuda doesn't require separate target.
132         if 'cuda' not in targets:
133             cmake_options += cuda_cmake_options
134     else:
135         cuda_cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=OFF")
136
137     cmake_options.append("-DCMAKE_INSTALL_PREFIX=%s" % (install_dir))
138
139     cmake_options += cmake_extra_options
140
141     # Prepare chroot command prefix if needed
142     if chroot_name:
143         chroot_prefix = ['schroot', '-c', chroot_name, '--']
144     else:
145         chroot_prefix = []
146     if cuda_chroot_name:
147         cuda_chroot_prefix = ['schroot', '-c', cuda_chroot_name, '--']
148     else:
149         cuda_chroot_prefix = chroot_prefix[:]
150
151     # Make sure no garbage remained from the previous run
152     if os.path.isdir(install_dir):
153         shutil.rmtree(install_dir)
154
155     for target in targets:
156         print("Building target %s" % (target))
157         # Construct build directory name based on the target
158         target_build_dir = build_dir
159         target_chroot_prefix = chroot_prefix[:]
160         if target != 'blender':
161             target_build_dir += '_' + target
162         target_name = 'install'
163         # Tweaking CMake options to respect the target
164         target_cmake_options = cmake_options[:]
165         if target == 'cuda':
166             target_cmake_options += cuda_cmake_options
167             target_chroot_prefix = cuda_chroot_prefix[:]
168             target_name = 'cycles_kernel_cuda'
169         # If cuda binaries are compiled as a separate target, make sure
170         # other targets don't compile cuda binaries.
171         if 'cuda' in targets and target != 'cuda':
172             target_cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=OFF")
173         # Do extra git fetch because not all platform/git/buildbot combinations
174         # update the origin remote, causing buildinfo to detect local changes.
175         os.chdir(blender_dir)
176         print("Fetching remotes")
177         command = ['git', 'fetch', '--all']
178         print(command)
179         retcode = subprocess.call(target_chroot_prefix + command)
180         if retcode != 0:
181             sys.exit(retcode)
182         # Make sure build directory exists and enter it
183         if not os.path.isdir(target_build_dir):
184             os.mkdir(target_build_dir)
185         os.chdir(target_build_dir)
186         # Configure the build
187         print("CMake options:")
188         print(target_cmake_options)
189         if os.path.exists('CMakeCache.txt'):
190             print("Removing CMake cache")
191             os.remove('CMakeCache.txt')
192         retcode = subprocess.call(target_chroot_prefix + ['cmake', blender_dir] + target_cmake_options)
193         if retcode != 0:
194             print('Configuration FAILED!')
195             sys.exit(retcode)
196
197         if 'win32' in builder or 'win64' in builder:
198             command = ['cmake', '--build', '.', '--target', target_name, '--config', 'Release']
199         else:
200             command = ['make', '-s', '-j2', target_name]
201
202         print("Executing command:")
203         print(command)
204         retcode = subprocess.call(target_chroot_prefix + command)
205
206         if retcode != 0:
207             sys.exit(retcode)
208
209         if builder.startswith('linux') and target == 'cuda':
210             blender_h = os.path.join(blender_dir, "source", "blender", "blenkernel", "BKE_blender_version.h")
211             blender_version = int(parse_header_file(blender_h, 'BLENDER_VERSION'))
212             blender_version = "%d.%d" % (blender_version // 100, blender_version % 100)
213             kernels = os.path.join(target_build_dir, 'intern', 'cycles', 'kernel')
214             install_kernels = os.path.join(install_dir, blender_version, 'scripts', 'addons', 'cycles', 'lib')
215             os.mkdir(install_kernels)
216             print("Copying cuda binaries from %s to %s" % (kernels, install_kernels))
217             os.system('cp %s/*.cubin %s' % (kernels, install_kernels))
218
219 else:
220     print("Unknown building system")
221     sys.exit(1)