Skip to content

Allow passing RTS options to GHC in stack.yaml #5568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Bug fixes:
[pantry#33](https://github.com/commercialhaskell/pantry/issues/33)
* When building the sanity check for a new GHC install, make sure to clear
`GHC_PACKAGE_PATH`.

* Specifying GHC RTS flags in the `stack.yaml` no longer fails with an error. [#5568](https://github.com/commercialhaskell/stack/pull/5568)

## v2.7.1

Expand Down
41 changes: 40 additions & 1 deletion src/Stack/Types/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -629,14 +629,53 @@ configureOptsNoDir econfig bco deps isLocal package = concat
flagNameString name)
(Map.toList flags)
, map T.unpack $ packageCabalConfigOpts package
, concatMap (\x -> [compilerOptionsCabalFlag wc, T.unpack x]) (packageGhcOptions package)
, processGhcOptions (packageGhcOptions package)
, map ("--extra-include-dirs=" ++) (configExtraIncludeDirs config)
, map ("--extra-lib-dirs=" ++) (configExtraLibDirs config)
, maybe [] (\customGcc -> ["--with-gcc=" ++ toFilePath customGcc]) (configOverrideGccPath config)
, ["--exact-configuration"]
, ["--ghc-option=-fhide-source-paths" | hideSourcePaths cv]
]
where
-- This function parses the GHC options that are providing in the
-- stack.yaml file. In order to handle RTS arguments correctly, we need
-- to provide the RTS arguments as a single argument.
processGhcOptions :: [Text] -> [String]
processGhcOptions args =
let
(preRtsArgs, mid) =
break ("+RTS" ==) args
(rtsArgs, end) =
break ("-RTS" ==) mid
fullRtsArgs =
case rtsArgs of
[] ->
-- This means that we didn't have any RTS args - no
-- `+RTS` - and therefore no need for a `-RTS`.
[]
_ ->
-- In this case, we have some RTS args. `break`
-- puts the `"-RTS"` string in the `snd` list, so
-- we want to append it on the end of `rtsArgs`
-- here.
--
-- We're not checking that `-RTS` is the first
-- element of `end`. This is because the GHC RTS
-- allows you to omit a trailing -RTS if that's the
-- last of the arguments. This permits a GHC
-- options in stack.yaml that matches what you
-- might pass directly to GHC.
[T.unwords $ rtsArgs ++ ["-RTS"]]
-- We drop the first element from `end`, because it is always
-- either `"-RTS"` (and we don't want that as a separate
-- argument) or the list is empty (and `drop _ [] = []`).
postRtsArgs =
drop 1 end
newArgs =
concat [preRtsArgs, fullRtsArgs, postRtsArgs]
in
concatMap (\x -> [compilerOptionsCabalFlag wc, T.unpack x]) newArgs

wc = view (actualCompilerVersionL.to whichCompiler) econfig
cv = view (actualCompilerVersionL.to getGhcVersion) econfig

Expand Down
6 changes: 6 additions & 0 deletions test/integration/tests/5180-ghc-rts-flags/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import StackTest

main :: IO ()
main = do
stack ["clean"]
stack ["build"]
11 changes: 11 additions & 0 deletions test/integration/tests/5180-ghc-rts-flags/files/files.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: files
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10

library
hs-source-dirs: src
exposed-modules: Lib
build-depends: base >= 4.7 && < 5, mtl
default-language: Haskell2010

4 changes: 4 additions & 0 deletions test/integration/tests/5180-ghc-rts-flags/files/src/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Lib where

main :: IO ()
main = putStrLn "hello world"
6 changes: 6 additions & 0 deletions test/integration/tests/5180-ghc-rts-flags/files/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resolver: lts-14.27
packages:
- .

ghc-options:
"$locals": -j8 +RTS -s -A128M