@@ -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" )
0 commit comments