35

I have a program written in Haskell and intended to be compiled with GHC. The program scales very well on multiple cores, so enabling multithreading is very important. In my .cabal file I've added ghc-options: -O3 -threaded to link with the threaded runtime. The problem is that with this approach the user would need to run the program with foo +RTS -N, which seems a bit cryptic and not very user friendly.

How can I tell cabal/ghc to enable those runtime flags invisibly to the user? I've read about --with-rtsopts, but GHC (7.0.3) just spits out unrecognized flag when I try to use it.

Viktor Dahl
  • 1,942
  • 3
  • 25
  • 36

2 Answers2

35

The flag is -with-rtsopts, not --with-rtsopts, so you should add -with-rtsopts=-N to the ghc-options field. GHC Flag Reference.

Note that this will also require you to link with runtime support by adding -rtsopts to the ghc-options.

John L
  • 27,937
  • 4
  • 73
  • 88
  • Thank you, this helped me! I also tried enabling the `-g1` flag with `-with-rtsopts="-N -g1"` but then I get `unrecognized flag: -g1`. Both `-N` and `-g1` work fine separately. – Viktor Dahl Jun 28 '11 at 16:12
  • 1
    @Viktor Dahl: I think the quotes are causing the problem. Try either using single quotes, or multiple `-with-rtsopts` lines. If that solves it, it's probably a ghc bug (or documentation error). – John L Jun 28 '11 at 18:54
  • 7
    Single quotes didn't solve it, but using two `-with-rtsopts` did. – Viktor Dahl Jun 28 '11 at 19:44
  • Looks like a cabal bug: If you write `-with-rtsopts="-flagA -flagB"` it calls GHC with `'-with-rtspots="-flagA' '-flagB"'`, simply putting single quotes in at every space. – nh2 May 24 '13 at 01:36
  • 17
    @Viktor Dahl: It turns out we just used the wrong syntax. You have to write `"-with-rtsopts=-N -g1"`, with the quotes on the very outside. [This is the related discurrion on Github](https://github.com/haskell/cabal/pull/1346). – nh2 Jun 20 '13 at 00:50
2

If you happen to use hpack to generate foo.cabal from package.yaml, this is the YAML syntax to use:

executables:
  foobar:
    main: Main.hs
    source-dirs: app
    ghc-options:
      - -threaded
      - -rtsopts
      - '"-with-rtsopts=-N -T"'
      - -Wall

    dependencies:
      […]

The string "-with-rtsopts=-N␣-T" should become one single argv item of the eventual ghc process.

Since YAML has quoted string literals too — both layers of escaping are necessary.

ulidtko
  • 14,740
  • 10
  • 56
  • 88