summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/pkg-config-wrapper/default.nix
blob: 7ae6b0207c73c298c55a5fd0f544c8a8bd8465d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# The wrapper script ensures variables like PKG_CONFIG_PATH and
# PKG_CONFIG_PATH_FOR_BUILD work properly.

{
  stdenvNoCC,
  lib,
  buildPackages,
  replaceVars,
  makeSetupHook,
  expand-response-params,
  pkg-config,
  baseBinName ? "pkg-config",
  propagateDoc ? pkg-config != null && pkg-config ? man,
  extraPackages ? [ ],
  extraBuildCommands ? "",
}:

let
  inherit (lib)
    attrByPath
    getBin
    optional
    optionalAttrs
    optionals
    optionalString
    replaceStrings
    ;

  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) (targetPlatform.config + "-");

  # See description in cc-wrapper.
  suffixSalt = replaceStrings [ "-" "." ] [ "_" "_" ] targetPlatform.config;

  wrapperName = "PKG_CONFIG_WRAPPER";
  wrapperBinName = "${targetPrefix}${baseBinName}";
in

stdenv.mkDerivation {
  pname = targetPrefix + pkg-config.pname + "-wrapper";
  inherit (pkg-config) version;

  enableParallelBuilding = true;

  preferLocalBuild = true;

  outputs = [ "out" ] ++ optionals propagateDoc ([ "man" ] ++ optional (pkg-config ? doc) "doc");

  strictDeps = true;
  dontBuild = true;
  dontConfigure = true;
  dontUnpack = true;

  installPhase =
    let
      addFlags = optionalString stdenv.targetPlatform.isStatic "--static";
      shell = getBin stdenvNoCC.shell + stdenvNoCC.shell.shellPath or "";
      prog = "${getBin pkg-config}/bin/${baseBinName}";
    in
    ''
      mkdir -p $out/bin $out/nix-support
      wrap() {
        local dst="$1"
        local wrapper="$2"
        export prog="$3"
        # Do not take variables from env but substitute them explicitly
        # to prepare for structuredAttrs
        # Avoid using a nested derivation since we need to substitute $out
        substitute "$wrapper" "$out/bin/$dst" \
          --replace-fail "@suffixSalt@" "${suffixSalt}" \
          --replace-fail "@shell@" "${shell}" \
          --replace-fail "@prog@" "$prog" \
          --replace-fail "@out@" "$out" \
          --replace-fail "@addFlags@" "${addFlags}"

        chmod +x "$out/bin/$dst"
      }

      echo $pkg-config > $out/nix-support/orig-pkg-config

      wrap ${wrapperBinName} ${./pkg-config-wrapper.sh} "${getBin pkg-config}/bin/${baseBinName}"
    ''
    # 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 coming first in
    # search paths. (https://github.com/NixOS/nixpkgs/pull/31414 took a crack
    # at this.)
    + ''
      ln -s ${pkg-config}/share $out/share
    '';

  setupHooks =
    let
      roleHook = makeSetupHook rec {
        name = "pkg-config-role-hook";
        substitutions = {
          inherit
            name
            suffixSalt
            wrapperName
            ;
        };
        meta.license = lib.licenses.mit;
      } ../setup-hooks/role.bash;
      setupHook = makeSetupHook {
        name = "pkgs-config-setup-hook";
        substitutions = {
          inherit
            targetPrefix
            baseBinName
            ;
        };
        meta.license = lib.licenses.mit;
      } ./setup-hook.sh;
    in
    [
      "${roleHook}/nix-support/setup-hook"
      "${setupHook}/nix-support/setup-hook"
    ];

  postFixup =
    let
      addFlags = replaceVars ./add-flags.sh { inherit suffixSalt; };
      utils = replaceVars ../wrapper-common/utils.bash {
        inherit
          suffixSalt
          wrapperName
          ;
        inherit (targetPlatform) darwinMinVersion;
        expandResponseParams = "${expand-response-params}/bin/expand-response-params";
      };

    in
    ##
    ## 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
      ''
    )

    + ''
      install -m444 -T ${addFlags} $out/nix-support/add-flags.sh
      install -m444 -T ${utils} $out/nix-support/utils.bash
    ''

    ##
    ## Extra custom steps
    ##
    + extraBuildCommands;

  passthru = {
    inherit
      targetPrefix
      suffixSalt
      pkg-config
      baseBinName
      wrapperName
      ;
  };

  __structuredAttrs = true;

  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_ + " (wrapper script)";
      priority = 10;
      mainProgram = wrapperBinName;
    };
}