C directory in Haskell .cabal file - haskell

I have this .cabal file:
...
main-is: Main.hs
other-extensions: ForeignFunctionInterface
build-depends: base >=4.9 && <4.10
hs-source-dirs: src/haskell
c-sources: src/c/main.c ...
default-language: Haskell2010
I have a lots of c source files, all in one directory(src/c), is there a way to add them all without specifying each one? something like a regex:
-- I know this won't work
c-sources: src/c/**
Or just including the whole directory:
-- I know this won't work
c-source-dirs: src/c
?

Try: c-sources: src/c/*.c
** wildcards were added in Cabal 2.4
Wildcard examples from here: https://www.haskell.org/cabal/users-guide/developing-packages.html?highlight=c%20source#pkg-field-data-files

Related

Cabal: How to configure transitive build-dependencies in the same project

I have a cabal project. It has library and test targets.
Test target depends on library because it tests the library's functionalities.
The problem is, whenever I add a package dependency to library (say, cryptohash-sha1)
library Lib
exposed-modules: Lib
other-extensions: DeriveGeneric
build-depends: base >=4.13 && <4.14,
cryptohash-sha1,
and run the test, I get error
Could not load module ‘Crypto.Hash.SHA1’.
It is a member of the hidden package ‘cryptohash-sha1-0.11.100.1’.
Perhaps you need to add ‘cryptohash-sha1’ to the build-depends in your .cabal file
What I do in this situation is to add the same package to test target
test-suite json-generator-test
hs-source-dirs: test, src
main-is: Test.hs
other-modules: Lib
build-depends: base >=4.13 && <4.14
cryptohash-sha1,
Only then the test would run.
I want test target to automatically depend on all the packages from library target. How can I do that?
You can use a cabal feature called common stanzas. You can read more about it in the following blog post:
https://vrom911.github.io/blog/common-stanzas
With this approach, you can put all common dependencies in a separate stanza, and just import it in both the library and test suite:
common common-dependencies
build-depends: base >=4.13 && <4.14
, cryptohash-sha1
library Lib
import: common-dependencies
exposed-modules: Lib
other-extensions: DeriveGeneric
test-suite json-generator-test
import: common-dependencies
hs-source-dirs: test, src
main-is: Test.hs
other-modules: Lib

How do I import a text file in a cabal project?

In app/Main.hs, I want to open a text file, "foo.txt". I know how to open a text file in a plain Haskell program. In my cabal project,
import System.IO
Main = do
contents <- readFile "foo.txt"
print $ Main.lex contents
return contents
type Keyword = String
lex :: String -> [Keyword]
lex "" = []
lex x = words x
gives the error
openFile: does not exist (No such file or directory)
What do I need to change in my cabal file, or the file path or location to be able to open the file? I've tried putting it next to the output binary, and that doesn't work either.
This is my cabal file:
-- This file has been generated from package.yaml by hpack version 0.28.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: baf2fc7e230f4b4937dfd918a13fefb55b66c7a4468b24d0e3e90cad675b26d5
name: CCompiler
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/CCompiler#readme>
homepage: https://github.com/githubuser/CCompiler#readme
bug-reports: https://github.com/githubuser/CCompiler/issues
author: Author name here
maintainer: example#example.com
copyright: 2018 Author name here
license: BSD3
license-file: LICENSE
build-type: Simple
cabal-version: >= 1.10
extra-source-files:
ChangeLog.md
README.md
source-repository head
type: git
location: https://github.com/githubuser/CCompiler
library
exposed-modules:
Lib
other-modules:
Paths_CCompiler
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
default-language: Haskell2010
executable CCompiler-exe
main-is: Main.hs
other-modules:
Paths_CCompiler
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
CCompiler
, base >=4.7 && <5
default-language: Haskell2010
test-suite CCompiler-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_CCompiler
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
CCompiler
, base >=4.7 && <5
default-language: Haskell2010
add
data-dir: data
in the top section of the cabal file.
create the directory 'data' next to src and app, and put all files in there.
Make sure your cabal file also has this line
other-modules:
Paths_CCompiler
with your project's name instead of CCompiler.
My main function is now this
module Main where
import Lib
import System.IO
import Paths_CCompiler
main = do
filepath <- getDataFileName "return_2.c"
contents <- readFile filepath
print $ Lib.lex contents
return contents
Thanks to this blog post.
I understand your question to be about finding files at runtime, which you want to process and aren't packaged with your package.
Where are severals ways, how you could find files at runtime, which aren't packaged.
Either add an command line flag and call your executable with the absolute path of the file you want to process.
Or implement a file chooser dialog, with e.g. gi-gtk.
Or hard coding relative paths which isn't advisable, as they are interpreted relative to the current working directory of your process, which can be different, depending on how the program did get started.
If you want to determine, which current working directory your program runs in, if started with cabal run, just do a litte test project with the following cabal file:
name: test2
build-type: Simple
cabal-version: >= 1.10
version: 0.0.0.1
executable test2
hs-source-dirs: .
main-is: test2.hs
build-depends:
base
, directory
and the following test2.hs:
module Main where
import System.Directory
main :: IO ()
main = do
cwd <- getCurrentDirectory
putStrLn cwd

How to make Stack only build one executable? stack build :exe1 doesn't work?

I have multiple executables in one of my Stack projects:
executable m1
hs-source-dirs: src
main-is: Main1.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
executable m2
hs-source-dirs: src
main-is: Main2.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
I would like to build only one of them at a time. From the documentation and this other question, it would seem that the following command should work:
stack build :m2
but what actually happens is that both executables, m1 and m2, are always built regardless of whether I include the package name, the package type, etc.
What could be causing this?
It's not possible at the moment. See https://github.com/commercialhaskell/stack/issues/1406

Executable within a library and while using the same library in cabal

This is my first try at an open source project, but I still can't get the right way to set up my .cabal file.
I have have library, an executable and a (soon to come) test config.
I want to use the library and within the executable, so the both of them can be used when it gets downloaded.
I followed this guide, but I'm still struggling with the cabal-config as I only get it to work while importing everything again.
My current directory
- src/
- Main.hs
- Format/
- C.hs
- Converter.hs
- Raw.hs
- RGB565.hs
- tests/...
- dist/...
- UTFTConverter.cabal
The executable Main.hs header looks like this.
module Main where
import Format.C
import Format.Converter
The library files in Format/ look like this.
module Format.{filename} where
...
This is what the cabal file looks like.
name: UTFTConverter
...
cabal-version: >=1.10
library
exposed-modules: Format.C
, Format.Converter
, Format.Raw
, Format.RGB565
build-depends: base >=4.7 && <4.8
, filepath >=1.3 && <1.4
, directory >=1.2 && <1.3
, time >=1.4 && <1.5
, bytestring >=0.10 && <0.11
, JuicyPixels >=3.2 && <3.3
hs-source-dirs: src
...
executable UTFTConverter
main-is: Main.hs
build-depends: base >=4.7 && <4.8
, filepath >=1.3 && <1.4
, directory >=1.2 && <1.3
, time >=1.4 && <1.5
, bytestring >=0.10 && <0.11
, JuicyPixels >=3.2 && <3.3
--, UTFTConverter ==0.1 <-- this does not work
hs-source-dirs: src
...
test-suite tests:
...
Without the comment this error comes up when I cabal build.
...
cabal: At least the following dependencies are missing:
UTFTConverter ==0.1
...
It works right now, but in the tutorial the executable was using the library in the same cabal file.
executable bassbull
main-is: Main.hs
ghc-options: -rtsopts -O2
build-depends: base,
bassbull, -- <-- this is the name of the library
bytestring,
cassava
hs-source-dirs: src
default-language: Haskell2010
I know this is currently working, but I would rather use it the right way from the start. Is this the "right" way?
That's due to your library version being 0.1.0.0, not 0.1. They don't match up exactly, thus cabal doesn't recognize your library as a candidate. Instead, use 0.1.* or 0.1.0.0 depending on your version policy:
executable UTFTConverter
main-is: Main.hs
build-depends: base >=4.7 && <4.8
, filepath >=1.3 && <1.4
, directory >=1.2 && <1.3
, time >=1.4 && <1.5
, bytestring >=0.10 && <0.11
, JuicyPixels >=3.2 && <3.3
, UTFTConverter ==0.1.0.0
hs-source-dirs: src
References
Cabal User Guide, section "Build Information".

Shared cabal "build-depends" (Haskell) [duplicate]

Here's a .cabal file:
Name: myprogram
Version: 0.1
-- blah blah blah
Cabal-version: >=1.9.2
Executable myprogram
HS-source-dirs: src
Main-is: Main.hs
Build-depends: attoparsec == 0.10.*,
base == 4.3.*,
-- long long list of packages
Test-Suite test
HS-source-dirs: test, src
Type: exitcode-stdio-1.0
Main-is: Main.hs
Build-depends: attoparsec == 0.10.*,
base == 4.3.*,
-- long long list of packages
QuickCheck == 2.4.*
Is there any way I can replace the long list of build-depends packages for the test suite with "same as for the executable, plus QuickCheck"?
Edit: version information.
cabal-dev 0.9
cabal-install 0.10.2
Cabal library 1.10.2.0
GHC 7.0.4
Haskell Platform 2011.4.0.0
NOTE: superseded by phadej's answer suggesting common stanzas.
Is there any way I can replace the long list of build-depends packages for the test suite with "same as for the executable, plus QuickCheck"?
Not that I know of. However, there is a way to only mention the list of build-depends packages once, by structuring your project into three targets:
a library that contains all your code, and needs the long build-depends list.
an executable that consists of only one file, and depends on base and the library from above.
a test-suite that depends on the library from above, and the testing packages you are using.
Maybe this approach is what indygemma's answer proposes, but the Cabal file proposed there will not quite achieve it, as Norman Ramsey points out in a comment. Here's the main points of what you need in a Cabal file. For a full example that works for me, you can look at this Cabal file.
name: my-program
version: ...
library
hs-source-dirs: src-lib
build-depends: base, containers, ...
exposed-modules: My.Program.Main, ...
executable my-program
hs-source-dirs: src-exec
main-is: my-program.hs
Build-depends: base, my-program
test-suite tests
type: exitcode-stdio-1.0
hs-source-dirs: src-test
main-is: tests.hs
other-modules: ...
build-depends: base, my-program, test-framework, ...
Important points:
There are three separate source directories for the three targets. This is necessary to stop GHC from recompiling library files when building the other targets.
All of the application code is in the library. The executable is just a wrapper, like this:
import My.Program.Main (realMain)
main = realMain
The library exposes all modules that are necessary for testing.
The last point highlights the drawback of this approach: You end up having to expose internal modules. The main benefit of this approach is that you have less duplication in the Cabal file, and maybe more importantly, less duplication in the build process: The library code will be built only once, and then linked into both the executable and the test-suite.
Since version 2.2 Cabal supports common stanzas, to dedup build info fields:
https://cabal.readthedocs.io/en/latest/developing-packages.html#common-stanzas
cabal-version: 2.2
name: myprogram
version: 0.1
-- blah blah blah
common deps
build-depends: base ^>= 4.11,
-- long long list of packages
ghc-options: -Wall
library
import: deps
exposed-modules: Foo
test-suite tests
import: deps
type: exitcode-stdio-1.0
main-is: Tests.hs
build-depends: foo
You could also consider using hpack instead of writing the .cabal file by hand:
In hpack's package.yaml format, you can specify a common dependencies field whose entries are added to every components' build-depends field when generating the .cabal file.
For example, see hpack's own package.yaml and the generated hpack.cabal.
To start using hpack with an existing package, you can use hpack-convert which will generate the package.yaml from an existing .cabal file.
To create a new package that uses hpack, you can use stack's simple-hpack template like so: stack new mypkg simple-hpack.
If you use stack for development, you don't have to call hpack manually to regenerate the .cabal file from an updated package.yaml – stack will do that automatically.
No easy way:
you can use m4 and specify your dependencies once, but then you will need to reprocess your Cabal file through m4 whenever you change it.
you can move the code you are testing out to a library, and then specify the library in your Build-depends for the test. That requires you to install a library even just to run the test.
You can just not put the test in the cabal file at all. Build it with ghc --make, which will pull in dependencies. But then you lose cabal integration.
There is an optional library section for .cabal files, which solves your problem.
name: myprogram
version: 0.1
-- blah blah blah
cabal-version: >=1.9.2
library
build-depends: attoparsec == 0.10.*
, base == 4.3.*
-- long long list of packages
executable myprogram
hs-source-dirs: src
main-is: Main.hs
test-suite test
hs-source-dirs: test, src
type: exitcode-stdio-1.0
main-is: Main.hs
build-depends: QuickCheck == 2.4.*

Resources