{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RecordWildCards   #-}

-- | Build configuration

module Stack.Config.Build
 ( benchmarkOptsFromMonoid
 , buildOptsFromMonoid
 , haddockOptsFromMonoid
 , testOptsFromMonoid
 ) where

import           Distribution.Verbosity ( normal )
import           Stack.Prelude
import           Stack.Types.BuildOpts
                   ( BenchmarkOpts (..), BenchmarkOptsMonoid (..)
                   , BuildOpts (..), BuildOptsMonoid (..), CabalVerbosity (..)
                   , HaddockOpts (..), HaddockOptsMonoid (..)
                   , ProgressBarFormat (..), TestOpts (..), TestOptsMonoid (..)
                   , defaultBenchmarkOpts, defaultHaddockOpts, defaultTestOpts
                   )

-- | Interprets BuildOptsMonoid options.

buildOptsFromMonoid :: BuildOptsMonoid -> BuildOpts
buildOptsFromMonoid :: BuildOptsMonoid -> BuildOpts
buildOptsFromMonoid BuildOptsMonoid{[Text]
Any
First Bool
First Text
First ProgressBarFormat
First CabalVerbosity
FirstFalse
FirstTrue
BenchmarkOptsMonoid
HaddockOptsMonoid
TestOptsMonoid
buildMonoidTrace :: Any
buildMonoidProfile :: Any
buildMonoidNoStrip :: Any
buildMonoidLibProfile :: FirstFalse
buildMonoidExeProfile :: FirstFalse
buildMonoidLibStrip :: FirstTrue
buildMonoidExeStrip :: FirstTrue
buildMonoidHaddock :: FirstFalse
buildMonoidHaddockOpts :: HaddockOptsMonoid
buildMonoidOpenHaddocks :: FirstFalse
buildMonoidHaddockDeps :: First Bool
buildMonoidHaddockInternal :: FirstFalse
buildMonoidHaddockHyperlinkSource :: FirstTrue
buildMonoidHaddockForHackage :: FirstFalse
buildMonoidInstallExes :: FirstFalse
buildMonoidInstallCompilerTool :: FirstFalse
buildMonoidPreFetch :: FirstFalse
buildMonoidKeepGoing :: First Bool
buildMonoidKeepTmpFiles :: FirstFalse
buildMonoidForceDirty :: FirstFalse
buildMonoidTests :: FirstFalse
buildMonoidTestOpts :: TestOptsMonoid
buildMonoidBenchmarks :: FirstFalse
buildMonoidBenchmarkOpts :: BenchmarkOptsMonoid
buildMonoidReconfigure :: FirstFalse
buildMonoidCabalVerbose :: First CabalVerbosity
buildMonoidSplitObjs :: FirstFalse
buildMonoidSkipComponents :: [Text]
buildMonoidInterleavedOutput :: FirstTrue
buildMonoidProgressBar :: First ProgressBarFormat
buildMonoidDdumpDir :: First Text
buildMonoidTrace :: BuildOptsMonoid -> Any
buildMonoidProfile :: BuildOptsMonoid -> Any
buildMonoidNoStrip :: BuildOptsMonoid -> Any
buildMonoidLibProfile :: BuildOptsMonoid -> FirstFalse
buildMonoidExeProfile :: BuildOptsMonoid -> FirstFalse
buildMonoidLibStrip :: BuildOptsMonoid -> FirstTrue
buildMonoidExeStrip :: BuildOptsMonoid -> FirstTrue
buildMonoidHaddock :: BuildOptsMonoid -> FirstFalse
buildMonoidHaddockOpts :: BuildOptsMonoid -> HaddockOptsMonoid
buildMonoidOpenHaddocks :: BuildOptsMonoid -> FirstFalse
buildMonoidHaddockDeps :: BuildOptsMonoid -> First Bool
buildMonoidHaddockInternal :: BuildOptsMonoid -> FirstFalse
buildMonoidHaddockHyperlinkSource :: BuildOptsMonoid -> FirstTrue
buildMonoidHaddockForHackage :: BuildOptsMonoid -> FirstFalse
buildMonoidInstallExes :: BuildOptsMonoid -> FirstFalse
buildMonoidInstallCompilerTool :: BuildOptsMonoid -> FirstFalse
buildMonoidPreFetch :: BuildOptsMonoid -> FirstFalse
buildMonoidKeepGoing :: BuildOptsMonoid -> First Bool
buildMonoidKeepTmpFiles :: BuildOptsMonoid -> FirstFalse
buildMonoidForceDirty :: BuildOptsMonoid -> FirstFalse
buildMonoidTests :: BuildOptsMonoid -> FirstFalse
buildMonoidTestOpts :: BuildOptsMonoid -> TestOptsMonoid
buildMonoidBenchmarks :: BuildOptsMonoid -> FirstFalse
buildMonoidBenchmarkOpts :: BuildOptsMonoid -> BenchmarkOptsMonoid
buildMonoidReconfigure :: BuildOptsMonoid -> FirstFalse
buildMonoidCabalVerbose :: BuildOptsMonoid -> First CabalVerbosity
buildMonoidSplitObjs :: BuildOptsMonoid -> FirstFalse
buildMonoidSkipComponents :: BuildOptsMonoid -> [Text]
buildMonoidInterleavedOutput :: BuildOptsMonoid -> FirstTrue
buildMonoidProgressBar :: BuildOptsMonoid -> First ProgressBarFormat
buildMonoidDdumpDir :: BuildOptsMonoid -> First Text
..} = BuildOpts
  { boptsLibProfile :: Bool
boptsLibProfile = FirstFalse -> Bool
fromFirstFalse
      (FirstFalse
buildMonoidLibProfile FirstFalse -> FirstFalse -> FirstFalse
forall a. Semigroup a => a -> a -> a
<>
       Maybe Bool -> FirstFalse
FirstFalse (if Bool
tracing Bool -> Bool -> Bool
|| Bool
profiling then Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True else Maybe Bool
forall a. Maybe a
Nothing))
  , boptsExeProfile :: Bool
boptsExeProfile = FirstFalse -> Bool
fromFirstFalse
      (FirstFalse
buildMonoidExeProfile FirstFalse -> FirstFalse -> FirstFalse
forall a. Semigroup a => a -> a -> a
<>
       Maybe Bool -> FirstFalse
FirstFalse (if Bool
tracing Bool -> Bool -> Bool
|| Bool
profiling then Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True else Maybe Bool
forall a. Maybe a
Nothing))
  , boptsLibStrip :: Bool
boptsLibStrip = FirstTrue -> Bool
fromFirstTrue
      (FirstTrue
buildMonoidLibStrip FirstTrue -> FirstTrue -> FirstTrue
forall a. Semigroup a => a -> a -> a
<>
       Maybe Bool -> FirstTrue
FirstTrue (if Bool
noStripping then Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False else Maybe Bool
forall a. Maybe a
Nothing))
  , boptsExeStrip :: Bool
boptsExeStrip = FirstTrue -> Bool
fromFirstTrue
      (FirstTrue
buildMonoidExeStrip FirstTrue -> FirstTrue -> FirstTrue
forall a. Semigroup a => a -> a -> a
<>
       Maybe Bool -> FirstTrue
FirstTrue (if Bool
noStripping then Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False else Maybe Bool
forall a. Maybe a
Nothing))
  , boptsHaddock :: Bool
boptsHaddock = FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidHaddock
  , boptsHaddockOpts :: HaddockOpts
boptsHaddockOpts = HaddockOptsMonoid -> HaddockOpts
haddockOptsFromMonoid HaddockOptsMonoid
buildMonoidHaddockOpts
  , boptsOpenHaddocks :: Bool
boptsOpenHaddocks =
      Bool -> Bool
not Bool
isHaddockFromHackage Bool -> Bool -> Bool
&& FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidOpenHaddocks
  , boptsHaddockDeps :: Maybe Bool
boptsHaddockDeps = if Bool
isHaddockFromHackage
      then Maybe Bool
forall a. Maybe a
Nothing
      else First Bool -> Maybe Bool
forall a. First a -> Maybe a
getFirst First Bool
buildMonoidHaddockDeps
  , boptsHaddockInternal :: Bool
boptsHaddockInternal =
      Bool -> Bool
not Bool
isHaddockFromHackage Bool -> Bool -> Bool
&& FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidHaddockInternal
  , boptsHaddockHyperlinkSource :: Bool
boptsHaddockHyperlinkSource =
      Bool
isHaddockFromHackage Bool -> Bool -> Bool
|| FirstTrue -> Bool
fromFirstTrue FirstTrue
buildMonoidHaddockHyperlinkSource
  , boptsHaddockForHackage :: Bool
boptsHaddockForHackage = Bool
isHaddockFromHackage
  , boptsInstallExes :: Bool
boptsInstallExes = FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidInstallExes
  , boptsInstallCompilerTool :: Bool
boptsInstallCompilerTool = FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidInstallCompilerTool
  , boptsPreFetch :: Bool
boptsPreFetch = FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidPreFetch
  , boptsKeepGoing :: Maybe Bool
boptsKeepGoing = First Bool -> Maybe Bool
forall a. First a -> Maybe a
getFirst First Bool
buildMonoidKeepGoing
  , boptsKeepTmpFiles :: Bool
boptsKeepTmpFiles = FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidKeepTmpFiles
  , boptsForceDirty :: Bool
boptsForceDirty =
      Bool
isHaddockFromHackage Bool -> Bool -> Bool
|| FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidForceDirty
  , boptsTests :: Bool
boptsTests = FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidTests
  , boptsTestOpts :: TestOpts
boptsTestOpts =
      TestOptsMonoid -> Maybe [String] -> TestOpts
testOptsFromMonoid TestOptsMonoid
buildMonoidTestOpts Maybe [String]
additionalArgs
  , boptsBenchmarks :: Bool
boptsBenchmarks = FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidBenchmarks
  , boptsBenchmarkOpts :: BenchmarkOpts
boptsBenchmarkOpts =
      BenchmarkOptsMonoid -> Maybe [String] -> BenchmarkOpts
benchmarkOptsFromMonoid BenchmarkOptsMonoid
buildMonoidBenchmarkOpts Maybe [String]
additionalArgs
  , boptsReconfigure :: Bool
boptsReconfigure = FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidReconfigure
  , boptsCabalVerbose :: CabalVerbosity
boptsCabalVerbose =
      CabalVerbosity -> First CabalVerbosity -> CabalVerbosity
forall a. a -> First a -> a
fromFirst (Verbosity -> CabalVerbosity
CabalVerbosity Verbosity
normal) First CabalVerbosity
buildMonoidCabalVerbose
  , boptsSplitObjs :: Bool
boptsSplitObjs = FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidSplitObjs
  , boptsSkipComponents :: [Text]
boptsSkipComponents = [Text]
buildMonoidSkipComponents
  , boptsInterleavedOutput :: Bool
boptsInterleavedOutput = FirstTrue -> Bool
fromFirstTrue FirstTrue
buildMonoidInterleavedOutput
  , boptsProgressBar :: ProgressBarFormat
boptsProgressBar = ProgressBarFormat -> First ProgressBarFormat -> ProgressBarFormat
forall a. a -> First a -> a
fromFirst ProgressBarFormat
CappedBar First ProgressBarFormat
buildMonoidProgressBar
  , boptsDdumpDir :: Maybe Text
boptsDdumpDir = First Text -> Maybe Text
forall a. First a -> Maybe a
getFirst First Text
buildMonoidDdumpDir
  }
 where
  isHaddockFromHackage :: Bool
isHaddockFromHackage = FirstFalse -> Bool
fromFirstFalse FirstFalse
buildMonoidHaddockForHackage
  -- These options are not directly used in bopts, instead they

  -- transform other options.

  tracing :: Bool
tracing = Any -> Bool
getAny Any
buildMonoidTrace
  profiling :: Bool
profiling = Any -> Bool
getAny Any
buildMonoidProfile
  noStripping :: Bool
noStripping = Any -> Bool
getAny Any
buildMonoidNoStrip
  -- Additional args for tracing / profiling

  additionalArgs :: Maybe [String]
additionalArgs =
    if Bool
tracing Bool -> Bool -> Bool
|| Bool
profiling
      then [String] -> Maybe [String]
forall a. a -> Maybe a
Just ([String] -> Maybe [String]) -> [String] -> Maybe [String]
forall a b. (a -> b) -> a -> b
$ String
"+RTS" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [Maybe String] -> [String]
forall a. [Maybe a] -> [a]
catMaybes [Maybe String
trac, Maybe String
prof, String -> Maybe String
forall a. a -> Maybe a
Just String
"-RTS"]
      else Maybe [String]
forall a. Maybe a
Nothing
  trac :: Maybe String
trac =
    if Bool
tracing
      then String -> Maybe String
forall a. a -> Maybe a
Just String
"-xc"
      else Maybe String
forall a. Maybe a
Nothing
  prof :: Maybe String
prof =
    if Bool
profiling
      then String -> Maybe String
forall a. a -> Maybe a
Just String
"-p"
      else Maybe String
forall a. Maybe a
Nothing

haddockOptsFromMonoid :: HaddockOptsMonoid -> HaddockOpts
haddockOptsFromMonoid :: HaddockOptsMonoid -> HaddockOpts
haddockOptsFromMonoid HaddockOptsMonoid{[String]
hoMonoidAdditionalArgs :: [String]
hoMonoidAdditionalArgs :: HaddockOptsMonoid -> [String]
..} = HaddockOpts
defaultHaddockOpts
  { hoAdditionalArgs :: [String]
hoAdditionalArgs = [String]
hoMonoidAdditionalArgs }

testOptsFromMonoid :: TestOptsMonoid -> Maybe [String] -> TestOpts
testOptsFromMonoid :: TestOptsMonoid -> Maybe [String] -> TestOpts
testOptsFromMonoid TestOptsMonoid{[String]
First (Maybe Int)
FirstFalse
FirstTrue
toMonoidRerunTests :: FirstTrue
toMonoidAdditionalArgs :: [String]
toMonoidCoverage :: FirstFalse
toMonoidDisableRun :: FirstFalse
toMonoidMaximumTimeSeconds :: First (Maybe Int)
toMonoidAllowStdin :: FirstTrue
toMonoidRerunTests :: TestOptsMonoid -> FirstTrue
toMonoidAdditionalArgs :: TestOptsMonoid -> [String]
toMonoidCoverage :: TestOptsMonoid -> FirstFalse
toMonoidDisableRun :: TestOptsMonoid -> FirstFalse
toMonoidMaximumTimeSeconds :: TestOptsMonoid -> First (Maybe Int)
toMonoidAllowStdin :: TestOptsMonoid -> FirstTrue
..} Maybe [String]
madditional = TestOpts
defaultTestOpts
  { toRerunTests :: Bool
toRerunTests = FirstTrue -> Bool
fromFirstTrue FirstTrue
toMonoidRerunTests
  , toAdditionalArgs :: [String]
toAdditionalArgs = [String] -> Maybe [String] -> [String]
forall a. a -> Maybe a -> a
fromMaybe [] Maybe [String]
madditional [String] -> [String] -> [String]
forall a. Semigroup a => a -> a -> a
<> [String]
toMonoidAdditionalArgs
  , toCoverage :: Bool
toCoverage = FirstFalse -> Bool
fromFirstFalse FirstFalse
toMonoidCoverage
  , toDisableRun :: Bool
toDisableRun = FirstFalse -> Bool
fromFirstFalse FirstFalse
toMonoidDisableRun
  , toMaximumTimeSeconds :: Maybe Int
toMaximumTimeSeconds =
      Maybe Int -> First (Maybe Int) -> Maybe Int
forall a. a -> First a -> a
fromFirst (TestOpts -> Maybe Int
toMaximumTimeSeconds TestOpts
defaultTestOpts) First (Maybe Int)
toMonoidMaximumTimeSeconds
  , toAllowStdin :: Bool
toAllowStdin = FirstTrue -> Bool
fromFirstTrue FirstTrue
toMonoidAllowStdin
  }

benchmarkOptsFromMonoid ::
     BenchmarkOptsMonoid
  -> Maybe [String]
  -> BenchmarkOpts
benchmarkOptsFromMonoid :: BenchmarkOptsMonoid -> Maybe [String] -> BenchmarkOpts
benchmarkOptsFromMonoid BenchmarkOptsMonoid{First Bool
First String
beoMonoidAdditionalArgs :: First String
beoMonoidDisableRun :: First Bool
beoMonoidAdditionalArgs :: BenchmarkOptsMonoid -> First String
beoMonoidDisableRun :: BenchmarkOptsMonoid -> First Bool
..} Maybe [String]
madditional =
  BenchmarkOpts
defaultBenchmarkOpts
    { beoAdditionalArgs :: Maybe String
beoAdditionalArgs =
        ([String] -> String) -> Maybe [String] -> Maybe String
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\[String]
args -> [String] -> String
unwords [String]
args String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" ") Maybe [String]
madditional Maybe String -> Maybe String -> Maybe String
forall a. Semigroup a => a -> a -> a
<>
        First String -> Maybe String
forall a. First a -> Maybe a
getFirst First String
beoMonoidAdditionalArgs
    , beoDisableRun :: Bool
beoDisableRun = Bool -> First Bool -> Bool
forall a. a -> First a -> a
fromFirst
        (BenchmarkOpts -> Bool
beoDisableRun BenchmarkOpts
defaultBenchmarkOpts)
        First Bool
beoMonoidDisableRun
    }