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