82d62044f293ec65cc1d48fe3b63948596ed3bb1
[hadrian.git] / src / Settings / Builders / Ar.hs
1 module Settings.Builders.Ar (arArgs, arCmd) where
2
3 import Expression
4 import Oracles
5 import Predicates (builder)
6
7 arArgs :: Args
8 arArgs = builder Ar ? mconcat [ arg "q"
9 , arg =<< getOutput
10 , append =<< getInputs ]
11
12 -- This count includes arg "q" and arg file parameters in arArgs (see above).
13 -- Update this value appropriately when changing arArgs.
14 arFlagsCount :: Int
15 arFlagsCount = 2
16
17 -- Ar needs to be invoked in a special way: we pass the list of files to be
18 -- archived via a temporary file as otherwise Ar (or rather Windows command
19 -- line) chokes up. Alternatively, we split argument list into chunks and call
20 -- ar multiple times (when passing files via a separate file is not supported).
21 arCmd :: FilePath -> [String] -> Action ()
22 arCmd path argList = do
23 arSupportsAtFile <- flag ArSupportsAtFile
24 let flagArgs = take arFlagsCount argList
25 fileArgs = drop arFlagsCount argList
26 if arSupportsAtFile
27 then useAtFile path flagArgs fileArgs
28 else useSuccessiveInvokations path flagArgs fileArgs
29
30 useAtFile :: FilePath -> [String] -> [String] -> Action ()
31 useAtFile path flagArgs fileArgs = withTempFile $ \tmp -> do
32 writeFile' tmp $ unwords fileArgs
33 cmd [path] flagArgs ('@' : tmp)
34
35 useSuccessiveInvokations :: FilePath -> [String] -> [String] -> Action ()
36 useSuccessiveInvokations path flagArgs fileArgs = do
37 maxChunk <- cmdLineLengthLimit
38 forM_ (chunksOfSize maxChunk fileArgs) $ \argsChunk ->
39 unit . cmd [path] $ flagArgs ++ argsChunk