summary refs log tree commit diff
path: root/pkgs/build-support/pkg-config-wrapper
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2020-04-27 22:39:58 -0400
committerJohn Ericson <John.Ericson@Obsidian.Systems>2020-05-16 00:21:21 +0000
commitc71ab32a678a98ce1ec069956bc55d6ea606f55c (patch)
tree4a026e21bae3c09815613a4d66bf535c5e1fc449 /pkgs/build-support/pkg-config-wrapper
parent27edd9efb3cea58629091778fcbf1c3ca994f075 (diff)
downloadnixpkgs-c71ab32a678a98ce1ec069956bc55d6ea606f55c.tar
nixpkgs-c71ab32a678a98ce1ec069956bc55d6ea606f55c.tar.gz
nixpkgs-c71ab32a678a98ce1ec069956bc55d6ea606f55c.tar.bz2
nixpkgs-c71ab32a678a98ce1ec069956bc55d6ea606f55c.tar.lz
nixpkgs-c71ab32a678a98ce1ec069956bc55d6ea606f55c.tar.xz
nixpkgs-c71ab32a678a98ce1ec069956bc55d6ea606f55c.tar.zst
nixpkgs-c71ab32a678a98ce1ec069956bc55d6ea606f55c.zip
pkg-config-wrapper: Init
This fixes longstanding build issues
Diffstat (limited to 'pkgs/build-support/pkg-config-wrapper')
-rw-r--r--pkgs/build-support/pkg-config-wrapper/add-flags.sh12
-rw-r--r--pkgs/build-support/pkg-config-wrapper/default.nix117
-rw-r--r--pkgs/build-support/pkg-config-wrapper/pkg-config-wrapper.sh21
-rw-r--r--pkgs/build-support/pkg-config-wrapper/setup-hook.sh29
4 files changed, 179 insertions, 0 deletions
diff --git a/pkgs/build-support/pkg-config-wrapper/add-flags.sh b/pkgs/build-support/pkg-config-wrapper/add-flags.sh
new file mode 100644
index 00000000000..35ecf62ca23
--- /dev/null
+++ b/pkgs/build-support/pkg-config-wrapper/add-flags.sh
@@ -0,0 +1,12 @@
+# See cc-wrapper for comments.
+var_templates_list=(
+    PKG_CONFIG_PATH
+)
+
+accumulateRoles
+
+for var in "${var_templates_list[@]}"; do
+    mangleVarList "$var" ${role_suffixes[@]+"${role_suffixes[@]}"}
+done
+
+export NIX_PKG_CONFIG_WRAPPER_FLAGS_SET_@suffixSalt@=1
diff --git a/pkgs/build-support/pkg-config-wrapper/default.nix b/pkgs/build-support/pkg-config-wrapper/default.nix
new file mode 100644
index 00000000000..87efc3b18aa
--- /dev/null
+++ b/pkgs/build-support/pkg-config-wrapper/default.nix
@@ -0,0 +1,117 @@
+# The wrapper script ensures variables like PKG_CONFIG_PATH and
+# PKG_CONFIG_PATH_FOR_BUILD work properly.
+
+{ stdenvNoCC
+, buildPackages
+, pkg-config
+, propagateDoc ? pkg-config != null && pkg-config ? man
+, extraPackages ? [], extraBuildCommands ? ""
+}:
+
+with stdenvNoCC.lib;
+
+let
+  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 = stdenv.lib.optionalString (targetPlatform != hostPlatform)
+                                        (targetPlatform.config + "-");
+
+  # See description in cc-wrapper.
+  suffixSalt = replaceStrings ["-" "."] ["_" "_"] targetPlatform.config;
+
+in
+
+stdenv.mkDerivation {
+  pname = targetPrefix + pkg-config.pname + "-wrapper";
+  inherit (pkg-config) version;
+
+  preferLocalBuild = true;
+
+  shell = getBin stdenvNoCC.shell + stdenvNoCC.shell.shellPath or "";
+
+  inherit targetPrefix suffixSalt;
+
+  outputs = [ "out" ] ++ optionals propagateDoc [ "man" ];
+
+  passthru = {
+    inherit pkg-config;
+  };
+
+  dontBuild = true;
+  dontConfigure = true;
+
+  unpackPhase = ''
+    src=$PWD
+  '';
+
+  installPhase =
+    ''
+      mkdir -p $out/bin $out/nix-support
+
+      wrap() {
+        local dst="$1"
+        local wrapper="$2"
+        export prog="$3"
+        substituteAll "$wrapper" "$out/bin/$dst"
+        chmod +x "$out/bin/$dst"
+      }
+
+      echo $pkg-config > $out/nix-support/orig-pkg-config
+
+      wrap ${targetPrefix}pkg-config ${./pkg-config-wrapper.sh} "${getBin pkg-config}/bin/pkg-config"
+    '';
+
+  strictDeps = true;
+
+  wrapperName = "PKG_CONFIG_WRAPPER";
+
+  setupHooks = [
+    ../setup-hooks/role.bash
+    ./setup-hook.sh
+  ];
+
+  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
+    ''
+
+    + optionalString propagateDoc ''
+      ##
+      ## Man page and info support
+      ##
+
+      ln -s ${pkg-config.man} $man
+    ''
+
+    + ''
+      substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh
+      substituteAll ${../wrapper-common/utils.bash} $out/nix-support/utils.bash
+
+      ##
+      ## Extra custom steps
+      ##
+    ''
+
+    + extraBuildCommands;
+
+  meta =
+    let pkg-config_ = if pkg-config != null then pkg-config else {}; in
+    (if pkg-config_ ? meta then removeAttrs pkg-config.meta ["priority"] else {}) //
+    { description =
+        stdenv.lib.attrByPath ["meta" "description"] "pkg-config" pkg-config_
+        + " (wrapper script)";
+      priority = 10;
+  };
+}
diff --git a/pkgs/build-support/pkg-config-wrapper/pkg-config-wrapper.sh b/pkgs/build-support/pkg-config-wrapper/pkg-config-wrapper.sh
new file mode 100644
index 00000000000..f7c7429eb0b
--- /dev/null
+++ b/pkgs/build-support/pkg-config-wrapper/pkg-config-wrapper.sh
@@ -0,0 +1,21 @@
+#! @shell@
+set -eu -o pipefail +o posix
+shopt -s nullglob
+
+if (( "${NIX_DEBUG:-0}" >= 7 )); then
+    set -x
+fi
+
+source @out@/nix-support/utils.bash
+
+if [ -z "${NIX_PKG_CONFIG_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then
+    source @out@/nix-support/add-flags.sh
+fi
+
+if (( ${#role_suffixes[@]} > 0 )); then
+	# replace env var with nix-modified one
+    PKG_CONFIG_PATH=$PKG_CONFIG_PATH_@suffixSalt@ exec @prog@ "$@"
+else
+	# pkg-config isn't a bonafied dependency so ignore setup hook entirely
+	exec @prog@ "$@"
+fi
diff --git a/pkgs/build-support/pkg-config-wrapper/setup-hook.sh b/pkgs/build-support/pkg-config-wrapper/setup-hook.sh
new file mode 100644
index 00000000000..12b9af5e583
--- /dev/null
+++ b/pkgs/build-support/pkg-config-wrapper/setup-hook.sh
@@ -0,0 +1,29 @@
+# pkg-config Wrapper hygiene
+#
+# See comments in cc-wrapper's setup hook. This works exactly the same way.
+
+# Skip setup hook if we're neither a build-time dep, nor, temporarily, doing a
+# native compile.
+#
+# TODO(@Ericson2314): No native exception
+[[ -z ${strictDeps-} ]] || (( "$hostOffset" < 0 )) || return 0
+
+pkgConfigWrapper_addPkgConfigPath () {
+    # See ../setup-hooks/role.bash
+    local role_post
+    getHostRoleEnvHook
+
+    addToSearchPath "PKG_CONFIG_PATH${role_post}" "$1/lib/pkgconfig"
+    addToSearchPath "PKG_CONFIG_PATH${role_post}" "$1/share/pkgconfig"
+}
+
+# See ../setup-hooks/role.bash
+getTargetRole
+getTargetRoleWrapper
+
+addEnvHooks "$targetOffset" pkgConfigWrapper_addPkgConfigPath
+
+export PKG_CONFIG${role_post}=@targetPrefix@pkg-config
+
+# No local scope in sourced file
+unset -v role_post