Skip to content

Commit

Permalink
Handle some more edge cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
lennart-augustsson-epicgames committed Jul 27, 2024
1 parent 04ace11 commit 65cb518
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
28 changes: 18 additions & 10 deletions src/MicroCabal/Backend/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ findMainIs env (d:ds) fn = do
else
findMainIs env ds fn

-- It can happen that there are no exposed modules.
-- E.g., for a package only needed for certain versions.
getExposedModules :: [Field] -> [String]
getExposedModules flds = getFieldStrings flds (error "no exposed-modules") "exposed-modules"
getExposedModules flds = getFieldStrings flds [] "exposed-modules"

getOtherModules :: [Field] -> [String]
getOtherModules flds = getFieldStrings flds [] "other-modules"
Expand All @@ -120,17 +122,21 @@ ghcBuildLib :: Env -> Section -> Section -> IO ()
ghcBuildLib env (Section _ _ glob) (Section _ name flds) = do
initDB env
stdArgs <- setupStdArgs env flds
let mdls = getExposedModules flds
let emdls = getExposedModules flds
omdls = getOtherModules flds
mdls = emdls ++ omdls
ver = getVersion glob "version"
args = unwords $ ["-O"] ++ stdArgs ++
["--make", "-no-link", "-this-unit-id", key ] ++
["-fbuilding-cabal-package", "-static" ] ++
(omdls ++ mdls) ++
mdls ++
[ ">/dev/null" | verbose env <= 0 ]
key = name ++ "-" ++ showVersion ver ++ "-mcabal"
message env 0 $ "Building library " ++ name ++ " with ghc"
cmd env $ "ghc " ++ args
if null mdls then
message env 0 $ "Building library " ++ name ++ " with ghc skipped, no modules"
else do
message env 0 $ "Building library " ++ name ++ " with ghc"
cmd env $ "ghc " ++ args

ghcInstallExe :: Env -> Section -> Section -> IO ()
ghcInstallExe env (Section _ _ _glob) (Section _ name _) = do
Expand Down Expand Up @@ -159,19 +165,21 @@ ghcInstallLib env (Section _ _ glob) (Section _ name flds) = do
archOut = destDir </> "libHS" ++ namever ++ "-mcabal.a"
mkdir env destDir
rmrf env archOut
cmd env $ "ar -c -r -s " ++ archOut ++ " `find " ++ buildDir ++ " -name '*.o'`"

let files = map mdlToHi (omdls ++ mdls)
mdls = getExposedModules flds
omdls = getOtherModules flds
mdlToHi = (++ ".hi") . map (\ c -> if c == '.' then '/' else c)
copyFiles env buildDir files destDir

when (not (null files)) $ do
cmd env $ "ar -c -r -s " ++ archOut ++ " `find " ++ buildDir ++ " -name '*.o'`"
copyFiles env buildDir files destDir

