diff options
| author | ruki <[email protected]> | 2025-05-16 10:42:01 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-05-16 10:42:01 +0800 |
| commit | 77bd76e903cf2dbcef1ca7e4e1fa495edc6432fb (patch) | |
| tree | 3e795dd6865f8c32c39002c60fad025ccc296e38 | |
| parent | 9375cff987bea0a04e9772f72d266757925ff112 (diff) | |
| parent | 0e8e07edbee9be33e7e47e9b030e47fe56785fc4 (diff) | |
Merge pull request #6445 from Doekin/pkgconfig
package(install): use relative paths in generated .pc files to improve relocatability
| -rw-r--r-- | xmake/modules/private/action/require/impl/actions/install.lua | 38 | ||||
| -rw-r--r-- | xmake/modules/target/action/install/pkgconfig_importfiles.lua | 15 |
2 files changed, 37 insertions, 16 deletions
diff --git a/xmake/modules/private/action/require/impl/actions/install.lua b/xmake/modules/private/action/require/impl/actions/install.lua index 5e0c2c542..4d0bada1f 100644 --- a/xmake/modules/private/action/require/impl/actions/install.lua +++ b/xmake/modules/private/action/require/impl/actions/install.lua @@ -41,17 +41,33 @@ function _patch_pkgconfig(package) return end + local installdir = path.unix(path.normalize(package:installdir())) + -- get lib/pkgconfig/*.pc or share/pkgconfig/*.pc file local libpkgconfigdir = path.join(package:installdir(), "lib", "pkgconfig") local sharepkgconfigdir = path.join(package:installdir(), "share", "pkgconfig") - local pcfile = (os.isdir(libpkgconfigdir) and find_file("*.pc", libpkgconfigdir)) - or (os.isdir(sharepkgconfigdir) and find_file("*.pc", sharepkgconfigdir)) or nil - if pcfile then + local pcfiles = {} + table.join2(pcfiles, os.isdir(libpkgconfigdir) and os.files(path.join(libpkgconfigdir, "*.pc")) or {}) + table.join2(pcfiles, os.isdir(sharepkgconfigdir) and os.files(path.join(sharepkgconfigdir, "*.pc")) or {}) + if #pcfiles > 0 then + for _, pcfile in ipairs(pcfiles) do + local pcfile_content = io.readfile(pcfile) + if pcfile_content then + local pcfile_content, count = pcfile_content:replace(installdir, "${installdir}", {plain = true}) + if count > 0 then + local line_ending = pcfile_content:find("\r\n") and "\r\n" or "\n" + pcfile_content = "# Modified by Xmake: Using relative paths to make package relocatable" .. line_ending + .. "installdir=${pcfiledir}/" .. path.unix(path.relative(installdir, path.directory(pcfile))) .. line_ending + .. pcfile_content + io.writefile(pcfile, pcfile_content) + end + end + end return end -- trace - pcfile = path.join(libpkgconfigdir, package:name() .. ".pc") + local pcfile = path.join(libpkgconfigdir, package:name() .. ".pc") vprint("patching %s ..", pcfile) -- fetch package @@ -62,10 +78,10 @@ function _patch_pkgconfig(package) -- get libs local libs = "" - local installdir = package:installdir() + local base_libdir = path.join(installdir, "lib") for _, linkdir in ipairs(fetchinfo.linkdirs) do - if linkdir ~= path.join(installdir, "lib") then - libs = libs .. " -L" .. (linkdir:gsub("\\", "/")) + if linkdir ~= base_libdir then + libs = libs .. " -L" .. "${libdir}/" .. path.unix(path.relative(linkdir, base_libdir)) end end libs = libs .. " -L${libdir}" @@ -78,9 +94,10 @@ function _patch_pkgconfig(package) -- cflags local cflags = "" + local base_includedir = path.join(installdir, "include") for _, includedir in ipairs(fetchinfo.includedirs or fetchinfo.sysincludedirs) do - if includedir ~= path.join(installdir, "include") then - cflags = cflags .. " -I" .. (includedir:gsub("\\", "/")) + if includedir ~= base_includedir then + cflags = cflags .. " -I" .. "${includedir}/" .. path.unix(path.relative(includedir, base_includedir)) end end cflags = cflags .. " -I${includedir}" @@ -91,7 +108,8 @@ function _patch_pkgconfig(package) -- patch a *.pc file local file = io.open(pcfile, 'w') if file then - file:print("prefix=%s", installdir:gsub("\\", "/")) + file:print("# Generated by Xmake") + file:print("prefix=%s", "${pcfiledir}/" .. path.unix(path.relative(installdir, path.directory(pcfile)))) file:print("exec_prefix=${prefix}") file:print("libdir=${exec_prefix}/lib") file:print("includedir=${prefix}/include") diff --git a/xmake/modules/target/action/install/pkgconfig_importfiles.lua b/xmake/modules/target/action/install/pkgconfig_importfiles.lua index 65da32085..67ab51b64 100644 --- a/xmake/modules/target/action/install/pkgconfig_importfiles.lua +++ b/xmake/modules/target/action/install/pkgconfig_importfiles.lua @@ -24,7 +24,7 @@ function main(target, opt) -- check opt = opt or {} assert(target:is_library(), 'pkgconfig_importfiles: only support for library target(%s)!', target:name()) - local installdir = target:installdir() + local installdir = path.unix(path.normalize(target:installdir())) if not installdir then return end @@ -41,9 +41,10 @@ function main(target, opt) -- get libs local libs = "" + local base_libdir = path.join(installdir, "lib") for _, linkdir in ipairs(linkdirs) do - if linkdir ~= path.join(installdir, "lib") then - libs = libs .. " -L" .. (linkdir:gsub("\\", "/")) + if linkdir ~= base_libdir then + libs = libs .. " -L" .. "${libdir}/" .. path.unix(path.relative(linkdir, base_libdir)) end end libs = libs .. " -L${libdir}" @@ -56,9 +57,10 @@ function main(target, opt) -- get cflags local cflags = "" + local base_includedir = path.join(installdir, "include") for _, includedir in ipairs(includedirs) do - if includedir ~= path.join(installdir, "include") then - cflags = cflags .. " -I" .. (includedir:gsub("\\", "/")) + if includedir ~= base_includedir then + cflags = cflags .. " -I" .. "${includedir}/" .. path.unix(path.relative(includedir, base_includedir)) end end cflags = cflags .. " -I${includedir}" @@ -70,7 +72,8 @@ function main(target, opt) -- generate a *.pc file local file = io.open(pcfile, 'w') if file then - file:print("prefix=%s", installdir:gsub("\\", "/")) + file:print("# Generated by Xmake") + file:print("prefix=%s", "${pcfiledir}/" .. path.unix(path.relative(installdir, path.directory(pcfile)))) file:print("exec_prefix=${prefix}") file:print("libdir=${exec_prefix}/lib") file:print("includedir=${prefix}/include") |
