Skip to content

Commit

Permalink
Make pkg-config files relocatable post-installation (#1346)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Besard <[email protected]>
Co-authored-by: Mosè Giordano <[email protected]>
  • Loading branch information
3 people authored Nov 7, 2024
1 parent 49f57bf commit 8348fb3
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Auditor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,39 @@ function audit(prefix::Prefix, src_name::AbstractString = "";
rm(f; force=true)
end

# Make sure that `prefix` in pkg-config files use a relative prefix
pc_files = collect_files(prefix, endswith(".pc"))
pc_re = r"^prefix=(/.*)$"
for f in pc_files
# We want to replace every instance of `prefix=...` with
# `prefix=${pcfiledir}/../..`
changed = false
buf = IOBuffer()
for l in readlines(f)
m = match(pc_re, l)
if m !== nothing
# dealing with an absolute path we need to relativize;
# determine how many directories we need to go up.
dir = m.captures[1]
f_rel = relpath(f, prefix.path)
ndirs = count('/', f_rel)
prefix_rel = join([".." for _ in 1:ndirs], "/")
l = "prefix=\${pcfiledir}/$prefix_rel"
changed = true
end
println(buf, l)
end
str = String(take!(buf))

if changed
# Overwrite file
if verbose
@info("Relocatize pkg-config file $f")
end
write(f, str)
end
end

if Sys.iswindows(platform)
# We also cannot allow any symlinks in Windows because it requires
# Admin privileges to create them. Orz
Expand Down
46 changes: 46 additions & 0 deletions test/auditing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,52 @@ end
end
end

@testset "Auditor - relocatable pkg-config prefix" begin
for os in ["linux", "macos", "freebsd", "windows"]
platform = Platform("x86_64", os)
mktempdir() do build_path
build_output_meta = nothing
@test_logs (:info, r"Relocatize pkg-config file .*/destdir/lib/pkgconfig/libfoo.pc$") match_mode=:any begin
build_output_meta = autobuild(
build_path,
"libfoo",
v"1.0.0",
# Copy in the libfoo sources
[DirectorySource(build_tests_dir)],
libfoo_autotools_script,
# Build for our platform
[platform],
# The products we expect to be build
libfoo_products,
# No dependencies
Dependency[];
verbose = true,
)
end

# Extract our platform's build
@test haskey(build_output_meta, platform)
tarball_path, tarball_hash = build_output_meta[platform][1:2]
@test isfile(tarball_path)

# Unpack it somewhere else
@test verify(tarball_path, tarball_hash)
testdir = joinpath(build_path, "testdir")
mkdir(testdir)
unpack(tarball_path, testdir)
prefix = Prefix(testdir)

# Test that `libfoo.pc` exists and contains a relative prefix
# TODO: actually use pkg-config with PKG_CONFIG_PATH
contents = list_tarball_files(tarball_path)
@test "lib/pkgconfig/libfoo.pc" in contents
libfoo_pc = read(joinpath(testdir, "lib/pkgconfig/libfoo.pc"), String)
@test contains(libfoo_pc, "prefix=\${pcfiledir}")
@test !contains(libfoo_pc, "/workplace/destdir")
end
end
end

@testset "Auditor - .dll moving" begin
for platform in [Platform("x86_64", "windows")]
mktempdir() do build_path
Expand Down
1 change: 1 addition & 0 deletions test/build_tests/libfoo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ elseif (UNIX)
set_target_properties(fooifier PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")
endif()

install(FILES libfoo.pc DESTINATION lib/pkgconfig)
install(TARGETS foo DESTINATION lib)
install(TARGETS fooFramework DESTINATION lib)
install(TARGETS fooifier RUNTIME DESTINATION bin)
Expand Down
3 changes: 3 additions & 0 deletions test/build_tests/libfoo/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
ACLOCAL_AMFLAGS = -I m4

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libfoo.pc

lib_LTLIBRARIES = libfoo.la
libfoo_la_SOURCES = libfoo.c
libfoo_la_LDFLAGS = -no-undefined
Expand Down
10 changes: 10 additions & 0 deletions test/build_tests/libfoo/libfoo.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
prefix=/workplace/destdir
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: Libfoo
Description: Foo library, now without bar!
Version: 1.0.0
Cflags: -I${includedir}
Libs: -L${libdir} -lfoo
2 changes: 2 additions & 0 deletions test/build_tests/libfoo/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ endif
executable('fooifier', 'fooifier.cpp',
link_with : libfoo, install : true,
install_rpath : rpath_dir)

install_data('libfoo.pc', install_dir: get_option('libdir')/'pkgconfig')

0 comments on commit 8348fb3

Please sign in to comment.