db <- getGhcDir env
let extraLibs = getFieldStrings flds [] "extra-libraries"
deps = getBuildDependsPkg flds
depends <- nub <$> mapM (getPackageId env) deps
let desc = unlines
let desc = unlines $
[ "name: " ++ name
, "version: " ++ showVersion vers
, "visibility: public"
Expand All @@ -182,10 +190,10 @@ ghcInstallLib env (Section _ _ glob) (Section _ name flds) = do
, "import-dirs: " ++ destDir
, "library-dirs: " ++ destDir
, "library-dirs-static: " ++ destDir
, "hs-libraries: HS" ++ key
, "extra-libraries: " ++ unwords extraLibs
, "depends: " ++ unwords depends
]
] ++
[ "hs-libraries: HS" ++ key | not (null files) ]
key = namever ++ "-mcabal"
pkgFn = db </> key ++ ".conf"
quiet = if verbose env > 0 then "" else " >/dev/null"
Expand Down
7 changes: 6 additions & 1 deletion src/MicroCabal/Env.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module MicroCabal.Env(
Env(..),
Target(..),
Backend(..),
PackageName,
message,
Expand All @@ -13,9 +14,13 @@ data Env = Env {
verbose :: Int, -- how chatty, default is 0, -1=say nothing, 0=minimal messages, 1=debug info
depth :: Int, -- nesting depth for recursive builds, default is 0
recursive:: Bool, -- do recursive builds, default is False
backend :: Backend -- which compiler to use, default is MHS
backend :: Backend, -- which compiler to use, default is MHS
targets :: [Target] -- only build/install these
}

data Target = TgtLib | TgtExe
deriving (Eq)

data Backend = Backend {
backendNameVers:: Env -> IO (String, Version), -- name and version
doesPkgExist :: Env -> PackageName -> IO Bool, -- is the package available in the database?
Expand Down
26 changes: 18 additions & 8 deletions src/MicroCabal/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ setupEnv :: IO Env
setupEnv = do
home <- getEnv "HOME"
let cdir = home </> ".mcabal"
return Env{ cabalDir = cdir, distDir = "dist-mcabal", verbose = 0, depth = 0, backend = mhsBackend, recursive = False }
return Env{ cabalDir = cdir, distDir = "dist-mcabal", verbose = 0, depth = 0,
backend = mhsBackend, recursive = False, targets = [TgtLib, TgtExe] }

decodeCommonArgs :: Env -> IO (Env, [String])
decodeCommonArgs env = do
Expand Down Expand Up @@ -236,10 +237,10 @@ build env = do
info = FlagInfo { os = I.os, arch = I.arch, flags = [], impl = comp }
ncbl@(Cabal sects) = normalize info cbl
glob = getGlobal ncbl
sect s@(Section "executable" _ _) = buildExe env glob s
sect s@(Section "library" _ _) = buildLib env glob s
sect s@(Section "executable" _ _) | TgtExe `elem` targets env = buildExe env glob s
sect s@(Section "library" _ _) | TgtLib `elem` targets env = buildLib env glob s
sect _ = return ()
mapM_ sect sects
mapM_ sect $ addMissing sects

buildExe :: Env -> Section -> Section -> IO ()
buildExe env glob sect@(Section _ name flds) = do
Expand All @@ -266,10 +267,16 @@ checkDep env pkg = do
if recursive env then do
let env' = env { depth = depth env + 1 }
preserveCurrentDirectory $
cmdInstall env' [pkg]
cmdInstallLib env' [pkg]
else
error $ "dependency not installed: " ++ pkg

-- If there is no section, except the global one, then just make a
-- library section.
addMissing :: [Section] -> [Section]
addMissing [glb@(Section "global" _ flds)] = [glb, Section "library" (getFieldString flds "name") flds]
addMissing sects = sects

-----------------------------------------

cmdInstall :: Env -> [String] -> IO ()
Expand All @@ -278,6 +285,9 @@ cmdInstall env args = do
cmdBuild env args
install env

cmdInstallLib :: Env -> [String] -> IO ()
cmdInstallLib env args = cmdInstall env{ targets = [TgtLib] } args

install :: Env -> IO ()
install env = do
fn <- findCabalFile env
Expand All @@ -287,10 +297,10 @@ install env = do
info = FlagInfo { os = I.os, arch = I.arch, flags = [], impl = comp }
ncbl@(Cabal sects) = normalize info cbl
glob = getGlobal ncbl
sect s@(Section "executable" _ _) = installExe env glob s
sect s@(Section "library" _ _) = installLib env glob s
sect s@(Section "executable" _ _) | TgtExe `elem` targets env = installExe env glob s
sect s@(Section "library" _ _) | TgtLib `elem` targets env = installLib env glob s
sect _ = return ()
mapM_ sect sects
mapM_ sect $ addMissing sects

installExe :: Env -> Section -> Section -> IO ()
installExe env glob sect@(Section _ name _) = do
Expand Down
2 changes: 1 addition & 1 deletion src/MicroCabal/Normalize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ libName (Cabal []) = undefined
libName (Cabal (g@(Section _ _ gs):ss)) = Cabal $ g : map set ss
where set (Section "library" "" fs) = Section "library" name fs
set s = s
name = head $ [ n | Field "name" (VItem n) <- gs ] ++ [error "no name field"]
name = getFieldString gs "name"

reduce :: FlagInfo -> Cabal -> Cabal
reduce info c = reduce' (addFlags c) c
Expand Down
2 changes: 1 addition & 1 deletion src/MicroCabal/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ parsers =
, "package-url" # pFreeText
, "stability" # pFreeText
, "subdir" # pFreeText
, "synopsis" # pFreeText
, "synopsis" # pFreeTextX
, "tested-with" # pFreeText
, "version" # (VVersion <$> pVersion)
-- test suite fields
Expand Down

0 comments on commit 65cb518

Please sign in to comment.