blob: ca34843b6fff3263f74199310098524e4fd824a2 [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
21MIN_SUPPORTED_VERSION="10.10"
22
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')
264 dst = os.path.join(self.bundle, 'Contents', 'Platform')
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700265 shutil.copytree(src, dst, symlinks=False)
266
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700267 for subdir, dirs, files in os.walk(dst):
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700268 for file in files:
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700269 abs = subdir + "/" + file
270 self.handle_binary_libs(abs)
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700271
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700272 def set_min_macosx_version(self, version):
273 '''
274 Set the minimum version of Mac OS X version that this App will run on.
275 '''
276 print ' * Setting minimum Mac OS X version to: %s' % (version)
277 self.infoplist['LSMinimumSystemVersion'] = version
278
279 def done(self):
280 plistlib.writePlist(self.infoplist, self.infopath)
281 print ' * Done!'
282 print ''
283
284class FolderObject(object):
285 class Exception(exceptions.Exception):
286 pass
287
288 def __init__(self):
289 self.tmp = tempfile.mkdtemp()
290
291 def copy(self, src, dst='/'):
292 '''
293 Copy a file or directory into foler
294 '''
295 asrc = os.path.abspath(src)
296
297 if dst[0] != '/':
298 raise self.Exception
299
300 # Determine destination
301 if dst[-1] == '/':
302 adst = os.path.abspath(self.tmp + '/' + dst + os.path.basename(src))
303 else:
304 adst = os.path.abspath(self.tmp + '/' + dst)
305
306 if os.path.isdir(asrc):
307 print ' * Copying directory: %s' % os.path.basename(asrc)
308 shutil.copytree(asrc, adst, symlinks=True)
309 elif os.path.isfile(asrc):
310 print ' * Copying file: %s' % os.path.basename(asrc)
311 shutil.copy(asrc, adst)
312
313 def symlink(self, src, dst):
314 '''
315 Create a symlink inside the folder
316 '''
317 asrc = os.path.abspath(src)
318 adst = self.tmp + '/' + dst
319 print " * Creating symlink %s" % os.path.basename(asrc)
320 os.symlink(asrc, adst)
321
322 def mkdir(self, name):
323 '''
324 Create a directory inside the folder.
325 '''
326 print ' * Creating directory %s' % os.path.basename(name)
327 adst = self.tmp + '/' + name
328 os.makedirs(adst)
329
330class DiskImage(FolderObject):
331
332 def __init__(self, filename, volname):
333 FolderObject.__init__(self)
334 print ' * Preparing to create diskimage'
335 self.filename = filename
336 self.volname = volname
337
338 def create(self):
339 '''
340 Create the disk image
341 '''
342 print ' * Creating disk image. Please wait...'
343 if os.path.exists(self.filename):
Alexander Afanasyev2fda1262016-03-20 23:26:23 -0700344 os.remove(self.filename)
345 shutil.rmtree(self.filename, ignore_errors=True)
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700346 p = Popen(['hdiutil', 'create',
347 '-srcfolder', self.tmp,
348 '-format', 'UDBZ',
349 '-volname', self.volname,
350 self.filename])
351
352 retval = p.wait()
353 print ' * Removing temporary directory.'
354 shutil.rmtree(self.tmp)
355 print ' * Done!'
356
357
358if __name__ == '__main__':
359 parser = OptionParser()
360 parser.add_option('-r', '--release', dest='release', help='Build a release. This determines the version number of the release.')
361 parser.add_option('-s', '--snapshot', dest='snapshot', help='Build a snapshot release. This determines the \'snapshot version\'.')
362 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)
363 parser.add_option('--codesign', dest='codesign', help='Identity to use for code signing. (If not set, no code signing will occur)')
364
365 options, args = parser.parse_args()
366
367 # Release
368 if options.release:
369 ver = options.release
370 # Snapshot
371 elif options.snapshot or options.git:
372 if not options.git:
373 ver = options.snapshot
374 else:
375 ver = gitrev()
376 else:
377 print 'ERROR: Neither snapshot or release selected. Bailing.'
378 parser.print_help ()
379 sys.exit(1)
380
381 # Do the finishing touches to our Application bundle before release
382 shutil.rmtree('build/%s/NDN.app' % (MIN_SUPPORTED_VERSION), ignore_errors=True)
383 a = AppBundle('build/%s/NDN.app' % (MIN_SUPPORTED_VERSION), ver, 'build/NFD Control Center.app')
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700384 # a.copy_qt_plugins()
385 # a.handle_libs()
Alexander Afanasyevb2cf5c02016-03-21 11:04:28 -0700386 a.copy_ndn_deps("build/deps")
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700387 # a.copy_resources(['qt.conf'])
388 a.copy_etc(['nfd.conf'])
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700389 a.set_min_macosx_version('%s.0' % MIN_SUPPORTED_VERSION)
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700390 a.macdeployqt()
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800391 a.copy_framework("osx/Frameworks/Sparkle.framework")
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700392 a.done()
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700393
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700394 # Sign our binaries, etc.
395 if options.codesign:
396 print ' * Signing binaries with identity `%s\'' % options.codesign
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800397 codesign(a.bundle)
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700398 print ''
Alexander Afanasyevcf18c802016-03-20 22:48:15 -0700399
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700400 # Create diskimage
401 title = "NDN-%s-%s" % (ver, MIN_SUPPORTED_VERSION)
402 fn = "build/%s.dmg" % title
403 d = DiskImage(fn, title)
404 d.symlink('/Applications', '/Applications')
405 d.copy('build/%s/NDN.app' % MIN_SUPPORTED_VERSION, '/NDN.app')
406 d.create()
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800407
408 if options.codesign:
409 print ' * Signing .dmg with identity `%s\'' % options.codesign
410 codesign(fn)
411 print ''
Alexander Afanasyev712dedc2017-02-09 16:31:41 -0800412
413Popen('tail -n +3 RELEASE_NOTES.md | pandoc -f markdown -t html > build/release-notes-%s.html' % ver, shell=True).wait()