make :: Makefile -> BuildMaps
make makefile = BuildMaps buildMap childrenMap
  where
    outputs =
      [ (outputPath, descOfTarget target)
      | target <- makefileTargets makefile
      , outputPath <- targetOutputs target
      ]
    childrenMap =
      M.fromListWith mappend $
      [ (takeDirectory outputPath, mempty { dbmTargets = [targetDesc] })
      | (outputPath, targetDesc) <- outputs
      ] ++

      [ (outPatDir, mempty { dbmPatterns = [targetPattern] })
      | targetPattern <- makefilePatterns makefile
      , outPatDir <- nub (map Makefile.filePatternDirectory (targetOutputs targetPattern))
      ]

    overlappingOutputs path (TargetDesc _ a) (TargetDesc _ b) =
      error $ "Overlapping output paths for: " ++ show path ++ " at:\n" ++
      show (targetPos a) ++ "vs.\n" ++ show (targetPos b)
    buildMap =
      M.fromListWithKey overlappingOutputs
      [ (outputPath, targetDesc)
      | (outputPath, targetDesc) <- outputs ]
make :: Makefile -> BuildMaps
make makefile = BuildMaps buildMap childrenMap
  where
    outputs =
      [ (outputPath, descOfTarget target)
      | target <- makefileTargets makefile
      , outputPath <- targetOutputs target
      ]
    childrenMap =
      M.fromListWith mappend $
      [ (takeDirectory outputPath, mempty { dbmTargets = [targetDesc] })
      | (outputPath, targetDesc) <- outputs
      ] ++

      [ (outPatDir, mempty { dbmPatterns = [targetPattern] })
      | targetPattern <- makefilePatterns makefile
      , outPatDir <- nub (map Makefile.filePatternDirectory (targetOutputs targetPattern))
      ]

    overlappingOutputs path (TargetDesc _ a) (TargetDesc _ b) =
      error $ "Overlapping output paths for: " ++ show path ++ " at:\n" ++
      show (targetPos a) ++ "vs.\n" ++ show (targetPos b)
    buildMap =
      M.fromListWithKey overlappingOutputs
      [ (outputPath, targetDesc)
      | (outputPath, targetDesc) <- outputs ]
make makefile = BuildMaps buildMap childrenMap
  where
    outputs =
      [ (outputPath, descOfTarget target)
      | target <- makefileTargets makefile
      , outputPath <- targetOutputs target
      ]
    childrenMap =
      M.fromListWith mappend $
      [ (takeDirectory outputPath, mempty { dbmTargets = [targetDesc] })
      | (outputPath, targetDesc) <- outputs
      ] ++

      [ (outPatDir, mempty { dbmPatterns = [targetPattern] })
      | targetPattern <- makefilePatterns makefile
      , outPatDir <- nub (map Makefile.filePatternDirectory (targetOutputs targetPattern))
      ]

    overlappingOutputs path (TargetDesc _ a) (TargetDesc _ b) =
      error $ "Overlapping output paths for: " ++ show path ++ " at:\n" ++
      show (targetPos a) ++ "vs.\n" ++ show (targetPos b)
    buildMap =
      M.fromListWithKey overlappingOutputs
      [ (outputPath, targetDesc)
      | (outputPath, targetDesc) <- outputs ]