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