core/pkgs/build-support/make-pkgconfigitem/default.nix

66 lines
2.3 KiB
Nix
Raw Normal View History

2024-05-02 00:46:19 +00:00
{ lib, writeTextFile, buildPackages }:
# See https://people.freedesktop.org/~dbn/pkg-config-guide.html#concepts
{ name # The name of the pc file
2024-05-13 21:24:10 +00:00
# keywords
# provide a default description for convenience. it's not important but still required by pkg-config.
, description ? "A pkg-config file for ${name}", url ? "", version ? ""
, requires ? [ ], requiresPrivate ? [ ], conflicts ? [ ], cflags ? [ ]
, libs ? [ ], libsPrivate ? [ ], variables ? { } }:
2024-05-02 00:46:19 +00:00
let
# only 'out' has to be changed, otherwise it would be replaced by the out of the writeTextFile
2024-05-13 21:24:10 +00:00
placeholderToSubstVar =
builtins.replaceStrings [ "${placeholder "out"}" ] [ "@out@" ];
2024-05-02 00:46:19 +00:00
replacePlaceholderAndListToString = x:
2024-05-13 21:24:10 +00:00
if builtins.isList x then
placeholderToSubstVar (builtins.concatStringsSep " " x)
else
placeholderToSubstVar x;
2024-05-02 00:46:19 +00:00
2024-05-13 21:24:10 +00:00
keywordsSection = let
mustBeAList = attr: attrName:
lib.throwIfNot (lib.isList attr) "'${attrName}' must be a list" 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";
};
2024-05-02 00:46:19 +00:00
renderVariable = name: value:
2024-05-13 21:24:10 +00:00
lib.optionalString (value != "" && value != [ ])
"${name}=${replacePlaceholderAndListToString value}";
2024-05-02 00:46:19 +00:00
renderKeyword = name: value:
2024-05-13 21:24:10 +00:00
lib.optionalString (value != "" && value != [ ])
"${name}: ${replacePlaceholderAndListToString value}";
2024-05-02 00:46:19 +00:00
renderSomething = renderFunc: attrs:
lib.pipe attrs [
(lib.mapAttrsToList renderFunc)
(builtins.filter (v: v != ""))
(builtins.concatStringsSep "\n")
2024-05-13 21:24:10 +00:00
(section: ''
${section}
2024-05-02 00:46:19 +00:00
'')
];
variablesSectionRendered = renderSomething renderVariable variables;
keywordsSectionRendered = renderSomething renderKeyword keywordsSection;
content = [ variablesSectionRendered keywordsSectionRendered ];
2024-05-13 21:24:10 +00:00
in writeTextFile {
2024-05-02 00:46:19 +00:00
name = "${name}.pc";
destination = "/lib/pkgconfig/${name}.pc";
text = builtins.concatStringsSep "\n" content;
2024-05-13 21:24:10 +00:00
checkPhase = ''
${buildPackages.pkg-config}/bin/${buildPackages.pkg-config.targetPrefix}pkg-config --validate "$target"'';
2024-05-02 00:46:19 +00:00
}