From 5b1e10cdfacc574773676be4100031b6a7b6b91d Mon Sep 17 00:00:00 2001 From: Andrea Bedini Date: Thu, 13 Jul 2023 20:17:09 +0800 Subject: [PATCH] Add component-identifier to components metadata (#1993) * Add component-identifier to components metadata * Use component-id in mkFlakeApps and mkFlakePackages * Lint --- builder/comp-builder.nix | 21 ++++++++------- lib/default.nix | 56 ++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/builder/comp-builder.nix b/builder/comp-builder.nix index 10e51d0fc7..70b0771667 100644 --- a/builder/comp-builder.nix +++ b/builder/comp-builder.nix @@ -171,7 +171,7 @@ let ) ++ [ "$(cat $configFiles/configure-flags)" ] ++ commonConfigureFlags); - commonConfigureFlags = ([ + commonConfigureFlags = [ # GHC "--with-ghc=${ghc.targetPrefix}ghc" "--with-ghc-pkg=${ghc.targetPrefix}ghc-pkg" @@ -208,7 +208,7 @@ let ++ lib.optional (enableLibraryProfiling || enableProfiling) "--profiling-detail=${profilingDetail}" ++ lib.optional stdenv.hostPlatform.isLinux (enableFeature enableDeadCodeElimination "split-sections") ++ lib.optionals haskellLib.isCrossHost ( - map (arg: "--hsc2hs-option=" + arg) (["--cross-compile"] ++ lib.optionals (stdenv.hostPlatform.isWindows) ["--via-asm"]) + map (arg: "--hsc2hs-option=" + arg) (["--cross-compile"] ++ lib.optionals stdenv.hostPlatform.isWindows ["--via-asm"]) ++ lib.optional (package.buildType == "Configure") "--configure-option=--host=${ # Older ghcjs patched config.sub to support "js-unknown-ghcjs" (not "javascript-unknown-ghcjs") if stdenv.hostPlatform.isGhcjs && builtins.compareVersions defaults.ghc.version "9" < 0 @@ -222,7 +222,7 @@ let ++ lib.optionals useLLVM [ "--ghc-option=-fPIC" "--gcc-option=-fPIC" ] - ++ map (o: ''--ghc${lib.optionalString (stdenv.hostPlatform.isGhcjs) "js"}-options="${o}"'') ghcOptions + ++ map (o: ''--ghc${lib.optionalString stdenv.hostPlatform.isGhcjs "js"}-options="${o}"'') ghcOptions ++ lib.optional ( # GHC 9.2 cross compiler built with older versions of GHC seem to have problems # with unique conters. Perhaps because the name changed for the counters. @@ -230,15 +230,14 @@ let haskellLib.isCrossHost && builtins.compareVersions defaults.ghc.version "9.2.1" >= 0 && builtins.compareVersions defaults.ghc.version "9.3" < 0) - "--ghc-options=-j1" - ); + "--ghc-options=-j1"; # the build-tools version might be depending on the version of the package, similarly to patches executableToolDepends = (lib.concatMap (c: if c.isHaskell or false then builtins.attrValues (c.components.exes or {}) else [c]) - (builtins.filter (x: !(isNull x) + (builtins.filter (x: x != null # We always exclude hsc2hs from build-tools because it is unecessary as it is provided by ghc # and hsc2hs from ghc is first in PATH so the one from build-tools is never used. && x.identifier.name or "" != "hsc2hs") @@ -347,7 +346,11 @@ let inherit dontPatchELF dontStrip; passthru = { - inherit (package) identifier; + identifier = package.identifier // { + component-id = "${package.identifier.name}:${componentId.ctype}:${componentId.cname}"; + component-name = componentId.cname; + component-type = componentId.ctype; + }; config = component; srcSubDir = cleanSrc.subDir; srcSubDirPath = cleanSrc.root + cleanSrc.subDir; @@ -393,7 +396,7 @@ let # These only need to be propagated for library components (otherwise they # will be in `buildInputs`) ++ lib.optionals (haskellLib.isLibrary componentId) configFiles.libDeps # libDeps is already deduplicated - ++ lib.optionals (stdenv.hostPlatform.isWindows) + ++ lib.optionals stdenv.hostPlatform.isWindows (haskellLib.uniqueWithName (lib.flatten component.libs))); buildInputs = haskellLib.checkUnique "${ghc.targetPrefix}${fullName} buildInputs" ( @@ -414,7 +417,7 @@ let prePatch = # emcc is very slow if it cannot cache stuff in $HOME - (lib.optionalString (stdenv.hostPlatform.isGhcjs) '' + (lib.optionalString stdenv.hostPlatform.isGhcjs '' export HOME=$(mktemp -d) '') + (lib.optionalString (!canCleanSource) '' diff --git a/lib/default.nix b/lib/default.nix index 16e0fb7914..4dc1358917 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -35,7 +35,7 @@ in { ]; foldrAttrVals = f: z: attrs: - lib.foldr (g: acc: g acc) z (lib.mapAttrsToList (_name: f) attrs); + lib.foldr f z (builtins.attrValues attrs); foldComponents = tys: f: z: conf: let @@ -91,18 +91,17 @@ in { isExe componentId || isTest componentId || isBenchmark componentId; - mayHaveExecutable = componentId: - isExecutableType componentId; + mayHaveExecutable = isExecutableType; # Was there a reference to the package source in the `cabal.project` or `stack.yaml` file. # This is used to make the default `packages` list for `shellFor`. isLocalPackage = p: p.isLocal or false; - selectLocalPackages = ps: lib.filterAttrs (n: p: p != null && isLocalPackage p) ps; + selectLocalPackages = lib.filterAttrs (n: p: p != null && isLocalPackage p); # if it's a project package it has a src attribute set with an origSubDir attribute. # project packages are a subset of localPackages isProjectPackage = p: p.isProject or false; - selectProjectPackages = ps: lib.filterAttrs (n: p: p != null && isLocalPackage p && isProjectPackage p) ps; + selectProjectPackages = lib.filterAttrs (n: p: p != null && isLocalPackage p && isProjectPackage p); # Format a componentId as it should appear as a target on the # command line of the setup script. @@ -133,7 +132,7 @@ in { ## flatLibDepends :: Component -> [Package] flatLibDepends = component: let - makePairs = map (p: rec { key=val.name; val=(p.components.library or p); }); + makePairs = map (p: rec { key=val.name; val=p.components.library or p; }); closure = builtins.genericClosure { startSet = makePairs component.depends; operator = {val,...}: makePairs val.config.depends; @@ -427,35 +426,24 @@ in { # Flake package names that are flat and match the cabal component names. mkFlakePackages = haskellPackages: builtins.listToAttrs ( lib.concatLists (lib.mapAttrsToList (packageName: package: - lib.optional (package.components ? library) - { name = "${packageName}:lib:${packageName}"; value = package.components.library; } - ++ lib.mapAttrsToList (n: v: - { name = "${packageName}:lib:${n}"; value = v; }) - (package.components.sublibs) - ++ lib.mapAttrsToList (n: v: - { name = "${packageName}:exe:${n}"; value = v; }) - (package.components.exes) - ++ lib.mapAttrsToList (n: v: - { name = "${packageName}:test:${n}"; value = v; }) - (package.components.tests) - ++ lib.mapAttrsToList (n: v: - { name = "${packageName}:bench:${n}"; value = v; }) - (package.components.benchmarks) - ) haskellPackages)); + builtins.groupBy + (c: c.passthru.identifier.component-id) + ((lib.optional (package.components ? library) package.components.library) + ++ package.components.sublibs + ++ package.components.exes + ++ package.components.tests + ++ package.components.benchmarks) + ) haskellPackages)); # Flake package names that are flat and match the cabal component names. mkFlakeApps = haskellPackages: builtins.listToAttrs ( lib.concatLists (lib.mapAttrsToList (packageName: package: - lib.mapAttrsToList (n: v: - { name = "${packageName}:exe:${n}"; value = { type = "app"; program = v.exePath; }; }) - (package.components.exes) - ++ lib.mapAttrsToList (n: v: - { name = "${packageName}:test:${n}"; value = { type = "app"; program = v.exePath; }; }) - (package.components.tests) - ++ lib.mapAttrsToList (n: v: - { name = "${packageName}:benchmark:${n}"; value = { type = "app"; program = v.exePath; }; }) - (package.components.benchmarks) - ) haskellPackages)); + builtins.groupBy + (c: c.passthru.identifier.component-id) + (package.components.exes + ++ package.components.tests + ++ package.components.benchmarks) + ) haskellPackages)); # Flatten the result of collectChecks or collectChecks' for use in flake `checks` mkFlakeChecks = allChecks: builtins.listToAttrs ( @@ -464,7 +452,7 @@ in { lib.optionals (lib.isAttrs checks) ( lib.mapAttrsToList (n: v: { name = "${packageName}:test:${n}"; value = v; }) - (lib.filterAttrs (_: v: lib.isDerivation v) checks)) + (lib.filterAttrs (_: lib.isDerivation) checks)) ) allChecks)); removeRecurseForDerivations = x: @@ -494,11 +482,11 @@ in { } # Build the plan-nix and check it if materialized // lib.optionalAttrs (checkedProject ? plan-nix) { - plan-nix = checkedProject.plan-nix; + inherit (checkedProject) plan-nix; } # Build the stack-nix and check it if materialized // lib.optionalAttrs (checkedProject ? stack-nix) { - stack-nix = checkedProject.stack-nix; + inherit (checkedProject) stack-nix; }; mkFlake = project: {