From 7249b8a2f3318bb03c50429f5907015e99901c0b Mon Sep 17 00:00:00 2001 From: Artturin Date: Wed, 22 Jun 2022 15:22:51 +0300 Subject: [PATCH 1/3] makePkgconfigItem: init new function to generate pc files A function to generate pkg-config files for Nix packages that need to create them ad hoc, like blas and lapack. Inspiration taken from `makeDesktopItem`. --- .../make-pkgconfigitem/default.nix | 69 +++++++++++++++++++ .../setup-hooks/copy-pkgconfig-items.sh | 46 +++++++++++++ pkgs/top-level/all-packages.nix | 4 ++ 3 files changed, 119 insertions(+) create mode 100644 pkgs/build-support/make-pkgconfigitem/default.nix create mode 100644 pkgs/build-support/setup-hooks/copy-pkgconfig-items.sh diff --git a/pkgs/build-support/make-pkgconfigitem/default.nix b/pkgs/build-support/make-pkgconfigitem/default.nix new file mode 100644 index 000000000000..288ca3810eb8 --- /dev/null +++ b/pkgs/build-support/make-pkgconfigitem/default.nix @@ -0,0 +1,69 @@ +{ 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 ? "A 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: 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"; + }; + + 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 != "")) + (builtins.concatStringsSep "\n") + (section: ''${section} + '') + ]; + + 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/pkg-config --validate "$target"''; +} diff --git a/pkgs/build-support/setup-hooks/copy-pkgconfig-items.sh b/pkgs/build-support/setup-hooks/copy-pkgconfig-items.sh new file mode 100644 index 000000000000..8c04ec9b5f0e --- /dev/null +++ b/pkgs/build-support/setup-hooks/copy-pkgconfig-items.sh @@ -0,0 +1,46 @@ +# shellcheck shell=bash + +# Setup hook that installs specified pkgconfig items. +# +# Example usage in a derivation: +# +# { …, makePkgconfigItem, copyPkgconfigItems, … }: +# +# let pkgconfigItem = makePkgconfigItem { … }; in +# stdenv.mkDerivation { +# … +# nativeBuildInputs = [ copyPkgconfigItems ]; +# +# pkgconfigItems = [ pkgconfigItem ]; +# … +# } +# +# This hook will copy files which are either given by full path +# or all '*.pc' files placed inside the 'lib/pkgconfig' +# folder of each `pkgconfigItems` argument. + +postInstallHooks+=(copyPkgconfigItems) + +copyPkgconfigItems() { + if [ "${dontCopyPkgconfigItems-}" = 1 ]; then return; fi + + if [ -z "$pkgconfigItems" ]; then + return + fi + + pkgconfigdir="${!outputDev}/lib/pkgconfig" + for pkgconfigItem in $pkgconfigItems; do + if [[ -f "$pkgconfigItem" ]]; then + substituteAllInPlace "$pkgconfigItem" + echo "Copying '$pkgconfigItem' into '${pkgconfigdir}'" + install -D -m 444 -t "${pkgconfigdir}" "$pkgconfigItem" + substituteAllInPlace "${pkgconfigdir}"/* + else + for f in "$pkgconfigItem"/lib/pkgconfig/*.pc; do + echo "Copying '$f' into '${pkgconfigdir}'" + install -D -m 444 -t "${pkgconfigdir}" "$f" + substituteAllInPlace "${pkgconfigdir}"/* + done + fi + done +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index e9102ff37f57..f1520606dd70 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -826,6 +826,10 @@ with pkgs; makeDesktopItem = callPackage ../build-support/make-desktopitem { }; + copyPkgconfigItems = makeSetupHook { } ../build-support/setup-hooks/copy-pkgconfig-items.sh; + + makePkgconfigItem = callPackage ../build-support/make-pkgconfigitem { }; + makeDarwinBundle = callPackage ../build-support/make-darwin-bundle { }; makeAutostartItem = callPackage ../build-support/make-startupitem { }; From 0b7a3b24d606b794785e5d8a7b81138a9815d216 Mon Sep 17 00:00:00 2001 From: Artturin Date: Wed, 22 Jun 2022 19:24:31 +0300 Subject: [PATCH 2/3] stb: add pkgconfig file --- pkgs/development/libraries/stb/default.nix | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pkgs/development/libraries/stb/default.nix b/pkgs/development/libraries/stb/default.nix index 22a97d890e56..fee0cb42164e 100644 --- a/pkgs/development/libraries/stb/default.nix +++ b/pkgs/development/libraries/stb/default.nix @@ -1,6 +1,6 @@ -{ lib, stdenv, fetchFromGitHub }: +{ lib, stdenv, fetchFromGitHub, copyPkgconfigItems, makePkgconfigItem }: -stdenv.mkDerivation { +stdenv.mkDerivation rec { pname = "stb"; version = "unstable-2021-09-10"; @@ -11,11 +11,28 @@ stdenv.mkDerivation { sha256 = "0qq35cd747lll4s7bmnxb3pqvyp2hgcr9kyf758fax9lx76iwjhr"; }; + nativeBuildInputs = [ copyPkgconfigItems ]; + + pkgconfigItems = [ + (makePkgconfigItem rec { + name = "stb"; + version = "1"; + cflags = [ "-I${variables.includedir}/stb" ]; + variables = rec { + prefix = "${placeholder "out"}"; + includedir = "${prefix}/include"; + }; + inherit (meta) description; + }) + ]; + dontBuild = true; installPhase = '' + runHook preInstall mkdir -p $out/include/stb cp *.h $out/include/stb/ + runHook postInstall ''; meta = with lib; { From c6a9d069e938c3018d58ad2624cfd660e805841b Mon Sep 17 00:00:00 2001 From: Artturin Date: Wed, 20 Jul 2022 06:22:14 +0300 Subject: [PATCH 3/3] tinycc: use makePkgconfigItem --- pkgs/development/compilers/tinycc/default.nix | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/pkgs/development/compilers/tinycc/default.nix b/pkgs/development/compilers/tinycc/default.nix index ced8312dbf5a..693326620530 100644 --- a/pkgs/development/compilers/tinycc/default.nix +++ b/pkgs/development/compilers/tinycc/default.nix @@ -1,6 +1,8 @@ { lib , stdenv , fetchFromRepoOrCz +, copyPkgconfigItems +, makePkgconfigItem , perl , texinfo , which @@ -17,11 +19,32 @@ stdenv.mkDerivation rec { }; nativeBuildInputs = [ + copyPkgconfigItems perl texinfo which ]; + pkgconfigItems = [ + (makePkgconfigItem rec { + name = "libtcc"; + inherit version; + cflags = [ "-I${variables.includedir}" ]; + libs = [ + "-L${variables.libdir}" + "-Wl,--rpath ${variables.libdir}" + "-ltcc" + "-ldl" + ]; + variables = rec { + prefix = "${placeholder "out"}"; + includedir = "${prefix}/include"; + libdir = "${prefix}/lib"; + }; + description = "Tiny C compiler backend"; + }) + ]; + postPatch = '' patchShebangs texi2pod.pl ''; @@ -43,17 +66,6 @@ stdenv.mkDerivation rec { configureFlagsArray+=("--elfinterp=$(< $NIX_CC/nix-support/dynamic-linker)") ''; - postFixup = '' - cat >libtcc.pc <