Skip to content
This repository has been archived by the owner on Aug 2, 2020. It is now read-only.

Commit

Permalink
Add matchVersionedFilePath and use for matching library targets.
Browse files Browse the repository at this point in the history
  • Loading branch information
snowleopard committed Feb 16, 2016
1 parent 1aec72e commit 5fcb480
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
19 changes: 18 additions & 1 deletion src/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ module Base (
-- * Miscellaneous utilities
bimap, minusOrd, intersectOrd, replaceEq, quote, replaceSeparators,
decodeModule, encodeModule, unifyPath, (-/-), versionToInt,
removeFileIfExists, removeDirectoryIfExists
removeFileIfExists, removeDirectoryIfExists, matchVersionedFilePath
) where

import Control.Applicative
import Control.Monad.Extra
import Control.Monad.Reader
import Data.Char
import Data.Function
import Data.List.Extra
import Data.Maybe
Expand Down Expand Up @@ -175,3 +176,19 @@ removeFileIfExists f = liftIO . whenM (IO.doesFileExist f) $ IO.removeFile f
removeDirectoryIfExists :: FilePath -> Action ()
removeDirectoryIfExists d =
liftIO . whenM (IO.doesDirectoryExist d) $ IO.removeDirectoryRecursive d

-- | Given a @prefix@ and a @suffix@ check whether a @filePath@ matches the
-- template @prefix ++ version ++ suffix@ where @version@ is an arbitrary string
-- comprising digits (@0-9@), dashes (@-@), and dots (@.@). Examples:
--
--- * @'matchVersionedFilePath' "foo/bar" ".a" "foo/bar.a" '==' 'True'@
--- * @'matchVersionedFilePath' "foo/bar" "a" "foo/bar.a" '==' 'True'@
--- * @'matchVersionedFilePath' "foo/bar" "" "foo/bar.a" '==' 'False'@
--- * @'matchVersionedFilePath' "foo/bar" "a" "foo/bar-0.1.a" '==' 'True'@
--- * @'matchVersionedFilePath' "foo/bar-" "a" "foo/bar-0.1.a" '==' 'True'@
--- * @'matchVersionedFilePath' "foo/bar/" "a" "foo/bar-0.1.a" '==' 'False'@
matchVersionedFilePath :: String -> String -> FilePath -> Bool
matchVersionedFilePath prefix suffix filePath =
case stripPrefix prefix (unifyPath filePath) >>= stripSuffix suffix of
Nothing -> False
Just version -> all (\c -> isDigit c || c == '-' || c == '.') version
22 changes: 12 additions & 10 deletions src/Rules/Library.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import Target
buildPackageLibrary :: Context -> Rules ()
buildPackageLibrary context @ (Context {..}) = do
let buildPath = targetPath stage package -/- "build"
libHs = buildPath -/- "libHS" ++ pkgNameString package

-- TODO: handle dynamic libraries
buildPath <//> "*" ++ waySuffix way ++ ".a" %> \a -> do
matchVersionedFilePath libHs (waySuffix way <.> "a") ?> \a -> do
removeFileIfExists a
cSrcs <- cSources context
hSrcs <- hSources context
Expand Down Expand Up @@ -61,15 +62,16 @@ buildPackageLibrary context @ (Context {..}) = do
-- TODO: this looks fragile as haskell objects can match this rule if their
-- names start with "HS" and they are on top of the module hierarchy.
-- This happens with hsc2hs, which has top-level file HSCParser.hs.
when (package /= hsc2hs) $ priority 2 $ (buildPath -/- "HS*.o") %> \obj -> do
cSrcs <- cSources context
hSrcs <- hSources context
let cObjs = [ buildPath -/- src -<.> "o" | src <- cSrcs
, not ("//AutoApply.cmm" ?== src) ]
++ [ src -<.> "o" | src <- cSrcs, "//AutoApply.cmm" ?== src ]
hObjs = [ buildPath -/- src <.> "o" | src <- hSrcs ]
need $ cObjs ++ hObjs
build $ Target context Ld (cObjs ++ hObjs) [obj]
priority 2 $ when (package /= hsc2hs && way == vanilla) $
(buildPath -/- "HS*.o") %> \obj -> do
cSrcs <- cSources context
hSrcs <- hSources context
let cObjs = [ buildPath -/- src -<.> "o" | src <- cSrcs
, not ("//AutoApply.cmm" ?== src) ]
++ [ src -<.> "o" | src <- cSrcs, "//AutoApply.cmm" ?== src ]
hObjs = [ buildPath -/- src <.> "o" | src <- hSrcs ]
need $ cObjs ++ hObjs
build $ Target context Ld (cObjs ++ hObjs) [obj]

cSources :: Context -> Action [FilePath]
cSources context = interpretInContext context $ getPkgDataList CSrcs
Expand Down

1 comment on commit 5fcb480

@snowleopard
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes a regression which happened while working on #207.

Please sign in to comment.