Refactor builder path manipulation
[hadrian.git] / src / Settings / Builders / GhcCabal.hs
index 582a56c..7a0669a 100644 (file)
@@ -1,51 +1,50 @@
 module Settings.Builders.GhcCabal (
-    cabalArgs, ghcCabalHsColourArgs, bootPackageDbArgs, customPackageArgs,
-    ccArgs, cppArgs, ccWarnings, argStagedSettingList
+    ghcCabalBuilderArgs, ghcCabalHsColourBuilderArgs, buildDll0
     ) where
 
-import Expression
-import Predicates
-import Settings
+import Context
+import Flavour
+import Rules.Actions
+import Settings.Builders.Common
 
-cabalArgs :: Args
-cabalArgs = builder GhcCabal ? do
-    path <- getPackagePath
-    dir  <- getTargetDirectory
+ghcCabalBuilderArgs :: Args
+ghcCabalBuilderArgs = builder GhcCabal ? do
+    verbosity <- lift $ getVerbosity
     mconcat [ arg "configure"
-            , arg path
-            , arg dir
-            , dllArgs
-            , withStaged Ghc
+            , arg =<< getPackagePath
+            , arg =<< getContextDirectory
+            , dll0Args
+            , withStaged $ Ghc CompileHs
             , withStaged GhcPkg
-            , stage0 ? bootPackageDbArgs
+            , bootPackageDatabaseArgs
             , libraryArgs
             , with HsColour
             , configureArgs
             , packageConstraints
-            , withStaged Gcc
+            , withStaged $ Cc CompileC
             , notStage0 ? with Ld
             , with Ar
             , with Alex
-            , with Happy ]
+            , with Happy
+            , verbosity < Chatty ? append [ "-v0", "--configure-option=--quiet"
+                , "--configure-option=--disable-option-checking"  ] ]
 
-ghcCabalHsColourArgs :: Args
-ghcCabalHsColourArgs = builder GhcCabalHsColour ? do
+ghcCabalHsColourBuilderArgs :: Args
+ghcCabalHsColourBuilderArgs = builder GhcCabalHsColour ? do
     path <- getPackagePath
-    dir  <- getTargetDirectory
-    mconcat [ arg "hscolour"
-            , arg path
-            , arg dir ]
+    dir  <- getContextDirectory
+    append [ "hscolour", path, dir ]
 
 -- TODO: Isn't vanilla always built? If yes, some conditions are redundant.
 -- TODO: Need compiler_stage1_CONFIGURE_OPTS += --disable-library-for-ghci?
 libraryArgs :: Args
 libraryArgs = do
-    ways   <- getWays
-    ghcInt <- lift $ ghcWithInterpreter
+    ways     <- getLibraryWays
+    withGhci <- lift ghcWithInterpreter
     append [ if vanilla `elem` ways
              then  "--enable-library-vanilla"
              else "--disable-library-vanilla"
-           , if vanilla `elem` ways && ghcInt && not dynamicGhcPrograms
+           , if vanilla `elem` ways && withGhci && not (dynamicGhcPrograms flavour)
              then  "--enable-library-for-ghci"
              else "--disable-library-for-ghci"
            , if profiling `elem` ways
@@ -58,10 +57,12 @@ libraryArgs = do
 -- TODO: LD_OPTS?
 configureArgs :: Args
 configureArgs = do
+    top <- getTopDirectory
     let conf key = appendSubD $ "--configure-option=" ++ key
-        cFlags   = mconcat [ ccArgs
+        cFlags   = mconcat [ cArgs
                            , remove ["-Werror"]
-                           , argStagedSettingList ConfCcArgs ]
+                           , argStagedSettingList ConfCcArgs
+                           , arg $ "-I" ++ top -/- generatedPath ]
         ldFlags  = ldArgs  <> (argStagedSettingList ConfGccLinkerArgs)
         cppFlags = cppArgs <> (argStagedSettingList ConfCppArgs)
     mconcat
@@ -69,153 +70,220 @@ configureArgs = do
         , conf "LDFLAGS"  ldFlags
         , conf "CPPFLAGS" cppFlags
         , appendSubD "--gcc-options" $ cFlags <> ldFlags
-        , conf "--with-iconv-includes"    $ argSettingList IconvIncludeDirs
-        , conf "--with-iconv-libraries"   $ argSettingList IconvLibDirs
-        , conf "--with-gmp-includes"      $ argSettingList GmpIncludeDirs
-        , conf "--with-gmp-libraries"     $ argSettingList GmpLibDirs
+        , conf "--with-iconv-includes"    $ argSetting IconvIncludeDir
+        , conf "--with-iconv-libraries"   $ argSetting IconvLibDir
+        , conf "--with-gmp-includes"      $ argSetting GmpIncludeDir
+        , conf "--with-gmp-libraries"     $ argSetting GmpLibDir
         , crossCompiling ? (conf "--host" $ argSetting TargetPlatformFull)
-        , conf "--with-cc" $ argStagedBuilderPath Gcc ]
-
-bootPackageDbArgs :: Args
-bootPackageDbArgs = do
-    path <- getSetting GhcSourcePath
-    arg $ "--package-db=" ++ path -/- "libraries/bootstrapping.conf"
-
--- This is a positional argument, hence:
--- * if it is empty, we need to emit one empty string argument;
--- * otherwise, we must collapse it into one space-separated string.
--- TODO: should be non-empty for compiler
-dllArgs :: Args
-dllArgs = arg ""
+        , conf "--with-cc" $ argStagedBuilderPath (Cc CompileC) ]
 
 packageConstraints :: Args
 packageConstraints = stage0 ? do
     constraints <- lift . readFileLines $ bootPackageConstraints
     append $ concat [ ["--constraint", c] | c <- constraints ]
 
