haskellcabalcabal-install

What are cabal components and how do I use them?


cabal build --help and others mention components like in sentences like

Build one or more targets from within the project. The available targets are   
the packages in the project as well as individual components within those
packages, including libraries, executables, test-suites or benchmarks. Targets
can be specified by name or location. If no target is specified then the
default is to build the package in the current directory.

From the cabal user guide mentions them too in 9. Setup.hs Commands, and gives two prefixes exe: and lib: to select those. Are there more of those prefixes?


Solution

  • A component is anything behind a stanza with its own set of dependencies, etc. So it can be multiple sublibraries, multiple executables, test-suites, etc.

    You're right that this is underdocumented! In the source code we can see the following (https://github.com/haskell/cabal/blob/00a2351789a460700a2567eb5ecc42cca0af913f/Cabal/src/Distribution/Simple/BuildTarget.hs#L569)

    matchComponentKind :: String -> Match ComponentKind
    matchComponentKind s
      | s `elem` ["lib", "library"]                 = return' LibKind
      | s `elem` ["flib", "foreign-lib", "foreign-library"] = return' FLibKind
      | s `elem` ["exe", "executable"]              = return' ExeKind
      | s `elem` ["tst", "test", "test-suite"]      = return' TestKind
      | s `elem` ["bench", "benchmark"]             = return' BenchKind
      | otherwise = matchErrorExpected "component kind" s
      where
        return' ck = increaseConfidence >> return ck
    

    So that's the full list!