How to make pkg-config libs not using RPATH after pkg_check_modules

I am working on a kind-of-outdated aarch64 system. And want to add support of new gRPC version for my program. The system already have a old gRPC which i dont want to break off. so i build my project dependencies like this:

project-root/
  src/ ...
  3rdparty/
    out.aarch64/
      usr/{include/,lib/,lib/cmake/}
    libs-helper.sh
    grpc.PKGBUILD
    grpc.<any local edition patch>.patch
    re2.PKGBUILD
    <other *.PKGBUILD files ...>

The output tree out.aarch64/ is built with my libs-helper.sh script. Which fetch (download sources) and then setup (config, build, and then install) all PKGBUILDs one by one (with respect of package dependence order).

The gRPC package requires re2 package. so i build re2 and install it into out.aarch64/ tree first, then do the same on gRPC, with the following CMake command line arguments:

  cmake_args=(
    #"-DCMAKE_STAGING_PREFIX='${pkgdir}/usr'"  # should not use this. dnt know why.
    "-DCMAKE_PREFIX_PATH='${pkgdir}/usr/local;${pkgdir}/usr'"
    "-DCMAKE_INSTALL_PREFIX='/usr'"
    "-DCMAKE_FIND_NO_INSTALL_PREFIX='FALSE'"
  )

where the ${pkgdir} is out.aarch64/

The gRPC make progress report error, as this (stripped):

/tmp/tmp.tv8tkL6qgd/3rdparty/grpc-1.39.1/build$ /usr/bin/c++ \
> -fPIC \
> -DNDEBUG \
> -shared \
> -Wl,-soname,libgrpc.so.18 \
> -o \
> libgrpc.so.18.0.0 \
> CMakeFiles/grpc.dir/src/core/lib/debug/stats.cc.o \
> CMakeFiles/grpc.dir/src/core/lib/debug/stats_data.cc.o \
> CMakeFiles/grpc.dir/src/core/lib/debug/trace.cc.o \
> <many .cc.o here> \
> -Wl,-rpath,/tmp/tmp.tv8tkL6qgd/3rdparty/out.aarch64/usr/lib/aarch64-linux-gnu \
> -Wl,-rpath,/tmp/tmp.tv8tkL6qgd/3rdparty/grpc-1.39.1/build \
> -Wl,-rpath,/tmp/tmp.tv8tkL6qgd/3rdparty/out.aarch64/usr/lib \
> /usr/lib/aarch64-linux-gnu/libz.so \
> /tmp/tmp.tv8tkL6qgd/3rdparty/out.aarch64/usr/lib/aarch64-linux-gnu/libcares.so.2.4.3 \
> -ldl \
> -lrt \
> -lm \
> -lpthread \
> /tmp/tmp.tv8tkL6qgd/3rdparty/out.aarch64/usr/lib/aarch64-linux-gnu/libabsl_statusor.so.2103.0.1 \
> libgpr.so.18.0.0 \
> /tmp/tmp.tv8tkL6qgd/3rdparty/out.aarch64/usr/lib/libssl.so \
> /tmp/tmp.tv8tkL6qgd/3rdparty/out.aarch64/usr/lib/libcrypto.so \
> libaddress_sorting.so.18.0.0 \
> -pthread \
> -lre2 \
> /tmp/tmp.tv8tkL6qgd/3rdparty/out.aarch64/usr/lib/aarch64-linux-gnu/libabsl_hash.so.2103.0.1 \
> <many .so and -l here> \
> 
/usr/bin/ld: cannot find -lre2
collect2: error: ld returned 1 exit status

I have confirmed the file exists:

/tmp/tmp.tv8tkL6qgd/3rdparty/out.aarch64/usr/lib/libre2.so

and make this change can fix the error:

- -Wl,-rpath,/tmp/tmp.tv8tkL6qgd/3rdparty/out.aarch64/usr/lib \
+ -L/tmp/tmp.tv8tkL6qgd/3rdparty/out.aarch64/usr/lib \

I have located the re2 is searched by pkg_check_modules(RE2 QUIET re2) @grpc-source and have messaged:

-- Found RE2 via pkg-config.

the re2.pc file:

.../3rdparty$ cat out.aarch64/usr/lib/pkgconfig/re2.pc
includedir=/usr/include
libdir=/usr/lib

Name: re2
Description: RE2 is a fast, safe, thread-friendly regular expression engine.
Version: 0.0.0
Cflags: -std=c++11 -pthread -I${includedir}
Libs: -pthread -L${libdir} -lre2
.../3rdparty$

So I assume the pkg-config result restrict the cmake in RPATH, which breaks the normal lib finding progress.

How do I fix it? Any method can be accepted, prefer in this order:

  1. package specific changes in .PKGBUILD script
  2. package specific changes in the package source tree by .patch files
  3. system wide changes in shell environment variables, which can affect cmake/make/configure command, or their args
  4. system wide changes in all .PKGBUILD scripts
  5. system wide changes in package source tree makes them “dirty”

And another way to solve this, i dnt know whether i can describe it, is to link it with .../usr/lib/libre2.so instead of -lre2. I dnt know how to achieve that purpose with least source code changes.


[[toc]]

Code about the Question (stripped)

re2.PKGBUILD

