all repos — mgba @ ea0b6a14ccae1fa55d99b32112839988481fd65d

mGBA Game Boy Advance Emulator

tools/deploy-mac.py (view raw)

  1#!/usr/bin/env python
  2from __future__ import print_function
  3import argparse
  4import errno
  5import os
  6import re
  7import shutil
  8import subprocess
  9
 10qtPath = None
 11verbose = False
 12
 13def splitPath(path):
 14	folders = []
 15	while True:
 16		path, folder = os.path.split(path)
 17		if folder != '':
 18			folders.append(folder)
 19		else:
 20			if path != '':
 21				folders.append(path)
 22			break
 23	folders.reverse()
 24	return folders
 25
 26def joinPath(path):
 27	return reduce(os.path.join, path, '')
 28
 29def findFramework(path):
 30	child = []
 31	while path and not path[-1].endswith('.framework'):
 32		child.append(path.pop())
 33	child.reverse()
 34	return path, child
 35
 36def findQtPath(path):
 37	parent, child = findFramework(splitPath(path))
 38	return joinPath(parent[:-2])
 39
 40def makedirs(path):
 41	split = splitPath(path)
 42	accum = []
 43	split.reverse()
 44	while split:
 45		accum.append(split.pop())
 46		newPath = joinPath(accum)
 47		try:
 48			os.mkdir(newPath)
 49		except OSError as e:
 50			if e.errno != errno.EEXIST:
 51				raise
 52
 53
 54def parseOtoolLine(line, execPath, root):
 55	if not line.startswith('\t'):
 56		return None, None, None, None
 57	line = line[1:]
 58	match = re.match('([@/].*) \(compatibility version.*\)', line)
 59	path = match.group(1)
 60	split = splitPath(path)
 61	newExecPath = ['@executable_path', '..', 'Frameworks']
 62	newPath = execPath[:-1]
 63	newPath.append('Frameworks')
 64	if split[:3] == ['/', 'usr', 'lib'] or split[:2] == ['/', 'System']:
 65		return None, None, None, None
 66	if split[0] == '@executable_path':
 67		split[:1] = execPath
 68	if split[0] == '/' and not os.access(joinPath(split), os.F_OK):
 69		split[:1] = root
 70	try:
 71		oldPath = joinPath(split)
 72		while True:
 73			linkPath = os.readlink(os.path.abspath(oldPath))
 74			oldPath = os.path.join(os.path.dirname(oldPath), linkPath)
 75	except OSError as e:
 76		if e.errno != errno.EINVAL:
 77			raise
 78		split = splitPath(oldPath)
 79	isFramework = False
 80	if not split[-1].endswith('.dylib'):
 81		isFramework = True
 82		split, framework = findFramework(split)
 83	newPath.append(split[-1])
 84	newExecPath.append(split[-1])
 85	if isFramework:
 86		newPath.extend(framework)
 87		newExecPath.extend(framework)
 88		split.extend(framework)
 89	newPath = joinPath(newPath)
 90	newExecPath = joinPath(newExecPath)
 91	return joinPath(split), newPath, path, newExecPath
 92
 93def updateMachO(bin, execPath, root):
 94	global qtPath
 95	otoolOutput = subprocess.check_output([otool, '-L', bin])
 96	toUpdate = []
 97	for line in otoolOutput.split('\n'):
 98		oldPath, newPath, oldExecPath, newExecPath = parseOtoolLine(line, execPath, root)
 99		if not newPath:
100			continue
101		if os.access(newPath, os.F_OK):
102			if verbose:
103				print('Skipping copying {}, already done.'.format(oldPath))
104		elif os.path.abspath(oldPath) != os.path.abspath(newPath):
105			if verbose:
106				print('Copying {} to {}...'.format(oldPath, newPath))
107			parent, child = os.path.split(newPath)
108			makedirs(parent)
109			shutil.copy2(oldPath, newPath)
110			os.chmod(newPath, 0o644)
111		toUpdate.append((newPath, oldExecPath, newExecPath))
112		if not qtPath and 'Qt' in oldPath:
113			qtPath = findQtPath(oldPath)
114			if verbose:
115				print('Found Qt path at {}.'.format(qtPath))
116	for path, oldExecPath, newExecPath in toUpdate:
117		if path != bin:
118			updateMachO(path, execPath, root)
119			if verbose:
120				print('Updating Mach-O load from {} to {}...'.format(oldExecPath, newExecPath))
121			subprocess.check_call([installNameTool, '-change', oldExecPath, newExecPath, bin])
122		else:
123			if verbose:
124				print('Updating Mach-O id from {} to {}...'.format(oldExecPath, newExecPath))
125			subprocess.check_call([installNameTool, '-id', newExecPath, bin])
126
127if __name__ == '__main__':
128	parser = argparse.ArgumentParser()
129	parser.add_argument('-R', '--root', metavar='ROOT', default='/', help='root directory to search')
130	parser.add_argument('-I', '--install-name-tool', metavar='INSTALL_NAME_TOOL', default='install_name_tool', help='path to install_name_tool')
131	parser.add_argument('-O', '--otool', metavar='OTOOL', default='otool', help='path to otool')
132	parser.add_argument('-p', '--qt-plugins', metavar='PLUGINS', default='', help='Qt plugins to include (comma-separated)')
133	parser.add_argument('-v', '--verbose', action='store_true', default=False, help='output more information')
134	parser.add_argument('bundle', help='application bundle to deploy')
135	args = parser.parse_args()
136
137	otool = args.otool
138	installNameTool = args.install_name_tool
139	verbose = args.verbose
140
141	try:
142		shutil.rmtree(os.path.join(args.bundle, 'Contents/Frameworks/'))
143	except OSError as e:
144		if e.errno != errno.ENOENT:
145			raise
146
147	for executable in os.listdir(os.path.join(args.bundle, 'Contents/MacOS')):
148		if executable.endswith('.dSYM'):
149			continue
150		fullPath = os.path.join(args.bundle, 'Contents/MacOS/', executable)
151		updateMachO(fullPath, splitPath(os.path.join(args.bundle, 'Contents/MacOS')), splitPath(args.root))
152	if args.qt_plugins:
153		try:
154			shutil.rmtree(os.path.join(args.bundle, 'Contents/PlugIns/'))
155		except OSError as e:
156			if e.errno != errno.ENOENT:
157				raise
158		makedirs(os.path.join(args.bundle, 'Contents/PlugIns'))
159		makedirs(os.path.join(args.bundle, 'Contents/Resources'))
160		with open(os.path.join(args.bundle, 'Contents/Resources/qt.conf'), 'w') as conf:
161			conf.write('[Paths]\nPlugins = PlugIns\n')
162		plugins = args.qt_plugins.split(',')
163		for plugin in plugins:
164			plugin = plugin.strip()
165			kind, plug = os.path.split(plugin)
166			newDir = os.path.join(args.bundle, 'Contents/PlugIns/', kind)
167			makedirs(newDir)
168			newPath = os.path.join(newDir, plug)
169			shutil.copy2(os.path.join(qtPath, 'plugins', plugin), newPath)
170			updateMachO(newPath, splitPath(os.path.join(args.bundle, 'Contents/MacOS')), splitPath(args.root))