diff --git a/Cabal-tests/tests/UnitTests/Distribution/Utils/Structured.hs b/Cabal-tests/tests/UnitTests/Distribution/Utils/Structured.hs index 37d38f2bac4..2dc84db4237 100644 --- a/Cabal-tests/tests/UnitTests/Distribution/Utils/Structured.hs +++ b/Cabal-tests/tests/UnitTests/Distribution/Utils/Structured.hs @@ -29,7 +29,7 @@ tests = testGroup "Distribution.Utils.Structured" , testCase "GenericPackageDescription" $ md5Check (Proxy :: Proxy GenericPackageDescription) 0x8d8f340f10a58b8d8a87bf42213dac89 , testCase "LocalBuildInfo" $ - md5Check (Proxy :: Proxy LocalBuildInfo) 0xbb22c3258d3092f31e992bc093d09170 + md5Check (Proxy :: Proxy LocalBuildInfo) 0x618ab257e99d0b21c617e1f8c39a5a4b #endif ] diff --git a/Cabal/src/Distribution/Simple/GHC.hs b/Cabal/src/Distribution/Simple/GHC.hs index 2ef352f300b..e3f5adcc487 100644 --- a/Cabal/src/Distribution/Simple/GHC.hs +++ b/Cabal/src/Distribution/Simple/GHC.hs @@ -705,35 +705,45 @@ buildOrReplLib mReplFlags verbosity numJobs pkg_descr lbi lib clbi = do | filename <- cxxSources libBi] -- build any C sources - unless (not has_code || null (cSources libBi)) $ do + let (cSrcs', others) = partition (\filepath -> ".c"`isSuffixOf` filepath) (cSources libBi) + unless (not has_code || null cSrcs') $ do info verbosity "Building C Sources..." - sequence_ - [ do let baseCcOpts = Internal.componentCcGhcOptions verbosity implInfo - lbi libBi clbi relLibTargetDir filename - vanillaCcOpts = if isGhcDynamic - -- Dynamic GHC requires C sources to be built - -- with -fPIC for REPL to work. See #2207. - then baseCcOpts { ghcOptFPic = toFlag True } - else baseCcOpts - profCcOpts = vanillaCcOpts `mappend` mempty { - ghcOptProfilingMode = toFlag True, - ghcOptObjSuffix = toFlag "p_o" - } - sharedCcOpts = vanillaCcOpts `mappend` mempty { - ghcOptFPic = toFlag True, - ghcOptDynLinkMode = toFlag GhcDynamicOnly, - ghcOptObjSuffix = toFlag "dyn_o" - } - odir = fromFlag (ghcOptObjDir vanillaCcOpts) - createDirectoryIfMissingVerbose verbosity True odir - let runGhcProgIfNeeded ccOpts = do - needsRecomp <- checkNeedsRecompilation filename ccOpts - when needsRecomp $ runGhcProg ccOpts - runGhcProgIfNeeded vanillaCcOpts - unless forRepl $ - whenSharedLib forceSharedLib (runGhcProgIfNeeded sharedCcOpts) - unless forRepl $ whenProfLib (runGhcProgIfNeeded profCcOpts) - | filename <- cSources libBi] + unless (null others) $ do + let files = intercalate ", " others + let libraryName = case libName lib of + LMainLibName -> "the main library" + LSubLibName name -> "library " <> prettyShow name + warn verbosity $ unlines + [ "The following files listed in " <> libraryName <> "'s c-sources will not be used: " <> files <> "." + , "Header files should be in the 'include' or 'install-include' stanza." + , "See https://cabal.readthedocs.io/en/3.10/cabal-package.html#pkg-field-includes" + ] + forM_ cSrcs' $ \filename -> do + let baseCcOpts = Internal.componentCcGhcOptions verbosity implInfo + lbi libBi clbi relLibTargetDir filename + vanillaCcOpts = if isGhcDynamic + -- Dynamic GHC requires C sources to be built + -- with -fPIC for REPL to work. See #2207. + then baseCcOpts { ghcOptFPic = toFlag True } + else baseCcOpts + profCcOpts = vanillaCcOpts `mappend` mempty { + ghcOptProfilingMode = toFlag True, + ghcOptObjSuffix = toFlag "p_o" + } + sharedCcOpts = vanillaCcOpts `mappend` mempty { + ghcOptFPic = toFlag True, + ghcOptDynLinkMode = toFlag GhcDynamicOnly, + ghcOptObjSuffix = toFlag "dyn_o" + } + odir = fromFlag (ghcOptObjDir vanillaCcOpts) + createDirectoryIfMissingVerbose verbosity True odir + let runGhcProgIfNeeded ccOpts = do + needsRecomp <- checkNeedsRecompilation filename ccOpts + when needsRecomp $ runGhcProg ccOpts + runGhcProgIfNeeded vanillaCcOpts + unless forRepl $ + whenSharedLib forceSharedLib (runGhcProgIfNeeded sharedCcOpts) + unless forRepl $ whenProfLib (runGhcProgIfNeeded profCcOpts) -- build any JS sources unless (not has_code || not hasJsSupport || null (jsSources libBi)) $ do @@ -1527,32 +1537,40 @@ gbuild verbosity numJobs pkg_descr lbi bm clbi = do | filename <- cxxSrcs ] -- build any C sources - unless (null cSrcs) $ do - info verbosity "Building C Sources..." - sequence_ - [ do let baseCcOpts = Internal.componentCcGhcOptions verbosity implInfo + let (cSrcs', others) = partition (\filepath -> ".c"`isSuffixOf` filepath) cSrcs + unless (null cSrcs') $ do + info verbosity "Building C Sources..." + unless (null others) $ do + let files = intercalate ", " others + let currentComponentName = gbuildName bm + warn verbosity $ unlines + [ "The following files listed in " <> currentComponentName <> "'s c-sources will not be used: " <> files <> "." + , "Header files should be in the 'include' or 'install-include' stanza." + , "See https://cabal.readthedocs.io/en/3.10/cabal-package.html#pkg-field-includes" + ] + forM_ cSrcs' $ \filename -> do + let baseCcOpts = Internal.componentCcGhcOptions verbosity implInfo lbi bnfo clbi tmpDir filename - vanillaCcOpts = if isGhcDynamic - -- Dynamic GHC requires C sources to be built - -- with -fPIC for REPL to work. See #2207. - then baseCcOpts { ghcOptFPic = toFlag True } - else baseCcOpts - profCcOpts = vanillaCcOpts `mappend` mempty { - ghcOptProfilingMode = toFlag True - } - sharedCcOpts = vanillaCcOpts `mappend` mempty { - ghcOptFPic = toFlag True, - ghcOptDynLinkMode = toFlag GhcDynamicOnly - } - opts | needProfiling = profCcOpts - | needDynamic = sharedCcOpts - | otherwise = vanillaCcOpts - odir = fromFlag (ghcOptObjDir opts) - createDirectoryIfMissingVerbose verbosity True odir - needsRecomp <- checkNeedsRecompilation filename opts - when needsRecomp $ - runGhcProg opts - | filename <- cSrcs ] + let vanillaCcOpts = if isGhcDynamic + -- Dynamic GHC requires C sources to be built + -- with -fPIC for REPL to work. See #2207. + then baseCcOpts { ghcOptFPic = toFlag True } + else baseCcOpts + let profCcOpts = vanillaCcOpts `mappend` mempty { + ghcOptProfilingMode = toFlag True + } + let sharedCcOpts = vanillaCcOpts `mappend` mempty { + ghcOptFPic = toFlag True, + ghcOptDynLinkMode = toFlag GhcDynamicOnly + } + let opts | needProfiling = profCcOpts + | needDynamic = sharedCcOpts + | otherwise = vanillaCcOpts + let odir = fromFlag (ghcOptObjDir opts) + createDirectoryIfMissingVerbose verbosity True odir + needsRecomp <- checkNeedsRecompilation filename opts + when needsRecomp $ + runGhcProg opts -- TODO: problem here is we need the .c files built first, so we can load them -- with ghci, but .c files can depend on .h files generated by ghc by ffi diff --git a/cabal-testsuite/PackageTests/CSourcesSanitisation/Main.hs b/cabal-testsuite/PackageTests/CSourcesSanitisation/Main.hs new file mode 100644 index 00000000000..694e8564328 --- /dev/null +++ b/cabal-testsuite/PackageTests/CSourcesSanitisation/Main.hs @@ -0,0 +1,4 @@ +module Main where + +main :: IO () +main = putStrLn "main" diff --git a/cabal-testsuite/PackageTests/CSourcesSanitisation/build.out b/cabal-testsuite/PackageTests/CSourcesSanitisation/build.out new file mode 100644 index 00000000000..1e423a54cb9 --- /dev/null +++ b/cabal-testsuite/PackageTests/CSourcesSanitisation/build.out @@ -0,0 +1,25 @@ +# cabal build +Resolving dependencies... +Build profile: -w ghc- -O1 +In order, the following will be built: + - repro-0.1.0.0 (lib) (first run) + - repro-0.1.0.0 (exe:exec1) (first run) + - repro-0.1.0.0 (lib:lib2) (first run) +Configuring library for repro-0.1.0.0.. +Preprocessing library for repro-0.1.0.0.. +Building library for repro-0.1.0.0.. +Warning: The following files listed in the main library's c-sources will not be used: cbits/gwinsz.h. +Header files should be in the 'include' or 'install-include' stanza. +See https://cabal.readthedocs.io/en/3.10/cabal-package.html#pkg-field-includes +Configuring executable 'exec1' for repro-0.1.0.0.. +Preprocessing executable 'exec1' for repro-0.1.0.0.. +Building executable 'exec1' for repro-0.1.0.0.. +Warning: The following files listed in exec1's c-sources will not be used: cbits/gwinsz.h. +Header files should be in the 'include' or 'install-include' stanza. +See https://cabal.readthedocs.io/en/3.10/cabal-package.html#pkg-field-includes +Configuring library 'lib2' for repro-0.1.0.0.. +Preprocessing library 'lib2' for repro-0.1.0.0.. +Building library 'lib2' for repro-0.1.0.0.. +Warning: The following files listed in library lib2's c-sources will not be used: cbits/gwinsz.h. +Header files should be in the 'include' or 'install-include' stanza. +See https://cabal.readthedocs.io/en/3.10/cabal-package.html#pkg-field-includes diff --git a/cabal-testsuite/PackageTests/CSourcesSanitisation/build.test.hs b/cabal-testsuite/PackageTests/CSourcesSanitisation/build.test.hs new file mode 100644 index 00000000000..db6203e6546 --- /dev/null +++ b/cabal-testsuite/PackageTests/CSourcesSanitisation/build.test.hs @@ -0,0 +1,4 @@ +import Test.Cabal.Prelude + +main = cabalTest $ do + cabal "build" [] diff --git a/cabal-testsuite/PackageTests/CSourcesSanitisation/cabal.project b/cabal-testsuite/PackageTests/CSourcesSanitisation/cabal.project new file mode 100644 index 00000000000..e6fdbadb439 --- /dev/null +++ b/cabal-testsuite/PackageTests/CSourcesSanitisation/cabal.project @@ -0,0 +1 @@ +packages: . diff --git a/cabal-testsuite/PackageTests/CSourcesSanitisation/cbits/gwinsz.c b/cabal-testsuite/PackageTests/CSourcesSanitisation/cbits/gwinsz.c new file mode 100644 index 00000000000..e69de29bb2d diff --git a/cabal-testsuite/PackageTests/CSourcesSanitisation/cbits/gwinsz.h b/cabal-testsuite/PackageTests/CSourcesSanitisation/cbits/gwinsz.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/cabal-testsuite/PackageTests/CSourcesSanitisation/repro.cabal b/cabal-testsuite/PackageTests/CSourcesSanitisation/repro.cabal new file mode 100644 index 00000000000..659f1510a7e --- /dev/null +++ b/cabal-testsuite/PackageTests/CSourcesSanitisation/repro.cabal @@ -0,0 +1,23 @@ +cabal-version: 3.0 +name: repro +version: 0.1.0.0 +build-type: Simple + +library + default-language: Haskell2010 + c-sources: cbits/gwinsz.h + cbits/gwinsz.c + build-depends: base + +library lib2 + default-language: Haskell2010 + c-sources: cbits/gwinsz.h + cbits/gwinsz.c + build-depends: base + +executable exec1 + main-is: Main.hs + default-language: Haskell2010 + c-sources: cbits/gwinsz.h + cbits/gwinsz.c + build-depends: base