Skip to content

Commit 8348fb3

Browse files
albinahlbackmaleadtgiordano
authored
Make pkg-config files relocatable post-installation (#1346)
Co-authored-by: Tim Besard <[email protected]> Co-authored-by: Mosè Giordano <[email protected]>
1 parent 49f57bf commit 8348fb3

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

src/Auditor.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,39 @@ function audit(prefix::Prefix, src_name::AbstractString = "";
209209
rm(f; force=true)
210210
end
211211

212+
# Make sure that `prefix` in pkg-config files use a relative prefix
213+
pc_files = collect_files(prefix, endswith(".pc"))
214+
pc_re = r"^prefix=(/.*)$"
215+
for f in pc_files
216+
# We want to replace every instance of `prefix=...` with
217+
# `prefix=${pcfiledir}/../..`
218+
changed = false
219+
buf = IOBuffer()
220+
for l in readlines(f)
221+
m = match(pc_re, l)
222+
if m !== nothing
223+
# dealing with an absolute path we need to relativize;
224+
# determine how many directories we need to go up.
225+
dir = m.captures[1]
226+
f_rel = relpath(f, prefix.path)
227+
ndirs = count('/', f_rel)
228+
prefix_rel = join([".." for _ in 1:ndirs], "/")
229+
l = "prefix=\${pcfiledir}/$prefix_rel"
230+
changed = true
231+
end
232+
println(buf, l)
233+
end
234+
str = String(take!(buf))
235+
236+
if changed
237+
# Overwrite file
238+
if verbose
239+
@info("Relocatize pkg-config file $f")
240+
end
241+
write(f, str)
242+
end
243+
end
244+
212245
if Sys.iswindows(platform)
213246
# We also cannot allow any symlinks in Windows because it requires
214247
# Admin privileges to create them. Orz

test/auditing.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,52 @@ end
332332
end
333333
end
334334

335+
@testset "Auditor - relocatable pkg-config prefix" begin
336+
for os in ["linux", "macos", "freebsd", "windows"]
337+
platform = Platform("x86_64", os)
338+
mktempdir() do build_path
339+
build_output_meta = nothing
340+
@test_logs (:info, r"Relocatize pkg-config file .*/destdir/lib/pkgconfig/libfoo.pc$") match_mode=:any begin
341+
build_output_meta = autobuild(
342+
build_path,
343+
"libfoo",
344+
v"1.0.0",
345+
# Copy in the libfoo sources
346+
[DirectorySource(build_tests_dir)],
347+
libfoo_autotools_script,
348+
# Build for our platform
349+
[platform],
350+
# The products we expect to be build
351+
libfoo_products,
352+
# No dependencies
353+
Dependency[];
354+
verbose = true,
355+
)
356+
end
357+
358+
# Extract our platform's build
359+
@test haskey(build_output_meta, platform)
360+
tarball_path, tarball_hash = build_output_meta[platform][1:2]
361+
@test isfile(tarball_path)
362+
363+
# Unpack it somewhere else
364+
@test verify(tarball_path, tarball_hash)
365+
testdir = joinpath(build_path, "testdir")
366+
mkdir(testdir)
367+
unpack(tarball_path, testdir)
368+
prefix = Prefix(testdir)
369+
370+
# Test that `libfoo.pc` exists and contains a relative prefix
371+
# TODO: actually use pkg-config with PKG_CONFIG_PATH
372+
contents = list_tarball_files(tarball_path)
373+
@test "lib/pkgconfig/libfoo.pc" in contents
374+
libfoo_pc = read(joinpath(testdir, "lib/pkgconfig/libfoo.pc"), String)
375+
@test contains(libfoo_pc, "prefix=\${pcfiledir}")
376+
@test !contains(libfoo_pc, "/workplace/destdir")
377+
end
378+
end
379+
end
380+
335381
@testset "Auditor - .dll moving" begin
336382
for platform in [Platform("x86_64", "windows")]
337383
mktempdir() do build_path

test/build_tests/libfoo/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ elseif (UNIX)
1515
set_target_properties(fooifier PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")
1616
endif()
1717

18+
install(FILES libfoo.pc DESTINATION lib/pkgconfig)
1819
install(TARGETS foo DESTINATION lib)
1920
install(TARGETS fooFramework DESTINATION lib)
2021
install(TARGETS fooifier RUNTIME DESTINATION bin)

test/build_tests/libfoo/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
ACLOCAL_AMFLAGS = -I m4
22

3+
pkgconfigdir = $(libdir)/pkgconfig
4+
pkgconfig_DATA = libfoo.pc
5+
36
lib_LTLIBRARIES = libfoo.la
47
libfoo_la_SOURCES = libfoo.c
58
libfoo_la_LDFLAGS = -no-undefined

test/build_tests/libfoo/libfoo.pc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=/workplace/destdir
2+
exec_prefix=${prefix}
3+
includedir=${prefix}/include
4+
libdir=${exec_prefix}/lib
5+
6+
Name: Libfoo
7+
Description: Foo library, now without bar!
8+
Version: 1.0.0
9+
Cflags: -I${includedir}
10+
Libs: -L${libdir} -lfoo

test/build_tests/libfoo/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ endif
1616
executable('fooifier', 'fooifier.cpp',
1717
link_with : libfoo, install : true,
1818
install_rpath : rpath_dir)
19+
20+
install_data('libfoo.pc', install_dir: get_option('libdir')/'pkgconfig')

0 commit comments

Comments
 (0)