Skip to content

Commit

Permalink
initial changes for adding "all" component
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Jan 26, 2019
1 parent 6139ef2 commit e4036ee
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
25 changes: 18 additions & 7 deletions builder/comp-builder.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
, preInstall ? null, postInstall ? null
, shellHook ? null

, doCheck ? component.doCheck || componentId.ctype == "test"
, doCheck ? component.doCheck || haskellLib.isTest componentId
, doCrossCheck ? component.doCrossCheck || false
, dontPatchELF ? true
, dontStrip ? true
Expand All @@ -28,18 +28,29 @@
}:

let
fullName = "${name}-${componentId.ctype}-${componentId.cname}";
fullName = if haskellLib.isAll componentId
then "${name}-all"
else "${name}-${componentId.ctype}-${componentId.cname}";

flagsAndConfig = field: xs: lib.optionalString (xs != []) ''
echo ${lib.concatStringsSep " " (map (x: "--${field}=${x}") xs)} >> $out/configure-flags
echo "${field}: ${lib.concatStringsSep " " xs}" >> $out/cabal.config
'';

componentDepends = if haskellLib.isAll componentId
then
# fixme: this needs to be the union of depends of all other components.
# The depends could be combined at the module option level but I
# had trouble with infinite recursion.
component.depends
else
component.depends;

flatDepends =
let
makePairs = map (p: rec { key="${val}"; val=p.components.library; });
closure = builtins.genericClosure {
startSet = makePairs component.depends;
startSet = makePairs componentDepends;
operator = {val,...}: makePairs val.config.depends;
};
in map ({val,...}: val) closure;
Expand Down Expand Up @@ -85,7 +96,7 @@ let
echo "allow-newer: ${package.identifier.name}:*" >> $out/cabal.config
echo "allow-older: ${package.identifier.name}:*" >> $out/cabal.config
${lib.concatMapStringsSep "\n" (p: exactDep "--package-db ${p.components.library}/package.conf.d" p.identifier.name) component.depends}
${lib.concatMapStringsSep "\n" (p: exactDep "--package-db ${p.components.library}/package.conf.d" p.identifier.name) componentDepends}
${lib.concatMapStringsSep "\n" (exactDep "") nonReinstallablePkgs}
''
Expand Down Expand Up @@ -123,7 +134,7 @@ let

