blob: 00ac760526414714db3c0a21356a9647a22ab00a [file] [log] [blame]
Alexander Afanasyevcf18c802016-03-20 22:48:15 -07001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4#
5# Loosely based on original bash-version by Sebastian Schlingmann (based, again, on a OSX application bundler
6# by Thomas Keller).
7#
8
9import sys, os, string, re, shutil, plistlib, tempfile, exceptions, datetime, tarfile
10from subprocess import Popen, PIPE
11from optparse import OptionParser
12
13import platform
14
15if platform.system () != 'Darwin':
16 print "This script is indended to be run only on OSX platform"
17 exit (1)
18
19MIN_SUPPORTED_VERSION="10.10"
20
21current_version = tuple(int(i) for i in platform.mac_ver()[0].split('.')[0:2])
22min_supported_version = tuple(int(i) for i in MIN_SUPPORTED_VERSION.split('.')[0:2])
23
24if current_version < min_supported_version:
25 print "This script is indended to be run only on OSX >= %s platform" % MIN_SUPPORTED_VERSION
26 exit (1)
27
28options = None
29
30def gitrev():
31 return os.popen('git describe').read()[:-1]
32
33def codesign(path):
34 '''Call the codesign executable.'''
35
36 if hasattr(path, 'isalpha'):
37 path = (path,)
38
39 for p in path:
40 p = Popen(('codesign', '-vvvv', '--deep', '--force', '--sign', options.codesign, p))
41 retval = p.wait()
42 if retval != 0:
43 return retval
44 return 0
45
46class AppBundle(object):
47
48 def __init__(self, bundle, version, binary):
49 shutil.copytree (src = binary, dst = bundle, symlinks = True)
50
51 self.framework_path = ''
52 self.handled_libs = {}
53 self.bundle = bundle
54 self.version = version
55 self.infopath = os.path.join(os.path.abspath(bundle), 'Contents', 'Info.plist')
56 self.infoplist = plistlib.readPlist(self.infopath)
57 self.binary = os.path.join(os.path.abspath(bundle), 'Contents', 'MacOS', self.infoplist['CFBundleExecutable'])
58 print ' * Preparing AppBundle'
59
60 def is_system_lib(self, lib):
61 '''
62 Is the library a system library, meaning that we should not include it in our bundle?
63 '''
64 if lib.startswith('/System/Library/'):
65 return True
66 if lib.startswith('/usr/lib/'):
67 return True
68
69 return False
70
71 def is_dylib(self, lib):
72 '''
73 Is the library a dylib?
74 '''
75 return lib.endswith('.dylib')
76
77 def get_framework_base(self, fw):
78 '''
79 Extracts the base .framework bundle path from a library in an abitrary place in a framework.
80 '''
81 paths = fw.split('/')
82 for i, str in enumerate(paths):
83 if str.endswith('.framework'):
84 return '/'.join(paths[:i+1])
85 return None
86
87 def is_framework(self, lib):
88 '''
89 Is the library a framework?
90 '''
91 return bool(self.get_framework_base(lib))
92
93 def get_binary_libs(self, path):
94 '''
95 Get a list of libraries that we depend on.
96 '''
97 m = re.compile('^\t(.*)\ \(.*$')
98 libs = Popen(['otool', '-L', path], stdout=PIPE).communicate()[0]
99 libs = string.split(libs, '\n')
100 ret = []
101 bn = os.path.basename(path)
102 for line in libs:
103 g = m.match(line)
104 if g is not None:
105 lib = g.groups()[0]
106 if lib != bn:
107 ret.append(lib)
108 return ret
109
110 def handle_libs(self):
111 '''
112 Copy non-system libraries that we depend on into our bundle, and fix linker
113 paths so they are relative to our bundle.
114 '''
115 print ' * Taking care of libraries'
116
117 # Does our fwpath exist?
118 fwpath = os.path.join(os.path.abspath(self.bundle), 'Contents', 'Frameworks')
119 if not os.path.exists(fwpath):
120 os.mkdir(fwpath)
121
122 self.handle_binary_libs()
123
124 def handle_binary_libs(self, macho=None, loader_path=None):
125 '''
126 Fix up dylib depends for a specific binary.
127 '''
128 # Does our fwpath exist already? If not, create it.
129 if not self.framework_path:
130 self.framework_path = self.bundle + '/Contents/Frameworks'
131 if not os.path.exists(self.framework_path):
132 os.mkdir(self.framework_path)
133 else:
134 shutil.rmtree(self.framework_path)
135 os.mkdir(self.framework_path)
136
137 # If we weren't explicitly told which binary to operate on, pick the
138 # bundle's default executable from its property list.
139 if macho is None:
140 macho = os.path.abspath(self.binary)
141 else:
142 macho = os.path.abspath(macho)
143
144 print "Processing [%s]" % macho
145
146 libs = self.get_binary_libs(macho)
147
148 for lib in libs:
149
150 # Skip system libraries
151 if self.is_system_lib(lib):
152 continue
153
154 # Frameworks are 'special'.
155 if self.is_framework(lib):
156 fw_path = self.get_framework_base(lib)
157 basename = os.path.basename(fw_path)
158 name = basename.split('.framework')[0]
159 rel = basename + '/' + name
160
161 abs = self.framework_path + '/' + rel
162
163 if not basename in self.handled_libs:
164 dst = self.framework_path + '/' + basename
165 print "COPY ", fw_path, dst
166 shutil.copytree(fw_path, dst, symlinks=True)
167 if name.startswith('Qt'):
168 os.remove(dst + '/' + name + '.prl')
169 os.remove(dst + '/Headers')
170 shutil.rmtree(dst + '/Versions/Current/Headers')
171
172 os.chmod(abs, 0755)
173 os.system('install_name_tool -id "@executable_path/../Frameworks/%s" "%s"' % (rel, abs))
174 self.handled_libs[basename] = True
175 self.handle_binary_libs(abs)
176
177 os.chmod(macho, 0755)
Alexander Afanasyev2fda1262016-03-20 23:26:23 -0700178 # print 'install_name_tool -change "%s" "@executable_path/../Frameworks/%s" "%s"' % (lib, rel, macho)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700179 os.system('install_name_tool -change "%s" "@executable_path/../Frameworks/%s" "%s"' % (lib, rel, macho))
180
181 # Regular dylibs
182 else:
183 basename = os.path.basename(lib)
184 rel = basename
185
186 if not basename in self.handled_libs:
187 if lib.startswith('@loader_path'):
188 copypath = lib.replace('@loader_path', loader_path)
189 else:
190 copypath = lib
191
192 print "COPY ", copypath
193 shutil.copy(copypath, self.framework_path + '/' + basename)
194
195 abs = self.framework_path + '/' + rel
196 os.chmod(abs, 0755)
197 os.system('install_name_tool -id "@executable_path/../Frameworks/%s" "%s"' % (rel, abs))
198 self.handled_libs[basename] = True
199 self.handle_binary_libs(abs, loader_path=os.path.dirname(lib) if loader_path is None else loader_path)
200
Alexander Afanasyev2fda1262016-03-20 23:26:23 -0700201 # print 'install_name_tool -change "%s" "@executable_path/../Frameworks/%s" "%s"' % (lib, rel, macho)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700202 os.chmod(macho, 0755)
203 os.system('install_name_tool -change "%s" "@executable_path/../Frameworks/%s" "%s"' % (lib, rel, macho))
204
205 def copy_resources(self, rsrcs):
206 '''
207 Copy needed resources into our bundle.
208 '''
209 print ' * Copying needed resources'
210 rsrcpath = os.path.join(self.bundle, 'Contents', 'Resources')
211 if not os.path.exists(rsrcpath):
212 os.mkdir(rsrcpath)
213
214 # Copy resources already in the bundle
215 for rsrc in rsrcs:
216 b = os.path.basename(rsrc)
217 if os.path.isdir(rsrc):
218 shutil.copytree(rsrc, os.path.join(rsrcpath, b), symlinks=True)
219 elif os.path.isfile(rsrc):
220 shutil.copy(rsrc, os.path.join(rsrcpath, b))
221
222 return
223
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700224 def copy_etc(self, rsrcs):
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700225 '''
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700226 Copy needed config files into our bundle.
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700227 '''
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700228 print ' * Copying needed config files'
229 rsrcpath = os.path.join(self.bundle, 'Contents', 'etc')
230 if not os.path.exists(rsrcpath):
231 os.mkdir(rsrcpath)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700232
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700233 # Copy resources already in the bundle
234 for rsrc in rsrcs:
235 b = os.path.basename(rsrc)
236 if os.path.isdir(rsrc):
237 shutil.copytree(rsrc, os.path.join(rsrcpath, b), symlinks=True)
238 elif os.path.isfile(rsrc):
239 shutil.copy(rsrc, os.path.join(rsrcpath, b))
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700240
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700241 return
242 # def copy_qt_plugins(self):
243 # '''
244 # Copy over any needed Qt plugins.
245 # '''
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700246
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700247 # print ' * Copying Qt and preparing plugins'
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700248
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700249 # src = os.popen('qmake -query QT_INSTALL_PLUGINS').read().strip()
250 # dst = os.path.join(self.bundle, 'Contents', 'QtPlugins')
251 # shutil.copytree(src, dst, symlinks=False)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700252
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700253 # top = dst
254 # files = {}
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700255
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700256 # def cb(arg, dirname, fnames):
257 # if dirname == top:
258 # return
259 # files[os.path.basename(dirname)] = fnames
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700260
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700261 # os.path.walk(top, cb, None)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700262
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700263 # exclude = ( 'phonon_backend', 'designer', 'script' )
264
265 # for dir, files in files.items():
266 # absdir = dst + '/' + dir
267 # if dir in exclude:
268 # shutil.rmtree(absdir)
269 # continue
270 # for file in files:
271 # abs = absdir + '/' + file
272 # if file.endswith('_debug.dylib'):
273 # os.remove(abs)
274 # else:
275 # os.system('install_name_tool -id "%s" "%s"' % (file, abs))
276 # self.handle_binary_libs(abs)
277
278 def macdeployqt(self):
279 Popen(['macdeployqt', self.bundle, '-qmldir=src', '-executable=%s' % self.binary]).communicate()
280
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700281 def copy_ndn_deps(self, path):
282 '''
283 Copy over NDN dependencies (NFD and related apps)
284 '''
285 print ' * Copying NDN dependencies'
286
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700287 src = os.path.join(path, 'bin')
288 dst = os.path.join(self.bundle, 'Contents', 'Platform')
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700289 shutil.copytree(src, dst, symlinks=False)
290
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700291 for subdir, dirs, files in os.walk(dst):
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700292 for file in files:
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700293 abs = subdir + "/" + file
294 self.handle_binary_libs(abs)
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700295
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700296 # top = dst
297 # files = {}
298
299 # def cb(arg, dirname, fnames):
300 # if dirname == top:
301 # return
302 # files[dirname] = fnames
303
304 # os.path.walk(top, cb, None)
305
306 # # Cleanup debug folders stuff
307 # excludeDirs = ['include', 'pkgconfig', 'lib'] # lib already processed
308 # excludeFiles = ['libndn-cxx.dylib', 'nfd-start', 'nfd-stop']
309
310 # for dir, files in files.items():
311 # basename = os.path.basename(dir)
312 # if basename in excludeDirs:
313 # shutil.rmtree(dir)
314 # continue
315 # for file in files:
316 # if file in excludeFiles:
317 # abs = dir + '/' + file
318 # os.remove(abs)
319
320 # top = dst
321 # files = {}
322
323 # os.path.walk(top, cb, None)
324
325 # for dir, files in files.items():
326 # for file in files:
327 # abs = dir + '/' + file
328 # type = Popen(['file', '-b', abs], stdout=PIPE).communicate()[0].strip()
329 # if type.startswith('Mach-O'):
330 # self.handle_binary_libs(abs)
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700331
332
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700333 def set_min_macosx_version(self, version):
334 '''
335 Set the minimum version of Mac OS X version that this App will run on.
336 '''
337 print ' * Setting minimum Mac OS X version to: %s' % (version)
338 self.infoplist['LSMinimumSystemVersion'] = version
339
340 def done(self):
341 plistlib.writePlist(self.infoplist, self.infopath)
342 print ' * Done!'
343 print ''
344
345class FolderObject(object):
346 class Exception(exceptions.Exception):
347 pass
348
349 def __init__(self):
350 self.tmp = tempfile.mkdtemp()
351
352 def copy(self, src, dst='/'):
353 '''
354 Copy a file or directory into foler
355 '''
356 asrc = os.path.abspath(src)
357
358 if dst[0] != '/':
359 raise self.Exception
360
361 # Determine destination
362 if dst[-1] == '/':
363 adst = os.path.abspath(self.tmp + '/' + dst + os.path.basename(src))
364 else:
365 adst = os.path.abspath(self.tmp + '/' + dst)
366
367 if os.path.isdir(asrc):
368 print ' * Copying directory: %s' % os.path.basename(asrc)
369 shutil.copytree(asrc, adst, symlinks=True)
370 elif os.path.isfile(asrc):
371 print ' * Copying file: %s' % os.path.basename(asrc)
372 shutil.copy(asrc, adst)
373
374 def symlink(self, src, dst):
375 '''
376 Create a symlink inside the folder
377 '''
378 asrc = os.path.abspath(src)
379 adst = self.tmp + '/' + dst
380 print " * Creating symlink %s" % os.path.basename(asrc)
381 os.symlink(asrc, adst)
382
383 def mkdir(self, name):
384 '''
385 Create a directory inside the folder.
386 '''
387 print ' * Creating directory %s' % os.path.basename(name)
388 adst = self.tmp + '/' + name
389 os.makedirs(adst)
390
391class DiskImage(FolderObject):
392
393 def __init__(self, filename, volname):
394 FolderObject.__init__(self)
395 print ' * Preparing to create diskimage'
396 self.filename = filename
397 self.volname = volname
398
399 def create(self):
400 '''
401 Create the disk image
402 '''
403 print ' * Creating disk image. Please wait...'
404 if os.path.exists(self.filename):
Alexander Afanasyev2fda1262016-03-20 23:26:23 -0700405 os.remove(self.filename)
406 shutil.rmtree(self.filename, ignore_errors=True)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700407 p = Popen(['hdiutil', 'create',
408 '-srcfolder', self.tmp,
409 '-format', 'UDBZ',
410 '-volname', self.volname,
411 self.filename])
412
413 retval = p.wait()
414 print ' * Removing temporary directory.'
415 shutil.rmtree(self.tmp)
416 print ' * Done!'
417
418
419if __name__ == '__main__':
420 parser = OptionParser()
421 parser.add_option('-r', '--release', dest='release', help='Build a release. This determines the version number of the release.')
422 parser.add_option('-s', '--snapshot', dest='snapshot', help='Build a snapshot release. This determines the \'snapshot version\'.')
423 parser.add_option('-g', '--git', dest='git', help='Build a snapshot release. Use the git revision number as the \'snapshot version\'.', action='store_true', default=False)
424 parser.add_option('--codesign', dest='codesign', help='Identity to use for code signing. (If not set, no code signing will occur)')
425
426 options, args = parser.parse_args()
427
428 # Release
429 if options.release:
430 ver = options.release
431 # Snapshot
432 elif options.snapshot or options.git:
433 if not options.git:
434 ver = options.snapshot
435 else:
436 ver = gitrev()
437 else:
438 print 'ERROR: Neither snapshot or release selected. Bailing.'
439 parser.print_help ()
440 sys.exit(1)
441
442 # Do the finishing touches to our Application bundle before release
443 shutil.rmtree('build/%s/NDN.app' % (MIN_SUPPORTED_VERSION), ignore_errors=True)
444 a = AppBundle('build/%s/NDN.app' % (MIN_SUPPORTED_VERSION), ver, 'build/NFD Control Center.app')
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700445 # a.copy_qt_plugins()
446 # a.handle_libs()
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700447 a.copy_ndn_deps("build/deps")
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700448 # a.copy_resources(['qt.conf'])
449 a.copy_etc(['nfd.conf'])
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700450 a.set_min_macosx_version('%s.0' % MIN_SUPPORTED_VERSION)
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700451 a.macdeployqt()
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700452 a.done()
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700453
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700454 # Sign our binaries, etc.
455 if options.codesign:
456 print ' * Signing binaries with identity `%s\'' % options.codesign
457 binaries = (
458 'build/%s/ChronoChat.app' % (MIN_SUPPORTED_VERSION),
459 )
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700460
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700461 codesign(binaries)
462 print ''
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700463
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700464 # Create diskimage
465 title = "NDN-%s-%s" % (ver, MIN_SUPPORTED_VERSION)
466 fn = "build/%s.dmg" % title
467 d = DiskImage(fn, title)
468 d.symlink('/Applications', '/Applications')
469 d.copy('build/%s/NDN.app' % MIN_SUPPORTED_VERSION, '/NDN.app')
470 d.create()