diff options
| author | ruki <[email protected]> | 2024-07-17 00:53:43 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2024-07-17 00:53:43 +0800 |
| commit | 10df5c458fcf66832c0ef437157c7397f2283b18 (patch) | |
| tree | f36c3c47493e442828226aebd4e065b80202b3ef | |
| parent | 4c983a4dc236397c6bee9cf6e06f089d12a9f259 (diff) | |
install rpath
| -rw-r--r-- | tests/actions/install/xmake.lua | 1 | ||||
| -rw-r--r-- | xmake/modules/target/action/install/main.lua | 25 | ||||
| -rw-r--r-- | xmake/modules/utils/binary/rpath.lua | 16 |
3 files changed, 42 insertions, 0 deletions
diff --git a/tests/actions/install/xmake.lua b/tests/actions/install/xmake.lua index 8dc12f176..18617c971 100644 --- a/tests/actions/install/xmake.lua +++ b/tests/actions/install/xmake.lua @@ -16,6 +16,7 @@ target("app") set_kind("binary") add_deps("foo") add_files("src/main.cpp") + add_rpathdirs("@loader_path/../lib", {installonly = true}) -- set_prefixdir("app") includes("@builtin/xpack") diff --git a/xmake/modules/target/action/install/main.lua b/xmake/modules/target/action/install/main.lua index 82a6eea8d..6abc513b8 100644 --- a/xmake/modules/target/action/install/main.lua +++ b/xmake/modules/target/action/install/main.lua @@ -22,6 +22,7 @@ import("core.base.option") import("core.base.hashset") import("utils.binary.deplibs", {alias = "get_depend_libraries"}) +import("utils.binary.rpath", {alias = "rpath_utils"}) function _get_target_package_libfiles(target, opt) if option.get("nopkgs") then @@ -132,6 +133,29 @@ function _install_shared_libraries(target, opt) end end +-- update install rpath, we can only get and update rpathdirs with `{installonly = true}` +-- e.g. add_rpathdirs("@loader_path/../lib", {installonly = true}) +function _update_install_rpath(target, opt) + local bindir = target:bindir() + local targetfile = path.join(bindir, target:filename()) + rpath_utils.remove_all(targetfile, {plat = target:plat(), arch = target:arch()}) + if target:policy("install.rpath") then + local result, sources = target:get_from("rpathdirs", "*") + if result and sources then + for idx, rpathdirs in ipairs(result) do + local source = sources[idx] + local extraconf = target:extraconf_from("rpathdirs", source) + for _, rpathdir in ipairs(rpathdirs) do + local extra = extraconf[rpathdir] + if extra and extra.installonly then + rpath_utils.insert(targetfile, rpathdir, {plat = target:plat(), arch = target:arch()}) + end + end + end + end + end +end + -- install binary function _install_binary(target, opt) local bindir = target:bindir() @@ -139,6 +163,7 @@ function _install_binary(target, opt) os.vcp(target:targetfile(), bindir) os.trycp(target:symbolfile(), path.join(bindir, path.filename(target:symbolfile()))) _install_shared_libraries(target, opt) + _update_install_rpath(target, opt) end -- install shared library diff --git a/xmake/modules/utils/binary/rpath.lua b/xmake/modules/utils/binary/rpath.lua index ad42970ca..c47f7c768 100644 --- a/xmake/modules/utils/binary/rpath.lua +++ b/xmake/modules/utils/binary/rpath.lua @@ -22,6 +22,18 @@ import("core.base.option") import("lib.detect.find_tool") +function _replace_rpath_vars(rpath, opt) + local plat = opt.plat or os.host() + local arch = opt.arch or os.arch() + if plat == "macosx" or plat == "iphoneos" or plat == "appletvos" or plat == "watchos" then + rpath = rpath:gsub("@loader_path", "$ORIGIN") + rpath = rpath:gsub("@executable_path", "$ORIGIN") + else + rpath = rpath:gsub("%$ORIGIN", "%loader_path") + end + return rpath +end + function _get_rpath_list_by_objdump(binaryfile, opt) local list local plat = opt.plat or os.host() @@ -218,6 +230,7 @@ function insert(binaryfile, rpath, opt) if is_host("macosx") then table.insert(ops, 1, _insert_rpath_by_install_name_tool) end + rpath = _replace_rpath_vars(rpath, opt) for _, op in ipairs(ops) do if op(binaryfile, rpath, opt) then break @@ -232,6 +245,8 @@ function change(binaryfile, rpath_old, rpath_new, opt) if is_host("macosx") then table.insert(ops, 1, _change_rpath_by_install_name_tool) end + rpath_old = _replace_rpath_vars(rpath_old, opt) + rpath_new = _replace_rpath_vars(rpath_new, opt) for _, op in ipairs(ops) do if op(binaryfile, rpath_old, rpath_new, opt) then break @@ -246,6 +261,7 @@ function remove(binaryfile, rpath, opt) if is_host("macosx") then table.insert(ops, 1, _remove_rpath_by_install_name_tool) end + rpath = _replace_rpath_vars(rpath, opt) for _, op in ipairs(ops) do if op(binaryfile, rpath, opt) then break |
