ad74653db0552861c299b326f7c28b8f8eec2b3f
[ghc.git] / hadrian / src / Hadrian / Builder / Ar.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Hadrian.Builder.Ar
4 -- Copyright : (c) Andrey Mokhov 2014-2017
5 -- License : MIT (see the file LICENSE)
6 -- Maintainer : andrey.mokhov@gmail.com
7 -- Stability : experimental
8 --
9 -- Support for invoking the archiving utility @ar@. We take care not to exceed
10 -- the limit on command line length, which differs across supported operating
11 -- systems (see 'cmdLineLengthLimit'). We need to handle @ar@ in a special way
12 -- because we sometimes archive __a lot__ of files (in the Cabal library, for
13 -- example, command line length can reach 2MB!). To work around the limit on the
14 -- command line length we pass the list of files to be archived via a temporary
15 -- file (see 'runAr'), or alternatively, we split the argument list into chunks
16 -- and call @ar@ multiple times, e.g. when passing arguments via a temporary
17 -- file is not supported (see 'runArWithoutTempFile').
18 -----------------------------------------------------------------------------
19 module Hadrian.Builder.Ar (ArMode (..), args, runAr, runArWithoutTempFile) where
20
21 import Control.Monad
22 import Development.Shake
23 import Development.Shake.Classes
24 import GHC.Generics
25 import Hadrian.Expression
26 import Hadrian.Utilities
27
28 -- | We support packing and unpacking archives with @ar@.
29 data ArMode = Pack | Unpack deriving (Eq, Generic, Show)
30
31 instance Binary ArMode
32 instance Hashable ArMode
33 instance NFData ArMode
34
35 -- NOTE: Make sure to appropriately update 'arFlagsCount' when changing 'args'.
36 -- | Default command line arguments for invoking the archiving utility @ar@.
37 args :: (ShakeValue c, ShakeValue b) => ArMode -> Args c b
38 args Pack = mconcat [ arg "q", arg =<< getOutput, getInputs ]
39 args Unpack = mconcat [ arg "x", arg =<< getInput ]
40
41 -- This count includes "q" and the output file argumentes in 'args'. This is
42 -- only relevant for the 'Pack' @ar@ mode.
43 arFlagsCount :: Int
44 arFlagsCount = 2
45
46 -- | Invoke @ar@ given a path to it and a list of arguments. The list of files
47 -- to be archived is passed via a temporary file. Passing arguments via a
48 -- temporary file is not supported by some versions of @ar@, in which case you
49 -- should use 'runArWithoutTempFile' instead.
50 runAr :: FilePath -> [String] -> Action ()
51 runAr arPath argList = withTempFile $ \tmp -> do
52 writeFile' tmp $ unwords fileArgs
53 cmd [arPath] flagArgs ('@' : tmp)
54 where
55 flagArgs = take arFlagsCount argList
56 fileArgs = drop arFlagsCount argList
57
58 -- | Invoke @ar@ given a path to it and a list of arguments. Note that @ar@
59 -- will be called multiple times if the list of files to be archived is too
60 -- long and doesn't fit into the command line length limit. This function is
61 -- typically much slower than 'runAr'.
62 runArWithoutTempFile :: FilePath -> [String] -> Action ()
63 runArWithoutTempFile arPath argList =
64 forM_ (chunksOfSize cmdLineLengthLimit fileArgs) $ \argsChunk ->
65 unit . cmd [arPath] $ flagArgs ++ argsChunk
66 where
67 flagArgs = take arFlagsCount argList
68 fileArgs = drop arFlagsCount argList