--- TODO: should be in a different file
--- TODO: put all validating options together in one file
-ccArgs :: Args
-ccArgs = validating ? ccWarnings
-
--- TODO: should be in a different file
-ccWarnings :: Args
-ccWarnings = do
-    let gccGe46 = notM $ (flag GccIsClang ||^ flag GccLt46)
-    mconcat [ arg "-Werror"
-            , arg "-Wall"
-            , flag GccIsClang ? arg "-Wno-unknown-pragmas"
-            , gccGe46 ? notM windowsHost ? arg "-Werror=unused-but-set-variable"
-            , gccGe46 ? arg "-Wno-error=inline" ]
-
-ldArgs :: Args
-ldArgs = mempty
-
-ghcIncludeDirs :: [FilePath]
-ghcIncludeDirs = [ "includes", "includes/dist"
-                 , "includes/dist-derivedconstants/header"
-                 , "includes/dist-ghcconstants/header" ]
-
 cppArgs :: Args
-cppArgs = append $ map ("-I" ++) ghcIncludeDirs
-
--- TODO: Is this needed?
--- ifeq "$(GMP_PREFER_FRAMEWORK)" "YES"
--- libraries/integer-gmp_CONFIGURE_OPTS += --with-gmp-framework-preferred
--- endif
-
--- TODO: move this somewhere
-customPackageArgs :: Args
-customPackageArgs = do
-    nextStage <- fmap succ getStage
-    rtsWays   <- getRtsWays
-    mconcat
-        [ package integerGmp ?
-          mconcat [ windowsHost ? builder GhcCabal ?
-                    arg "--configure-option=--with-intree-gmp"
-                  , appendCcArgs ["-I" ++ pkgPath integerGmp -/- "gmp"] ]
-
-        , package base ?
-          builder GhcCabal ?
-          arg ("--flags=" ++ takeFileName (pkgPath integerLibrary))
-
-        , package ghcPrim ?
-          builder GhcCabal ? arg "--flag=include-ghc-prim"
-
-        , package compiler ?
-          builder GhcCabal ?
-          mconcat [ arg $ "--ghc-option=-DSTAGE=" ++ show nextStage
-                  , arg $ "--flags=stage" ++ show nextStage
-                  , arg "--disable-library-for-ghci"
-                  , anyTargetOs ["openbsd"] ? arg "--ld-options=-E"
-                  , flag GhcUnregisterised ? arg "--ghc-option=-DNO_REGS"
-                  , notM ghcWithSMP ? arg "--ghc-option=-DNOSMP"
-                  , notM ghcWithSMP ? arg "--ghc-option=-optc-DNOSMP"
-                  , (threaded `elem` rtsWays) ?
-                    notStage0 ? arg "--ghc-option=-optc-DTHREADED_RTS"
-                  , ghcWithNativeCodeGen ? arg "--flags=ncg"
-                  , ghcWithInterpreter ?
-                    notStage0 ? arg "--flags=ghci"
-                  , ghcWithInterpreter ?
-                    ghcEnableTablesNextToCode ?
-                    notM (flag GhcUnregisterised) ?
-                    notStage0 ? arg "--ghc-option=-DGHCI_TABLES_NEXT_TO_CODE"
-                  , ghcWithInterpreter ?
-                    ghciWithDebugger ?
-                    notStage0 ? arg "--ghc-option=-DDEBUGGER"
-                  , ghcProfiled ?
-                    notStage0 ? arg "--ghc-pkg-option=--force"
-                  ]
-        , package ghc ?
-          builder GhcCabal ?
-          mconcat [ arg $ "--flags=stage" ++ show nextStage
-                  , ghcWithInterpreter ?
-                    notStage0 ? arg "--flags=ghci"
-                  ]
-        ]
+cppArgs = arg $ "-I" ++ generatedPath
 
 withBuilderKey :: Builder -> String
 withBuilderKey b = case b of
     Ar       -> "--with-ar="
     Ld       -> "--with-ld="
