Merge pull request #10493 from Sdywolf/doc-refine/repl
[cabal.git] / cabal-testsuite / PackageTests / CustomPreProcess / Setup.hs
blob769f130933978d6577c7f57a508c85122d62786d
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# OPTIONS_GHC -Wall #-}
5 -- The logic here is tricky.
6 -- If this is compiled by cabal-install, then the MIN_VERSION_Cabal is set
7 -- otherwise, we are compiling against Cabal library under test,
8 -- which is new!
9 #ifndef MIN_VERSION_Cabal
10 #define MIN_VERSION_Cabal(x,y,z) 1
11 #endif
13 import Distribution.PackageDescription
14 import Distribution.Simple
15 import Distribution.Simple.LocalBuildInfo
16 import Distribution.Simple.PreProcess
17 import Distribution.Simple.Utils
18 #if MIN_VERSION_Cabal(3,13,0)
19 import Distribution.Utils.Path (getSymbolicPath)
20 #endif
22 import System.Exit
23 import System.FilePath
24 import System.Process (rawSystem)
26 main :: IO ()
27 main = defaultMainWithHooks
28 simpleUserHooks { hookedPreProcessors = [("pre", myCustomPreprocessor)] }
29 where
30 #if MIN_VERSION_Cabal(2,0,0)
31 myCustomPreprocessor :: BuildInfo -> LocalBuildInfo -> ComponentLocalBuildInfo -> PreProcessor
32 myCustomPreprocessor _bi lbi _clbi =
33 #else
34 myCustomPreprocessor :: BuildInfo -> LocalBuildInfo -> PreProcessor
35 myCustomPreprocessor _bi lbi =
36 #endif
37 PreProcessor {
38 platformIndependent = True,
39 runPreProcessor = mkSimplePreProcessor $ \inFile outFile verbosity ->
40 do info verbosity ("Preprocessing " ++ inFile ++ " to " ++ outFile)
41 #if MIN_VERSION_Cabal(3,7,0)
42 callProcess progPath [inFile, outFile],
43 ppOrdering = unsorted
44 #else
45 callProcess progPath [inFile, outFile]
46 #endif
48 where
49 builddir =
50 #if MIN_VERSION_Cabal(3,13,0)
51 getSymbolicPath $
52 #endif
53 buildDir lbi
54 progName = "my-custom-preprocessor"
55 progPath = builddir </> progName </> progName
57 -- Backwards compat with process < 1.2.
58 callProcess :: FilePath -> [String] -> IO ()
59 callProcess path args =
60 do exitCode <- rawSystem path args
61 case exitCode of ExitSuccess -> return ()
62 f@(ExitFailure _) -> fail $ "callProcess " ++ show path
63 ++ " " ++ show args ++ " failed: "
64 ++ show f