From 985b2a9d54df1ac40a420dc853a7b35cd5e6e377 Mon Sep 17 00:00:00 2001 From: wsw0108 Date: Fri, 27 Oct 2023 10:13:06 +0800 Subject: [PATCH 1/2] add @rpath to link flags when soname enabled --- xmake/modules/core/tools/gcc.lua | 13 +++++++++++-- xmake/rules/linker/soname/xmake.lua | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 6589b6f134b..4f6eb903f6f 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -488,8 +488,17 @@ function linkargv(self, objectfiles, targetkind, targetfile, flags, opt) -- add rpath for dylib (macho), e.g. -install_name @rpath/file.dylib local flags_extra = {} if targetkind == "shared" and self:is_plat("macosx", "iphoneos", "watchos") then - table.insert(flags_extra, "-install_name") - table.insert(flags_extra, "@rpath/" .. path.filename(targetfile)) + local soname_enabled = false + for _, v in pairs(flags) do + if string.find(v, "-install_name") then + soname_enabled = true + break + end + end + if not soname_enabled then + table.insert(flags_extra, "-install_name") + table.insert(flags_extra, "@rpath/" .. path.filename(targetfile)) + end end -- add `-Wl,--out-implib,outputdir/libxxx.a` for xxx.dll on mingw/gcc diff --git a/xmake/rules/linker/soname/xmake.lua b/xmake/rules/linker/soname/xmake.lua index 8bb160b642f..34179f858ac 100644 --- a/xmake/rules/linker/soname/xmake.lua +++ b/xmake/rules/linker/soname/xmake.lua @@ -24,9 +24,9 @@ rule("linker.soname") if target:is_shared() and soname then if target:has_tool("sh", "gcc", "gxx", "clang", "clangxx") then if target:is_plat("macosx", "iphoneos", "watchos", "appletvos") then - target:add("shflags", "-Wl,-install_name," .. soname, {force = true}) + target:add("shflags", "-Wl,-install_name,@rpath/" .. soname, {force = true}) else - target:add("shflags", "-Wl,-soname," .. soname, {force = true}) + target:add("shflags", "-Wl,-soname,@rpath/" .. soname, {force = true}) end target:data_set("soname.enabled", true) end From 30b8576c2930cb1d188c92aed4d6bd74dbd2f644 Mon Sep 17 00:00:00 2001 From: wsw0108 Date: Fri, 27 Oct 2023 16:05:46 +0800 Subject: [PATCH 2/2] use simple api table.contains --- xmake/modules/core/tools/gcc.lua | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 4f6eb903f6f..a6a7247fb97 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -488,14 +488,7 @@ function linkargv(self, objectfiles, targetkind, targetfile, flags, opt) -- add rpath for dylib (macho), e.g. -install_name @rpath/file.dylib local flags_extra = {} if targetkind == "shared" and self:is_plat("macosx", "iphoneos", "watchos") then - local soname_enabled = false - for _, v in pairs(flags) do - if string.find(v, "-install_name") then - soname_enabled = true - break - end - end - if not soname_enabled then + if not table.contains(flags, "-install_name") then table.insert(flags_extra, "-install_name") table.insert(flags_extra, "@rpath/" .. path.filename(targetfile)) end