744a6a8b3a7b2ae469688056f3dc3f2aa9b48265
[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     remove_cache = False  # Remove CMake cache to be sure config is totally up-to-date
60     remove_install_dir = False  # Remove installation folder before building
61     bits = 64
62
63     # Config file to be used (relative to blender's sources root)
64     cmake_config_file = "build_files/cmake/config/blender_full.cmake"
65     cmake_player_config_file = None
66     cmake_cuda_config_file = None
67
68     # Set build options.
69     cmake_options = []
70     cmake_extra_options = ['-DCMAKE_BUILD_TYPE:STRING=Release']
71     cuda_cmake_options = []
72
73     if builder.startswith('mac'):
74         remove_cache = True
75         install_dir = None
76         # Set up OSX architecture
77         if builder.endswith('x86_64_10_6_cmake'):
78             cmake_extra_options.append('-DCMAKE_OSX_ARCHITECTURES:STRING=x86_64')
79
80     elif builder.startswith('win'):
81         install_dir = None
82         if builder.startswith('win64'):
83             cmake_options.append(['-G', '"Visual Studio 12 2013 Win64"'])
84         elif builder.startswith('win32'):
85             bits = 32
86             cmake_options.append(['-G', '"Visual Studio 12 2013"'])
87
88     elif builder.startswith('linux'):
89         remove_cache = True
90         remove_install_dir = True
91         cmake_config_file = "build_files/buildbot/config/blender_linux.cmake"
92         cmake_player_config_file = "build_files/buildbot/config/blender_linux_player.cmake"
93         if builder.endswith('x86_64_cmake'):
94             chroot_name = 'buildbot_squeeze_x86_64'
95             targets = ['player', 'blender']
96         elif builder.endswith('i386_cmake'):
97             bits = 32
98             chroot_name = 'buildbot_squeeze_i686'
99             cuda_chroot_name = 'buildbot_squeeze_x86_64'
100             targets = ['player', 'blender', 'cuda']
101
102     cmake_options.append("-C" + os.path.join(blender_dir, cmake_config_file))
103
104     # Prepare CMake options needed to configure cuda binaries compilation.
105     cuda_cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=%s" % ('ON' if build_cubins else 'OFF'))
106     if build_cubins or 'cuda' in targets:
107         if bits == 32:
108             cuda_cmake_options.append("-DCUDA_64_BIT_DEVICE_CODE=OFF")
109         else:
110             cuda_cmake_options.append("-DCUDA_64_BIT_DEVICE_CODE=ON")
111
112     # Only modify common cmake options if cuda doesn't require separate target.
113     if 'cuda' not in targets:
114         cmake_options += cuda_cmake_options
115
116     if install_dir:
117         cmake_options.append("-DCMAKE_INSTALL_PREFIX=%s" % (install_dir))
118
119     cmake_options += cmake_extra_options
120
121     # Prepare chroot command prefix if needed
122     if chroot_name:
123         chroot_prefix = ['schroot', '-c', chroot_name, '--']
124     else:
125         chroot_prefix = []
126     if cuda_chroot_name:
127         cuda_chroot_prefix = ['schroot', '-c', cuda_chroot_name, '--']
128     else:
129         cuda_chroot_prefix = chroot_prefix[:]
130
131     # Make sure no garbage remained from the previous run
132     # (only do it if builder requested this)
133     if remove_install_dir:
134         if os.path.isdir(install_dir):
135             shutil.rmtree(install_dir)
136
137     for target in targets:
138         print("Building target %s" % (target))
139         # Construct build directory name based on the target
140         target_build_dir = build_dir
141         target_chroot_prefix = chroot_prefix[:]
142         if target != 'blender':
143             target_build_dir += '_' + target
144         target_name = 'install'
145         # Make sure build directory exists and enter it
146         if not os.path.isdir(target_build_dir):
147             os.mkdir(target_build_dir)
148         os.chdir(target_build_dir)
149         # Tweaking CMake options to respect the target
150         target_cmake_options = cmake_options[:]
151         if target == 'player':
152             target_cmake_options.append("-C" + os.path.join(blender_dir, cmake_player_config_file))
153         elif target == 'cuda':
154             target_cmake_options += cuda_cmake_options
155             target_chroot_prefix = cuda_chroot_prefix[:]
156             target_name = 'cycles_kernel_cuda'
157         # If cuda binaries are compiled as a separate target, make sure
158         # other targets don't compile cuda binaries.
159         if 'cuda' in targets and target != 'cuda':
160             target_cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=OFF")
161         # Configure the build
162         print("CMake options:")
163         print(target_cmake_options)
164 #        if remove_cache and os.path.exists('CMakeCache.txt'):
165 #            print("Removing CMake cache")
166 #            os.remove('CMakeCache.txt')
167         retcode = subprocess.call(target_chroot_prefix + ['cmake', blender_dir] + target_cmake_options)
168         if retcode != 0:
169             print('Condifuration FAILED!')
170             sys.exit(retcode)
171
172         if 'win32' in builder:
173             command = ['msbuild', 'INSTALL.vcxproj', '/Property:PlatformToolset=v120_xp', '/p:Configuration=Release']
174         elif 'win64' in builder:
175             command = ['msbuild', 'INSTALL.vcxproj', '/p:Configuration=Release']
176         else:
177             command = target_chroot_prefix + ['make', '-s', '-j2', target_name]
178
179         print("Executing command:")
180         print(command)
181         retcode = subprocess.call(command)
182
183         if retcode != 0:
184             sys.exit(retcode)
185
186         if builder.startswith('linux') and target == 'cuda':
187             blender_h = os.path.join(blender_dir, "source", "blender", "blenkernel", "BKE_blender.h")
188             blender_version = int(parse_header_file(blender_h, 'BLENDER_VERSION'))
189             blender_version = "%d.%d" % (blender_version // 100, blender_version % 100)
190             kernels = os.path.join(target_build_dir, 'intern', 'cycles', 'kernel')
191             install_kernels = os.path.join(install_dir, blender_version, 'scripts', 'addons', 'cycles', 'lib')
192             os.mkdir(install_kernels)
193             print("Copying cuda binaries from %s to %s" % (kernels, install_kernels))
194             os.system('cp %s/*.cubin %s' % (kernels, install_kernels))
195
196 else:
197     print("Unknown building system")
198     sys.exit(1)