Search code examples
haskellbuildcompilationhaskell-stack

Force static compilation in stack


I use stack with multiple projects, I need to have two ways to compile them a "normal one" for the day-to-day development and one for the deployment.

I use hpack and my projects look like that:

name:                test
version:             0.1.0
github:              "th/ng"
author:              "Me"
description:         Ok


dependencies:
- base >= 4.7 && < 5

executables:
  test-bootstrap:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N

For my deployments I need to configure them as following:

executables:
  test-bootstrap:
    main:                Main.hs
    source-dirs:         app
    cc-options:          -static
    ld-options:          -static -pthread
    extra-lib-dirs:      ./.system-work/lib
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    - -static

Which forces me to run with stack build --docker.

I have tried to leverage stack build --ghc-options "-static"

But I have an horrible trace:

test          > /usr/bin/ld.gold: error: /home/stackage/.stack/programs/x86_64-linux/ghc-8.8.3/lib/ghc-8.8.3/ghc-prim-0.5.3/libHSghc-prim-0.5.3.a(Classes.o): requires unsupported dynamic reloc 11; recompile with -fPIC

Is there a way to give cc-options and ld-options to stack, or to have multi-projects flags?


Solution

  • I have leveraged the Yaml include mechanism, here is my flags.yaml for static compilation:

    - &deployed_exe
      cc-options:          -static
      ld-options:          -static -pthread
      extra-lib-dirs:      ./.system-work/lib
      ghc-options:
        - -threaded
        - -rtsopts
        - -with-rtsopts=-N
        - -static
    

    And my package.yamls:

    _flags: !include "../flags.yaml"
    
    
    executables:
      test-bootstrap:
        <<: *deployed_exe
        main:                Main.hs
        source-dirs:         app
    

    I have a symbolic link I switch depending of my needs.