summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/make-pkgconfigitem/default.nix
blob: 0502c70ee860b88adfd41b689ea7ee9c71f9659b (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
{
  lib,
  writeTextFile,
  buildPackages,
}:

# See https://people.freedesktop.org/~dbn/pkg-config-guide.html#concepts
{
  name, # The name of the pc file
  # keywords
  # provide a default description for convenience. it's not important but still required by pkg-config.
  description ? "Pkg-config file for ${name}",
  url ? "",
  version ? "",
  requires ? [ ],
  requiresPrivate ? [ ],
  conflicts ? [ ],
  cflags ? [ ],
  libs ? [ ],
  libsPrivate ? [ ],
  variables ? { },
}:

let
  # only 'out' has to be changed, otherwise it would be replaced by the out of the writeTextFile
  placeholderToSubstVar = builtins.replaceStrings [ "${placeholder "out"}" ] [ "@out@" ];

  replacePlaceholderAndListToString =
    x:
    if builtins.isList x then
      placeholderToSubstVar (builtins.concatStringsSep " " x)
    else
      placeholderToSubstVar x;

  keywordsSection =
    let
      mustBeAList =
        attr: attrName: if !(lib.isList attr) then throw "'${attrName}' must be a list" else attr;
    in
    {
      "Name" = name;
      "Description" = description;
      "URL" = url;
      "Version" = version;
      "Requires" = mustBeAList requires "requires";
      "Requires.private" = mustBeAList requiresPrivate "requiresPrivate";
      "Conflicts" = mustBeAList conflicts "conflicts";
      "Cflags" = mustBeAList cflags "cflags";
      "Libs" = mustBeAList libs "libs";
      "Libs.private" = mustBeAList libsPrivate "libsPrivate";
    };

  renderVariable =
    name: value:
    lib.optionalString (
      value != "" && value != [ ]
    ) "${name}=${replacePlaceholderAndListToString value}";
  renderKeyword =
    name: value:
    lib.optionalString (
      value != "" && value != [ ]
    ) "${name}: ${replacePlaceholderAndListToString value}";

  renderSomething =
    renderFunc: attrs:
    lib.pipe attrs [
      (lib.mapAttrsToList renderFunc)
      (builtins.filter (v: v != ""))
      (lib.concatLines)
    ];

  variablesSectionRendered = renderSomething renderVariable variables;
  keywordsSectionRendered = renderSomething renderKeyword keywordsSection;

  content = [
    variablesSectionRendered
    keywordsSectionRendered
  ];
in
writeTextFile {
  name = "${name}.pc";
  destination = "/lib/pkgconfig/${name}.pc";
  text = builtins.concatStringsSep "\n" content;
  checkPhase = ''${buildPackages.pkg-config}/bin/${buildPackages.pkg-config.targetPrefix}pkg-config --validate "$target"'';
}