2012-08-13 10:08:22 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import sys
|
2013-04-08 19:44:46 +00:00
|
|
|
from optparse import OptionParser
|
2013-06-30 07:12:01 +00:00
|
|
|
from textwrap import dedent
|
2012-08-13 10:08:22 +00:00
|
|
|
|
|
|
|
def sh(command):
|
|
|
|
return os.popen(command).read()
|
|
|
|
|
|
|
|
def dylib_lst(input_file):
|
|
|
|
return sh("otool -L %s | grep -e '\t' | awk '{ print $1 }'" % input_file)
|
|
|
|
|
|
|
|
sys_re = re.compile("/System")
|
|
|
|
exe_re = re.compile("@executable_path")
|
|
|
|
binary_name = sys.argv[1]
|
|
|
|
|
|
|
|
def is_user_lib(libname, input_file):
|
|
|
|
return not sys_re.match(libname) and \
|
|
|
|
not exe_re.match(libname) and \
|
|
|
|
not "libobjc" in libname and \
|
|
|
|
not "libSystem" in libname and \
|
|
|
|
not "libgcc" in libname and \
|
|
|
|
not os.path.basename(input_file) in libname and \
|
|
|
|
not libname == ''
|
|
|
|
|
|
|
|
def user_dylib_lst(input_file):
|
|
|
|
return [lib for lib in dylib_lst(input_file).split("\n") if
|
|
|
|
is_user_lib(lib, input_file)]
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
def bundle_path(binary_name):
|
2012-08-13 10:08:22 +00:00
|
|
|
return "%s.app" % binary_name
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
def bundle_name(binary_name):
|
|
|
|
return os.path.basename(bundle_path(binary_name))
|
|
|
|
|
2013-04-08 19:44:46 +00:00
|
|
|
def target_plist(binary_name):
|
2013-07-16 11:28:28 +00:00
|
|
|
return os.path.join(bundle_path(binary_name), 'Contents', 'Info.plist')
|
2012-08-13 10:08:22 +00:00
|
|
|
|
2013-04-08 19:44:46 +00:00
|
|
|
def target_directory(binary_name):
|
2013-07-16 11:28:28 +00:00
|
|
|
return os.path.join(bundle_path(binary_name), 'Contents', 'MacOS')
|
2012-08-13 10:08:22 +00:00
|
|
|
|
2013-04-08 19:44:46 +00:00
|
|
|
def target_binary(binary_name):
|
2013-07-16 11:28:28 +00:00
|
|
|
return os.path.join(target_directory(binary_name),
|
|
|
|
os.path.basename(binary_name))
|
2012-08-13 10:08:22 +00:00
|
|
|
|
2013-04-08 19:44:46 +00:00
|
|
|
def copy_bundle(binary_name):
|
2013-07-16 11:28:28 +00:00
|
|
|
if os.path.isdir(bundle_path(binary_name)):
|
|
|
|
shutil.rmtree(bundle_path(binary_name))
|
2012-08-13 10:08:22 +00:00
|
|
|
shutil.copytree(
|
2013-04-08 19:44:46 +00:00
|
|
|
os.path.join('TOOLS', 'osxbundle', bundle_name(binary_name)),
|
2013-07-16 11:28:28 +00:00
|
|
|
bundle_path(binary_name))
|
2012-08-13 10:08:22 +00:00
|
|
|
|
2013-04-08 19:44:46 +00:00
|
|
|
def copy_binary(binary_name):
|
|
|
|
shutil.copy(binary_name, target_binary(binary_name))
|
2012-08-13 10:08:22 +00:00
|
|
|
|
2012-11-05 22:49:24 +00:00
|
|
|
def run_install_name_tool(target_file, dylib_path, dest_dir, root=True):
|
2012-08-13 10:08:22 +00:00
|
|
|
new_dylib_path = os.path.join("@executable_path", "lib",
|
|
|
|
os.path.basename(dylib_path))
|
|
|
|
|
|
|
|
sh("install_name_tool -change %s %s %s" % \
|
|
|
|
(dylib_path, new_dylib_path, target_file))
|
2012-11-05 22:49:24 +00:00
|
|
|
if root:
|
|
|
|
sh("install_name_tool -id %s %s" % \
|
|
|
|
(new_dylib_path, os.path.join(dest_dir,
|
|
|
|
os.path.basename(dylib_path))))
|
2012-08-13 10:08:22 +00:00
|
|
|
|
2012-11-05 22:49:24 +00:00
|
|
|
def cp_dylibs(target_file, dest_dir):
|
2012-08-13 10:08:22 +00:00
|
|
|
for dylib_path in user_dylib_lst(target_file):
|
2012-11-05 22:49:24 +00:00
|
|
|
dylib_dest_path = os.path.join(dest_dir, os.path.basename(dylib_path))
|
2012-12-16 09:56:25 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
shutil.copy(dylib_path, dylib_dest_path)
|
|
|
|
except IOError:
|
2013-06-30 07:12:01 +00:00
|
|
|
if re.match("dylib$", target_file):
|
|
|
|
reinstall_what = target_file
|
|
|
|
else:
|
|
|
|
reinstall_what = dylib_path
|
|
|
|
|
|
|
|
sys.exit(dedent("""\
|
|
|
|
%s uses library %s which is not available anymore.
|
|
|
|
This is most likely because you uninstalled %s.
|
|
|
|
Please reinstall %s to fix it's dependencies.""" % \
|
|
|
|
(target_file, dylib_path, dylib_path, reinstall_what) ))
|
2012-12-16 09:56:25 +00:00
|
|
|
|
2012-11-05 22:49:24 +00:00
|
|
|
os.chmod(dylib_dest_path, 0o755)
|
|
|
|
cp_dylibs(dylib_dest_path, dest_dir)
|
2012-08-13 10:08:22 +00:00
|
|
|
|
2012-11-05 22:49:24 +00:00
|
|
|
def fix_dylibs_paths(target_file, dest_dir, root=True):
|
2012-08-13 10:08:22 +00:00
|
|
|
for dylib_path in user_dylib_lst(target_file):
|
2012-11-05 22:49:24 +00:00
|
|
|
dylib_dest_path = os.path.join(dest_dir, os.path.basename(dylib_path))
|
|
|
|
run_install_name_tool(target_file, dylib_path, dest_dir, root)
|
|
|
|
fix_dylibs_paths(dylib_dest_path, dest_dir, False)
|
2012-08-13 10:08:22 +00:00
|
|
|
|
|
|
|
def apply_plist_template(plist_file, version):
|
2013-07-16 11:28:28 +00:00
|
|
|
sh("sed -i -e 's/${VERSION}/%s/g' %s" % (version, plist_file))
|
2012-08-13 10:08:22 +00:00
|
|
|
|
2013-04-08 19:44:46 +00:00
|
|
|
def bundle_dependencies(binary_name):
|
|
|
|
lib_bundle_directory = os.path.join(target_directory(binary_name), "lib")
|
|
|
|
cp_dylibs(binary_name, lib_bundle_directory)
|
|
|
|
fix_dylibs_paths(target_binary(binary_name), lib_bundle_directory)
|
2012-08-13 10:08:22 +00:00
|
|
|
|
2013-04-08 19:44:46 +00:00
|
|
|
def main():
|
2013-12-09 20:45:09 +00:00
|
|
|
version = sh("./version.sh --print").strip()
|
2012-08-13 10:08:22 +00:00
|
|
|
|
2013-04-08 19:44:46 +00:00
|
|
|
usage = "usage: %prog [options] arg"
|
|
|
|
parser = OptionParser(usage)
|
|
|
|
parser.add_option("-s", "--skip-deps", action="store_false", dest="deps",
|
|
|
|
default=True,
|
|
|
|
help="don't bundle the dependencies")
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if len(args) != 1:
|
|
|
|
parser.error("incorrect number of arguments")
|
|
|
|
else:
|
|
|
|
binary_name = args[0]
|
|
|
|
|
|
|
|
print("Creating Mac OS X application bundle (version: %s)..." % version)
|
|
|
|
print("> copying bundle skeleton")
|
|
|
|
copy_bundle(binary_name)
|
|
|
|
print("> copying binary")
|
|
|
|
copy_binary(binary_name)
|
|
|
|
print("> generating Info.plist")
|
|
|
|
apply_plist_template(target_plist(binary_name), version)
|
|
|
|
|
|
|
|
if options.deps:
|
|
|
|
print("> bundling dependencies")
|
|
|
|
bundle_dependencies(binary_name)
|
|
|
|
|
|
|
|
print("done.")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|