/
build-mac-package
executable file
·185 lines (154 loc) · 7.2 KB
/
build-mac-package
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
from optparse import OptionParser
from os import chdir, listdir, makedirs, sep
from os.path import abspath, basename, dirname, isdir, isfile, join
from shutil import copytree, rmtree
from subprocess import PIPE, Popen, call
from sys import argv, exit, stdout
from tempfile import mkdtemp
from util import (
MAJOR_VERSION,
MINOR_VERSION,
PLATFORM_MACX,
REVISION,
getPlatform,
writeTemplate
)
def copyLibraries(binaryFile, installPath, libDestinationDir,
ignoreDirectories=[], executablePath=None, dependencyMap={}):
if executablePath is None:
executablePath = binaryFile
ignoreDirectories = [(abspath(d) + sep) for d in ignoreDirectories]
for dependency in getDependencies(binaryFile):
dependencyPath = join(executablePath, dependency)
if any((dependencyPath.startswith(d) for d in ignoreDirectories)):
continue
libFile = basename(dependency)
if libFile == basename(binaryFile):
continue
# The dependency isn't a self-reference. See if it's in the dependency
# map.
location = dependencyMap.get(libFile)
if location is None:
# We'll infer the new location of the dependency.
destinationPath = join(libDestinationDir, libFile)
location = join(installPath, libFile)
if not isfile(destinationPath):
# The dependency hasn't been copied over. So, copy it over,
# and update library references.
if not isfile(dependencyPath):
raise Exception("dependency '%s' does not exist" %
dependencyPath)
destinationDir = dirname(destinationPath)
if not isdir(destinationDir):
makedirs(destinationDir)
stdout.write("Copying library '%s' to '%s' ...\n" %
(dependencyPath, destinationPath))
copyfile(dependencyPath, destinationPath)
copyLibraries(destinationPath, installPath, libDestinationDir,
ignoreDirectories, executablePath)
setLibraryLocation(destinationPath, location)
stdout.write("Replacing dependency '%s' with '%s' in '%s' ...\n" %
(dependency, location, binaryFile))
args = ["install_name_tool", "-change", dependency, location,
binaryFile]
if call(args):
raise Exception("error replacing dependency '%s' with '%s' in "
"'%s'" % (dependency, location, binaryFile))
def getDependencies(binaryFile):
"""Uses `otool` to get the libraries that the specified binary file (either
an executable or a dynamic library) depends on. Returns the list of
libraries."""
args = ["otool", "-L", binaryFile]
process = Popen(args, stdout=PIPE)
stdoutData, stderrData = process.communicate()
if process.returncode:
raise Exception("error getting dependencies for '%s'" % binaryFile)
dependencies = []
for s in stdoutData.strip().splitlines()[1:]:
s = s.strip()
pos = s.find(" (compatibility ")
if pos == -1:
raise Exception("unexpected line output from libtool: '%s'" % s)
dependencies.append(s[:pos].strip())
return dependencies
def setLibraryLocation(libraryFile, location):
"""Uses `install_name_tool` to set the id of the given library file to the
given location."""
stdout.write("Setting library location of '%s' to '%s' ...\n" %
(libraryFile, location))
args = ["install_name_tool", "-id", location, libraryFile]
if call(args):
raise Exception("error setting library location to '%s' for '%s'" %
(location, libraryFile))
def main():
if getPlatform() != PLATFORM_MACX:
exit("This build script is only meant to be run on Mac OSX")
parser = OptionParser("usage: %prog package")
options, args = parser.parse_args()
# Validate arguments
if len(args) != 1:
parser.error("incorrect number of required arguments")
package = args[0]
tempDir = mkdtemp()
try:
stdout.write("Configuring synthclone for build ...\n")
appDir = join(tempDir, "synthclone.app")
contentsDir = join(appDir, "Contents")
execDir = join(contentsDir, "MacOS")
frameworksDir = join(contentsDir, "Frameworks")
pluginsDir = join(contentsDir, "PlugIns")
args = ["./configure", "--bin-dir=%s" % execDir,
"--data-dir=%s" % join(contentsDir, "Resources"),
"--lib-dir=%s" % frameworksDir, "--plugin-dir=%s" % pluginsDir,
"--skip-api-docs=1", "--skip-headers=1", "--skip-lv2=1",
"--skip-renoise=1"]
if call(args):
raise Exception("configure returned an error")
stdout.write("Building synthclone ...\n")
args = ["make"]
if call(args):
raise Exception("make returned an error")
stdout.write("Creating package skeleton in '%s' ...\n" % tempDir)
args = ["make", "install"]
if call(args):
raise Exception("make install returned an error")
stdout.write("Copying dependencies to package skeleton ...\n")
if call(["macdeployqt", appDir]):
parser.error("macdeployqt returned an error")
stdout.write("Generating Qt dependency map ...\n")
executablePath = join(contentsDir, "MacOS", "synthclone")
qtDependencyMap = {}
for dependency in getDependencies(executablePath):
baseLibName = basename(dependency)
if baseLibName.startswith("Qt"):
qtDependencyMap[baseLibName] = dependency
stdout.write("Copying plugin dependencies to package skeleton ...\n")
ignoreDirectories = ["/lib", "/usr/lib", "/Library/Frameworks",
"/System/Library/Frameworks"]
relativeFrameworksPath = "@executable_path/../Frameworks/"
for f in listdir(pluginsDir):
plugin = join(pluginsDir, f)
if not isfile(plugin):
continue
setLibraryLocation(plugin, "@executable_path/../PlugIns/%s" % f)
stdout.write("Copying dependencies for '%s' ...\n" % plugin)
copyLibraries(plugin, relativeFrameworksPath, frameworksDir,
ignoreDirectories, executablePath, qtDependencyMap)
stdout.write("Copying library dependencies to package skeleton ...\n")
libPath = join(frameworksDir, "libsynthclone.dylib")
copyLibraries(libPath, relativeFrameworksPath, frameworksDir,
ignoreDirectories, executablePath, qtDependencyMap)
stdout.write("Generating Info.plist ...\n")
# XXX: Write me.
stdout.write("Generating package from skeleton ...\n")
args = ["hdiutil", "create", "-format", "UDBZ", "-srcfolder", appDir,
package]
if call(args):
parser.error("hdiutil returned an error")
finally:
stdout.write("Check %s ...\n" % tempDir)
# rmtree(tempDir)
stdout.write("Package '%s' built successfully.\n" % package)
if __name__ == "__main__":
main()