-    Gcc _    -> "--with-gcc="
-    Ghc _    -> "--with-ghc="
+    Cc  _ _  -> "--with-gcc="
+    Ghc _ _  -> "--with-ghc="
     Alex     -> "--with-alex="
     Happy    -> "--with-happy="
     GhcPkg _ -> "--with-ghc-pkg="
     HsColour -> "--with-hscolour="
-    _        -> error "withBuilderKey: not supported builder"
+    _        -> error $ "withBuilderKey: not supported builder " ++ show b
 
--- Expression 'with Gcc' appends "--with-gcc=/path/to/gcc" and needs Gcc.
+-- Expression 'with Alex' appends "--with-alex=/path/to/alex" and needs Alex.
 with :: Builder -> Args
-with b = specified b ? do
+with b = isSpecified b ? do
+    top  <- getTopDirectory
     path <- getBuilderPath b
-    lift $ needBuilder laxDependencies b
-    append [withBuilderKey b ++ path]
+    lift $ needBuilder b
+    arg $ withBuilderKey b ++ unifyPath (top </> path)
 
 withStaged :: (Stage -> Builder) -> Args
-withStaged sb = (with . sb) =<< getStage
-
-argM :: Action String -> Args
-argM = (arg =<<) . lift
-
-argSetting :: Setting -> Args
-argSetting = argM . setting
+withStaged sb = with . sb =<< getStage
 
-argSettingList :: SettingList -> Args
-argSettingList = (append =<<) . lift . settingList
-
-argStagedSettingList :: (Stage -> SettingList) -> Args
-argStagedSettingList ss = (argSettingList . ss) =<< getStage
-
-argStagedBuilderPath :: (Stage -> Builder) -> Args
-argStagedBuilderPath sb = (argM . builderPath . sb) =<< getStage
-
--- Pass arguments to Gcc and corresponding lists of sub-arguments of GhcCabal
-appendCcArgs :: [String] -> Args
-appendCcArgs xs = do
-    mconcat [ stagedBuilder Gcc  ? append xs
-            , stagedBuilder GccM ? append xs
-            , builder GhcCabal   ? appendSub "--configure-option=CFLAGS" xs
-            , builder GhcCabal   ? appendSub "--gcc-options" xs ]
+buildDll0 :: Context -> Action Bool
+buildDll0 Context {..} = do
+    windows <- windowsHost
+    return $ windows && stage == Stage1 && package == compiler
 