pkgname=re2
gitver=2021-08-01
pkgver=${gitver//-}
gitrepo=https://github.com/google/$pkgname.git

build() {
  make "${automake_args[@]}"
}

check() {
  make test
}

package() {
  make prefix=/usr DESTDIR="$pkgdir" install
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

grpc.PKGBUILD

pkgname='grpc'
pkgver=1.39.1
_gtestver=c9ccac7cb7345901884aabf5d1a786cfa6e2f397
gitrepo=https://github.com/grpc/${gitname:-$pkgname}.git
gitver=v$pkgver
depends=('c-ares' 'protobuf' 'openssl' 're2' 'abseil-cpp')

prepare() {
  # ...
}

build() {
  cmake "${cmake_args[@]}" -Bbuild \
    -DCMAKE_BUILD_TYPE=None \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS} -DNDEBUG" \
    -DgRPC_INSTALL=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_CXX_STANDARD=17 \
    -DCMAKE_SKIP_INSTALL_RPATH=ON \
    -DgRPC_BACKWARDS_COMPATIBILITY_MODE=OFF \
    -DgRPC_ZLIB_PROVIDER='package' \
    -DgRPC_CARES_PROVIDER='package' \
    -DgRPC_RE2_PROVIDER='package' \
    -DgRPC_SSL_PROVIDER='package' \
    -DgRPC_PROTOBUF_PROVIDER='package' \
    -DgRPC_PROTOBUF_PACKAGE_TYPE='MODULE' \
    -DgRPC_BENCHMARK_PROVIDER:STRING='package' \
    -DgRPC_ABSL_PROVIDER:STRING='package' \
    -DgRPC_USE_PROTO_LITE=OFF \
    -DgRPC_BUILD_GRPC_CPP_PLUGIN=ON \
    -DgRPC_BUILD_TESTS=ON \
    #-DgRPC_BUILD_GRPC_CSHARP_PLUGIN=ON \
    #-DgRPC_BUILD_GRPC_NODE_PLUGIN=ON \
    #-DgRPC_BUILD_GRPC_OBJECTIVE_C_PLUGIN=ON \
    #-DgRPC_BUILD_GRPC_PHP_PLUGIN=ON \
    #-DgRPC_BUILD_GRPC_PYTHON_PLUGIN=ON \
    #-DgRPC_BUILD_GRPC_RUBY_PLUGIN=ON \
    #-DgRPC_BUILD_CODEGEN=ON \
    #-DgRPC_BUILD_CSHARP_EXT=ON \
    #-GNinja

  cmake --build build -j 48 #-- VERBOSE=1
}

package() {
  depends=('c-ares' 'protobuf' 'openssl' 're2' 'abseil-cpp')
  provides=(
    'libupb.so'
    'libgrpcpp_channelz.so'
    'libgrpc_unsecure.so'
    'libgrpc_plugin_support.so'
    'libgrpc.so'
    'libgrpc++_unsecure.so'
    'libgrpc++_reflection.so'
    'libgrpc++_error_details.so'
    'libgrpc++_alts.so'
    'libgrpc++.so'
    'libgpr.so'
    'libaddress_sorting.so'
  )

  DESTDIR="$pkgdir" cmake --install build
  install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}

libs-helper.sh

do__setup() {
  ( #set -x
    test -d "$pkgname-$pkgver" &&
    { ! ${force_refetch:-false} ||
      { do__clean; false; } } ||
    do__fetch ) ||
  { log_error '[%s %s] failed on fetching' "$pkgname" "$pkgver" && return 1; }

  log_info '[%s %s] %s...' "$pkgname" "$pkgver" "building"

  ( #set -x
    cd $pkgname-$pkgver
    ! declare -F prepare >/dev/null ||
    prepare ) ||
  { log_error '[%s %s] failed on configuring' "$pkgname" "$pkgver" && return 1; }

  ( #set -x
    cd $pkgname-$pkgver
    ! declare -F build >/dev/null ||
    build ) ||
  { log_error '[%s %s] failed on building' "$pkgname" "$pkgver" && return 1; }

  ! declare -F package >/dev/null &&
  log_error '[%s %s] no setup defined in %s.PKGBUILD' "$pkgname" "$pkgver" "$pkgname" ||
  for instdir in "${instdirs[@]}"; do
    ( setup_into "$instdir"; ) ||
    log_error '[%s %s] failed to install: [%s]' "$pkgname" "$pkgver" "$instdir"
  done
}

About the PKGBUILDs

adopted from Archlinux Build System

After following some CMakeCache.txt and document tracing. I pick the CMAKE_<TYPE>_LINKER_FLAGS_INIT hard coding trick to solve this.

I have tried to put them into cmake_args, but they may break other packages build progress. I did not check why that happens.

If there is any other way to solve this, please let me know.

   cmake "${cmake_args[@]}" -Bbuild \
     -DCMAKE_BUILD_TYPE=None \
     -DCMAKE_CXX_FLAGS="${CXXFLAGS} -DNDEBUG" \
+    -DCMAKE_MODULE_LINKER_FLAGS_INIT="-L$pkgdir/usr/lib" \
+    -DCMAKE_SHARED_LINKER_FLAGS_INIT="-L$pkgdir/usr/lib" \
+    -DCMAKE_STATIC_LINKER_FLAGS_INIT="-L$pkgdir/usr/lib" \
+    -DCMAKE_EXE_LINKER_FLAGS_INIT="-L$pkgdir/usr/lib" \
     -DgRPC_INSTALL=ON \
     -DBUILD_SHARED_LIBS=ON \
     -DCMAKE_CXX_STANDARD=17 \