Merge branch 'blender2.7'
[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     build_cubins = True  # Whether to build Cycles CUDA kernels
58     bits = 64
59
60     # Config file to be used (relative to blender's sources root)
61     cmake_config_file = "build_files/cmake/config/blender_release.cmake"
62
63     # Set build options.
64     cmake_options = []
65     cmake_extra_options = ['-DCMAKE_BUILD_TYPE:STRING=Release']
66
67     if builder.startswith('mac'):
68         # Set up OSX architecture
69         if builder.endswith('x86_64_10_9_cmake'):
70             cmake_extra_options.append('-DCMAKE_OSX_ARCHITECTURES:STRING=x86_64')
71         cmake_extra_options.append('-DCMAKE_OSX_DEPLOYMENT_TARGET=10.9')
72
73     elif builder.startswith('win'):
74         if builder.startswith('win64'):
75             cmake_options.extend(['-G', 'Visual Studio 15 2017 Win64'])
76         elif builder.startswith('win32'):
77             bits = 32
78             cmake_options.extend(['-G', 'Visual Studio 15 2017'])
79
80     elif builder.startswith('linux'):
81         tokens = builder.split("_")
82         glibc = tokens[1]
83         if glibc == 'glibc224':
84             deb_name = "stretch"
85         elif glibc == 'glibc219':
86             deb_name = "jessie"
87         elif glibc == 'glibc211':
88             deb_name = "squeeze"
89         cmake_config_file = "build_files/buildbot/config/blender_linux.cmake"
90         if builder.endswith('x86_64_cmake'):
91             chroot_name = 'buildbot_' + deb_name + '_x86_64'
92             targets = ['blender']
93         elif builder.endswith('i686_cmake'):
94             bits = 32
95             chroot_name = 'buildbot_' + deb_name + '_i686'
96             targets = ['blender']
97         if deb_name != "stretch":
98             cmake_extra_options.extend(["-DCMAKE_C_COMPILER=/usr/bin/gcc-7",
99                                         "-DCMAKE_CXX_COMPILER=/usr/bin/g++-7"])
100
101     cmake_options.append("-C" + os.path.join(blender_dir, cmake_config_file))
102
103     # Prepare CMake options needed to configure cuda binaries compilation, 64bit only.
104     if bits == 64 and build_cubins:
105         cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=ON")
106         cmake_options.append("-DCUDA_64_BIT_DEVICE_CODE=ON")
107     else:
108         cmake_options.append("-DWITH_CYCLES_CUDA_BINARIES=OFF")
109
110     cmake_options.append("-DCMAKE_INSTALL_PREFIX=%s" % (install_dir))
111
112     cmake_options += cmake_extra_options
113
114     # Prepare chroot command prefix if needed
115     if chroot_name:
116         chroot_prefix = ['schroot', '-c', chroot_name, '--']
117     else:
118         chroot_prefix = []
119
120     # Make sure no garbage remained from the previous run
121     if os.path.isdir(install_dir):
122         shutil.rmtree(install_dir)
123
124     for target in targets:
125         print("Building target %s" % (target))
126         # Construct build directory name based on the target
127         target_build_dir = build_dir
128         target_chroot_prefix = chroot_prefix[:]
129         if target != 'blender':
130             target_build_dir += '_' + target
131         target_name = 'install'
132         # Tweaking CMake options to respect the target
133         target_cmake_options = cmake_options[:]
134         # Do extra git fetch because not all platform/git/buildbot combinations
135         # update the origin remote, causing buildinfo to detect local changes.
136         os.chdir(blender_dir)
137         print("Fetching remotes")
138         command = ['git', 'fetch', '--all']
139         print(command)
140         retcode = subprocess.call(target_chroot_prefix + command)
141         if retcode != 0:
142             sys.exit(retcode)
143         # Make sure build directory exists and enter it
144         if not os.path.isdir(target_build_dir):
145             os.mkdir(target_build_dir)
146         os.chdir(target_build_dir)
147         # Configure the build
148         print("CMake options:")
149         print(target_cmake_options)
150         if os.path.exists('CMakeCache.txt'):
151             print("Removing CMake cache")
152             os.remove('CMakeCache.txt')
153         # Remove buildinfo files to force buildbot to re-generate them.
154         for buildinfo in ('buildinfo.h', 'buildinfo.h.txt', ):
155             full_path = os.path.join('source', 'creator', buildinfo)
156             if os.path.exists(full_path):
157                 print("Removing {}" . format(buildinfo))
158                 os.remove(full_path)
159         retcode = subprocess.call(target_chroot_prefix + ['cmake', blender_dir] + target_cmake_options)
160         if retcode != 0:
161             print('Configuration FAILED!')
162             sys.exit(retcode)
163
164         if 'win32' in builder or 'win64' in builder:
165             command = ['cmake', '--build', '.', '--target', target_name, '--config', 'Release']
166         else:
167             command = ['make', '-s', '-j2', target_name]
168
169         print("Executing command:")
170         print(command)
171         retcode = subprocess.call(target_chroot_prefix + command)
172
173         if retcode != 0:
174             sys.exit(retcode)
175
176 else:
177     print("Unknown building system")
178     sys.exit(1)