Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack: Compile stand-alone source file

Once you've installed Stack, you can use it to install GHC for you. Great!

...now how do I compile a file with it?

To be clear: What you're supposed to do is write a package specification and have Stack build that. But surely there must be a way to trivially compile a tiny helper file inside that project? It seems silly to have to construct an entire project just so I can build a tiny helper program and have it easily runnable from the shell.

I know I can run any Haskell file using Stack. But I cannot for the life of me figure out how to compile the thing...

like image 594
MathematicalOrchid Avatar asked Apr 08 '18 12:04

MathematicalOrchid


People also ask

How do I compile a Haskell stack?

You can use stack ghc -- <file names> to compile and link a set of files (it's briefly mentioned in Stack's user guide): You'll sometimes want to just compile (or run) a single Haskell source file, instead of creating an entire Cabal package for it. You can use stack exec ghc or stack exec runghc for that.

What is stack Ghci?

stack ghci allows you to load components and files of your project into ghci . It uses the same TARGET syntax as stack build , and can also take options like --test , --bench , and --flag . Similarly to stack build , the default is to load up ghci with all libraries and executables in the project.

Where does stack install GHC?

In its default configuration, Stack will simply ignore any system GHC installation and use a sandboxed GHC that it has installed itself. You can find these sandboxed GHC installations in the ghc-* directories in the stack path --programs directory.

How do you create a project stack?

To build your project, Stack uses a project-level configuration file, named stack. yaml , in the root directory of your project as a sort of blueprint. That file contains a reference, called a resolver, to the snapshot which your package will be built against.


1 Answers

You can use stack ghc -- <file names> to compile and link a set of files (it's briefly mentioned in Stack's user guide):

You'll sometimes want to just compile (or run) a single Haskell source file, instead of creating an entire Cabal package for it. You can use stack exec ghc or stack exec runghc for that. As simple helpers, we also provide the stack ghc and stack runghc commands, for these common cases.

The -- is to ensure the arguments we pass are sent to ghc, rather than being parsed as arguments to stack exec. It's the same thing as when trying to pass arguments to an executable you've made using the normal stack toolchain: stack exec myExe -foo passes -foo to exec, not myExe, stack exec myExe -- -foo behaves as desired.


For example:

Bar.hs

module Bar where

bar :: Int
bar = 5

Foo.hs

import Bar

main :: IO ()
main = print bar

Compilation (don't even need to specify Bar.hs in the build files, it's sourced automatically):

> stack ghc -- Foo.hs
[1 of 2] Compiling Bar              ( Bar.hs, Bar.o )
[2 of 2] Compiling Main             ( Foo.hs, Foo.o )
Linking Foo ...
> ./Foo
5

There's no problem with dependencies either - it looks like all the packages installed locally are available to the build (you can use containers or QuickCheck without any additional build params).

like image 60
hnefatl Avatar answered Oct 13 '22 21:10

hnefatl