@@ -14,7 +14,7 @@ module Obelisk.Command.Run where
1414import Control.Arrow ((&&&))
1515import Control.Exception (Exception, bracket)
1616import Control.Lens (ifor, (.~), (&))
17−import Control.Monad (filterM, unless)
17+import Control.Monad (filterM)
1818import Control.Monad.Except (runExceptT, throwError)
1919import Control.Monad.IO.Class (liftIO)
2020import Control.Monad.Reader (MonadIO)
@@ -46,7 +46,7 @@ import Distribution.Compiler (CompilerFlavor(..))
4646import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
4747import Distribution.Parsec.ParseResult (runParseResult)
4848import qualified Distribution.System as Dist
49−import Distribution.Types.BuildInfo (buildable, defaultExtensions, defaultLanguage, hsSourceDirs, options)
49+import Distribution.Types.BuildInfo (buildable, cppOptions, defaultExtensions, defaultLanguage, hsSourceDirs, options)
5050import Distribution.Types.CondTree (simplifyCondTree)
5151import Distribution.Types.GenericPackageDescription (ConfVar (Arch, Impl, OS), condLibrary)
5252import Distribution.Types.Library (libBuildInfo)
@@ -94,6 +94,8 @@ data CabalPackageInfo = CabalPackageInfo
9494 -- ^ List of globally set languages of the library component
9595 , _cabalPackageInfo_compilerOptions :: [(CompilerFlavor, [String])]
9696 -- ^ List of compiler-specific options (e.g., the "ghc-options" field of the cabal file)
97+ , _cabalPackageInfo_cppOptions :: [String]
98+ -- ^ List of CPP (C Preprocessor) options (e.g. the "cpp-options" field of the cabal file)
9799 }
98100
99101-- | 'Bool' with a better name for its purpose.
@@ -302,15 +304,16 @@ parseCabalPackage
302304 -> m (Maybe CabalPackageInfo)
303305parseCabalPackage dir = parseCabalPackage' dir >>= \case
304306 Left err -> Nothing <$ putLog Error err
305− Right (warnings, pkgInfo) -> do
307+ Right (Just (warnings, pkgInfo)) -> do
306308 for_ warnings $ putLog Warning . T.pack . show
307309 pure $ Just pkgInfo
310+ Right Nothing -> pure Nothing
308311
309312-- | Like 'parseCabalPackage' but returns errors and warnings directly so as to avoid 'MonadObelisk'.
310313parseCabalPackage'
311314 :: (MonadIO m)
312315 => FilePath -- ^ Package directory
313− -> m (Either T.Text ([Dist.PWarning], CabalPackageInfo))
316+ -> m (Either T.Text (Maybe ([Dist.PWarning], CabalPackageInfo)))
314317parseCabalPackage' pkg = runExceptT $ do
315318 (cabalContents, packageFile, packageName) <- guessCabalPackageFile pkg >>= \case
316319 Left GuessPackageFileError_NotFound -> throwError $ "No .cabal or package.yaml file found in " <> T.pack pkg
@@ -338,7 +341,7 @@ parseCabalPackage' pkg = runExceptT $ do
338341 case condLibrary <$> result of
339342 Right (Just condLib) -> do
340343 let (_, lib) = simplifyCondTree evalConfVar condLib
341− pure $ (warnings,) $ CabalPackageInfo
344+ pure $ Just $ (warnings,) $ CabalPackageInfo
342345 { _cabalPackageInfo_packageName = T.pack packageName
343346 , _cabalPackageInfo_packageFile = packageFile
344347 , _cabalPackageInfo_packageRoot = takeDirectory packageFile
@@ -350,18 +353,19 @@ parseCabalPackage' pkg = runExceptT $ do
350353 , _cabalPackageInfo_defaultLanguage =
351354 defaultLanguage $ libBuildInfo lib
352355 , _cabalPackageInfo_compilerOptions = options $ libBuildInfo lib
356+ , _cabalPackageInfo_cppOptions = cppOptions $ libBuildInfo lib
353357 }
354− Right Nothing -> throwError "Haskell package has no library component"
358+ Right Nothing -> pure Nothing
355359 Left (_, errors) ->
356360 throwError $ T.pack $ "Failed to parse " <> packageFile <> ":\n" <> unlines (map show errors)
357361
358362parsePackagesOrFail :: (MonadObelisk m, Foldable f) => f FilePath -> m (NE.NonEmpty CabalPackageInfo)
359363parsePackagesOrFail dirs' = do
360− (pkgDirErrs, packageInfos') <- fmap partitionEithers $ for dirs $ \dir -> do
364+ packageInfos' <- fmap catMaybes $ for dirs $ \dir -> do
361365 flip fmap (parseCabalPackage dir) $ \case
362366 Just packageInfo
363− | _cabalPackageInfo_buildable packageInfo -> Right packageInfo
364− _ -> Left dir
367+ | _cabalPackageInfo_buildable packageInfo -> Just packageInfo
368+ _ -> Nothing
365369
366370 -- Sort duplicate packages such that we prefer shorter paths, but fall back to alphabetical ordering.
367371 let packagesByName = Map.map (NE.sortBy $ comparing $ \p -> let n = _cabalPackageInfo_packageFile p in (length n, n))
@@ -383,9 +387,6 @@ parsePackagesOrFail dirs' = do
383387 "No valid, buildable packages found" <> (if null dirs then "" else " in " <> intercalate ", " dirs)
384388 Just xs -> pure xs
385389
386− unless (null pkgDirErrs) $
387− putLog Warning $ T.pack $ "Failed to find buildable packages in " <> intercalate ", " pkgDirErrs
388−
389390 pure packageInfos
390391 where
391392 dirs = toList dirs'