about summary refs log tree commit diff
path: root/pkgs/top-level/pkg-config
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-01-29 08:54:13 +0100
committerRobert Hensing <robert@roberthensing.nl>2023-01-29 09:51:55 +0100
commit3be7ea8c891b3209e92e961fe689649fb0333bcc (patch)
tree491de62a6676ccb943f8900628598990ea5f9bd2 /pkgs/top-level/pkg-config
parent811bf8ade022b34148a5b71242ca1a713865c7fa (diff)
top-level/pkg-config: Make tests easy to find
Diffstat (limited to 'pkgs/top-level/pkg-config')
-rw-r--r--pkgs/top-level/pkg-config/defaultPkgConfigPackages.nix45
-rw-r--r--pkgs/top-level/pkg-config/test-defaultPkgConfigPackages.nix83
-rw-r--r--pkgs/top-level/pkg-config/tests.nix21
3 files changed, 149 insertions, 0 deletions
diff --git a/pkgs/top-level/pkg-config/defaultPkgConfigPackages.nix b/pkgs/top-level/pkg-config/defaultPkgConfigPackages.nix
new file mode 100644
index 0000000000000..b3cf3cdca2fcd
--- /dev/null
+++ b/pkgs/top-level/pkg-config/defaultPkgConfigPackages.nix
@@ -0,0 +1,45 @@
+/* A set of aliases to be used in generated expressions.
+
+   In case of ambiguity, this will pick a sensible default.
+
+   This was initially based on cabal2nix's mapping.
+
+   It'd be nice to generate this mapping, based on a set of derivations.
+   It can not be fully automated, so it should be a expression or tool
+   that makes suggestions about which pkg-config module names can be added.
+ */
+pkgs:
+
+let
+  inherit (pkgs) lib;
+  inherit (lib)
+    all
+    flip
+    mapAttrs
+    mapAttrsToList
+    getAttrFromPath
+    importJSON
+    ;
+
+  data = importJSON ./pkg-config-data.json;
+  inherit (data) modules;
+
+  platform = pkgs.stdenv.hostPlatform;
+
+  isSupported = moduleData:
+    moduleData?supportedWhenPlatformAttrsEqual ->
+      all (x: x) (
+        mapAttrsToList
+          (k: v: platform?${k} && platform.${k} == v)
+          moduleData.supportedWhenPlatformAttrsEqual
+      );
+
+  modulePkgs = flip mapAttrs modules (_moduleName: moduleData:
+    if moduleData?attrPath && isSupported moduleData then
+      getAttrFromPath moduleData.attrPath pkgs
+    else
+      null
+  );
+
+in
+  modulePkgs
diff --git a/pkgs/top-level/pkg-config/test-defaultPkgConfigPackages.nix b/pkgs/top-level/pkg-config/test-defaultPkgConfigPackages.nix
new file mode 100644
index 0000000000000..e34e1435c3c5c
--- /dev/null
+++ b/pkgs/top-level/pkg-config/test-defaultPkgConfigPackages.nix
@@ -0,0 +1,83 @@
+# cd nixpkgs
+# nix-build -A tests.pkg-config.defaultPkgConfigPackages
+{ lib, pkg-config, defaultPkgConfigPackages, runCommand }:
+let
+  inherit (lib.strings) escapeNixIdentifier;
+
+  allTests = lib.mapAttrs (k: v: if v == null then null else makePkgConfigTestMaybe k v) defaultPkgConfigPackages;
+
+  # nix-build rejects attribute names with periods
+  # This will build those regardless.
+  tests-combined = runCommand "pkg-config-checks" {
+    allTests = lib.attrValues allTests;
+  } ''
+    touch $out
+  '';
+
+  makePkgConfigTestMaybe = moduleName: pkg:
+    if ! lib.isDerivation pkg
+    then
+      throw "pkg-config module `${escapeNixIdentifier moduleName}` is not defined to be a derivation. Please check the attribute value for `${escapeNixIdentifier moduleName}` in `pkgs/top-level/pkg-config-packages.nix` in Nixpkgs."
+
+    else if ! pkg?meta.unsupported
+    then
+      throw "pkg-config module `${escapeNixIdentifier moduleName}` does not have a `meta.unsupported` attribute. This can't be right. Please check the attribute value for `${escapeNixIdentifier moduleName}` in `pkgs/top-level/pkg-config-packages.nix` in Nixpkgs."
+
+    else if pkg.meta.unsupported
+    then
+      # We return `null` instead of doing a `filterAttrs`, because with
+      # `filterAttrs` the evaluator would not be able to return the attribute
+      # set without first evaluating all of the attribute _values_. This would
+      # be rather expensive, and severly slow down the use case of getting a
+      # single test, which we want to do in `passthru.tests`, or interactively.
+      null
+
+    else if ! pkg?meta.broken
+    then
+      throw "pkg-config module `${escapeNixIdentifier moduleName}` does not have a `meta.broken` attribute. This can't be right. Please check the attribute value for `${escapeNixIdentifier moduleName}` in `pkgs/top-level/pkg-config.packages.nix` in Nixpkgs."
+
+    else if pkg.meta.broken
+    then null
+
+    else makePkgConfigTest moduleName pkg;
+
+  makePkgConfigTest = moduleName: pkg: runCommand "check-pkg-config-${moduleName}" {
+    nativeBuildInputs = [ pkg-config ];
+    buildInputs = [ pkg ];
+    inherit moduleName;
+    meta = {
+      description = "Test whether ${pkg.name} exposes pkg-config module ${moduleName}";
+    }
+    # Make sure licensing info etc is preserved, as this is a concern for e.g. cache.nixos.org,
+    # as hydra can't check this meta info in dependencies.
+    # The test itself is just Nixpkgs, with MIT license.
+    // builtins.intersectAttrs
+        {
+          available = throw "unused";
+          broken = throw "unused";
+          insecure = throw "unused";
+          license = throw "unused";
+          maintainers = throw "unused";
+          platforms = throw "unused";
+          unfree = throw "unused";
+          unsupported = throw "unused";
+        }
+        pkg.meta;
+  } ''
+    echo "checking pkg-config module $moduleName in $buildInputs"
+    set +e
+    version="$(pkg-config --modversion $moduleName)"
+    r=$?
+    set -e
+    if [[ $r = 0 ]]; then
+      echo "✅ pkg-config module $moduleName exists and has version $version"
+      echo "$version" > $out
+    else
+      echo "These modules were available in the input propagation closure:"
+      pkg-config --list-all
+      echo "❌ pkg-config module $moduleName was not found"
+      false
+    fi
+  '';
+in
+  allTests // { inherit tests-combined; }
diff --git a/pkgs/top-level/pkg-config/tests.nix b/pkgs/top-level/pkg-config/tests.nix
new file mode 100644
index 0000000000000..bcc8e7b4ee3e0
--- /dev/null
+++ b/pkgs/top-level/pkg-config/tests.nix
@@ -0,0 +1,21 @@
+# cd nixpkgs
+# nix-build -A tests.pkg-config
+{ lib, stdenv, ... }:
+
+let
+  # defaultPkgConfigPackages test needs a Nixpkgs with allowUnsupportedPlatform
+  # in order to filter out the unsupported packages without throwing any errors
+  # tryEval would be too fragile, masking different problems as if they're
+  # unsupported platform problems.
+  allPkgs = import ../default.nix {
+    system = stdenv.hostPlatform.system;
+    localSystem = stdenv.hostPlatform.system;
+    config = {
+      allowUnsupportedSystem = true;
+    };
+    overlays = [];
+  };
+in
+lib.recurseIntoAttrs {
+  defaultPkgConfigPackages = allPkgs.callPackage ./test-defaultPkgConfigPackages.nix { };
+}