From f8d37dc12ab069454940756473509a2bd87f0612 Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Tue, 4 Nov 2025 14:56:08 +0100 Subject: [PATCH 1/5] export Unix.tmpFile --- src/MicroCabal/Unix.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/MicroCabal/Unix.hs b/src/MicroCabal/Unix.hs index 25a2805..05595cb 100644 --- a/src/MicroCabal/Unix.hs +++ b/src/MicroCabal/Unix.hs @@ -5,6 +5,7 @@ module MicroCabal.Unix( gitClone, tarx, rmrf, + tmpFile, cp, cpr, copyFiles, preserveCurrentDirectory, From e53a35d370ddfd2be4ef04363dd5bfb7ed2c4732 Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Tue, 4 Nov 2025 15:03:29 +0100 Subject: [PATCH 2/5] check if a library compiled with -optc or -optl can generate an executable --- src/MicroCabal/Backend/MHS.hs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/MicroCabal/Backend/MHS.hs b/src/MicroCabal/Backend/MHS.hs index c2c4629..ef3a3f2 100644 --- a/src/MicroCabal/Backend/MHS.hs +++ b/src/MicroCabal/Backend/MHS.hs @@ -4,6 +4,7 @@ import Data.List(dropWhileEnd, (\\), stripPrefix) import Data.Maybe import Data.Version import System.Directory +import System.IO import MicroCabal.Cabal import MicroCabal.Env import MicroCabal.Macros @@ -172,21 +173,28 @@ mhsBuildLib env (Section _ _ glob) (Section _ name flds) = do pkgfn = distDir env ++ "/" ++ namever ++ ".pkg" cs = getFieldStrings flds [] "c-sources" ldf = getFieldStrings flds [] "extra-libraries" - args = unwords $ map ("-optc " ++) cs ++ - map ("-optl -l" ++) ldf ++ - ["-P" ++ namever, - "-o" ++ pkgfn] ++ - stdArgs ++ - ["-a."] ++ - mdls + cargs = map ("-optc " ++) cs + pargs = map ("-optl -l" ++) ldf ++ + ["-P" ++ namever, + "-o" ++ pkgfn, + "-a."] isMdl (' ':_) = True -- Relies on -L output format isMdl _ = False - mhs env args + mhs env $ unwords (stdArgs ++ cargs ++ pargs ++ mdls) pkgmdls <- words . unlines . filter isMdl . lines <$> mhsOut env ("-L" ++ pkgfn) let bad = pkgmdls \\ (mdls ++ omdls) when (not (null bad)) $ do message env (-1) "Warning: package modules not mentioned in exposed-modules nor other-modules" mapM_ (message env (-1)) bad + unless (null cs && null ldf) $ do + -- we are linking to some c lib so we test if the package compiles + when (verbose env > 0) $ + putStrLn $ "Testing the library " ++ namever + (fn, h) <- tmpFile + hPutStr h $ unlines $ map ("import " ++) mdls ++ ["main = return ()"] + hFlush h >> hClose h + mhs env $ unwords $ cargs ++ ["-p" ++ pkgfn, "-r", fn] + removeFile fn mhsInstallExe :: Env -> Section -> Section -> IO () mhsInstallExe env (Section _ _ _glob) (Section _ name _) = do From f76c1c162834533c19bee303d7a7ae92a017399a Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Tue, 4 Nov 2025 17:39:38 +0100 Subject: [PATCH 3/5] move generated linker flags in stdArgs Even executables may need to define the "extra-libraries" field --- src/MicroCabal/Backend/MHS.hs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/MicroCabal/Backend/MHS.hs b/src/MicroCabal/Backend/MHS.hs index ef3a3f2..5c87414 100644 --- a/src/MicroCabal/Backend/MHS.hs +++ b/src/MicroCabal/Backend/MHS.hs @@ -97,6 +97,7 @@ setupStdArgs env flds = do opts = getFieldStrings flds [] "mhs-options" cppOpts = getFieldStrings flds [] "cpp-options" incs = getFieldStrings flds [] "include-dirs" + lopts = getFieldStrings flds [] "extra-libraries" exts' = filter (`elem` mhsX) (exts ++ oexts) deps = getBuildDependsPkg flds mhsX = ["CPP"] @@ -108,6 +109,7 @@ setupStdArgs env flds = do map ("-X" ++) exts' ++ map ("-I" ++) incs ++ opts ++ + map ("-optl -l" ++) lopts ++ macros ++ cppOpts @@ -174,8 +176,7 @@ mhsBuildLib env (Section _ _ glob) (Section _ name flds) = do cs = getFieldStrings flds [] "c-sources" ldf = getFieldStrings flds [] "extra-libraries" cargs = map ("-optc " ++) cs - pargs = map ("-optl -l" ++) ldf ++ - ["-P" ++ namever, + pargs = ["-P" ++ namever, "-o" ++ pkgfn, "-a."] isMdl (' ':_) = True -- Relies on -L output format @@ -193,7 +194,7 @@ mhsBuildLib env (Section _ _ glob) (Section _ name flds) = do (fn, h) <- tmpFile hPutStr h $ unlines $ map ("import " ++) mdls ++ ["main = return ()"] hFlush h >> hClose h - mhs env $ unwords $ cargs ++ ["-p" ++ pkgfn, "-r", fn] + mhs env $ unwords $ stdArgs ++ cargs ++ ["-p" ++ pkgfn, "-r", fn] removeFile fn mhsInstallExe :: Env -> Section -> Section -> IO () From 6a60b673059711665dcc82189fa647f03e1052cb Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Tue, 4 Nov 2025 17:48:14 +0100 Subject: [PATCH 4/5] parse "pkgconfig-depends" with pVLibs Otherwise pVComma will fail when library versions are specified --- src/MicroCabal/Parse.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MicroCabal/Parse.hs b/src/MicroCabal/Parse.hs index 8dad839..5f54694 100644 --- a/src/MicroCabal/Parse.hs +++ b/src/MicroCabal/Parse.hs @@ -413,7 +413,7 @@ parsers = , "other-extensions" # pVOptComma , "other-languages" # (VItem <$> pItem) , "other-modules" # pVOptComma - , "pkgconfig-depends" # pVComma + , "pkgconfig-depends" # pVLibs , "virtual-modules" # pVComma --- library fields , "visibility" # (VItem <$> pItem) From 943c0cbd3f08bf50ca324554ea4021c1785d2e7f Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Tue, 4 Nov 2025 21:51:47 +0100 Subject: [PATCH 5/5] when building a library with FFI imports create an executable to check the linking flags running with -r will not call the linker --- src/MicroCabal/Backend/MHS.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MicroCabal/Backend/MHS.hs b/src/MicroCabal/Backend/MHS.hs index 2203385..a78a6ef 100644 --- a/src/MicroCabal/Backend/MHS.hs +++ b/src/MicroCabal/Backend/MHS.hs @@ -194,8 +194,8 @@ mhsBuildLib env (Section _ _ glob) (Section _ name flds) = do (fn, h) <- tmpFile hPutStr h $ unlines $ map ("import " ++) mdls ++ ["main = return ()"] hFlush h >> hClose h - mhs env $ unwords $ stdArgs ++ cargs ++ ["-p" ++ pkgfn, "-r", fn] - removeFile fn + mhs env $ unwords $ stdArgs ++ cargs ++ ["-p" ++ pkgfn, "-o" ++ fn ++ ".bin", fn] + mapM_ removeFile [fn ++ ".bin", fn] mhsInstallExe :: Env -> Section -> Section -> IO () mhsInstallExe env (Section _ _ _glob) (Section _ name _) = do