on iko/ghcjs-patch-master help sign in
Add ghcOptions on packages (#257)

* Add `ghcOptions` with example and docs

commit147cf2134100be0b72e14a47ded9a1c8b57d5b57 authorHamish Mackenzie <Hamish.Mackenzie@iohk.io> dateSun 13 Oct 2019, 19:19 (7 years ago) parent933becf
14 files changed+138−5
builder/comp-builder.nix+4−2 docs/user-guide/projects.md+2 modules/package.nix+5 nix-tools/nix-tools-src.json+3−3 test/default.nix+2 test/ghc-options/.gitignore+2 test/ghc-options/Setup.hs+2 test/ghc-options/app/Main.hs+9 test/ghc-options/cabal.nix+36 test/ghc-options/cabal.project+4 test/ghc-options/src/Lib.hs+9 test/ghc-options/stack.nix+33 test/ghc-options/stack.yaml+7 test/ghc-options/test-ghc-options.cabal+20
builder/comp-builder.nix+4−2
@@ -88,6 +88,8 @@ let
8888 ++ component.configureFlags
8989 );
9090
91+ setupGhcOptions = lib.optional (package.ghcOptions != null) '' --ghc-options="${package.ghcOptions}"'';
92+
9193 executableToolDepends =
9294 (lib.concatMap (c: if c.isHaskell or false
9395 then builtins.attrValues (c.components.exes or {})
@@ -190,7 +192,7 @@ stdenv.mkDerivation ({
190192 buildPhase = ''
191193 runHook preBuild
192194 # https://gitlab.haskell.org/ghc/ghc/issues/9221
193− $SETUP_HS build -j$(($NIX_BUILD_CORES > 4 ? 4 : $NIX_BUILD_CORES)) ${lib.concatStringsSep " " component.setupBuildFlags}
195+ $SETUP_HS build -j$(($NIX_BUILD_CORES > 4 ? 4 : $NIX_BUILD_CORES)) ${lib.concatStringsSep " " (component.setupBuildFlags ++ setupGhcOptions)}
194196 runHook postBuild
195197 '';
196198
@@ -211,7 +213,7 @@ stdenv.mkDerivation ({
211213 "--html" \
212214 ${lib.optionalString doHoogle "--hoogle"} \
213215 ${lib.optionalString hyperlinkSource "--hyperlink-source"} \
214− ${lib.concatStringsSep " " component.setupHaddockFlags}
216+ ${lib.concatStringsSep " " (component.setupHaddockFlags ++ setupGhcOptions)}
215217
216218 html="dist/doc/html/${componentId.cname}"
217219
docs/user-guide/projects.md+2
@@ -27,6 +27,7 @@ let
2727 modules = [
2828 # specific package overrides would go here
2929 # example:
30+ # packages.cbors.package.ghcOptions = "-Werror";
3031 # packages.cbors.patches = [ ./one.patch ];
3132 # packages.cbors.flags.optimize-gmp = false;
3233 # It may be better to set flags in stack.yaml instead (overrides will
@@ -41,6 +42,7 @@ let
4142 modules = [
4243 # specific package overrides would go here
4344 # example:
45+ # packages.cbors.package.ghcOptions = "-Werror";
4446 # packages.cbors.patches = [ ./one.patch ];
4547 # To override a flag you will need to use mkOverride to make
4648 # it clear that you wish to replace the value found in the
modules/package.nix+5
@@ -131,6 +131,11 @@ in {
131131 type = bool;
132132 default = false;
133133 };
134+
135+ ghcOptions = mkOption {
136+ type = nullOr str;
137+ default = null;
138+ };
134139 };
135140
136141 components = let
nix-tools/nix-tools-src.json+3−3
@@ -1,7 +1,7 @@
11{
22 "url": "https://github.com/input-output-hk/nix-tools",
3− "rev": "5d4649a833cb8b963266cea016d5a12a30022266",
4− "date": "2019-10-11T14:24:33+13:00",
5− "sha256": "1xf3llli08axrhhda80hx95nmbwmdcz0y4r2vmgfhi3lqbcww8vk",
3+ "rev": "87d97c50a53e421ed016d4c9a1dc41f9e6edda53",
4+ "date": "2019-10-12T14:56:33+08:00",
5+ "sha256": "1zvk13haiiyb6mcmypjn20i6vcx98y8zp5ipj0vpbflpg7f6rldm",
66 "fetchSubmodules": false
77}
test/default.nix+2
@@ -24,6 +24,8 @@ in pkgs.recurseIntoAttrs {
2424 buildable = haskell.callPackage ./buildable {};
2525 project-flags-cabal = haskell.callPackage ./project-flags/cabal.nix {};
2626 project-flags-stack = haskell.callPackage ./project-flags/stack.nix {};
27+ ghc-options-cabal = haskell.callPackage ./ghc-options/cabal.nix {};
28+ ghc-options-stack = haskell.callPackage ./ghc-options/stack.nix {};
2729
2830 # Run unit tests with: nix-instantiate --eval --strict -A unit.tests
2931 # An empty list means success.
test/ghc-options/.gitignorenew file+2
@@ -0,0 +1,2 @@
1+.stack-work/
2+*~
test/ghc-options/Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
test/ghc-options/app/Main.hsnew file+9
@@ -0,0 +1,9 @@
1+{-# LANGUAGE CPP #-}
2+module Main where
3+
4+import Lib
5+
6+#ifdef TEST_GHC_OPTION
7+main :: IO ()
8+main = someFunc
9+#endif
test/ghc-options/cabal.nixnew file+36
@@ -0,0 +1,36 @@
1+{ stdenv, mkCabalProjectPkgSet, callCabalProjectToNix, importAndFilterProject }:
2+
3+with stdenv.lib;
4+
5+let
6+ plan = (importAndFilterProject (callCabalProjectToNix {
7+ index-state = "2019-04-30T00:00:00Z";
8+ src = ./.;
9+ }));
10+ pkgSet = mkCabalProjectPkgSet {
11+ plan-pkgs = plan.pkgs;
12+ # TODO find a way to get the ghc-options into plan.json so we can use it in plan-to-nix
13+ modules = [ { packages.test-ghc-options.package.ghcOptions = "-DTEST_GHC_OPTION"; } ];
14+ };
15+ packages = pkgSet.config.hsPkgs;
16+in
17+ stdenv.mkDerivation {
18+ name = "call-cabal-project-to-nix-test";
19+
20+ buildCommand = ''
21+ exe="${packages.test-ghc-options.components.exes.test-ghc-options-exe}/bin/test-ghc-options-exe"
22+
23+ printf "checking whether executable runs... " >& 2
24+ $exe
25+
26+ touch $out
27+ '';
28+
29+ meta.platforms = platforms.all;
30+
31+ passthru = {
32+ # Attributes used for debugging with nix repl
33+ inherit pkgSet packages;
34+ plan-nix = plan.nix;
35+ };
36+ }
test/ghc-options/cabal.projectnew file+4
@@ -0,0 +1,4 @@
1+packages: .
2+
3+package test-ghc-options
4+ ghc-options: -DTEST_GHC_OPTION
test/ghc-options/src/Lib.hsnew file+9
@@ -0,0 +1,9 @@
1+{-# LANGUAGE CPP #-}
2+module Lib
3+ ( someFunc
4+ ) where
5+
6+#ifdef TEST_GHC_OPTION
7+someFunc :: IO ()
8+someFunc = putStrLn "someFunc"
9+#endif
test/ghc-options/stack.nixnew file+33
@@ -0,0 +1,33 @@
1+{ stdenv, mkStackPkgSet, callStackToNix, importAndFilterProject }:
2+
3+with stdenv.lib;
4+
5+let
6+ stack = (importAndFilterProject (callStackToNix {
7+ src = ./.;
8+ }));
9+ pkgSet = mkStackPkgSet {
10+ stack-pkgs = stack.pkgs;
11+ };
12+ packages = pkgSet.config.hsPkgs;
13+in
14+ stdenv.mkDerivation {
15+ name = "callStackToNix-test";
16+
17+ buildCommand = ''
18+ exe="${packages.test-ghc-options.components.exes.test-ghc-options-exe}/bin/test-ghc-options-exe"
19+
20+ printf "checking whether executable runs... " >& 2
21+ $exe
22+
23+ touch $out
24+ '';
25+
26+ meta.platforms = platforms.all;
27+
28+ passthru = {
29+ # Attributes used for debugging with nix repl
30+ inherit pkgSet packages;
31+ stack-nix = stack.nix;
32+ };
33+ }
test/ghc-options/stack.yamlnew file+7
@@ -0,0 +1,7 @@
1+resolver: lts-13.19
2+
3+packages:
4+- .
5+
6+ghc-options:
7+ test-ghc-options: -DTEST_GHC_OPTION
test/ghc-options/test-ghc-options.cabalnew file+20
@@ -0,0 +1,20 @@
1+cabal-version: >=1.10
2+name: test-ghc-options
3+version: 0.1.0.0
4+license: PublicDomain
5+author: Hamish Mackenzie
6+maintainer: Hamish.Mackenzie@iohk.io
7+build-type: Simple
8+
9+library
10+ build-depends: base >=4.7 && <5
11+ exposed-modules: Lib
12+ hs-source-dirs: src
13+ default-language: Haskell2010
14+
15+executable test-ghc-options-exe
16+ main-is: Main.hs
17+ build-depends: base >=4.7 && <5, test-ghc-options
18+ hs-source-dirs: app
19+ ghc-options: -threaded -rtsopts -with-rtsopts=-N
20+ default-language: Haskell2010