-- We do not use 'flattenPackageDescription' or 'finalizePackageDescription'
-- from 'Distribution.PackageDescription.Configuration' here, because we
-- want to keep the condition tree, but simplify much of the test.

-- | Convert a generic package description to a solver-specific 'PInfo'.
convGPD :: OS -> Arch -> CompilerInfo -> Bool ->
           PI PN -> GenericPackageDescription -> PInfo
convGPD os arch cinfo strfl pi
        (GenericPackageDescription pkg flags libs exes tests benchs) =
  let
    fds  = flagInfo strfl flags

    conv :: Mon.Monoid a => Component -> (a -> BuildInfo) ->
            CondTree ConfVar [Dependency] a -> FlaggedDeps Component PN
    conv comp getInfo = convCondTree os arch cinfo pi fds comp getInfo .
                        PDC.addBuildableCondition getInfo
  in
    PInfo
      (maybe []    (conv ComponentLib                     libBuildInfo         ) libs    ++
       maybe []    (convSetupBuildInfo pi)    (setupBuildInfo pkg)    ++
       concatMap   (\(nm, ds) -> conv (ComponentExe nm)   buildInfo          ds) exes    ++
      prefix (Stanza (SN pi TestStanzas))
        (L.map     (\(nm, ds) -> conv (ComponentTest nm)  testBuildInfo      ds) tests)  ++
      prefix (Stanza (SN pi BenchStanzas))
        (L.map     (\(nm, ds) -> conv (ComponentBench nm) benchmarkBuildInfo ds) benchs))
      fds
      Nothing
convGPD :: OS -> Arch -> CompilerInfo -> Bool ->
           PI PN -> GenericPackageDescription -> PInfo
convGPD os arch cinfo strfl pi
        (GenericPackageDescription pkg flags libs exes tests benchs) =
  let
    fds  = flagInfo strfl flags

    conv :: Mon.Monoid a => Component -> (a -> BuildInfo) ->
            CondTree ConfVar [Dependency] a -> FlaggedDeps Component PN
    conv comp getInfo = convCondTree os arch cinfo pi fds comp getInfo .
                        PDC.addBuildableCondition getInfo
  in
    PInfo
      (maybe []    (conv ComponentLib                     libBuildInfo         ) libs    ++
       maybe []    (convSetupBuildInfo pi)    (setupBuildInfo pkg)    ++
       concatMap   (\(nm, ds) -> conv (ComponentExe nm)   buildInfo          ds) exes    ++
      prefix (Stanza (SN pi TestStanzas))
        (L.map     (\(nm, ds) -> conv (ComponentTest nm)  testBuildInfo      ds) tests)  ++
      prefix (Stanza (SN pi BenchStanzas))
        (L.map     (\(nm, ds) -> conv (ComponentBench nm) benchmarkBuildInfo ds) benchs))
      fds
      Nothing
convGPD os arch cinfo strfl pi
        (GenericPackageDescription pkg flags libs exes tests benchs) =
  let
    fds  = flagInfo strfl flags

    conv :: Mon.Monoid a => Component -> (a -> BuildInfo) ->
            CondTree ConfVar [Dependency] a -> FlaggedDeps Component PN
    conv comp getInfo = convCondTree os arch cinfo pi fds comp getInfo .
                        PDC.addBuildableCondition getInfo
  in
    PInfo
      (maybe []    (conv ComponentLib                     libBuildInfo         ) libs    ++
       maybe []    (convSetupBuildInfo pi)    (setupBuildInfo pkg)    ++
       concatMap   (\(nm, ds) -> conv (ComponentExe nm)   buildInfo          ds) exes    ++
      prefix (Stanza (SN pi TestStanzas))
        (L.map     (\(nm, ds) -> conv (ComponentTest nm)  testBuildInfo      ds) tests)  ++
      prefix (Stanza (SN pi BenchStanzas))
        (L.map     (\(nm, ds) -> conv (ComponentBench nm) benchmarkBuildInfo ds) benchs))
      fds
      Nothing