From 896f7347f0b2cf947492ff3cc5d708df1b81af29 Mon Sep 17 00:00:00 2001 From: Thomas Ferreira de Lima Date: Tue, 26 Jun 2018 03:24:02 -0400 Subject: [PATCH] full python embed with dependencies. includes ssl --- macbuild/build4mac.py | 28 +++++++++++++++++++++++----- macbuild/build4mac_util.py | 16 +++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/macbuild/build4mac.py b/macbuild/build4mac.py index fad99ac65..b10769dd4 100755 --- a/macbuild/build4mac.py +++ b/macbuild/build4mac.py @@ -797,23 +797,41 @@ def DeployBinariesForBundle(): os.chmod( targetDirM + "/klayout_console", 0o0755 ) print(" [2] Relinking dylib dependencies inside Python.framework") + print(" [2.1] Patching Python Framework") depdict = WalkFrameworkPaths(pythonFrameworkPath) appPythonFrameworkPath = '@executable_path/../Frameworks/Python.framework/' - PerformChanges(depdict, [(pythonOriginalFrameworkPath, appPythonFrameworkPath)], bundleExecPathAbs) + PerformChanges(depdict, [(pythonOriginalFrameworkPath, appPythonFrameworkPath, False)], bundleExecPathAbs) + print(" [2.2] Patching /usr/local/opt/ libs") usrLocalPath = '/usr/local/opt/' appUsrLocalPath = '@executable_path/../Frameworks/' - depdict = WalkFrameworkPaths(pythonFrameworkPath) - PerformChanges(depdict, [(usrLocalPath, appUsrLocalPath)], bundleExecPathAbs, libdir=True) + replacePairs = [(usrLocalPath, appUsrLocalPath, True)] + depdict = WalkFrameworkPaths(pythonFrameworkPath, search_path_filter=r'\t+/usr/local/(opt|Cellar)') + PerformChanges(depdict, replacePairs, bundleExecPathAbs) + + print(" [2.3] Patching openssl, gdbm, readline, sqlite, tcl-tk, xz") + usrLocalPath = '/usr/local/opt' + appUsrLocalPath = '@executable_path/../Frameworks/' + replacePairs = [(usrLocalPath, appUsrLocalPath, True)] + replacePairs.extend([(openssl_version, '@executable_path/../Frameworks/openssl', True) + for openssl_version in list(Path('/usr/local/Cellar/openssl').glob('*'))]) + depdict = WalkFrameworkPaths([pythonFrameworkPath + '/../openssl', + pythonFrameworkPath + '/../gdbm', + pythonFrameworkPath + '/../readline', + pythonFrameworkPath + '/../sqlite', + pythonFrameworkPath + '/../tcl-tk', + pythonFrameworkPath + '/../xz'], search_path_filter=r'\t+/usr/local/(opt|Cellar)') + + PerformChanges(depdict, replacePairs, bundleExecPathAbs) print(" [3] Relinking dylib dependencies for klayout") klayoutPath = bundleExecPathAbs depdict = WalkFrameworkPaths(klayoutPath, filter_regex=r'klayout$') - PerformChanges(depdict, [(pythonOriginalFrameworkPath, appPythonFrameworkPath)], bundleExecPathAbs) + PerformChanges(depdict, [(pythonOriginalFrameworkPath, appPythonFrameworkPath, False)], bundleExecPathAbs) libKlayoutPath = bundleExecPathAbs + '../Frameworks' depdict = WalkFrameworkPaths(libKlayoutPath, filter_regex=r'libklayout') - PerformChanges(depdict, [(pythonOriginalFrameworkPath, appPythonFrameworkPath)], bundleExecPathAbs) + PerformChanges(depdict, [(pythonOriginalFrameworkPath, appPythonFrameworkPath, False)], bundleExecPathAbs) print(" [4] Patching site.py, pip/, and distutils/") site_module = f"{pythonFrameworkPath}/Versions/3.6/lib/python3.6/site.py" diff --git a/macbuild/build4mac_util.py b/macbuild/build4mac_util.py index edfcf00f3..48c38d77a 100755 --- a/macbuild/build4mac_util.py +++ b/macbuild/build4mac_util.py @@ -206,10 +206,12 @@ if CAN_DEPLOY_PYTHON: else: raise RuntimeError("Exceeded maximum recursion depth.") - def WalkFrameworkPaths(frameworkPaths, filter_regex=r'\.(so|dylib)$'): + def WalkFrameworkPaths(frameworkPaths, filter_regex=r'\.(so|dylib)$', search_path_filter=r'\t+/usr/local/opt'): if isinstance(frameworkPaths, str): frameworkPathsIter = [frameworkPaths] + else: + frameworkPathsIter = frameworkPaths dependency_dict = dict() @@ -223,7 +225,7 @@ if CAN_DEPLOY_PYTHON: dependency_dict[frameworkPath] = list() for idx, file in enumerate(framework_files): - dict_file = {file: WalkLibDependencyTree(file)} + dict_file = {file: WalkLibDependencyTree(file, filter_regex=search_path_filter)} dependency_dict[frameworkPath].append(dict_file) return dependency_dict @@ -277,7 +279,7 @@ if CAN_DEPLOY_PYTHON: return libNameChanges - def PerformChanges(frameworkDependencyDict, replaceFromToPairs=None, executable_path="/tmp/klayout", libdir=False): + def PerformChanges(frameworkDependencyDict, replaceFromToPairs=None, executable_path="/tmp/klayout"): libNameChanges = DetectChanges(frameworkDependencyDict) cmdNameId = XcodeToolChain['nameID'] cmdNameChg = XcodeToolChain['nameCH'] @@ -290,11 +292,15 @@ if CAN_DEPLOY_PYTHON: dependencies = next(libNameChangeIterator) except StopIteration: dependencies = list() - for replaceFrom, replaceTo in replaceFromToPairs: + for replaceFrom, replaceTo, libdir in replaceFromToPairs: replaceFrom = str(Path(replaceFrom)) replaceTo = str(Path(replaceTo)) fileName = ResolveExecutablePath(lib.replace(replaceFrom, replaceTo), executable_path) + if str(fileName).startswith('/usr'): + # print(f'skipping fileName: {fileName}') + continue + if lib.find(replaceFrom) >= 0: if libdir: frameworkPath = FindFramework(lib, replaceFrom) @@ -320,7 +326,7 @@ if CAN_DEPLOY_PYTHON: for dependency in dependencies: if dependency.find(replaceFrom) >= 0: - print("In:", fileName) + print("\tIn:", fileName) print("\tRENAME", dependency, " -> ", dependency.replace(replaceFrom, replaceTo)) # Try changing id first