blob: 79d7222cc4a1d7c6b69f19d211a7ac1a205c03e9 [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))
Qi Zhao9d6442e2017-03-22 00:40:11 -0500273 os.symlink("../../Helpers/%s" % file, "%s/%s" % (dstResource, file))
Alexander Afanasyev9c45c642017-03-18 15:29:42 -0700274 else:
275 shutil.copy("%s/%s" % (src, file), "%s/%s" % (dstResource, file))
276 Popen(['/usr/bin/sed', '-i', '', '-e', 's|`dirname "$0"`/|`dirname "$0"`/../../Helpers/|', "%s/%s" % (dstResource, file)]).communicate()[0]
277
278 for subdir, dirs, files in os.walk(dstBinary):
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700279 for file in files:
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700280 abs = subdir + "/" + file
281 self.handle_binary_libs(abs)
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700282
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700283 def set_min_macosx_version(self, version):
284 '''
285 Set the minimum version of Mac OS X version that this App will run on.
286 '''
287 print ' * Setting minimum Mac OS X version to: %s' % (version)
288 self.infoplist['LSMinimumSystemVersion'] = version
289
290 def done(self):
291 plistlib.writePlist(self.infoplist, self.infopath)
292 print ' * Done!'
293 print ''
294
295class FolderObject(object):
296 class Exception(exceptions.Exception):
297 pass
298
299 def __init__(self):
300 self.tmp = tempfile.mkdtemp()
301
302 def copy(self, src, dst='/'):
303 '''
304 Copy a file or directory into foler
305 '''
306 asrc = os.path.abspath(src)
307
308 if dst[0] != '/':
309 raise self.Exception
310
311 # Determine destination
312 if dst[-1] == '/':
313 adst = os.path.abspath(self.tmp + '/' + dst + os.path.basename(src))
314 else:
315 adst = os.path.abspath(self.tmp + '/' + dst)
316
317 if os.path.isdir(asrc):
318 print ' * Copying directory: %s' % os.path.basename(asrc)
319 shutil.copytree(asrc, adst, symlinks=True)
320 elif os.path.isfile(asrc):
321 print ' * Copying file: %s' % os.path.basename(asrc)
322 shutil.copy(asrc, adst)
323
324 def symlink(self, src, dst):
325 '''
326 Create a symlink inside the folder
327 '''
328 asrc = os.path.abspath(src)
329 adst = self.tmp + '/' + dst
330 print " * Creating symlink %s" % os.path.basename(asrc)
331 os.symlink(asrc, adst)
332
333 def mkdir(self, name):
334 '''
335 Create a directory inside the folder.
336 '''
337 print ' * Creating directory %s' % os.path.basename(name)
338 adst = self.tmp + '/' + name
339 os.makedirs(adst)
340
341class DiskImage(FolderObject):
342
343 def __init__(self, filename, volname):
344 FolderObject.__init__(self)
345 print ' * Preparing to create diskimage'
346 self.filename = filename
347 self.volname = volname
348
349 def create(self):
350 '''
351 Create the disk image
352 '''
353 print ' * Creating disk image. Please wait...'
354 if os.path.exists(self.filename):
Alexander Afanasyev2fda1262016-03-20 23:26:23 -0700355 os.remove(self.filename)
356 shutil.rmtree(self.filename, ignore_errors=True)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700357 p = Popen(['hdiutil', 'create',
358 '-srcfolder', self.tmp,
359 '-format', 'UDBZ',
360 '-volname', self.volname,
361 self.filename])
362
363 retval = p.wait()
364 print ' * Removing temporary directory.'
365 shutil.rmtree(self.tmp)
366 print ' * Done!'
367
368
369if __name__ == '__main__':
370 parser = OptionParser()
371 parser.add_option('-r', '--release', dest='release', help='Build a release. This determines the version number of the release.')
372 parser.add_option('-s', '--snapshot', dest='snapshot', help='Build a snapshot release. This determines the \'snapshot version\'.')
373 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)
Qi Zhao3ec33312017-02-12 02:51:05 -0800374 parser.add_option('--no-dmg', dest='no_dmg', action='store_true', default=False, help='''Disable creation of DMG''')
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700375 parser.add_option('--codesign', dest='codesign', help='Identity to use for code signing. (If not set, no code signing will occur)')
376
377 options, args = parser.parse_args()
378
379 # Release
380 if options.release:
381 ver = options.release
382 # Snapshot
383 elif options.snapshot or options.git:
384 if not options.git:
385 ver = options.snapshot
386 else:
387 ver = gitrev()
388 else:
389 print 'ERROR: Neither snapshot or release selected. Bailing.'
390 parser.print_help ()
391 sys.exit(1)
392
393 # Do the finishing touches to our Application bundle before release
Qi Zhao6d0399e2017-02-23 16:24:39 -0800394 shutil.rmtree('build/NDN.app', ignore_errors=True)
395 a = AppBundle('build/NDN.app', ver, 'build/NDN Control Center.app')
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700396 a.copy_ndn_deps("build/deps")
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700397 # a.copy_resources(['qt.conf'])
398 a.copy_etc(['nfd.conf'])
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700399 a.set_min_macosx_version('%s.0' % MIN_SUPPORTED_VERSION)
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700400 a.macdeployqt()
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800401 a.copy_framework("osx/Frameworks/Sparkle.framework")
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700402 a.done()
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700403
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700404 # Sign our binaries, etc.
405 if options.codesign:
406 print ' * Signing binaries with identity `%s\'' % options.codesign
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800407 codesign(a.bundle)
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700408 print ''
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700409
Qi Zhao3ec33312017-02-12 02:51:05 -0800410 if not options.no_dmg:
411 # Create diskimage
412 title = "NDN-%s" % ver
413 fn = "build/%s.dmg" % title
414 d = DiskImage(fn, title)
415 d.symlink('/Applications', '/Applications')
416 d.copy('build/NDN.app', '/NDN.app')
417 d.create()
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800418
Qi Zhao3ec33312017-02-12 02:51:05 -0800419 if options.codesign:
420 print ' * Signing .dmg with identity `%s\'' % options.codesign
421 codesign(fn)
422 print ''
Alexander Afanasyev712dedc2017-02-09 16:31:41 -0800423
424Popen('tail -n +3 RELEASE_NOTES.md | pandoc -f markdown -t html > build/release-notes-%s.html' % ver, shell=True).wait()