diff --git a/Cabal/doc/cabal-commands.rst b/Cabal/doc/cabal-commands.rst index c19df8b3570..8b119fd68ad 100644 --- a/Cabal/doc/cabal-commands.rst +++ b/Cabal/doc/cabal-commands.rst @@ -86,6 +86,12 @@ A target can take any of the following forms: - ``tests``, - ``benches``, ``benchmarks``. +- A module target: ``[package:][ctype:]module``, which specifies that the + component of which the given module is a part of will be built. + +- A filepath target: ``[package:][ctype:]filepath``, which specifies that the + component of which the given filepath is a part of will be built. + In component targets, ``package:`` and ``ctype:`` (valid component types are ``lib``, ``flib``, ``exe``, ``test`` and ``bench``) can be used to disambiguate when multiple packages define the same component, or the @@ -99,9 +105,12 @@ Some example targets: $ cabal v2-build lib:foo-pkg # build the library named foo-pkg $ cabal v2-build foo-pkg:foo-tests # build foo-tests in foo-pkg - -(There is also syntax for specifying module and file targets, but it -doesn't currently do anything.) + $ cabal v2-build src/Lib.s # build the library component to + # which "src/Lib.hs" belongs + $ cabal v2-build app/Main.hs # build the executable component of + # "app/Main.hs" + $ cabal v2-build Lib # build the library component to + # which the module "Lib" belongs Beyond a list of targets, ``cabal v2-build`` accepts all the flags that ``cabal v2-configure`` takes. Most of these flags are only taken into diff --git a/cabal-install/Distribution/Client/TargetSelector.hs b/cabal-install/Distribution/Client/TargetSelector.hs index 1ea5375341d..33ac305a7ba 100644 --- a/cabal-install/Distribution/Client/TargetSelector.hs +++ b/cabal-install/Distribution/Client/TargetSelector.hs @@ -2108,12 +2108,14 @@ matchComponentOtherFile :: [KnownComponent] -> String -> Match (FilePath, KnownComponent) matchComponentOtherFile cs = matchFile - [ (file, c) - | c <- cs - , file <- cinfoHsFiles c - ++ cinfoCFiles c - ++ cinfoJsFiles c + [ (normalise (srcdir file), c) + | c <- cs + , srcdir <- cinfoSrcDirs c + , file <- cinfoHsFiles c + ++ cinfoCFiles c + ++ cinfoJsFiles c ] + . normalise matchComponentModuleFile :: [KnownComponent] -> String diff --git a/cabal-install/cabal-install.cabal b/cabal-install/cabal-install.cabal index b3487292f84..42403907eb6 100644 --- a/cabal-install/cabal-install.cabal +++ b/cabal-install/cabal-install.cabal @@ -80,8 +80,10 @@ Extra-Source-Files: tests/IntegrationTests2/targets/multiple-tests/cabal.project tests/IntegrationTests2/targets/multiple-tests/p.cabal tests/IntegrationTests2/targets/simple/P.hs + tests/IntegrationTests2/targets/simple/app/Main.hs tests/IntegrationTests2/targets/simple/cabal.project tests/IntegrationTests2/targets/simple/p.cabal + tests/IntegrationTests2/targets/simple/q/Q.hs tests/IntegrationTests2/targets/simple/q/QQ.hs tests/IntegrationTests2/targets/simple/q/q.cabal tests/IntegrationTests2/targets/test-only/p.cabal diff --git a/cabal-install/cabal-install.cabal.pp b/cabal-install/cabal-install.cabal.pp index 0bf2b42ec71..5c558ee7a59 100644 --- a/cabal-install/cabal-install.cabal.pp +++ b/cabal-install/cabal-install.cabal.pp @@ -365,8 +365,10 @@ tests/IntegrationTests2/targets/multiple-tests/cabal.project tests/IntegrationTests2/targets/multiple-tests/p.cabal tests/IntegrationTests2/targets/simple/P.hs + tests/IntegrationTests2/targets/simple/app/Main.hs tests/IntegrationTests2/targets/simple/cabal.project tests/IntegrationTests2/targets/simple/p.cabal + tests/IntegrationTests2/targets/simple/q/Q.hs tests/IntegrationTests2/targets/simple/q/QQ.hs tests/IntegrationTests2/targets/simple/q/q.cabal tests/IntegrationTests2/targets/test-only/p.cabal diff --git a/cabal-install/tests/IntegrationTests2.hs b/cabal-install/tests/IntegrationTests2.hs index aef1694b1ce..3629c52ad01 100644 --- a/cabal-install/tests/IntegrationTests2.hs +++ b/cabal-install/tests/IntegrationTests2.hs @@ -248,9 +248,15 @@ testTargetSelectors reportSubCase = do ":pkg:p:lib:p:file:P.y" , "q/QQ.hs", "q:QQ.lhs", "lib:q:QQ.hsc", "q:q:QQ.hsc", ":pkg:q:lib:q:file:QQ.y" + , "q/Q.hs", "q:Q.lhs", "lib:q:Q.hsc", "q:q:Q.hsc", + ":pkg:q:lib:q:file:Q.y" + , "app/Main.hs", "p:app/Main.hs", "exe:ppexe:app/Main.hs", "p:ppexe:app/Main.hs", + ":pkg:p:exe:ppexe:file:app/Main.hs" ] ts @?= replicate 5 (TargetComponent "p-0.1" (CLibName LMainLibName) (FileTarget "P")) ++ replicate 5 (TargetComponent "q-0.1" (CLibName LMainLibName) (FileTarget "QQ")) + ++ replicate 5 (TargetComponent "q-0.1" (CLibName LMainLibName) (FileTarget "Q")) + ++ replicate 5 (TargetComponent "p-0.1" (CExeName "ppexe") (FileTarget ("app" "Main.hs"))) -- Note there's a bit of an inconsistency here: for the single-part -- syntax the target has to point to a file that exists, whereas for -- all the other forms we don't require that. @@ -356,6 +362,16 @@ testTargetSelectorAmbiguous reportSubCase = do , mkexe "other2" `withCFiles` ["Foo"] ] ] + -- File target is ambiguous, part of multiple components + reportSubCase "ambiguous: file in multiple comps" + assertAmbiguous "Bar.hs" + [ mkTargetFile "foo" (CExeName "bar") "Bar" + , mkTargetFile "foo" (CExeName "bar2") "Bar" + ] + [ mkpkg "foo" [ mkexe "bar" `withModules` ["Bar"] + , mkexe "bar2" `withModules` ["Bar"] ] + ] + -- non-exact case packages and components are ambiguous reportSubCase "ambiguous: non-exact-case pkg names" assertAmbiguous "Foo" diff --git a/cabal-install/tests/IntegrationTests2/targets/simple/app/Main.hs b/cabal-install/tests/IntegrationTests2/targets/simple/app/Main.hs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/cabal-install/tests/IntegrationTests2/targets/simple/p.cabal b/cabal-install/tests/IntegrationTests2/targets/simple/p.cabal index 4f7f5e3847d..87797575f8c 100644 --- a/cabal-install/tests/IntegrationTests2/targets/simple/p.cabal +++ b/cabal-install/tests/IntegrationTests2/targets/simple/p.cabal @@ -10,3 +10,8 @@ library executable pexe main-is: Main.hs other-modules: PMain + +executable ppexe + main-is: Main.hs + hs-source-dirs: app + other-modules: PMain \ No newline at end of file diff --git a/cabal-install/tests/IntegrationTests2/targets/simple/q/Q.hs b/cabal-install/tests/IntegrationTests2/targets/simple/q/Q.hs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/cabal-install/tests/IntegrationTests2/targets/simple/q/q.cabal b/cabal-install/tests/IntegrationTests2/targets/simple/q/q.cabal index d8a3609d127..788a23a4b2c 100644 --- a/cabal-install/tests/IntegrationTests2/targets/simple/q/q.cabal +++ b/cabal-install/tests/IntegrationTests2/targets/simple/q/q.cabal @@ -5,6 +5,7 @@ cabal-version: >= 1.2 library exposed-modules: QQ + other-modules: Q build-depends: base executable qexe