Skip to content

Commit 77bd76e

Browse files
authored
Merge pull request #6445 from Doekin/pkgconfig
package(install): use relative paths in generated .pc files to improve relocatability
2 parents 9375cff + 0e8e07e commit 77bd76e

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,33 @@ 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")
47-
local pcfile = (os.isdir(libpkgconfigdir) and find_file("*.pc", libpkgconfigdir))
48-
or (os.isdir(sharepkgconfigdir) and find_file("*.pc", sharepkgconfigdir)) or nil
49-
if pcfile then
49+
local pcfiles = {}
50+
table.join2(pcfiles, os.isdir(libpkgconfigdir) and os.files(path.join(libpkgconfigdir, "*.pc")) or {})
51+
table.join2(pcfiles, os.isdir(sharepkgconfigdir) and os.files(path.join(sharepkgconfigdir, "*.pc")) or {})
52+
if #pcfiles > 0 then
53+
for _, pcfile in ipairs(pcfiles) do
54+
local pcfile_content = io.readfile(pcfile)
55+
if pcfile_content then
56+
local pcfile_content, count = pcfile_content:replace(installdir, "${installdir}", {plain = true})
57+
if count > 0 then
58+
local line_ending = pcfile_content:find("\r\n") and "\r\n" or "\n"
59+
pcfile_content = "# Modified by Xmake: Using relative paths to make package relocatable" .. line_ending
60+
.. "installdir=${pcfiledir}/" .. path.unix(path.relative(installdir, path.directory(pcfile))) .. line_ending
61+
.. pcfile_content
62+
io.writefile(pcfile, pcfile_content)
63+
end
64+
end
65+
end
5066
return
5167
end
5268

5369
-- trace
54-
pcfile = path.join(libpkgconfigdir, package:name() .. ".pc")
70+
local pcfile = path.join(libpkgconfigdir, package:name() .. ".pc")
5571
vprint("patching %s ..", pcfile)
5672

5773
-- fetch package
@@ -62,10 +78,10 @@ function _patch_pkgconfig(package)
6278

6379
-- get libs
6480
local libs = ""
65-
local installdir = package:installdir()
81+
local base_libdir = path.join(installdir, "lib")
6682
for _, linkdir in ipairs(fetchinfo.linkdirs) do
67-
if linkdir ~= path.join(installdir, "lib") then
68-
libs = libs .. " -L" .. (linkdir:gsub("\\", "/"))
83+
if linkdir ~= base_libdir then
84+
libs = libs .. " -L" .. "${libdir}/" .. path.unix(path.relative(linkdir, base_libdir))
6985
end
7086
end
7187
libs = libs .. " -L${libdir}"
@@ -78,9 +94,10 @@ function _patch_pkgconfig(package)
7894

7995
-- cflags
8096
local cflags = ""
97+
local base_includedir = path.join(installdir, "include")
8198
for _, includedir in ipairs(fetchinfo.includedirs or fetchinfo.sysincludedirs) do
82-
if includedir ~= path.join(installdir, "include") then
83-
cflags = cflags .. " -I" .. (includedir:gsub("\\", "/"))
99+
if includedir ~= base_includedir then
100+
cflags = cflags .. " -I" .. "${includedir}/" .. path.unix(path.relative(includedir, base_includedir))
84101
end
85102
end
86103
cflags = cflags .. " -I${includedir}"
@@ -91,7 +108,8 @@ function _patch_pkgconfig(package)
91108
-- patch a *.pc file
92109
local file = io.open(pcfile, 'w')
93110
if file then
94-
file:print("prefix=%s", installdir:gsub("\\", "/"))
111+
file:print("# Generated by Xmake")
112+
file:print("prefix=%s", "${pcfiledir}/" .. path.unix(path.relative(installdir, path.directory(pcfile))))
95113
file:print("exec_prefix=${prefix}")
96114
file:print("libdir=${exec_prefix}/lib")
97115
file:print("includedir=${prefix}/include")

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

Lines changed: 9 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}"
@@ -70,7 +72,8 @@ function main(target, opt)
7072
-- generate a *.pc file
7173
local file = io.open(pcfile, 'w')
7274
if file then
73-
file:print("prefix=%s", installdir:gsub("\\", "/"))
75+
file:print("# Generated by Xmake")
76+
file:print("prefix=%s", "${pcfiledir}/" .. path.unix(path.relative(installdir, path.directory(pcfile))))
7477
file:print("exec_prefix=${prefix}")
7578
file:print("libdir=${exec_prefix}/lib")
7679
file:print("includedir=${prefix}/include")

0 commit comments

Comments
 (0)