lol
1# The wrapper script ensures variables like PKG_CONFIG_PATH and
2# PKG_CONFIG_PATH_FOR_BUILD work properly.
3
4{
5 stdenvNoCC,
6 lib,
7 buildPackages,
8 replaceVars,
9 makeSetupHook,
10 expand-response-params,
11 pkg-config,
12 baseBinName ? "pkg-config",
13 propagateDoc ? pkg-config != null && pkg-config ? man,
14 extraPackages ? [ ],
15 extraBuildCommands ? "",
16}:
17
18let
19 inherit (lib)
20 attrByPath
21 getBin
22 optional
23 optionalAttrs
24 optionals
25 optionalString
26 replaceStrings
27 ;
28
29 stdenv = stdenvNoCC;
30 inherit (stdenv) hostPlatform targetPlatform;
31
32 # Prefix for binaries. Customarily ends with a dash separator.
33 #
34 # TODO(@Ericson2314) Make unconditional, or optional but always true by
35 # default.
36 targetPrefix = optionalString (targetPlatform != hostPlatform) (targetPlatform.config + "-");
37
38 # See description in cc-wrapper.
39 suffixSalt = replaceStrings [ "-" "." ] [ "_" "_" ] targetPlatform.config;
40
41 wrapperName = "PKG_CONFIG_WRAPPER";
42 wrapperBinName = "${targetPrefix}${baseBinName}";
43in
44
45stdenv.mkDerivation {
46 pname = targetPrefix + pkg-config.pname + "-wrapper";
47 inherit (pkg-config) version;
48
49 enableParallelBuilding = true;
50
51 preferLocalBuild = true;
52
53 outputs = [ "out" ] ++ optionals propagateDoc ([ "man" ] ++ optional (pkg-config ? doc) "doc");
54
55 strictDeps = true;
56 dontBuild = true;
57 dontConfigure = true;
58 dontUnpack = true;
59
60 installPhase =
61 let
62 addFlags = optionalString stdenv.targetPlatform.isStatic "--static";
63 shell = getBin stdenvNoCC.shell + stdenvNoCC.shell.shellPath or "";
64 prog = "${getBin pkg-config}/bin/${baseBinName}";
65 in
66 ''
67 mkdir -p $out/bin $out/nix-support
68 wrap() {
69 local dst="$1"
70 local wrapper="$2"
71 export prog="$3"
72 # Do not take variables from env but substitute them explicitly
73 # to prepare for structuredAttrs
74 # Avoid using a nested derivation since we need to substitute $out
75 substitute "$wrapper" "$out/bin/$dst" \
76 --replace-fail "@suffixSalt@" "${suffixSalt}" \
77 --replace-fail "@shell@" "${shell}" \
78 --replace-fail "@prog@" "$prog" \
79 --replace-fail "@out@" "$out" \
80 --replace-fail "@addFlags@" "${addFlags}"
81
82 chmod +x "$out/bin/$dst"
83 }
84
85 echo $pkg-config > $out/nix-support/orig-pkg-config
86
87 wrap ${wrapperBinName} ${./pkg-config-wrapper.sh} "${getBin pkg-config}/bin/${baseBinName}"
88 ''
89 # symlink in share for autoconf to find macros
90
91 # TODO(@Ericson2314): in the future just make the unwrapped pkg-config a
92 # propagated dep once we can rely on downstream deps coming first in
93 # search paths. (https://github.com/NixOS/nixpkgs/pull/31414 took a crack
94 # at this.)
95 + ''
96 ln -s ${pkg-config}/share $out/share
97 '';
98
99 setupHooks =
100 let
101 roleHook = makeSetupHook rec {
102 name = "pkg-config-role-hook";
103 substitutions = {
104 inherit
105 name
106 suffixSalt
107 wrapperName
108 ;
109 };
110 } ../setup-hooks/role.bash;
111 setupHook = makeSetupHook {
112 name = "pkgs-config-setup-hook";
113 substitutions = {
114 inherit
115 targetPrefix
116 baseBinName
117 ;
118 };
119 } ./setup-hook.sh;
120 in
121 [
122 "${roleHook}/nix-support/setup-hook"
123 "${setupHook}/nix-support/setup-hook"
124 ];
125
126 postFixup =
127 let
128 addFlags = replaceVars ./add-flags.sh { inherit suffixSalt; };
129 utils = replaceVars ../wrapper-common/utils.bash {
130 inherit
131 suffixSalt
132 wrapperName
133 ;
134 inherit (targetPlatform) darwinMinVersion;
135 expandResponseParams = "${expand-response-params}/bin/expand-response-params";
136 };
137
138 in
139 ##
140 ## User env support
141 ##
142
143 # Propagate the underling unwrapped pkg-config so that if you
144 # install the wrapper, you get anything else it might provide.
145 ''
146 printWords ${pkg-config} > $out/nix-support/propagated-user-env-packages
147 ''
148
149 ##
150 ## Man page and doc support
151 ##
152 + optionalString propagateDoc (
153 ''
154 ln -s ${pkg-config.man} $man
155 ''
156 + optionalString (pkg-config ? doc) ''
157 ln -s ${pkg-config.doc} $doc
158 ''
159 )
160
161 + ''
162 install -m444 -T ${addFlags} $out/nix-support/add-flags.sh
163 install -m444 -T ${utils} $out/nix-support/utils.bash
164 ''
165
166 ##
167 ## Extra custom steps
168 ##
169 + extraBuildCommands;
170
171 passthru = {
172 inherit
173 targetPrefix
174 suffixSalt
175 pkg-config
176 baseBinName
177 wrapperName
178 ;
179 };
180
181 meta =
182 let
183 pkg-config_ = optionalAttrs (pkg-config != null) pkg-config;
184 in
185 (optionalAttrs (pkg-config_ ? meta) (
186 removeAttrs pkg-config.meta [
187 "priority"
188 "mainProgram"
189 ]
190 ))
191 // {
192 description = attrByPath [ "meta" "description" ] "pkg-config" pkg-config_ + " (wrapper script)";
193 priority = 10;
194 mainProgram = wrapperBinName;
195 };
196}