Commit 9fbf3c8c authored by Andrey Mokhov's avatar Andrey Mokhov
Browse files

Refactor duplicated code into pathArgs, outputArgs and includeArgs functions.

parent 89c8f794
......@@ -8,6 +8,7 @@ module Package.Base (
defaultSettings, libraryPackage,
commonCcArgs, commonLdArgs, commonCppArgs, commonCcWarninigArgs,
bootPkgConstraints,
pathArgs, outputArgs,
packageArgs, includeArgs, srcArgs
) where
......@@ -77,6 +78,15 @@ bootPkgConstraints = mempty
-- $(foreach p,$(basename $(notdir $(wildcard libraries/$d/*.cabal))),\
-- --constraint "$p == $(shell grep -i "^Version:" libraries/$d/$p.cabal | sed "s/[^0-9.]//g")"))
pathArgs :: ShowArgs a => String -> FilePath -> a -> Args
pathArgs prefix path as = map includePath <$> arg as
where
includePath dir | isRelative dir = prefix ++ normaliseEx (path </> dir)
| isAbsolute dir = prefix </> normaliseEx dir
outputArgs :: [String] -> FilePath -> Args
outputArgs keys dir = arg $ concatMap (\k -> [k, normaliseEx dir]) keys
packageArgs :: Stage -> FilePath -> Args
packageArgs stage pkgData = do
usePackageKey <- SupportsPackageKey || stage /= Stage0
......@@ -89,11 +99,17 @@ packageArgs stage pkgData = do
keyArgs False = prefixArgs "-package-name" (PackageKey pkgData) <>
prefixArgs "-package" (Deps pkgData)
includeArgs :: ShowArgs a => String -> FilePath -> a -> Args
includeArgs prefix path as = map includePath <$> arg as
where
includePath dir | isRelative dir = prefix ++ path </> dir
| isAbsolute dir = prefix </> dir
includeArgs :: FilePath -> FilePath -> Args
includeArgs path dist =
let buildDir = path </> dist
pkgData = buildDir </> "package-data.mk"
in arg "-i"
<> pathArgs "-i" path (SrcDirs pkgData)
<> pathArgs "-i" buildDir ["build", "build/autogen"]
<> pathArgs "-I" buildDir ["build", "build/autogen"]
<> pathArgs "-I" path (IncludeDirs pkgData)
<> arg "-optP-include" -- TODO: Shall we also add -cpp?
<> pathArgs "-optP" buildDir "build/autogen/cabal_macros.h"
srcArgs :: FilePath -> FilePath -> Args
srcArgs path pkgData = do
......
......@@ -21,23 +21,15 @@ buildPackageCompile (Package name path _) (stage, dist, settings) =
need [depFile]
depContents <- parseMakefile <$> (liftIO $ readFile depFile)
let deps = concat $ snd $ unzip $ filter ((== out) . fst) depContents
srcs = filter ("//*hs" ?==) deps
srcs = filter ("//*hs" ?==) deps -- TODO: handle *.c sources
need deps
run (Ghc stage) $ suffixArgs way
<> wayHcArgs way
<> arg SrcHcOpts
<> packageArgs stage pkgData
<> arg "-i"
<> includeArgs "-i" path (SrcDirs pkgData)
<> includeArgs "-i" buildDir ["build", "build/autogen"]
<> includeArgs "-I" buildDir ["build", "build/autogen"]
<> includeArgs "-I" path (IncludeDirs pkgData)
<> arg "-optP-include" -- TODO: Shall we also add -cpp?
<> arg ("-optP" ++ buildDir </> "build/autogen/cabal_macros.h")
<> includeArgs path dist
<> arg ["-Wall", "-XHaskell2010", "-O2"] -- TODO: now we have both -O and -O2
<> arg ["-odir" , buildDir </> "build"]
<> arg ["-hidir" , buildDir </> "build"]
<> arg ["-stubdir" , buildDir </> "build"]
<> outputArgs ["-odir", "-hidir", "-stubdir"] (buildDir </> "build")
<> arg "-split-objs"
<> arg ("-c":srcs)
<> arg ["-o", out]
......@@ -12,16 +12,9 @@ buildPackageDependencies (Package name path _) (stage, dist, settings) =
need ["shake/src/Package/Dependencies.hs"] -- Track changes in this file
run (Ghc stage) $ arg "-M"
<> packageArgs stage pkgData
<> arg "-i"
<> includeArgs "-i" path (SrcDirs pkgData)
<> includeArgs "-i" buildDir ["build", "build/autogen"]
<> includeArgs "-I" buildDir ["build", "build/autogen"]
<> includeArgs "-I" path (IncludeDirs pkgData)
<> arg "-optP-include" -- TODO: Shall we also add -cpp?
<> arg ("-optP" ++ buildDir </> "build/autogen/cabal_macros.h")
<> arg ["-odir" , buildDir </> "build"]
<> arg ["-stubdir" , buildDir </> "build"]
<> arg ["-dep-makefile", out ]
<> includeArgs path dist
<> outputArgs ["-odir", "-stubdir"] (buildDir </> "build")
<> arg ["-dep-makefile", out]
<> prefixArgs "-dep-suffix" (map suffix <$> ways settings)
<> srcArgs path pkgData
-- <> arg SrcHcOpts -- TODO: Check that skipping all _HC_OPTS is safe.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment