1{ lib, writeTextFile, buildPackages }:
2
3# See https://people.freedesktop.org/~dbn/pkg-config-guide.html#concepts
4{ name # The name of the pc file
5 # keywords
6 # provide a default description for convenience. it's not important but still required by pkg-config.
7, description ? "A pkg-config file for ${name}"
8, url ? ""
9, version ? ""
10, requires ? [ ]
11, requiresPrivate ? [ ]
12, conflicts ? [ ]
13, cflags ? [ ]
14, libs ? [ ]
15, libsPrivate ? [ ]
16, variables ? { }
17}:
18
19let
20 # only 'out' has to be changed, otherwise it would be replaced by the out of the writeTextFile
21 placeholderToSubstVar = builtins.replaceStrings [ "${placeholder "out"}" ] [ "@out@" ];
22
23 replacePlaceholderAndListToString = x:
24 if builtins.isList x
25 then placeholderToSubstVar (builtins.concatStringsSep " " x)
26 else placeholderToSubstVar x;
27
28 keywordsSection =
29 let
30 mustBeAList = attr: attrName: lib.throwIfNot (lib.isList attr) "'${attrName}' must be a list" attr;
31 in
32 {
33 "Name" = name;
34 "Description" = description;
35 "URL" = url;
36 "Version" = version;
37 "Requires" = mustBeAList requires "requires";
38 "Requires.private" = mustBeAList requiresPrivate "requiresPrivate";
39 "Conflicts" = mustBeAList conflicts "conflicts";
40 "Cflags" = mustBeAList cflags "cflags";
41 "Libs" = mustBeAList libs "libs";
42 "Libs.private" = mustBeAList libsPrivate "libsPrivate";
43 };
44
45 renderVariable = name: value:
46 lib.optionalString (value != "" && value != [ ]) "${name}=${replacePlaceholderAndListToString value}";
47 renderKeyword = name: value:
48 lib.optionalString (value != "" && value != [ ]) "${name}: ${replacePlaceholderAndListToString value}";
49
50 renderSomething = renderFunc: attrs:
51 lib.pipe attrs [
52 (lib.mapAttrsToList renderFunc)
53 (builtins.filter (v: v != ""))
54 (builtins.concatStringsSep "\n")
55 (section: ''${section}
56 '')
57 ];
58
59 variablesSectionRendered = renderSomething renderVariable variables;
60 keywordsSectionRendered = renderSomething renderKeyword keywordsSection;
61
62 content = [ variablesSectionRendered keywordsSectionRendered ];
63in
64writeTextFile {
65 name = "${name}.pc";
66 destination = "/lib/pkgconfig/${name}.pc";
67 text = builtins.concatStringsSep "\n" content;
68 checkPhase = ''${buildPackages.pkg-config}/bin/${buildPackages.pkg-config.targetPrefix}pkg-config --validate "$target"'';
69}