Signed-off-by: Michał Górny <mgo...@gentoo.org>
---
 eclass/llvm-r2.eclass | 173 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 172 insertions(+), 1 deletion(-)

diff --git a/eclass/llvm-r2.eclass b/eclass/llvm-r2.eclass
index 91e841821331..e499f9dba38d 100644
--- a/eclass/llvm-r2.eclass
+++ b/eclass/llvm-r2.eclass
@@ -216,6 +216,162 @@ get_llvm_prefix() {
        echo "${prefix}/usr/lib/llvm/${LLVM_SLOT}"
 }
 
+# @FUNCTION: generate_llvm_config
+# @DESCRIPTION:
+# Output a llvm-config compatible script that yields paths specific
+# to the requested LLVM version.
+generate_llvm_config() {
+       debug-print-function ${FUNCNAME} "$@"
+
+       local bindir=$(get_llvm_prefix -b)/bin
+       [[ ! -d ${bindir} ]] && bindir=
+
+       local prefix=$(get_llvm_prefix -d)
+       local includedir=${prefix}/include
+       local libdir=${prefix}/$(get_libdir)
+       local cmake_conf=${libdir}/cmake/llvm/LLVMConfig.cmake
+       if [[ ! -f ${cmake_conf} ]]; then
+               cat <<-EOF
+                       #!/usr/bin/env sh
+                       echo "LLVM ${LLVM_SLOT} not installed for ABI=${ABI}" 
>&2
+                       exit 127
+               EOF
+               return
+       fi
+
+       local version=$(
+               sed -ne 's:set(LLVM_PACKAGE_VERSION \(.*\)):\1:p' 
"${cmake_conf}" || die
+       )
+       [[ -n ${version} ]] || die
+       local cppdefs=$(
+               sed -ne 's:set(LLVM_DEFINITIONS "\(.*\)"):\1:p' "${cmake_conf}" 
|| die
+       )
+       [[ -n ${cppdefs} ]] || die
+       local targets=$(
+               sed -ne 's:set(LLVM_TARGETS_TO_BUILD \(.*\)):\1:p' 
"${cmake_conf}" || die
+       )
+       [[ -n ${targets} ]] || die
+       local libs=$(
+               sed -ne 's:set(LLVM_AVAILABLE_LIBS \(.*\)):\1:p' 
"${cmake_conf}" || die
+       )
+       [[ -n ${libs} ]] || die
+       local target_triple=$(
+               sed -ne 's:set(LLVM_TARGET_TRIPLE "\(.*\)"):\1:p' 
"${cmake_conf}" || die
+       )
+       [[ -n ${target_triple} ]] || die
+
+       readarray -d';' -t targets <<<"${targets}"
+       readarray -d';' -t libs <<<"${libs}"
+       # easier than parsing CMake booleans
+       local assertions=OFF
+       [[ ${cppdefs} == *-D_DEBUG* ]] && assertions=ON
+       # major + suffix
+       local shlib_name=LLVM-${version%%.*}
+       [[ ${version} == *git* ]] && shlib_name+="git${version##*git}"
+
+       local components=(
+               "${libs[@]#LLVM}" "${targets[@]}"
+               # special component groups (grep for add_llvm_component_group)
+               all all-targets engine native nativecodegen
+       )
+
+       cat <<-EOF
+               #!/usr/bin/env sh
+
+               echo "\${0} \${*}" >> "${T}/llvm-config-calls.txt"
+
+               do_echo() {
+                       echo "  \${*}" >> "${T}/llvm-config-calls.txt"
+                       echo "\${@}"
+               }
+
+               for arg; do
+                       case \${arg} in
+                               --assertion-mode)
+                                       do_echo "${assertions}"
+                                       ;;
+                               --bindir)
+                                       if [ -n "${bindir}" ]; then
+                                               do_echo "${bindir}"
+                                       else
+                                               do_echo "CBUILD LLVM not 
available" >&2
+                                               exit 1
+                                       fi
+                                       ;;
+                               --build-mode)
+                                       do_echo RelWithDebInfo
+                                       ;;
+                               --build-system)
+                                       do_echo cmake
+                                       ;;
+                               --cflags|--cppflags)
+                                       do_echo "-I${includedir} ${cppdefs[*]}"
+                                       ;;
+                               --cmakedir)
+                                       do_echo "${libdir}/cmake/llvm"
+                                       ;;
+                               --components)
+                                       do_echo "${components[*],,}"
+                                       ;;
+                               --cxxflags)
+                                       do_echo "-I${includedir} -std=c++17 
${cppdefs[*]}"
+                                       ;;
+                               --has-rtti)
+                                       do_echo YES
+                                       ;;
+                               --host-target)
+                                       do_echo "${target_triple}"
+                                       ;;
+                               --ignore-libllvm)
+                                       # ignored
+                                       ;;
+                               --includedir)
+                                       do_echo "${includedir}"
+                                       ;;
+                               --ldflags)
+                                       do_echo "-L${libdir}"
+                                       ;;
+                               --libdir)
+                                       do_echo "${libdir}"
+                                       ;;
+                               --libfiles)
+                                       do_echo "${libdir}/lib${shlib_name}.so"
+                                       ;;
+                               --libnames)
+                                       do_echo lib${shlib_name}.so
+                                       ;;
+                               --libs)
+                                       do_echo "-l${shlib_name}"
+                                       ;;
+                               --link-shared|--link-static)
+                                       # ignored
+                                       ;;
+                               --obj-root|--prefix)
+                                       do_echo "${prefix}"
+                                       ;;
+                               --shared-mode)
+                                       do_echo shared
+                                       ;;
+                               --system-libs)
+                                       do_echo
+                                       ;;
+                               --targets-built)
+                                       do_echo "${targets[*]}"
+                                       ;;
+                               --version)
+                                       do_echo "${version}"
+                                       ;;
+                               -*)
+                                       do_echo "Unsupported option: \${arg}" 
>&2
+                                       exit 1
+                                       ;;
+                               *)
+                                       # ignore components, we always return 
the dylib
+                                       ;;
+                       esac
+               done
+       EOF
+}
 # @FUNCTION: llvm_cbuild_setup
 # @DESCRIPTION:
 # Prepend the PATH for selected LLVM version in CBUILD.
