c8d69c3864433fd944abb1f39e32d2631c7174a9
[blender-staging.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.join('..', 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_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_6_cmake'):
74             cmake_extra_options.append('-DCMAKE_OSX_ARCHITECTURES:STRING=x86_64')
75         cmake_extra_options.append('-DWITH_CODEC_QUICKTIME=OFF')
76         cmake_extra_options.append('-DCMAKE_OSX_DEPLOYMENT_TARGET=10.6')
77
78
79     elif builder.startswith('win'):
80         if builder.endswith('_vc2015'):
81             if builder.startswith('win64'):
82                 cmake_options.extend(['-G', 'Visual Studio 14 2015 Win64'])
83             elif builder.startswith('win32'):
84                 bits = 32
85                 cmake_options.extend(['-G', 'Visual Studio 14 2015'])
86             cmake_extra_options.append('-DCUDA_NVCC_FLAGS=--cl-version;2013;' +
87                 '--compiler-bindir;C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin')
88         else:
89             if builder.startswith('win64'):
90                 cmake_options.extend(['-G', 'Visual Studio 12 2013 Win64'])
91             elif builder.startswith('win32'):
92                 bits = 32
93                 cmake_options.extend(['-G', 'Visual Studio 12 2013'])
94
95     elif builder.startswith('linux'):
96         tokens = builder.split("_")
97         glibc = tokens[1]
98         if glibc == 'glibc219':
99             deb_name = "jessie"
100         elif glibc == 'glibc211':
101             deb_name = "squeeze"
102         cmake_config_file = "build_files/buildbot/config/blender_linux.cmake"
103         cmake_player_config_file = "build_files/buildbot/config/blender_linux_player.cmake"
104         if builder.endswith('x86_64_cmake'):
105             chroot_name = 'buildbot_' + deb_name + '_x86_64'
106             targets = ['player', 'blender']
107         elif builder.endswith('i686_cmake'):
108             bits = 32
109             chroot_name = 'buildbot_' + deb_name + '_i686'
110             cuda_chroot_name = 'buildbot_' + deb_name + '_x86_64'
111             targets = ['player', 'blender', 'cuda']
112
113     cmake_options.append("-C" + os.path.join(blender_dir, cmake_config_file))
114
115     # Prepare CMake options needed to configure cuda binaries compilation.
116     cuda_cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=%s" % ('ON' if build_cubins else 'OFF'))
117     cuda_cmake_options.append("-DCYCLES_CUDA_BINARIES_ARCH=sm_20;sm_21;sm_30;sm_35;sm_37;sm_50;sm_52;sm_60;sm_61")
118     if build_cubins or 'cuda' in targets:
119         if bits == 32:
120             cuda_cmake_options.append("-DCUDA_64_BIT_DEVICE_CODE=OFF")
121         else:
122             cuda_cmake_options.append("-DCUDA_64_BIT_DEVICE_CODE=ON")
123
124     # Only modify common cmake options if cuda doesn't require separate target.
125     if 'cuda' not in targets:
126         cmake_options += cuda_cmake_options
127
128     cmake_options.append("-DCMAKE_INSTALL_PREFIX=%s" % (install_dir))
129
130     cmake_options += cmake_extra_options
131
132     # Prepare chroot command prefix if needed
133     if chroot_name:
134         chroot_prefix = ['schroot', '-c', chroot_name, '--']
135     else:
136         chroot_prefix = []
137     if cuda_chroot_name:
138         cuda_chroot_prefix = ['schroot', '-c', cuda_chroot_name, '--']
139     else:
140         cuda_chroot_prefix = chroot_prefix[:]
141
142     # Make sure no garbage remained from the previous run
143     if os.path.isdir(install_dir):
144         shutil.rmtree(install_dir)
145
146     for target in targets:
147         print("Building target %s" % (target))
148         # Construct build directory name based on the target
149         target_build_dir = build_dir
150         target_chroot_prefix = chroot_prefix[:]
151         if target != 'blender':
152             target_build_dir += '_' + target
153         target_name = 'install'
154         # Make sure build directory exists and enter it
155         if not os.path.isdir(target_build_dir):
156             os.mkdir(target_build_dir)
157         os.chdir(target_build_dir)
158         # Tweaking CMake options to respect the target
159         target_cmake_options = cmake_options[:]
160         if target == 'player':
161             target_cmake_options.append("-C" + os.path.join(blender_dir, cmake_player_config_file))
162         elif target == 'cuda':
163             target_cmake_options += cuda_cmake_options
164             target_chroot_prefix = cuda_chroot_prefix[:]
165             target_name = 'cycles_kernel_cuda'
166         # If cuda binaries are compiled as a separate target, make sure
167         # other targets don't compile cuda binaries.
168         if 'cuda' in targets and target != 'cuda':
169             target_cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=OFF")
170         # Configure the build
171         print("CMake options:")
172         print(target_cmake_options)
173         if os.path.exists('CMakeCache.txt'):
174             print("Removing CMake cache")
175             os.remove('CMakeCache.txt')
176         retcode = subprocess.call(target_chroot_prefix + ['cmake', blender_dir] + target_cmake_options)
177         if retcode != 0:
178             print('Condifuration FAILED!')
179             sys.exit(retcode)
180
181         if 'win32' in builder or 'win64' in builder:
182             command = ['cmake', '--build', '.', '--target', target_name, '--config', 'Release']
183         else:
184             command = target_chroot_prefix + ['make', '-s', '-j2', target_name]
185
186         print("Executing command:")
187         print(command)
188         retcode = subprocess.call(command)
189
190         if retcode != 0:
191             sys.exit(retcode)
192
193         if builder.startswith('linux') and target == 'cuda':
194             blender_h = os.path.join(blender_dir, "source", "blender", "blenkernel", "BKE_blender_version.h")
195             blender_version = int(parse_header_file(blender_h, 'BLENDER_VERSION'))
196             blender_version = "%d.%d" % (blender_version // 100, blender_version % 100)
197             kernels = os.path.join(target_build_dir, 'intern', 'cycles', 'kernel')
198             install_kernels = os.path.join(install_dir, blender_version, 'scripts', 'addons', 'cycles', 'lib')
199             os.mkdir(install_kernels)
200             print("Copying cuda binaries from %s to %s" % (kernels, install_kernels))
201             os.system('cp %s/*.cubin %s' % (kernels, install_kernels))
202
203 else:
204     print("Unknown building system")
205     sys.exit(1)