finalConfigureFlags = lib.concatStringsSep " " (
[ "--prefix=$out"
"${componentId.ctype}:${componentId.cname}"
"${haskellLib.componentTarget componentId}"
"$(cat ${configFiles}/configure-flags)"
# GHC
"--with-ghc=${ghc.targetPrefix}ghc"
Expand Down Expand Up @@ -222,12 +233,12 @@ in stdenv.mkDerivation ({
installPhase = ''
runHook preInstall
$SETUP_HS copy ${lib.concatStringsSep " " component.setupInstallFlags}
${lib.optionalString (haskellLib.isLibrary componentId) ''
${lib.optionalString (haskellLib.isLibrary componentId || haskellLib.isAll componentId) ''
$SETUP_HS register --gen-pkg-config=${name}.conf
${ghc.targetPrefix}ghc-pkg -v0 init $out/package.conf.d
${ghc.targetPrefix}ghc-pkg -v0 --package-db ${configFiles}/package.conf.d -f $out/package.conf.d register ${name}.conf
''}
${lib.optionalString (componentId.ctype == "test") ''
${lib.optionalString (haskellLib.isTest componentId || haskellLib.isAll componentId) ''
mkdir -p $out/${name}
if [ -f "dist/build/${componentId.cname}/${componentId.cname}" ]; then
cp dist/build/${componentId.cname}/${componentId.cname} $out/${name}/
Expand Down
20 changes: 17 additions & 3 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
with haskellLib;

{
# Within the package components, these are the attribute names of
# nested attrsets.
subComponentTypes = [
"sublibs"
"foreignlibs"
Expand Down Expand Up @@ -66,13 +68,25 @@ with haskellLib;
applyComponents = f: config:
let
comps = config.components;
libComp = lib.mapAttrs (cname: f {ctype="lib"; cname=config.package.identifier.name;}) (removeAttrs comps subComponentTypes);
applyLibrary = cname: f { cname = config.package.identifier.name; ctype = "lib"; };
applySubComp = ctype: cname: f { inherit cname; ctype = componentPrefix.${ctype}; };
applyAllComp = f { cname = config.package.identifier.name; ctype = "all"; };
libComp = lib.mapAttrs applyLibrary (removeAttrs comps (subComponentTypes ++ [ "all" ]));
subComps = lib.mapAttrs
(ctype: lib.mapAttrs (cname: f {inherit cname; ctype=componentPrefix.${ctype};}))
(ctype: lib.mapAttrs (applySubComp ctype))
(builtins.intersectAttrs (lib.genAttrs subComponentTypes (_: null)) comps);
in subComps // libComp;
allComp = { all = applyAllComp comps.all; };
in subComps // libComp // allComp;

isLibrary = componentId: componentId.ctype == "lib";
isAll = componentId: componentId.ctype == "all";
isTest = componentId: componentId.ctype == "test";

# Format a componentId as it should appear as a target on the
# command line of the setup script.
componentTarget = componentId:
if componentId.ctype == "all" then ""
else "${componentId.ctype}:${componentId.cname}";

# Avoid pkgs.callPackage for now. It does a lot of nonsense with OOP
# style programming that we should avoid until we know we want it.
Expand Down
4 changes: 4 additions & 0 deletions modules/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ in {
type = attrsOf (componentType false);
default = {};
};
all = mkOption {
type = componentType false;
default = {};
};
};

name = mkOption {
Expand Down
6 changes: 5 additions & 1 deletion test/unit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let
library = "library";
sublibs = { };
tests = { };
all = "all";
};
package.identifier.name = "empty";
};
Expand All @@ -21,20 +22,23 @@ let
library = "library";
sublibs = { };
tests = { ttt = "ttt"; };
all = "all";
};
package.identifier.name = "nnn";
};

in
lib.runTests {
# identity function for applyComponents
test-applyComponents-id = {
expr = haskellLib.applyComponents (componentId: component: component) emptyConfig;
expected = emptyConfig.components;
};

# map a component to its component name and check these are correct
test-applyComponents-library = {
expr = haskellLib.applyComponents (componentId: component: componentId.cname) emptyConfig;
expected = emptyConfig.components // { library = "empty"; };
expected = emptyConfig.components // { library = "empty"; all = "empty"; };
};

test-applyComponents-components = {
Expand Down
19 changes: 12 additions & 7 deletions test/with-packages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,30 @@ in
name = "with-packages-test";

buildCommand = let
inherit (packages.test-with-packages.components) library;
# inherit (packages.test-with-packages.components) all; # TODO
inherit (packages.test-with-packages) devEnv;
package = packages.test-with-packages;
inherit (package.components) library;
inherit (package) devEnv;
in ''
########################################################################
# test with-packages
# fixme: test does not work yet -- need to fix dependencies of all component
# printf "checking that the 'all' component works... " >& 2
# echo ''${package.components.all}
# echo >& 2
printf "checking that the package env has the dependencies... " >& 2
${devEnv}/bin/runghc ${./Point.hs}
echo
echo >& 2
# # fixme: probably don't want separate derivation for this -- just use all
# printf "checking that components.library.shell has the dependencies... " >& 2
# ''${library.shell}/bin/runghc ${./Point.hs}
# echo
# echo >& 2
# printf "checking that components.all.shell has the dependencies... " >& 2
# ''${all.shell}/bin/runghc ${./Point.hs}
# echo
# ''${package.components.all.shell}/bin/runghc ${./Point.hs}
# echo >& 2
touch $out
'';
Expand Down

0 comments on commit e4036ee

Please sign in to comment.