Buildbot: Another attmept to solve windows
[blender.git] / build_files / buildbot / slave_pack.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 # Runs on buildbot slave, creating a release package using the build
22 # system and zipping it into buildbot_upload.zip. This is then uploaded
23 # to the master in the next buildbot step.
24
25 import os
26 import subprocess
27 import sys
28 import zipfile
29
30 # get builder name
31 if len(sys.argv) < 2:
32     sys.stderr.write("Not enough arguments, expecting builder name\n")
33     sys.exit(1)
34
35 builder = sys.argv[1]
36 # Never write branch if it is master.
37 branch = sys.argv[2] if (len(sys.argv) >= 3 and sys.argv[2] != 'master') else ''
38
39 blender_dir = os.path.join('..', 'blender.git')
40 build_dir = os.path.join('..', 'build', builder)
41 install_dir = os.path.join('..', 'install', builder)
42 buildbot_upload_zip = os.path.join(os.path.dirname(install_dir), "buildbot_upload.zip")
43
44 upload_filename = None  # Name of the archive to be uploaded
45                         # (this is the name of archive which will appear on the
46                         # download page)
47 upload_filepath = None  # Filepath to be uploaded to the server
48                         # (this folder will be packed)
49
50
51 def parse_header_file(filename, define):
52     import re
53     regex = re.compile("^#\s*define\s+%s\s+(.*)" % define)
54     with open(filename, "r") as file:
55         for l in file:
56             match = regex.match(l)
57             if match:
58                 return match.group(1)
59     return None
60
61
62 # Make sure install directory always exists
63 if not os.path.exists(install_dir):
64     os.makedirs(install_dir)
65
66
67 # scons does own packaging
68 if builder.find('scons') != -1:
69     python_bin = 'python'
70     if builder.find('linux') != -1:
71         python_bin = '/opt/lib/python-2.7/bin/python2.7'
72
73     os.chdir('../blender.git')
74     scons_options = ['BF_QUICK=slnt', 'BUILDBOT_BRANCH=' + branch, 'buildslave', 'BF_FANCY=False']
75
76     buildbot_dir = os.path.dirname(os.path.realpath(__file__))
77     config_dir = os.path.join(buildbot_dir, 'config')
78
79     if builder.find('linux') != -1:
80         scons_options += ['WITH_BF_NOBLENDER=True', 'WITH_BF_PLAYER=False',
81                           'BF_BUILDDIR=' + build_dir,
82                           'BF_INSTALLDIR=' + install_dir,
83                           'WITHOUT_BF_INSTALL=True']
84
85         config = None
86         bits = None
87
88         if builder.endswith('linux_glibc211_x86_64_scons'):
89             config = 'user-config-glibc211-x86_64.py'
90             chroot_name = 'buildbot_squeeze_x86_64'
91             bits = 64
92         elif builder.endswith('linux_glibc211_i386_scons'):
93             config = 'user-config-glibc211-i686.py'
94             chroot_name = 'buildbot_squeeze_i686'
95             bits = 32
96
97         if config is not None:
98             config_fpath = os.path.join(config_dir, config)
99             scons_options.append('BF_CONFIG=' + config_fpath)
100
101         blender = os.path.join(install_dir, 'blender')
102         blenderplayer = os.path.join(install_dir, 'blenderplayer')
103         subprocess.call(['schroot', '-c', chroot_name, '--', 'strip', '--strip-all', blender, blenderplayer])
104
105         extra = "/home/sources/release-builder/extra/"
106         mesalibs = os.path.join(extra, 'mesalibs%d.tar.bz2' % bits)
107         software_gl = os.path.join(extra, 'blender-softwaregl')
108
109         os.system('tar -xpf %s -C %s' % (mesalibs, install_dir))
110         os.system('cp %s %s' % (software_gl, install_dir))
111         os.system('chmod 755 %s' % (os.path.join(install_dir, 'blender-softwaregl')))
112
113         retcode = subprocess.call(['schroot', '-c', chroot_name, '--', python_bin, 'scons/scons.py'] + scons_options)
114
115         sys.exit(retcode)
116     else:
117         if builder.find('win') != -1:
118             bitness = '32'
119
120             if builder.find('win64') != -1:
121                 bitness = '64'
122
123             scons_options.append('BF_INSTALLDIR=' + install_dir)
124             scons_options.append('BF_BUILDDIR=' + build_dir)
125             scons_options.append('BF_BITNESS=' + bitness)
126             scons_options.append('WITH_BF_CYCLES_CUDA_BINARIES=True')
127             scons_options.append('BF_CYCLES_CUDA_NVCC=nvcc.exe')
128             if builder.find('mingw') != -1:
129                 scons_options.append('BF_TOOLSET=mingw')
130             if builder.endswith('vc2013'):
131                 scons_options.append('MSVS_VERSION=12.0')
132                 scons_options.append('MSVC_VERSION=12.0')
133
134         elif builder.find('mac') != -1:
135             if builder.find('x86_64') != -1:
136                 config = 'user-config-mac-x86_64.py'
137             else:
138                 config = 'user-config-mac-i386.py'
139
140             scons_options.append('BF_CONFIG=' + os.path.join(config_dir, config))
141
142         retcode = subprocess.call([python_bin, 'scons/scons.py'] + scons_options)
143         sys.exit(retcode)
144 else:
145     # CMake
146     if 'win' in builder:
147         os.chdir(build_dir)
148
149         files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.zip')]
150         for f in files:
151             os.remove(f)
152         retcode = subprocess.call(['cpack', '-G', 'ZIP'])
153         result_file = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.zip')][0]
154
155         # TODO(sergey): Such magic usually happens in SCon's packaging but we don't have it
156         # in the CMake yet. For until then we do some magic here.
157         tokens = result_file.split('-')
158         blender_version = tokens[1].split('.')
159         blender_full_version = '.'.join(blender_version[0:2])
160         git_hash = tokens[2].split('.')[1]
161         platform = builder.split('_')[0]
162         builderified_name = 'blender-{}-{}-{}'.format(blender_full_version, git_hash, platform)
163         if branch != '':
164             builderified_name = branch + "-" + builderified_name
165
166         os.rename(result_file, "{}.zip".format(builderified_name))
167         # create zip file
168         try:
169             if os.path.exists(buildbot_upload_zip):
170                 os.remove(buildbot_upload_zip)
171             z = zipfile.ZipFile(buildbot_upload_zip, "w", compression=zipfile.ZIP_STORED)
172             z.write("{}.zip".format(builderified_name))
173             z.close()
174             sys.exit(retcode)
175         except Exception as ex:
176             sys.stderr.write('Create buildbot_upload.zip failed' + str(ex) + '\n')
177             sys.exit(1)
178
179     elif builder.startswith('linux_'):
180
181         blender = os.path.join(install_dir, 'blender')
182         blenderplayer = os.path.join(install_dir, 'blenderplayer')
183
184         buildinfo_h = os.path.join(build_dir, "source", "creator", "buildinfo.h")
185         blender_h = os.path.join(blender_dir, "source", "blender", "blenkernel", "BKE_blender.h")
186
187         if builder.endswith('x86_64_cmake'):
188             chroot_name = 'buildbot_squeeze_x86_64'
189             bits = 64
190             blender_arch = 'x86_64'
191         elif builder.endswith('i386_cmake'):
192             chroot_name = 'buildbot_squeeze_i686'
193             bits = 32
194             blender_arch = 'i686'
195
196         # Strip all unused symbols from the binaries
197         print("Stripping binaries...")
198         chroot_prefix = ['schroot', '-c', chroot_name, '--']
199         subprocess.call(chroot_prefix + ['strip', '--strip-all', blender, blenderplayer])
200
201         # Copy all specific files which are too specific to be copied by
202         # the CMake rules themselves
203         print("Copying extra scripts and libs...")
204
205         extra = '/' + os.path.join('home', 'sources', 'release-builder', 'extra')
206         mesalibs = os.path.join(extra, 'mesalibs' + str(bits) + '.tar.bz2')
207         software_gl = os.path.join(blender_dir, 'release', 'bin', 'blender-softwaregl')
208         icons = os.path.join(blender_dir, 'release', 'freedesktop', 'icons')
209
210         os.system('tar -xpf %s -C %s' % (mesalibs, install_dir))
211         os.system('cp %s %s' % (software_gl, install_dir))
212         os.system('cp -r %s %s' % (icons, install_dir))
213         os.system('chmod 755 %s' % (os.path.join(install_dir, 'blender-softwaregl')))
214
215         # Get version information for the archive name
216         blender_version = int(parse_header_file(blender_h, 'BLENDER_VERSION'))
217         blender_version = "%d.%d" % (blender_version / 100, blender_version % 100)
218         blender_hash = parse_header_file(buildinfo_h, 'BUILD_HASH')[1:-1]
219         blender_glibc = builder.split('_')[1]
220
221         upload_filename = 'blender-%s-%s-linux-%s-%s.tar.bz2' % (blender_version,
222                                                                  blender_hash,
223                                                                  blender_glibc,
224                                                                  blender_arch)
225         if branch != '':
226             upload_filename = branch + "-" + upload_filename
227
228         print("Creating .tar.bz2 archive")
229         os.system('tar -C../install -cjf ../install/%s.tar.bz2 %s' % (builder, builder))
230         upload_filepath = install_dir + '.tar.bz2'
231
232
233 if upload_filepath is None:
234     # clean release directory if it already exists
235     release_dir = 'release'
236
237     if os.path.exists(release_dir):
238         for f in os.listdir(release_dir):
239             if os.path.isfile(os.path.join(release_dir, f)):
240                 os.remove(os.path.join(release_dir, f))
241
242     # create release package
243     try:
244         subprocess.call(['make', 'package_archive'])
245     except Exception as ex:
246         sys.stderr.write('Make package release failed' + str(ex) + '\n')
247         sys.exit(1)
248
249     # find release directory, must exist this time
250     if not os.path.exists(release_dir):
251         sys.stderr.write("Failed to find release directory %r.\n" % release_dir)
252         sys.exit(1)
253
254     # find release package
255     file = None
256     filepath = None
257
258     for f in os.listdir(release_dir):
259         rf = os.path.join(release_dir, f)
260         if os.path.isfile(rf) and f.startswith('blender'):
261             file = f
262             filepath = rf
263
264     if not file:
265         sys.stderr.write("Failed to find release package.\n")
266         sys.exit(1)
267
268     upload_filename = file
269     upload_filepath = filepath
270
271 # create zip file
272 try:
273     upload_zip = os.path.join(buildbot_upload_zip)
274     if os.path.exists(upload_zip):
275         os.remove(upload_zip)
276     z = zipfile.ZipFile(upload_zip, "w", compression=zipfile.ZIP_STORED)
277     z.write(upload_filepath, arcname=upload_filename)
278     z.close()
279 except Exception as ex:
280     sys.stderr.write('Create buildbot_upload.zip failed' + str(ex) + '\n')
281     sys.exit(1)