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?
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.
Related
Is it possible to somehow pass --pedantic (as in stack build --pedantic) switch to Haskell-ide-engine? I'd like to see more errors in IDE during compile time as I'm very new to the language, for example for non-exhaustive case patterns.
I cannot find any infos on project page apart from this bug https://github.com/haskell/haskell-ide-engine/issues/449 but this does not seem to adress this issue.
So far as I can tell it is not possible to pass compiler or build tool switches in to HIE. HIE automatically determines your compiler flags based on your build tool and does not have an override mechanism.
Instead you should add the appropriate compiler flags to your build file. stack build --pedantic passes the -Wall and -Werror flags, so those are the flags you want to add to your build file. That way the flags will always be used by both stack build and HIE.
package.yaml
If you have a package.yaml file (the default for most Stack projects) then you should add the following lines to the ghc-options section of that file:
- -Wall
- -Werror
Example:
name: project-name
version: 0.1.0.0
github: "githubuser/project-name"
license: BSD3
author: "Author name here"
maintainer: "example#example.com"
copyright: "2020 Author name here"
description: Example
dependencies:
- base >= 4.7 && < 5
ghc-options:
- -Wall
- -Werror
executables:
project-name-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
package-name.cabal
If you do not have a package.yaml file, then add the following lines to all of the executable and library sections your cabal file:
ghc-options:
-Wall
-Werror
Example:
name: project-name
version: 0.1.0.0
-- synopsis:
-- description:
homepage: https://github.com/githubuser/project-name#readme
license: BSD3
license-file: LICENSE
author: Author name here
maintainer: example#example.com
copyright: 2020 Author name here
category: Web
build-type: Simple
cabal-version: >=1.10
extra-source-files: README.md
executable project-name
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
ghc-options:
-Wall
-Werror
More flags
As a side note, GHC's -Wall does not enable all of the warnings, only most of them. You might want to add these extra warnings:
-Wcompat
-Wincomplete-uni-patterns
-Wincomplete-record-updates
-Wredundant-constraints
-Wpartial-fields
See https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/using-warnings.html for more details.
During development you might want to remove -Werror from your package.yaml or cabal file.
I am trying to create a hello world yesod web application and encounter the error message
cannot satisfy -package yesod-1.6.0
What I've done is, create a project with stack new yeplay and then in the package.yaml file I put, yesod in the dependencies section, that looks as following:
executables:
yeplay-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- yeplay
- yesod
What am I doing wrong?
I would like to always use the "-Wall -Werror" options when building with stack (executing stack build) but adding these flags to ghc-options in package.yaml does nothing. I would also like to avoid passing the --pedantic flag to stack build. Here's the config files:
package.yaml
...
executables:
XYZ-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -Wall
- -Werror
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- XYZ
...
XYZ.cabal
...
executable XYZ-exe
main-is: Main.hs
hs-source-dirs:
app
ghc-options: -Wall -Werror -threaded -rtsopts -with-rtsopts=-N
...
The "-Wall -Werror" flags are specified in ghc-options but as-if ignored when building. This is the output for stack build:
stack build
Building all executables for `XYZ' once. After a successful build of all of
them, only specified executables will be rebuilt.
XYZ-0.1.0.0: configure (lib + exe)
Configuring XYZ-0.1.0.0...
XYZ-0.1.0.0: build (lib + exe)
Preprocessing library for XYZ-0.1.0.0..
Building library for XYZ-0.1.0.0..
[ 1 of 105] Compiling Data.List.Extras ( src\Data\List\Extras.hs, .stack-
work\dist\e626a42b\build\Data\List\Extras.o )
... the rest is omitted, all succeed ...
And here's the output for stack build --pedantic
stack build --pedantic
Building all executables for `HStat' once. After a successful build of all of them, only specified executables will be rebuilt.
HStat-0.1.0.0: configure (lib + exe)
Configuring HStat-0.1.0.0...
HStat-0.1.0.0: build (lib + exe)
Preprocessing library for HStat-0.1.0.0..
Building library for HStat-0.1.0.0..
[ 1 of 105] Compiling Data.List.Extras ( src\Data\List\Extras.hs, .stack-work\dist\e626a42b\build\Data\List\Extras.o )
src\Data\List\Extras.hs:4:1: error: [-Wunused-imports, -Werror=unused-imports]
The import of ‘Data.Maybe’ is redundant
except perhaps to import instances from ‘Data.Maybe’
To import instances alone, use: import Data.Maybe()
|
4 | import Data.Maybe
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
This works as expected - src\Data\List\Extras.hs indeed does have an unused Data.Maybe import. What am I doing wrong?
The ghc-options flags had to be separately defined in the library part of package.yaml:
library:
source-dirs: src
ghc-options:
- -Wall
- -Werror
- -fwarn-incomplete-uni-patterns
Doing that solved the issue.
I'm trying to make 2 executables "project". All duplicates of this question did not help me - their answers don't fix my problem. I have .cabal file like this:
name: int-tests
version: 0.1.0.0
synopsis: Integration Tests Suite
description: Integration Tests Suite
license: AllRightsReserved
author: Author name here
maintainer: example#example.com
copyright: 2018 Author name here
build-type: Custom
extra-source-files: README.md
cabal-version: >=1.10
library
hs-source-dirs: common
exposed-modules: Common
build-depends: base
, text
, aeson
, network-uri
default-language: Haskell2010
ghc-options: -Wall -Werror
executable api-tests-exe
hs-source-dirs: api
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall -Werror
build-depends: base
, hspec
, QuickCheck
default-language: Haskell2010
executable e2e-tests-exe
hs-source-dirs: e2e
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall -Werror
build-depends: base
, hspec
, QuickCheck
default-language: Haskell2010
and when I call stack ide targets I don't see these 2 targets. So, stack build api-tests and stack build e2e-tests don't work too.
How can I create 2 targets' project for stack? I tried also package.yaml but result is the same. Stack version is 1.9.1. I have folders tree like:
api/
...
e2e/
...
where are Main.hs files with content like:
module Main (main) where
main :: IO ()
main = print "Hello"
Also I tried option -main-is Main but without success.
Error looks like:
Error: While constructing the build plan, the following exceptions were encountered:
Unknown package: api-tests
AFAIK, stack build always builds all your targets. But if you want to be run just one executable, you'll need the full name including the -exe. So, stack exec api-tests-exe and stack exec e2e-tests-exe.
But what you really want to do is make these test targets: https://www.haskell.org/cabal/users-guide/developing-packages.html#test-suites
Problem was in stack.yaml file, I had to add '.' folder to "packages:" section.
Silly question. I have a cabal file with a library and an executable that I would like to use to profile the library, but I can't manage to see cost centers from my library (although I see some from other modules like GHC.IO.Encoding).
Here's a simplified version of my cabal file
flag dev
default: False
manual: True
library
exposed-modules: Foo
ghc-options: -Wall
ghc-prof-options: -fprof-auto
build-depends: base
executable dev-example
if !flag(dev)
buildable: False
ghc-options: -ddump-to-file -ddump-simpl -dsuppress-module-prefixes -dsuppress-uniques -ddump-core-stats -ddump-inlinings
ghc-options: -O2 -rtsopts
ghc-prof-options: -fprof-auto
hs-source-dirs: dev-example, ./
main-is: Main.hs
build-depends: base
Where I've been doing
$ cabal configure -fdev -w /usr/local/bin/ghc-7.6.3 --enable-library-profiling --enable-executable-profiling
$ cabal run dev-example -- +RTS -h -p
Ugh, the issue was simply that my library code was being inlined (or at least marked INLINABLE).