benchmarkAction :: (BenchmarkFlags, BuildFlags, BuildExFlags)
                   -> [String] -> GlobalFlags
                   -> IO ()
benchmarkAction (benchmarkFlags, buildFlags, buildExFlags)
                extraArgs globalFlags = do
  let verbosity      = fromFlagOrDefault normal
                       (benchmarkVerbosity benchmarkFlags)
      addConfigFlags = mempty { configBenchmarks = toFlag True }
      buildFlags'    = buildFlags
                       { buildVerbosity = benchmarkVerbosity benchmarkFlags }
      checkFlags flags
        | fromFlagOrDefault False (configBenchmarks flags) = Nothing
        | otherwise = Just "Re-configuring with benchmarks enabled."
      noAddSource   = fromFlagOrDefault DontSkipAddSourceDepsCheck
                      (buildOnly buildExFlags)

  -- reconfigure also checks if we're in a sandbox and reinstalls add-source
  -- deps if needed.
  (useSandbox, config, distPref) <-
    reconfigure verbosity (benchmarkDistPref benchmarkFlags)
                addConfigFlags [] globalFlags noAddSource
                (buildNumJobs buildFlags') checkFlags
  let setupOptions   = defaultSetupScriptOptions { useDistPref = distPref }
      benchmarkFlags'= benchmarkFlags { benchmarkDistPref = toFlag distPref }

  -- the package was just configured, so the LBI must be available
  lbi <- getPersistBuildConfig distPref
  let pkgDescr = LBI.localPkgDescr lbi
      nameBenchsOnly =
        LBI.foldComponent
          (const Nothing)
          (const Nothing)
          (const Nothing)
          (\b ->
            if buildable (benchmarkBuildInfo b)
              then Just (benchmarkName b)
            else Nothing)
      benchs = mapMaybe nameBenchsOnly $ LBI.pkgComponents pkgDescr
      extraArgs'
        | null extraArgs = benchs
        | otherwise = extraArgs

  if null benchs
    then notice verbosity "Package has no buildable benchmarks."
    else do
      maybeWithSandboxDirOnSearchPath useSandbox $
        build verbosity config distPref buildFlags' extraArgs'

      maybeWithSandboxDirOnSearchPath useSandbox $
        setupWrapper verbosity setupOptions Nothing
          Cabal.benchmarkCommand (const benchmarkFlags') extraArgs'
benchmarkAction :: (BenchmarkFlags, BuildFlags, BuildExFlags)
                   -> [String] -> GlobalFlags
                   -> IO ()
benchmarkAction (benchmarkFlags, buildFlags, buildExFlags)
                extraArgs globalFlags = do
  let verbosity      = fromFlagOrDefault normal
                       (benchmarkVerbosity benchmarkFlags)
      addConfigFlags = mempty { configBenchmarks = toFlag True }
      buildFlags'    = buildFlags
                       { buildVerbosity = benchmarkVerbosity benchmarkFlags }
      checkFlags flags
        | fromFlagOrDefault False (configBenchmarks flags) = Nothing
        | otherwise = Just "Re-configuring with benchmarks enabled."
      noAddSource   = fromFlagOrDefault DontSkipAddSourceDepsCheck
                      (buildOnly buildExFlags)

  -- reconfigure also checks if we're in a sandbox and reinstalls add-source
  -- deps if needed.
  (useSandbox, config, distPref) <-
    reconfigure verbosity (benchmarkDistPref benchmarkFlags)
                addConfigFlags [] globalFlags noAddSource
                (buildNumJobs buildFlags') checkFlags
  let setupOptions   = defaultSetupScriptOptions { useDistPref = distPref }
      benchmarkFlags'= benchmarkFlags { benchmarkDistPref = toFlag distPref }

  -- the package was just configured, so the LBI must be available
  lbi <- getPersistBuildConfig distPref
  let pkgDescr = LBI.localPkgDescr lbi
      nameBenchsOnly =
        LBI.foldComponent
          (const Nothing)
          (const Nothing)
          (const Nothing)
          (\b ->
            if buildable (benchmarkBuildInfo b)
              then Just (benchmarkName b)
            else Nothing)
      benchs = mapMaybe nameBenchsOnly $ LBI.pkgComponents pkgDescr
      extraArgs'
        | null extraArgs = benchs
        | otherwise = extraArgs

  if null benchs
    then notice verbosity "Package has no buildable benchmarks."
    else do
      maybeWithSandboxDirOnSearchPath useSandbox $
        build verbosity config distPref buildFlags' extraArgs'

      maybeWithSandboxDirOnSearchPath useSandbox $
        setupWrapper verbosity setupOptions Nothing
          Cabal.benchmarkCommand (const benchmarkFlags') extraArgs'
benchmarkAction (benchmarkFlags, buildFlags, buildExFlags)
                extraArgs globalFlags = do
  let verbosity      = fromFlagOrDefault normal
                       (benchmarkVerbosity benchmarkFlags)
      addConfigFlags = mempty { configBenchmarks = toFlag True }
      buildFlags'    = buildFlags
                       { buildVerbosity = benchmarkVerbosity benchmarkFlags }
      checkFlags flags
        | fromFlagOrDefault False (configBenchmarks flags) = Nothing
        | otherwise = Just "Re-configuring with benchmarks enabled."
      noAddSource   = fromFlagOrDefault DontSkipAddSourceDepsCheck
                      (buildOnly buildExFlags)

  -- reconfigure also checks if we're in a sandbox and reinstalls add-source
  -- deps if needed.
  (useSandbox, config, distPref) <-
    reconfigure verbosity (benchmarkDistPref benchmarkFlags)
                addConfigFlags [] globalFlags noAddSource
                (buildNumJobs buildFlags') checkFlags
  let setupOptions   = defaultSetupScriptOptions { useDistPref = distPref }
      benchmarkFlags'= benchmarkFlags { benchmarkDistPref = toFlag distPref }

  -- the package was just configured, so the LBI must be available
  lbi <- getPersistBuildConfig distPref
  let pkgDescr = LBI.localPkgDescr lbi
      nameBenchsOnly =
        LBI.foldComponent
          (const Nothing)
          (const Nothing)
          (const Nothing)
          (\b ->
            if buildable (benchmarkBuildInfo b)
              then Just (benchmarkName b)
            else Nothing)
      benchs = mapMaybe nameBenchsOnly $ LBI.pkgComponents pkgDescr
      extraArgs'
        | null extraArgs = benchs
        | otherwise = extraArgs

  if null benchs
    then notice verbosity "Package has no buildable benchmarks."
    else do
      maybeWithSandboxDirOnSearchPath useSandbox $
        build verbosity config distPref buildFlags' extraArgs'

      maybeWithSandboxDirOnSearchPath useSandbox $
        setupWrapper verbosity setupOptions Nothing
          Cabal.benchmarkCommand (const benchmarkFlags') extraArgs'