Skip to content

Commit 0682d7c

Browse files
committed
package(install): use relative paths in generated .pc files to improve relocatability
1 parent e2dafa0 commit 0682d7c

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

xmake/modules/private/action/require/impl/actions/install.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ function _patch_pkgconfig(package)
4141
return
4242
end
4343

44+
local installdir = path.unix(path.normalize(package:installdir()))
45+
4446
-- get lib/pkgconfig/*.pc or share/pkgconfig/*.pc file
4547
local libpkgconfigdir = path.join(package:installdir(), "lib", "pkgconfig")
4648
local sharepkgconfigdir = path.join(package:installdir(), "share", "pkgconfig")
@@ -62,10 +64,10 @@ function _patch_pkgconfig(package)
6264

6365
-- get libs
6466
local libs = ""
65-
local installdir = package:installdir()
67+
local base_libdir = path.join(installdir, "lib")
6668
for _, linkdir in ipairs(fetchinfo.linkdirs) do
67-
if linkdir ~= path.join(installdir, "lib") then
68-
libs = libs .. " -L" .. (linkdir:gsub("\\", "/"))
69+
if linkdir ~= base_libdir then
70+
libs = libs .. " -L" .. "${libdir}/" .. path.unix(path.relative(linkdir, base_libdir))
6971
end
7072
end
7173
libs = libs .. " -L${libdir}"
@@ -78,9 +80,10 @@ function _patch_pkgconfig(package)
7880

7981
-- cflags
8082
local cflags = ""
83+
local base_includedir = path.join(installdir, "include")
8184
for _, includedir in ipairs(fetchinfo.includedirs or fetchinfo.sysincludedirs) do
82-
if includedir ~= path.join(installdir, "include") then
83-
cflags = cflags .. " -I" .. (includedir:gsub("\\", "/"))
85+
if includedir ~= base_includedir then
86+
cflags = cflags .. " -I" .. "${includedir}/" .. path.unix(path.relative(includedir, base_includedir))
8487
end
8588
end
8689
cflags = cflags .. " -I${includedir}"
@@ -92,7 +95,7 @@ function _patch_pkgconfig(package)
9295
local file = io.open(pcfile, 'w')
9396
if file then
9497
file:print("# Generated by Xmake")
95-
file:print("prefix=%s", installdir:gsub("\\", "/"))
98+
file:print("prefix=%s", "${pcfiledir}/" .. path.unix(path.relative(installdir, path.directory(pcfile))))
9699
file:print("exec_prefix=${prefix}")
97100
file:print("libdir=${exec_prefix}/lib")
98101
file:print("includedir=${prefix}/include")

xmake/modules/target/action/install/pkgconfig_importfiles.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function main(target, opt)
2424
-- check
2525
opt = opt or {}
2626
assert(target:is_library(), 'pkgconfig_importfiles: only support for library target(%s)!', target:name())
27-
local installdir = target:installdir()
27+
local installdir = path.unix(path.normalize(target:installdir()))
2828
if not installdir then
2929
return
3030
end
@@ -41,9 +41,10 @@ function main(target, opt)
4141

4242
-- get libs
4343
local libs = ""
44+
local base_libdir = path.join(installdir, "lib")
4445
for _, linkdir in ipairs(linkdirs) do
45-
if linkdir ~= path.join(installdir, "lib") then
46-
libs = libs .. " -L" .. (linkdir:gsub("\\", "/"))
46+
if linkdir ~= base_libdir then
47+
libs = libs .. " -L" .. "${libdir}/" .. path.unix(path.relative(linkdir, base_libdir))
4748
end
4849
end
4950
libs = libs .. " -L${libdir}"
@@ -56,9 +57,10 @@ function main(target, opt)
5657

5758
-- get cflags
5859
local cflags = ""
60+
local base_includedir = path.join(installdir, "include")
5961
for _, includedir in ipairs(includedirs) do
60-
if includedir ~= path.join(installdir, "include") then
61-
cflags = cflags .. " -I" .. (includedir:gsub("\\", "/"))
62+
if includedir ~= base_includedir then
63+
cflags = cflags .. " -I" .. "${includedir}/" .. path.unix(path.relative(includedir, base_includedir))
6264
end
6365
end
6466
cflags = cflags .. " -I${includedir}"
@@ -71,7 +73,7 @@ function main(target, opt)
7173
local file = io.open(pcfile, 'w')
7274
if file then
7375
file:print("# Generated by Xmake")
74-
file:print("prefix=%s", installdir:gsub("\\", "/"))
76+
file:print("prefix=%s", "${pcfiledir}/" .. path.unix(path.relative(installdir, path.directory(pcfile))))
7577
file:print("exec_prefix=${prefix}")
7678
file:print("libdir=${exec_prefix}/lib")
7779
file:print("includedir=${prefix}/include")

0 commit comments

Comments
 (0)