Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pkg-config files relocatable post-installation #1346

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
53 changes: 53 additions & 0 deletions test/auditing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,59 @@ 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)],
# Build libfoo using autotools to create a real .la file, and also
# create a fake .la file (which should not be removed). Create also a
# symlink libqux.la -> libfoo.la, which will be broken after libfoo.la
# has been deleted: remove libqux.la as well
libfoo_autotools_script * raw"""
touch ${prefix}/lib/libbar.la
ln -s ${prefix}/lib/libfoo.la ${prefix}/lib/libqux.la
""",
maleadt marked this conversation as resolved.
Show resolved Hide resolved
# 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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

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')
Loading