@@ -251,11 +407,14 @@ llvm_cbuild_setup() {
 # @FUNCTION: llvm_chost_setup
 # @DESCRIPTION:
 # Set the environment for finding selected LLVM slot installed
-# for CHOST.
+# for CHOST.  Create llvm-config wrappers to satisfy legacy lookups.
 #
 # This function is meant to be used when the package in question uses
 # LLVM compiles against and links to LLVM.  It is called automatically
 # by llvm-r2_pkg_setup if LLVM is found installed in ESYSROOT.
+#
+# Note that the generated llvm-config may refer to CBUILD installation
+# of LLVM via --bindir, if it is found available.
 llvm_chost_setup() {
        debug-print-function ${FUNCNAME} "$@"
 
@@ -268,6 +427,18 @@ llvm_chost_setup() {
        export LLVM_ROOT="${esysroot_prefix}"
        export Clang_ROOT="${esysroot_prefix}"
        export LLD_ROOT="${esysroot_prefix}"
+
+       # satisfies llvm-config calls, e.g. from meson
+       export PATH="${T}/llvm-bin:${PATH}"
+       mkdir "${T}"/llvm-bin || die
+       # we need to generate it per-ABI, since libdir changes
+       local ABI
+       for ABI in $(get_all_abis); do
+               local path="${T}/llvm-bin/$(get_abi_CHOST)-llvm-config"
+               generate_llvm_config > "${path}" || die
+               chmod +x "${path}" || die
+       done
+       ln -s "$(get_abi_CHOST)-llvm-config" "${T}/llvm-bin/llvm-config" || die
 }
 
 # @FUNCTION: llvm-r2_pkg_setup
-- 
2.47.1


Reply via email to