From 1932a21f0beb5ffb4017a12c3e80058203288143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Ahlb=C3=A4ck?= Date: Thu, 24 Oct 2024 12:11:05 +0200 Subject: [PATCH 1/5] Make pkg-config files relocatable post-installation --- src/Auditor.jl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Auditor.jl b/src/Auditor.jl index 699274bfd..901620cfa 100644 --- a/src/Auditor.jl +++ b/src/Auditor.jl @@ -209,6 +209,36 @@ function audit(prefix::Prefix, src_name::AbstractString = ""; rm(f; force=true) end + # Make sure that `prefix` in pkg-config files *.pc points to the right + # directory. + pc_files = collect_files(prefix, endswith(".pc")) + for file in pc_files + # Make sure the file still exists on disk + if !isfile(file) + continue + end + + # We want to replace every instance of `prefix=...` with + # `prefix=${pcfiledir}/../..` + content = "" + open(file, "r") do fs + str = readline(fs, keep = true) + while !isempty(str) + tmp = replace(str, r"^prefix=.*$" => s"prefix=${pcfiledir}/../..") + content = content * tmp + str = readline(fs, keep = true) + end + end + + # Overwrite file + if verbose + @info("Relocatize pkg-config file $f") + end + open(file, "w") do fs + write(fs, content) + end + end + if Sys.iswindows(platform) # We also cannot allow any symlinks in Windows because it requires # Admin privileges to create them. Orz From 499949bcea1ba9e5e63200953c72d482bb898e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Ahlb=C3=A4ck?= Date: Thu, 24 Oct 2024 13:07:20 +0200 Subject: [PATCH 2/5] Add pkg-config file to test library libfoo --- test/build_tests/libfoo/CMakeLists.txt | 1 + test/build_tests/libfoo/Makefile.am | 3 +++ test/build_tests/libfoo/libfoo.pc | 10 ++++++++++ test/build_tests/libfoo/meson.build | 2 ++ 4 files changed, 16 insertions(+) create mode 100644 test/build_tests/libfoo/libfoo.pc diff --git a/test/build_tests/libfoo/CMakeLists.txt b/test/build_tests/libfoo/CMakeLists.txt index e4ae5453d..59235ccb6 100644 --- a/test/build_tests/libfoo/CMakeLists.txt +++ b/test/build_tests/libfoo/CMakeLists.txt @@ -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) diff --git a/test/build_tests/libfoo/Makefile.am b/test/build_tests/libfoo/Makefile.am index b285ab953..246c0bfd6 100644 --- a/test/build_tests/libfoo/Makefile.am +++ b/test/build_tests/libfoo/Makefile.am @@ -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 diff --git a/test/build_tests/libfoo/libfoo.pc b/test/build_tests/libfoo/libfoo.pc new file mode 100644 index 000000000..57bccfedb --- /dev/null +++ b/test/build_tests/libfoo/libfoo.pc @@ -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 diff --git a/test/build_tests/libfoo/meson.build b/test/build_tests/libfoo/meson.build index b10c54f07..85508f80f 100644 --- a/test/build_tests/libfoo/meson.build +++ b/test/build_tests/libfoo/meson.build @@ -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') From 043ed4547a91fe5d9bf5c5960d381f55ff1353c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Ahlb=C3=A4ck?= Date: Thu, 24 Oct 2024 13:27:19 +0200 Subject: [PATCH 3/5] Start on test for pkg-config stuff --- test/auditing.jl | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/auditing.jl b/test/auditing.jl index 07776f2ea..48cc69fc1 100644 --- a/test/auditing.jl +++ b/test/auditing.jl @@ -332,6 +332,51 @@ end end end +@testset "Auditor - relocatable pkg-config prefix" begin + for os in ["linux", "macos", "freebsd", "windows"] + platform = Platform("x86_64", os) + # FIXME: What should be done here? + 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 + """, + # Build for our platform + [platform], + # The products we expect to be build + libfoo_products, + # No dependencies + Dependency[]; + verbose = true, + ) + end + + # Test that `libfoo.pc` exists and that pkg-config yields the + # correct path. + contents = list_tarball_files(tarball_path) + @test "lib/pkgconfig/libfoo.pc" in contents + # FIXME: How do I specify ${prefix}/lib/pkgconfig for + # PKG_CONFIG_PATH here? + libfoo_pc_prefix = read(`PKG_CONFIG_PATH=FIXME pkg-config --variable=prefix libfoo`, String) + # FIXME: How do I find the prefix here? + path_to_prefix = "PATH_TO_PREFIX" * "/lib/pkgconfig/../.." + @test libfoo_pc_prefix == path_to_prefix + end + end +end + @testset "Auditor - .dll moving" begin for platform in [Platform("x86_64", "windows")] mktempdir() do build_path From ae8998d9b24791396de0d020c20b99eb27cbdfee Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 6 Nov 2024 16:12:59 +0100 Subject: [PATCH 4/5] Address review. --- src/Auditor.jl | 45 ++++++++++++++++++++++++--------------------- test/auditing.jl | 26 +++++++++++++++++--------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/Auditor.jl b/src/Auditor.jl index 901620cfa..4298fd41e 100644 --- a/src/Auditor.jl +++ b/src/Auditor.jl @@ -209,33 +209,36 @@ function audit(prefix::Prefix, src_name::AbstractString = ""; rm(f; force=true) end - # Make sure that `prefix` in pkg-config files *.pc points to the right - # directory. + # Make sure that `prefix` in pkg-config files use a relative prefix pc_files = collect_files(prefix, endswith(".pc")) - for file in pc_files - # Make sure the file still exists on disk - if !isfile(file) - continue - end - + pc_re = r"^prefix=(/.*)$" + for f in pc_files # We want to replace every instance of `prefix=...` with # `prefix=${pcfiledir}/../..` - content = "" - open(file, "r") do fs - str = readline(fs, keep = true) - while !isempty(str) - tmp = replace(str, r"^prefix=.*$" => s"prefix=${pcfiledir}/../..") - content = content * tmp - str = readline(fs, keep = true) + 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)) - # Overwrite file - if verbose - @info("Relocatize pkg-config file $f") - end - open(file, "w") do fs - write(fs, content) + if changed + # Overwrite file + if verbose + @info("Relocatize pkg-config file $f") + end + write(f, str) end end diff --git a/test/auditing.jl b/test/auditing.jl index 48cc69fc1..3ba9d2cdb 100644 --- a/test/auditing.jl +++ b/test/auditing.jl @@ -335,7 +335,6 @@ end @testset "Auditor - relocatable pkg-config prefix" begin for os in ["linux", "macos", "freebsd", "windows"] platform = Platform("x86_64", os) - # FIXME: What should be done here? 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 @@ -363,16 +362,25 @@ end ) end - # Test that `libfoo.pc` exists and that pkg-config yields the - # correct path. + # 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 - # FIXME: How do I specify ${prefix}/lib/pkgconfig for - # PKG_CONFIG_PATH here? - libfoo_pc_prefix = read(`PKG_CONFIG_PATH=FIXME pkg-config --variable=prefix libfoo`, String) - # FIXME: How do I find the prefix here? - path_to_prefix = "PATH_TO_PREFIX" * "/lib/pkgconfig/../.." - @test libfoo_pc_prefix == path_to_prefix + 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 From 0aad617f1997123d4187d7082d2b5b8f5977b503 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 7 Nov 2024 07:07:15 +0100 Subject: [PATCH 5/5] Update test/auditing.jl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mosè Giordano <765740+giordano@users.noreply.github.com> --- test/auditing.jl | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/test/auditing.jl b/test/auditing.jl index 3ba9d2cdb..a0e0ab3a9 100644 --- a/test/auditing.jl +++ b/test/auditing.jl @@ -344,14 +344,7 @@ end 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 - """, + libfoo_autotools_script, # Build for our platform [platform], # The products we expect to be build