core/pkgs/build-support/pkg-config-wrapper/default.nix

126 lines
3.4 KiB
Nix
Raw Normal View History

2024-05-02 00:46:19 +00:00
# The wrapper script ensures variables like PKG_CONFIG_PATH and
# PKG_CONFIG_PATH_FOR_BUILD work properly.
2024-05-13 21:24:10 +00:00
{ stdenvNoCC, lib, buildPackages, pkg-config, baseBinName ? "pkg-config"
, propagateDoc ? pkg-config != null && pkg-config ? man, extraPackages ? [ ]
, extraBuildCommands ? "" }:
2024-05-02 00:46:19 +00:00
let
inherit (lib)
2024-05-13 21:24:10 +00:00
attrByPath getBin optional optionalAttrs optionals optionalString
replaceStrings;
2024-05-02 00:46:19 +00:00
stdenv = stdenvNoCC;
inherit (stdenv) hostPlatform targetPlatform;
# Prefix for binaries. Customarily ends with a dash separator.
#
# TODO(@Ericson2314) Make unconditional, or optional but always true by
# default.
targetPrefix = optionalString (targetPlatform != hostPlatform)
2024-05-13 21:24:10 +00:00
(targetPlatform.config + "-");
2024-05-02 00:46:19 +00:00
# See description in cc-wrapper.
2024-05-13 21:24:10 +00:00
suffixSalt = replaceStrings [ "-" "." ] [ "_" "_" ] targetPlatform.config;
2024-05-02 00:46:19 +00:00
wrapperBinName = "${targetPrefix}${baseBinName}";
2024-05-13 21:24:10 +00:00
in stdenv.mkDerivation {
2024-05-02 00:46:19 +00:00
pname = targetPrefix + pkg-config.pname + "-wrapper";
inherit (pkg-config) version;
enableParallelBuilding = true;
preferLocalBuild = true;
2024-05-13 21:24:10 +00:00
outputs = [ "out" ]
++ optionals propagateDoc ([ "man" ] ++ optional (pkg-config ? doc) "doc");
2024-05-02 00:46:19 +00:00
passthru = {
inherit targetPrefix suffixSalt;
inherit pkg-config;
};
strictDeps = true;
dontBuild = true;
dontConfigure = true;
dontUnpack = true;
# Additional flags passed to pkg-config.
addFlags = optional stdenv.targetPlatform.isStatic "--static";
2024-05-13 21:24:10 +00:00
installPhase = ''
mkdir -p $out/bin $out/nix-support
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
wrap() {
local dst="$1"
local wrapper="$2"
export prog="$3"
substituteAll "$wrapper" "$out/bin/$dst"
chmod +x "$out/bin/$dst"
}
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
echo $pkg-config > $out/nix-support/orig-pkg-config
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
wrap ${wrapperBinName} ${./pkg-config-wrapper.sh} "${
getBin pkg-config
}/bin/${baseBinName}"
''
2024-05-02 00:46:19 +00:00
# symlink in share for autoconf to find macros
# TODO(@Ericson2314): in the future just make the unwrapped pkg-config a
# propagated dep once we can rely on downstream deps comming first in
# search paths. (https://github.com/NixOS/nixpkgs/pull/31414 took a crack
# at this.)
+ ''
ln -s ${pkg-config}/share $out/share
'';
2024-05-13 21:24:10 +00:00
setupHooks = [ ../setup-hooks/role.bash ./setup-hook.sh ];
2024-05-02 00:46:19 +00:00
postFixup =
##
## User env support
##
# Propagate the underling unwrapped pkg-config so that if you
# install the wrapper, you get anything else it might provide.
''
printWords ${pkg-config} > $out/nix-support/propagated-user-env-packages
''
##
## Man page and doc support
##
+ optionalString propagateDoc (''
ln -s ${pkg-config.man} $man
'' + optionalString (pkg-config ? doc) ''
ln -s ${pkg-config.doc} $doc
'')
+ ''
substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh
substituteAll ${../wrapper-common/utils.bash} $out/nix-support/utils.bash
''
##
## Extra custom steps
##
+ extraBuildCommands;
env = {
shell = getBin stdenvNoCC.shell + stdenvNoCC.shell.shellPath or "";
wrapperName = "PKG_CONFIG_WRAPPER";
inherit targetPrefix suffixSalt baseBinName;
};
2024-05-13 21:24:10 +00:00
meta = let pkg-config_ = optionalAttrs (pkg-config != null) pkg-config;
in (optionalAttrs (pkg-config_ ? meta)
(removeAttrs pkg-config.meta [ "priority" "mainProgram" ])) // {
description = attrByPath [ "meta" "description" ] "pkg-config" pkg-config_
2024-05-02 00:46:19 +00:00
+ " (wrapper script)";
priority = 10;
mainProgram = wrapperBinName;
2024-05-13 21:24:10 +00:00
};
2024-05-02 00:46:19 +00:00
}