+-- This is a positional argument, hence:
+-- * if it is empty, we need to emit one empty string argument;
+-- * otherwise, we must collapse it into one space-separated string.
+dll0Args :: Args
+dll0Args = do
+    context  <- getContext
+    dll0     <- lift $ buildDll0 context
+    withGhci <- lift ghcWithInterpreter
+    arg . unwords . concat $ [ modules     | dll0             ]
+                          ++ [ ghciModules | dll0 && withGhci ] -- see #9552
+  where
+    modules = [ "Annotations"
+              , "ApiAnnotation"
+              , "Avail"
+              , "Bag"
+              , "BasicTypes"
+              , "Binary"
+              , "BooleanFormula"
+              , "BreakArray"
+              , "BufWrite"
+              , "Class"
+              , "CmdLineParser"
+              , "CmmType"
+              , "CoAxiom"
+              , "ConLike"
+              , "Coercion"
+              , "Config"
+              , "Constants"
+              , "CoreArity"
+              , "CoreFVs"
+              , "CoreSubst"
+              , "CoreSyn"
+              , "CoreTidy"
+              , "CoreUnfold"
+              , "CoreUtils"
+              , "CoreSeq"
+              , "CoreStats"
+              , "CostCentre"
+              , "Ctype"
+              , "DataCon"
+              , "Demand"
+              , "Digraph"
+              , "DriverPhases"
+              , "DynFlags"
+              , "Encoding"
+              , "ErrUtils"
+              , "Exception"
+              , "ExtsCompat46"
+              , "FamInstEnv"
+              , "FastFunctions"
+              , "FastMutInt"
+              , "FastString"
+              , "FastTypes"
+              , "Fingerprint"
+              , "FiniteMap"
+              , "ForeignCall"
+              , "Hooks"
+              , "HsBinds"
+              , "HsDecls"
+              , "HsDoc"
+              , "HsExpr"
+              , "HsImpExp"
+              , "HsLit"
+              , "PlaceHolder"
+              , "HsPat"
+              , "HsSyn"
+              , "HsTypes"
+              , "HsUtils"
+              , "HscTypes"
+              , "IOEnv"
+              , "Id"
+              , "IdInfo"
+              , "IfaceSyn"
+              , "IfaceType"
+              , "InstEnv"
+              , "Kind"
+              , "Lexeme"
+              , "Lexer"
+              , "ListSetOps"
+              , "Literal"
+              , "Maybes"
+              , "MkCore"
+              , "MkId"
+              , "Module"
+              , "MonadUtils"
+              , "Name"
+              , "NameEnv"
+              , "NameSet"
+              , "OccName"
+              , "OccurAnal"
+              , "OptCoercion"
+              , "OrdList"
+              , "Outputable"
+              , "PackageConfig"
+              , "Packages"
+              , "Pair"
+              , "Panic"
+              , "PatSyn"
+              , "PipelineMonad"
+              , "Platform"
+              , "PlatformConstants"
+              , "PprCore"
+              , "PrelNames"
+              , "PrelRules"
+              , "Pretty"
+              , "PrimOp"
+              , "RdrName"
+              , "Rules"
+              , "Serialized"
+              , "SrcLoc"
+              , "StaticFlags"
+              , "StringBuffer"
+              , "TcEvidence"
+              , "TcRnTypes"
+              , "TcType"
+              , "TrieMap"
+              , "TyCon"
+              , "Type"
+              , "TypeRep"
+              , "TysPrim"
+              , "TysWiredIn"
+              , "Unify"
+              , "UniqFM"
+              , "UniqSet"
+              , "UniqSupply"
+              , "Unique"
+              , "Util"
+              , "Var"
+              , "VarEnv"
+              , "VarSet" ]
+    ghciModules = [ "Bitmap"
+                  , "BlockId"
+                  , "ByteCodeAsm"
+                  , "ByteCodeInstr"
+                  , "ByteCodeItbls"
+                  , "CLabel"
+                  , "Cmm"
+                  , "CmmCallConv"
+                  , "CmmExpr"
+                  , "CmmInfo"
+                  , "CmmMachOp"
+                  , "CmmNode"
+                  , "CmmSwitch"
+                  , "CmmUtils"
+                  , "CodeGen.Platform"
+                  , "CodeGen.Platform.ARM"
+                  , "CodeGen.Platform.ARM64"
+                  , "CodeGen.Platform.NoRegs"
+                  , "CodeGen.Platform.PPC"
+                  , "CodeGen.Platform.PPC_Darwin"
+                  , "CodeGen.Platform.SPARC"
+                  , "CodeGen.Platform.X86"
+                  , "CodeGen.Platform.X86_64"
+                  , "FastBool"
+                  , "Hoopl"
+                  , "Hoopl.Dataflow"
+                  , "InteractiveEvalTypes"
+                  , "MkGraph"
+                  , "PprCmm"
+                  , "PprCmmDecl"
+                  , "PprCmmExpr"
+                  , "Reg"
+                  , "RegClass"
+                  , "SMRep"
+                  , "StgCmmArgRep"
+                  , "StgCmmClosure"
+                  , "StgCmmEnv"
+                  , "StgCmmLayout"
+                  , "StgCmmMonad"
+                  , "StgCmmProf"
+                  , "StgCmmTicky"
+                  , "StgCmmUtils"
+                  , "StgSyn"
+                  , "Stream" ]