3 import Distribution.Simple
8 import Distribution.PackageDescription
9 import Distribution.Setup
10 import Distribution.Simple
11 import Distribution.Simple.Configure
12 import Distribution.Simple.LocalBuildInfo
15 import System.Directory
18 import Control.Exception
20 buildInfoName = "Kirschbaum.buildinfo"
21 cgiName = "Kirschbaum.cgi"
23 main = defaultMainWithHooks defaultUserHooks {preConf = preConf, postConf = postConf}
25 preConf :: [String] -> ConfigFlags -> IO HookedBuildInfo
27 = do try (removeFile buildInfoName)
28 return emptyHookedBuildInfo
29 postConf :: [String] -> ConfigFlags -> PackageDescription -> LocalBuildInfo -> IO ExitCode
30 postConf args flags _ localbuildinfo
31 = do binfo <- pkgConfigBuildInfo (configVerbose flags)
32 let default_binfo = emptyBuildInfo {extraLibs = ["xml2", "xslt", "exslt"]}
33 writeHookedBuildInfo buildInfoName (Just emptyBuildInfo,
34 [(cgiName, fromMaybe default_binfo binfo)])
38 message :: String -> IO ()
39 message s = putStrLn $ "configure: " ++ s
41 rawSystemGrabOutput :: Int -> FilePath -> [String] -> IO String
42 rawSystemGrabOutput verbose path args
43 = do when (verbose > 0) $
44 putStrLn (path ++ concatMap (' ':) args)
45 (inp,out,err,pid) <- runInteractiveProcess path args Nothing Nothing
46 exitCode <- waitForProcess pid
47 if exitCode /= ExitSuccess then
48 do errMsg <- hGetContents err
50 exitWith exitCode else
57 mergeBuildInfo :: BuildInfo -> BuildInfo -> BuildInfo
58 mergeBuildInfo b1 b2 = BuildInfo {
59 buildable = buildable b1 || buildable b2,
60 ccOptions = ccOptions b1 ++ ccOptions b2,
61 ldOptions = ldOptions b1 ++ ldOptions b2,
62 frameworks = frameworks b1 ++ frameworks b2,
63 cSources = cSources b1 ++ cSources b2,
64 hsSourceDirs = hsSourceDirs b1 ++ hsSourceDirs b2,
65 otherModules = otherModules b1 ++ otherModules b2,
66 extensions = extensions b1 ++ extensions b2,
67 extraLibs = extraLibs b1 ++ extraLibs b2,
68 extraLibDirs = extraLibDirs b1 ++ extraLibDirs b2,
69 includeDirs = includeDirs b1 ++ includeDirs b2,
70 includes = includes b1 ++ includes b2,
71 installIncludes = installIncludes b1 ++ installIncludes b2,
72 options = options b1 ++ options b2,
73 ghcProfOptions = ghcProfOptions b1 ++ ghcProfOptions b2
76 libXml2ConfigBuildInfo :: Int -> IO (Maybe BuildInfo)
77 libXml2ConfigBuildInfo verbose
78 = do mb_libxml2_config_path <- findProgram "xml2-config" Nothing
79 case mb_libxml2_config_path
80 of Just libxml2_config_path
81 -> do message "configuring libxml2 library"
82 res <- rawSystemGrabOutput verbose libxml2_config_path ["--libs"]
83 let (lib_dirs, libs, ld_opts) = splitLibsFlags (words res)
84 res <- rawSystemGrabOutput verbose libxml2_config_path ["--cflags"]
85 let (inc_dirs, cc_opts) = splitCFlags (words res)
86 let bi = emptyBuildInfo {
87 extraLibDirs = lib_dirs,
90 includeDirs = inc_dirs,
95 -> do message "This package will be built using default settings for libxml2 library"
98 libXsltConfigBuildInfo :: Int -> IO (Maybe BuildInfo)
99 libXsltConfigBuildInfo verbose
100 = do mb_libxslt_config_path <- findProgram "xslt-config" Nothing
101 case mb_libxslt_config_path
102 of Just libxslt_config_path
103 -> do message "configuring libxslt library"
104 res <- rawSystemGrabOutput verbose libxslt_config_path ["--libs"]
105 let (lib_dirs, libs, ld_opts) = splitLibsFlags (words res)
106 res <- rawSystemGrabOutput verbose libxslt_config_path ["--cflags"]
107 let (inc_dirs, cc_opts) = splitCFlags (words res)
108 let bi = emptyBuildInfo {
109 extraLibDirs = lib_dirs,
112 includeDirs = inc_dirs,
117 -> do message "This package will be built using default settings for libxslt library"
121 pkgConfigBuildInfo :: Int -> IO (Maybe BuildInfo)
122 pkgConfigBuildInfo verbose
123 = do mb_libxslt_config_path <- findProgram "pkg-config" Nothing
124 case mb_libxslt_config_path
125 of Just libxslt_config_path
126 -> do message "configuring libxml2, libxslt and libexslt library"
127 let modules = ["libxml-2.0", "libxslt", "libexslt"]
128 res <- rawSystemGrabOutput verbose libxslt_config_path (modules ++ ["--libs"])
129 let (lib_dirs, libs, ld_opts) = splitLibsFlags (words res)
130 res <- rawSystemGrabOutput verbose libxslt_config_path (modules ++ ["--cflags"])
131 let (inc_dirs, cc_opts) = splitCFlags (words res)
132 let bi = emptyBuildInfo {
133 extraLibDirs = lib_dirs,
136 includeDirs = inc_dirs,
141 -> do message "This package will be built using default settings for libxslt library"
144 splitLibsFlags :: [String] -> ([String], [String], [String])
145 splitLibsFlags [] = ([], [], [])
146 splitLibsFlags (arg:args)
148 of ('-':'L':lib_dir) -> let (lib_dirs, libs, ld_opts) = splitLibsFlags args
149 in (lib_dir:lib_dirs, libs, ld_opts)
150 ('-':'l':lib) -> let (lib_dirs, libs, ld_opts) = splitLibsFlags args
151 in (lib_dirs, lib:libs, ld_opts)
152 ld_opt -> let (lib_dirs, libs, ld_opts) = splitLibsFlags args
153 in (lib_dirs, libs, ld_opt:ld_opts)
155 splitCFlags :: [String] -> ([String], [String])
156 splitCFlags [] = ([], [])
157 splitCFlags (arg:args)
159 of ('-':'I':inc_dir) -> let (inc_dirs, c_opts) = splitCFlags args
160 in (inc_dir:inc_dirs, c_opts)
161 c_opt -> let (inc_dirs, c_opts) = splitCFlags args
162 in (inc_dirs, c_opt:c_opts)