Skip to content

Commit 47eb77b

Browse files
Filter out files that are not ending in .c from c-sources (#9200)
Co-authored-by: Andrea Bedini <[email protected]> fixes #9190
1 parent 9faa4db commit 47eb77b

File tree

9 files changed

+129
-54
lines changed

9 files changed

+129
-54
lines changed

Cabal-tests/tests/UnitTests/Distribution/Utils/Structured.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tests = testGroup "Distribution.Utils.Structured"
2929
, testCase "GenericPackageDescription" $
3030
md5Check (Proxy :: Proxy GenericPackageDescription) 0x8d8f340f10a58b8d8a87bf42213dac89
3131
, testCase "LocalBuildInfo" $
32-
md5Check (Proxy :: Proxy LocalBuildInfo) 0xbb22c3258d3092f31e992bc093d09170
32+
md5Check (Proxy :: Proxy LocalBuildInfo) 0x618ab257e99d0b21c617e1f8c39a5a4b
3333
#endif
3434
]
3535

Cabal/src/Distribution/Simple/GHC.hs

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -705,35 +705,45 @@ buildOrReplLib mReplFlags verbosity numJobs pkg_descr lbi lib clbi = do
705705
| filename <- cxxSources libBi]
706706

707707
-- build any C sources
708-
unless (not has_code || null (cSources libBi)) $ do
708+
let (cSrcs', others) = partition (\filepath -> ".c"`isSuffixOf` filepath) (cSources libBi)
709+
unless (not has_code || null cSrcs') $ do
709710
info verbosity "Building C Sources..."
710-
sequence_
711-
[ do let baseCcOpts = Internal.componentCcGhcOptions verbosity implInfo
712-
lbi libBi clbi relLibTargetDir filename
713-
vanillaCcOpts = if isGhcDynamic
714-
-- Dynamic GHC requires C sources to be built
715-
-- with -fPIC for REPL to work. See #2207.
716-
then baseCcOpts { ghcOptFPic = toFlag True }
717-
else baseCcOpts
718-
profCcOpts = vanillaCcOpts `mappend` mempty {
719-
ghcOptProfilingMode = toFlag True,
720-
ghcOptObjSuffix = toFlag "p_o"
721-
}
722-
sharedCcOpts = vanillaCcOpts `mappend` mempty {
723-
ghcOptFPic = toFlag True,
724-
ghcOptDynLinkMode = toFlag GhcDynamicOnly,
725-
ghcOptObjSuffix = toFlag "dyn_o"
726-
}
727-
odir = fromFlag (ghcOptObjDir vanillaCcOpts)
728-
createDirectoryIfMissingVerbose verbosity True odir
729-
let runGhcProgIfNeeded ccOpts = do
730-
needsRecomp <- checkNeedsRecompilation filename ccOpts
731-
when needsRecomp $ runGhcProg ccOpts
732-
runGhcProgIfNeeded vanillaCcOpts
733-
unless forRepl $
734-
whenSharedLib forceSharedLib (runGhcProgIfNeeded sharedCcOpts)
735-
unless forRepl $ whenProfLib (runGhcProgIfNeeded profCcOpts)
736-
| filename <- cSources libBi]
711+
unless (null others) $ do
712+
let files = intercalate ", " others
713+
let libraryName = case libName lib of
714+
LMainLibName -> "the main library"
715+
LSubLibName name -> "library " <> prettyShow name
716+
warn verbosity $ unlines
717+
[ "The following files listed in " <> libraryName <> "'s c-sources will not be used: " <> files <> "."
718+
, "Header files should be in the 'include' or 'install-include' stanza."
719+
, "See https://cabal.readthedocs.io/en/3.10/cabal-package.html#pkg-field-includes"
720+
]
721+
forM_ cSrcs' $ \filename -> do
722+
let baseCcOpts = Internal.componentCcGhcOptions verbosity implInfo
723+
lbi libBi clbi relLibTargetDir filename
724+
vanillaCcOpts = if isGhcDynamic
725+
-- Dynamic GHC requires C sources to be built
726+
-- with -fPIC for REPL to work. See #2207.
727+
then baseCcOpts { ghcOptFPic = toFlag True }
728+
else baseCcOpts
729+
profCcOpts = vanillaCcOpts `mappend` mempty {
730+
ghcOptProfilingMode = toFlag True,
731+
ghcOptObjSuffix = toFlag "p_o"
732+
}
733+
sharedCcOpts = vanillaCcOpts `mappend` mempty {
734+
ghcOptFPic = toFlag True,
735+
ghcOptDynLinkMode = toFlag GhcDynamicOnly,
736+
ghcOptObjSuffix = toFlag "dyn_o"
737+
}
738+
odir = fromFlag (ghcOptObjDir vanillaCcOpts)
739+
createDirectoryIfMissingVerbose verbosity True odir
740+
let runGhcProgIfNeeded ccOpts = do
741+
needsRecomp <- checkNeedsRecompilation filename ccOpts
742+
when needsRecomp $ runGhcProg ccOpts
743+
runGhcProgIfNeeded vanillaCcOpts
744+
unless forRepl $
745+
whenSharedLib forceSharedLib (runGhcProgIfNeeded sharedCcOpts)
746+
unless forRepl $ whenProfLib (runGhcProgIfNeeded profCcOpts)
737747

738748
-- build any JS sources
739749
unless (not has_code || not hasJsSupport || null (jsSources libBi)) $ do
@@ -1527,32 +1537,40 @@ gbuild verbosity numJobs pkg_descr lbi bm clbi = do
15271537
| filename <- cxxSrcs ]
15281538

15291539
-- build any C sources
1530-
unless (null cSrcs) $ do
1531-
info verbosity "Building C Sources..."
1532-
sequence_
1533-
[ do let baseCcOpts = Internal.componentCcGhcOptions verbosity implInfo
1540+
let (cSrcs', others) = partition (\filepath -> ".c"`isSuffixOf` filepath) cSrcs
1541+
unless (null cSrcs') $ do
1542+
info verbosity "Building C Sources..."
1543+
unless (null others) $ do
1544+
let files = intercalate ", " others
1545+
let currentComponentName = gbuildName bm
1546+
warn verbosity $ unlines
1547+
[ "The following files listed in " <> currentComponentName <> "'s c-sources will not be used: " <> files <> "."
1548+
, "Header files should be in the 'include' or 'install-include' stanza."
1549+
, "See https://cabal.readthedocs.io/en/3.10/cabal-package.html#pkg-field-includes"
1550+
]
1551+
forM_ cSrcs' $ \filename -> do
1552+
let baseCcOpts = Internal.componentCcGhcOptions verbosity implInfo
15341553
lbi bnfo clbi tmpDir filename
1535-
vanillaCcOpts = if isGhcDynamic
1536-
-- Dynamic GHC requires C sources to be built
1537-
-- with -fPIC for REPL to work. See #2207.
1538-
then baseCcOpts { ghcOptFPic = toFlag True }
1539-
else baseCcOpts
1540-
profCcOpts = vanillaCcOpts `mappend` mempty {
1541-
ghcOptProfilingMode = toFlag True
1542-
}
1543-
sharedCcOpts = vanillaCcOpts `mappend` mempty {
1544-
ghcOptFPic = toFlag True,
1545-
ghcOptDynLinkMode = toFlag GhcDynamicOnly
1546-
}
1547-
opts | needProfiling = profCcOpts
1548-
| needDynamic = sharedCcOpts
1549-
| otherwise = vanillaCcOpts
1550-
odir = fromFlag (ghcOptObjDir opts)
1551-
createDirectoryIfMissingVerbose verbosity True odir
1552-
needsRecomp <- checkNeedsRecompilation filename opts
1553-
when needsRecomp $
1554-
runGhcProg opts
1555-
| filename <- cSrcs ]
1554+
let vanillaCcOpts = if isGhcDynamic
1555+
-- Dynamic GHC requires C sources to be built
1556+
-- with -fPIC for REPL to work. See #2207.
1557+
then baseCcOpts { ghcOptFPic = toFlag True }
1558+
else baseCcOpts
1559+
let profCcOpts = vanillaCcOpts `mappend` mempty {
1560+
ghcOptProfilingMode = toFlag True
1561+
}
1562+
let sharedCcOpts = vanillaCcOpts `mappend` mempty {
1563+
ghcOptFPic = toFlag True,
1564+
ghcOptDynLinkMode = toFlag GhcDynamicOnly
1565+
}
1566+
let opts | needProfiling = profCcOpts
1567+
| needDynamic = sharedCcOpts
1568+
| otherwise = vanillaCcOpts
1569+
let odir = fromFlag (ghcOptObjDir opts)
1570+
createDirectoryIfMissingVerbose verbosity True odir
1571+
needsRecomp <- checkNeedsRecompilation filename opts
1572+
when needsRecomp $
1573+
runGhcProg opts
15561574

15571575
-- TODO: problem here is we need the .c files built first, so we can load them
15581576
-- with ghci, but .c files can depend on .h files generated by ghc by ffi
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Main where
2+
3+
main :: IO ()
4+
main = putStrLn "main"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# cabal build
2+
Resolving dependencies...
3+
Build profile: -w ghc-<GHCVER> -O1
4+
In order, the following will be built:
5+
- repro-0.1.0.0 (lib) (first run)
6+
- repro-0.1.0.0 (exe:exec1) (first run)
7+
- repro-0.1.0.0 (lib:lib2) (first run)
8+
Configuring library for repro-0.1.0.0..
9+
Preprocessing library for repro-0.1.0.0..
10+
Building library for repro-0.1.0.0..
11+
Warning: The following files listed in the main library's c-sources will not be used: cbits/gwinsz.h.
12+
Header files should be in the 'include' or 'install-include' stanza.
13+
See https://cabal.readthedocs.io/en/3.10/cabal-package.html#pkg-field-includes
14+
Configuring executable 'exec1' for repro-0.1.0.0..
15+
Preprocessing executable 'exec1' for repro-0.1.0.0..
16+
Building executable 'exec1' for repro-0.1.0.0..
17+
Warning: The following files listed in exec1's c-sources will not be used: cbits/gwinsz.h.
18+
Header files should be in the 'include' or 'install-include' stanza.
19+
See https://cabal.readthedocs.io/en/3.10/cabal-package.html#pkg-field-includes
20+
Configuring library 'lib2' for repro-0.1.0.0..
21+
Preprocessing library 'lib2' for repro-0.1.0.0..
22+
Building library 'lib2' for repro-0.1.0.0..
23+
Warning: The following files listed in library lib2's c-sources will not be used: cbits/gwinsz.h.
24+
Header files should be in the 'include' or 'install-include' stanza.
25+
See https://cabal.readthedocs.io/en/3.10/cabal-package.html#pkg-field-includes
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Test.Cabal.Prelude
2+
3+
main = cabalTest $ do
4+
cabal "build" []
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .

cabal-testsuite/PackageTests/CSourcesSanitisation/cbits/gwinsz.c

Whitespace-only changes.

cabal-testsuite/PackageTests/CSourcesSanitisation/cbits/gwinsz.h

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cabal-version: 3.0
2+
name: repro
3+
version: 0.1.0.0
4+
build-type: Simple
5+
6+
library
7+
default-language: Haskell2010
8+
c-sources: cbits/gwinsz.h
9+
cbits/gwinsz.c
10+
build-depends: base
11+
12+
library lib2
13+
default-language: Haskell2010
14+
c-sources: cbits/gwinsz.h
15+
cbits/gwinsz.c
16+
build-depends: base
17+
18+
executable exec1
19+
main-is: Main.hs
20+
default-language: Haskell2010
21+
c-sources: cbits/gwinsz.h
22+
cbits/gwinsz.c
23+
build-depends: base

0 commit comments

Comments
 (0)