|
22 | 22 | import("core.base.option") |
23 | 23 | import("core.theme.theme") |
24 | 24 | import("core.project.depend") |
| 25 | +import("lib.detect.find_library") |
25 | 26 | import("private.tools.codesign") |
| 27 | +import("private.utils.target", {alias = "target_utils"}) |
| 28 | +import("utils.binary.deplibs", {alias = "get_depend_libraries"}) |
26 | 29 | import("utils.progress") |
| 30 | + |
| 31 | +local function _is_non_system_dylib(libfile) |
| 32 | + return libfile and libfile:endswith(".dylib") |
| 33 | + and not libfile:startswith("/usr/lib/") |
| 34 | + and not libfile:startswith("/System/Library/") |
| 35 | +end |
| 36 | + |
| 37 | +local function _get_target_linkdirs(target) |
| 38 | + local linkdirs = {} |
| 39 | + for _, values in ipairs(table.wrap(target:get_from("linkdirs", "*"))) do |
| 40 | + for _, linkdir in ipairs(table.wrap(values)) do |
| 41 | + table.insert(linkdirs, path.absolute(linkdir)) |
| 42 | + end |
| 43 | + end |
| 44 | + return table.unique(linkdirs) |
| 45 | +end |
| 46 | + |
| 47 | +local function _get_target_linklibfiles(target) |
| 48 | + local linkdirs = _get_target_linkdirs(target) |
| 49 | + local libfiles = {} |
| 50 | + for _, values in ipairs(table.wrap(target:get_from("links", "*"))) do |
| 51 | + for _, link in ipairs(table.wrap(values)) do |
| 52 | + local libinfo = find_library(link, linkdirs, {plat = target:plat(), kind = "shared"}) |
| 53 | + if libinfo then |
| 54 | + table.insert(libfiles, path.join(libinfo.linkdir, libinfo.filename)) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + return table.unique(libfiles) |
| 59 | +end |
| 60 | + |
27 | 61 | function main (target, opt) |
28 | 62 |
|
29 | 63 | -- get app and resources directory |
@@ -51,18 +85,46 @@ function main (target, opt) |
51 | 85 | try { function () os.vrunv("install_name_tool", {"-delete_rpath", "@loader_path", targetfile}) end } |
52 | 86 | os.vrunv("install_name_tool", {"-add_rpath", "@executable_path/../Frameworks", targetfile}) |
53 | 87 |
|
54 | | - -- copy dependent dynamic libraries and frameworks |
| 88 | + -- copy dependent frameworks and dynamic libraries |
| 89 | + local frameworks_to_copy = {} |
| 90 | + local framework_targetfiles = {} |
55 | 91 | for _, dep in ipairs(target:orderdeps()) do |
56 | | - if dep:kind() == "shared" then |
57 | | - if not os.isdir(frameworksdir) then |
58 | | - os.mkdir(frameworksdir) |
59 | | - end |
60 | | - local frameworkdir = dep:data("xcode.bundle.rootdir") |
61 | | - if dep:rule("xcode.framework") and frameworkdir then |
62 | | - os.cp(frameworkdir, frameworksdir, {symlink = true}) |
63 | | - else |
64 | | - os.vcp(dep:targetfile(), frameworksdir) |
65 | | - end |
| 92 | + local frameworkdir = dep:data("xcode.bundle.rootdir") |
| 93 | + if dep:rule("xcode.framework") and frameworkdir then |
| 94 | + table.insert(frameworks_to_copy, frameworkdir) |
| 95 | + framework_targetfiles[path.absolute(dep:targetfile())] = true |
| 96 | + end |
| 97 | + end |
| 98 | + local libfiles = {} |
| 99 | + target_utils.get_target_libfiles(target, libfiles, target:targetfile(), {}) |
| 100 | + table.join2(libfiles, _get_target_linklibfiles(target)) |
| 101 | + local dependfiles = get_depend_libraries(target:targetfile(), { |
| 102 | + plat = target:plat(), |
| 103 | + arch = target:arch(), |
| 104 | + recursive = true, |
| 105 | + resolve_path = true, |
| 106 | + resolve_hint_paths = libfiles |
| 107 | + }) |
| 108 | + for _, dependfile in ipairs(table.wrap(dependfiles)) do |
| 109 | + if _is_non_system_dylib(dependfile) then |
| 110 | + table.insert(libfiles, dependfile) |
| 111 | + end |
| 112 | + end |
| 113 | + local dylibs_to_copy = {} |
| 114 | + for _, libfile in ipairs(table.unique(libfiles)) do |
| 115 | + if not framework_targetfiles[path.absolute(libfile)] then |
| 116 | + table.insert(dylibs_to_copy, libfile) |
| 117 | + end |
| 118 | + end |
| 119 | + if #frameworks_to_copy > 0 or #dylibs_to_copy > 0 then |
| 120 | + if not os.isdir(frameworksdir) then |
| 121 | + os.mkdir(frameworksdir) |
| 122 | + end |
| 123 | + for _, frameworkdir in ipairs(frameworks_to_copy) do |
| 124 | + os.cp(frameworkdir, frameworksdir, {symlink = true}) |
| 125 | + end |
| 126 | + for _, libfile in ipairs(dylibs_to_copy) do |
| 127 | + os.vcp(libfile, frameworksdir) |
66 | 128 | end |
67 | 129 | end |
68 | 130 |
|
@@ -109,4 +171,3 @@ function main (target, opt) |
109 | 171 |
|
110 | 172 | end, {dependfile = target:dependfile(bundledir), files = {bundledir, target:targetfile()}, changed = target:is_rebuilt()}) |
111 | 173 | end |
112 | | - |
|
0 commit comments