blob: dfbbbe4c2474e7fb0c06f29f5c251b1fd89080dd [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
Alexander Afanasyev38d74ec2016-11-14 10:22:48 +090013os.environ['PATH'] += ":/usr/local/opt/qt5/bin:/opt/qt5/5.8/clang_64/bin"
14
Alexander Afanasyevcf18c802016-03-20 22:48:15 -070015import platform
16
17if platform.system () != 'Darwin':
18 print "This script is indended to be run only on OSX platform"
19 exit (1)
20
Qi Zhao6d0399e2017-02-23 16:24:39 -080021MIN_SUPPORTED_VERSION="10.12"
Alexander Afanasyevcf18c802016-03-20 22:48:15 -070022
23current_version = tuple(int(i) for i in platform.mac_ver()[0].split('.')[0:2])
24min_supported_version = tuple(int(i) for i in MIN_SUPPORTED_VERSION.split('.')[0:2])
25
26if current_version < min_supported_version:
27 print "This script is indended to be run only on OSX >= %s platform" % MIN_SUPPORTED_VERSION
28 exit (1)
29
30options = None
31
32def gitrev():
33 return os.popen('git describe').read()[:-1]
34
35def codesign(path):
36 '''Call the codesign executable.'''
37
38 if hasattr(path, 'isalpha'):
39 path = (path,)
40
41 for p in path:
42 p = Popen(('codesign', '-vvvv', '--deep', '--force', '--sign', options.codesign, p))
43 retval = p.wait()
44 if retval != 0:
45 return retval
46 return 0
47
48class AppBundle(object):
49
50 def __init__(self, bundle, version, binary):
51 shutil.copytree (src = binary, dst = bundle, symlinks = True)
52
53 self.framework_path = ''
54 self.handled_libs = {}
55 self.bundle = bundle
56 self.version = version
57 self.infopath = os.path.join(os.path.abspath(bundle), 'Contents', 'Info.plist')
58 self.infoplist = plistlib.readPlist(self.infopath)
59 self.binary = os.path.join(os.path.abspath(bundle), 'Contents', 'MacOS', self.infoplist['CFBundleExecutable'])
60 print ' * Preparing AppBundle'
61
62 def is_system_lib(self, lib):
63 '''
64 Is the library a system library, meaning that we should not include it in our bundle?
65 '''
66 if lib.startswith('/System/Library/'):
67 return True
68 if lib.startswith('/usr/lib/'):
69 return True
70
71 return False
72
73 def is_dylib(self, lib):
74 '''
75 Is the library a dylib?
76 '''
77 return lib.endswith('.dylib')
78
79 def get_framework_base(self, fw):
80 '''
81 Extracts the base .framework bundle path from a library in an abitrary place in a framework.
82 '''
83 paths = fw.split('/')
84 for i, str in enumerate(paths):
85 if str.endswith('.framework'):
86 return '/'.join(paths[:i+1])
87 return None
88
89 def is_framework(self, lib):
90 '''
91 Is the library a framework?
92 '''
93 return bool(self.get_framework_base(lib))
94
95 def get_binary_libs(self, path):
96 '''
97 Get a list of libraries that we depend on.
98 '''
99 m = re.compile('^\t(.*)\ \(.*$')
100 libs = Popen(['otool', '-L', path], stdout=PIPE).communicate()[0]
101 libs = string.split(libs, '\n')
102 ret = []
103 bn = os.path.basename(path)
104 for line in libs:
105 g = m.match(line)
106 if g is not None:
107 lib = g.groups()[0]
108 if lib != bn:
109 ret.append(lib)
110 return ret
111
112 def handle_libs(self):
113 '''
114 Copy non-system libraries that we depend on into our bundle, and fix linker
115 paths so they are relative to our bundle.
116 '''
117 print ' * Taking care of libraries'
118
119 # Does our fwpath exist?
120 fwpath = os.path.join(os.path.abspath(self.bundle), 'Contents', 'Frameworks')
121 if not os.path.exists(fwpath):
122 os.mkdir(fwpath)
123
124 self.handle_binary_libs()
125
126 def handle_binary_libs(self, macho=None, loader_path=None):
127 '''
128 Fix up dylib depends for a specific binary.
129 '''
130 # Does our fwpath exist already? If not, create it.
131 if not self.framework_path:
132 self.framework_path = self.bundle + '/Contents/Frameworks'
133 if not os.path.exists(self.framework_path):
134 os.mkdir(self.framework_path)
135 else:
136 shutil.rmtree(self.framework_path)
137 os.mkdir(self.framework_path)
138
139 # If we weren't explicitly told which binary to operate on, pick the
140 # bundle's default executable from its property list.
141 if macho is None:
142 macho = os.path.abspath(self.binary)
143 else:
144 macho = os.path.abspath(macho)
145
146 print "Processing [%s]" % macho
Qi Zhao0e043e52016-12-05 18:27:09 -0800147
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700148 libs = self.get_binary_libs(macho)
149
150 for lib in libs:
151
152 # Skip system libraries
153 if self.is_system_lib(lib):
154 continue
155
156 # Frameworks are 'special'.
157 if self.is_framework(lib):
158 fw_path = self.get_framework_base(lib)
159 basename = os.path.basename(fw_path)
160 name = basename.split('.framework')[0]
161 rel = basename + '/' + name
162
163 abs = self.framework_path + '/' + rel
164
165 if not basename in self.handled_libs:
166 dst = self.framework_path + '/' + basename
167 print "COPY ", fw_path, dst
168 shutil.copytree(fw_path, dst, symlinks=True)
169 if name.startswith('Qt'):
170 os.remove(dst + '/' + name + '.prl')
171 os.remove(dst + '/Headers')
172 shutil.rmtree(dst + '/Versions/Current/Headers')
173
174 os.chmod(abs, 0755)
175 os.system('install_name_tool -id "@executable_path/../Frameworks/%s" "%s"' % (rel, abs))
176 self.handled_libs[basename] = True
177 self.handle_binary_libs(abs)
178
179 os.chmod(macho, 0755)
Alexander Afanasyev2fda1262016-03-20 23:26:23 -0700180 # print 'install_name_tool -change "%s" "@executable_path/../Frameworks/%s" "%s"' % (lib, rel, macho)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700181 os.system('install_name_tool -change "%s" "@executable_path/../Frameworks/%s" "%s"' % (lib, rel, macho))
182
183 # Regular dylibs
184 else:
185 basename = os.path.basename(lib)
186 rel = basename
187
188 if not basename in self.handled_libs:
189 if lib.startswith('@loader_path'):
190 copypath = lib.replace('@loader_path', loader_path)
191 else:
192 copypath = lib
193
194 print "COPY ", copypath
195 shutil.copy(copypath, self.framework_path + '/' + basename)
196
197 abs = self.framework_path + '/' + rel
198 os.chmod(abs, 0755)
199 os.system('install_name_tool -id "@executable_path/../Frameworks/%s" "%s"' % (rel, abs))
200 self.handled_libs[basename] = True
Qi Zhao0e043e52016-12-05 18:27:09 -0800201 self.handle_binary_libs(abs, loader_path=os.path.dirname(lib))
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700202
Alexander Afanasyev2fda1262016-03-20 23:26:23 -0700203 # print 'install_name_tool -change "%s" "@executable_path/../Frameworks/%s" "%s"' % (lib, rel, macho)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700204 os.chmod(macho, 0755)
205 os.system('install_name_tool -change "%s" "@executable_path/../Frameworks/%s" "%s"' % (lib, rel, macho))
206
207 def copy_resources(self, rsrcs):
208 '''
209 Copy needed resources into our bundle.
210 '''
211 print ' * Copying needed resources'
212 rsrcpath = os.path.join(self.bundle, 'Contents', 'Resources')
213 if not os.path.exists(rsrcpath):
214 os.mkdir(rsrcpath)
215
216 # Copy resources already in the bundle
217 for rsrc in rsrcs:
218 b = os.path.basename(rsrc)
219 if os.path.isdir(rsrc):
220 shutil.copytree(rsrc, os.path.join(rsrcpath, b), symlinks=True)
221 elif os.path.isfile(rsrc):
222 shutil.copy(rsrc, os.path.join(rsrcpath, b))
223
224 return
225
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700226 def copy_etc(self, rsrcs):
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700227 '''
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700228 Copy needed config files into our bundle.
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700229 '''
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700230 print ' * Copying needed config files'
Alexander Afanasyev964feb92016-03-22 13:03:11 -0700231 rsrcpath = os.path.join(self.bundle, 'Contents', 'etc', 'ndn')
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700232 if not os.path.exists(rsrcpath):
Alexander Afanasyev964feb92016-03-22 13:03:11 -0700233 os.makedirs(rsrcpath)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700234
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700235 # Copy resources already in the bundle
236 for rsrc in rsrcs:
237 b = os.path.basename(rsrc)
238 if os.path.isdir(rsrc):
239 shutil.copytree(rsrc, os.path.join(rsrcpath, b), symlinks=True)
240 elif os.path.isfile(rsrc):
241 shutil.copy(rsrc, os.path.join(rsrcpath, b))
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700242
Alexander Afanasyeve1236c72016-03-21 15:52:09 -0700243 return
Qi Zhao0e043e52016-12-05 18:27:09 -0800244
Alexander Afanasyeve1236c72016-03-21 15:52:09 -0700245 def copy_framework(self, framework):
246 '''
247 Copy frameworks
248 '''
249 print ' * Copying framework'
250 rsrcpath = os.path.join(self.bundle, 'Contents', 'Frameworks', os.path.basename(framework))
251
252 shutil.copytree(framework, rsrcpath, symlinks = True)
253
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700254 def macdeployqt(self):
255 Popen(['macdeployqt', self.bundle, '-qmldir=src', '-executable=%s' % self.binary]).communicate()
Qi Zhao0e043e52016-12-05 18:27:09 -0800256
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700257 def copy_ndn_deps(self, path):
258 '''
259 Copy over NDN dependencies (NFD and related apps)
260 '''
261 print ' * Copying NDN dependencies'
262
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700263 src = os.path.join(path, 'bin')
Alexander Afanasyev9c45c642017-03-18 15:29:42 -0700264 dstBinary = os.path.join(self.bundle, 'Contents', 'Helpers')
265 dstResource = os.path.join(self.bundle, 'Contents', 'Resources', 'bin')
266 os.makedirs(dstBinary)
267 os.makedirs(dstResource)
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700268
Alexander Afanasyev9c45c642017-03-18 15:29:42 -0700269 for file in os.listdir(src):
270 ftype = Popen(['/usr/bin/file', '-b', "%s/%s" % (src, file)], stdout=PIPE).communicate()[0]
271 if "Mach-O" in ftype:
272 shutil.copy("%s/%s" % (src, file), "%s/%s" % (dstBinary, file))
273 else:
274 shutil.copy("%s/%s" % (src, file), "%s/%s" % (dstResource, file))
275 Popen(['/usr/bin/sed', '-i', '', '-e', 's|`dirname "$0"`/|`dirname "$0"`/../../Helpers/|', "%s/%s" % (dstResource, file)]).communicate()[0]
276
277 for subdir, dirs, files in os.walk(dstBinary):
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700278 for file in files:
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700279 abs = subdir + "/" + file
280 self.handle_binary_libs(abs)
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700281
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700282 def set_min_macosx_version(self, version):
283 '''
284 Set the minimum version of Mac OS X version that this App will run on.
285 '''
286 print ' * Setting minimum Mac OS X version to: %s' % (version)
287 self.infoplist['LSMinimumSystemVersion'] = version
288
289 def done(self):
290 plistlib.writePlist(self.infoplist, self.infopath)
291 print ' * Done!'
292 print ''
293
294class FolderObject(object):
295 class Exception(exceptions.Exception):
296 pass
297
298 def __init__(self):
299 self.tmp = tempfile.mkdtemp()
300
301 def copy(self, src, dst='/'):
302 '''
303 Copy a file or directory into foler
304 '''
305 asrc = os.path.abspath(src)
306
307 if dst[0] != '/':
308 raise self.Exception
309
310 # Determine destination
311 if dst[-1] == '/':
312 adst = os.path.abspath(self.tmp + '/' + dst + os.path.basename(src))
313 else:
314 adst = os.path.abspath(self.tmp + '/' + dst)
315
316 if os.path.isdir(asrc):
317 print ' * Copying directory: %s' % os.path.basename(asrc)
318 shutil.copytree(asrc, adst, symlinks=True)
319 elif os.path.isfile(asrc):
320 print ' * Copying file: %s' % os.path.basename(asrc)
321 shutil.copy(asrc, adst)
322
323 def symlink(self, src, dst):
324 '''
325 Create a symlink inside the folder
326 '''
327 asrc = os.path.abspath(src)
328 adst = self.tmp + '/' + dst
329 print " * Creating symlink %s" % os.path.basename(asrc)
330 os.symlink(asrc, adst)
331
332 def mkdir(self, name):
333 '''
334 Create a directory inside the folder.
335 '''
336 print ' * Creating directory %s' % os.path.basename(name)
337 adst = self.tmp + '/' + name
338 os.makedirs(adst)
339
340class DiskImage(FolderObject):
341
342 def __init__(self, filename, volname):
343 FolderObject.__init__(self)
344 print ' * Preparing to create diskimage'
345 self.filename = filename
346 self.volname = volname
347
348 def create(self):
349 '''
350 Create the disk image
351 '''
352 print ' * Creating disk image. Please wait...'
353 if os.path.exists(self.filename):
Alexander Afanasyev2fda1262016-03-20 23:26:23 -0700354 os.remove(self.filename)
355 shutil.rmtree(self.filename, ignore_errors=True)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700356 p = Popen(['hdiutil', 'create',
357 '-srcfolder', self.tmp,
358 '-format', 'UDBZ',
359 '-volname', self.volname,
360 self.filename])
361
362 retval = p.wait()
363 print ' * Removing temporary directory.'
364 shutil.rmtree(self.tmp)
365 print ' * Done!'
366
367
368if __name__ == '__main__':
369 parser = OptionParser()
370 parser.add_option('-r', '--release', dest='release', help='Build a release. This determines the version number of the release.')
371 parser.add_option('-s', '--snapshot', dest='snapshot', help='Build a snapshot release. This determines the \'snapshot version\'.')
372 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)
373 parser.add_option('--codesign', dest='codesign', help='Identity to use for code signing. (If not set, no code signing will occur)')
374
375 options, args = parser.parse_args()
376
377 # Release
378 if options.release:
379 ver = options.release
380 # Snapshot
381 elif options.snapshot or options.git:
382 if not options.git:
383 ver = options.snapshot
384 else:
385 ver = gitrev()
386 else:
387 print 'ERROR: Neither snapshot or release selected. Bailing.'
388 parser.print_help ()
389 sys.exit(1)
390
391 # Do the finishing touches to our Application bundle before release
Qi Zhao6d0399e2017-02-23 16:24:39 -0800392 shutil.rmtree('build/NDN.app', ignore_errors=True)
393 a = AppBundle('build/NDN.app', ver, 'build/NDN Control Center.app')
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700394 a.copy_ndn_deps("build/deps")
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700395 # a.copy_resources(['qt.conf'])
396 a.copy_etc(['nfd.conf'])
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700397 a.set_min_macosx_version('%s.0' % MIN_SUPPORTED_VERSION)
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700398 a.macdeployqt()
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800399 a.copy_framework("osx/Frameworks/Sparkle.framework")
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700400 a.done()
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700401
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700402 # Sign our binaries, etc.
403 if options.codesign:
404 print ' * Signing binaries with identity `%s\'' % options.codesign
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800405 codesign(a.bundle)
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700406 print ''
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700407
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700408 # Create diskimage
Qi Zhao6d0399e2017-02-23 16:24:39 -0800409 title = "NDN-%s" % ver
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700410 fn = "build/%s.dmg" % title
411 d = DiskImage(fn, title)
412 d.symlink('/Applications', '/Applications')
Qi Zhao6d0399e2017-02-23 16:24:39 -0800413 d.copy('build/NDN.app', '/NDN.app')
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700414 d.create()
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800415
416 if options.codesign:
417 print ' * Signing .dmg with identity `%s\'' % options.codesign
418 codesign(fn)
419 print ''
Alexander Afanasyev712dedc2017-02-09 16:31:41 -0800420
421Popen('tail -n +3 RELEASE_NOTES.md | pandoc -f markdown -t html > build/release-notes-%s.html' % ver, shell=True).wait()