diff options
Diffstat (limited to 'configure.hs')
-rw-r--r-- | configure.hs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/configure.hs b/configure.hs new file mode 100644 index 0000000..3b0aaa3 --- /dev/null +++ b/configure.hs | |||
@@ -0,0 +1,90 @@ | |||
1 | #! /usr/bin/env runhaskell | ||
2 | {- configure.hs for hmatrix | ||
3 | ------------------------ | ||
4 | |||
5 | GSL and LAPACK may require auxiliary libraries which depend on OS, | ||
6 | distribution, and implementation. This script tries to to find out | ||
7 | the correct link command for your system. | ||
8 | Suggestions and contributions are welcome. | ||
9 | |||
10 | By default we try to link -lgsl -llapack. This works in ubuntu/debian, | ||
11 | both with and without ATLAS. | ||
12 | If this fails we try different sets of additional libraries which are | ||
13 | known to work in some systems. | ||
14 | |||
15 | The desired libraries can also be explicitly given by the user using cabal | ||
16 | flags (e.g., -fmkl, -faccelerate) or --configure-option=link:lib1,lib2,lib3,... | ||
17 | |||
18 | -} | ||
19 | |||
20 | import System | ||
21 | import Data.List(isPrefixOf) | ||
22 | import Distribution.Simple.LocalBuildInfo | ||
23 | import Distribution.Simple.Configure | ||
24 | import Distribution.PackageDescription | ||
25 | |||
26 | -- possible additional dependencies for the desired libs (by default gsl lapack) | ||
27 | |||
28 | opts = [ "" -- Ubuntu/Debian | ||
29 | , "blas" | ||
30 | , "blas cblas" | ||
31 | , "cblas" | ||
32 | , "gslcblas" | ||
33 | , "blas gslcblas" | ||
34 | , "f77blas cblas atlas gcc_s" -- Arch Linux (older version of atlas-lapack) | ||
35 | , "blas gslcblas gfortran" -- Arch Linux with normal blas and lapack | ||
36 | ] | ||
37 | |||
38 | -- compile a simple program with symbols from GSL and LAPACK with the given libs | ||
39 | testprog libs fmks = "echo \"int main(){zgesvd_(); gsl_sf_gamma();}\" > /tmp/dummy.c; gcc /tmp/dummy.c " | ||
40 | ++ f1 libs ++ " " ++ f2 fmks ++ " > /dev/null 2> /dev/null" | ||
41 | |||
42 | f1 = unwords . map ("-l"++) . words | ||
43 | f2 = unwords . map ("-framework "++) . words | ||
44 | |||
45 | check libs fmks = (ExitSuccess ==) `fmap` system (testprog libs fmks) | ||
46 | |||
47 | -- test different configurations until the first one works | ||
48 | try _ _ [] = return Nothing | ||
49 | try b f (opt:rest) = do | ||
50 | ok <- check (b ++ " " ++ opt) f | ||
51 | if ok then return (Just opt) | ||
52 | else try b f rest | ||
53 | |||
54 | -- read --configure-option=link:lib1,lib2,lib3,etc | ||
55 | linkop = "link:" | ||
56 | getUserLink = concatMap (g . drop (length linkop)) . filter (isPrefixOf linkop) | ||
57 | where g = map cs | ||
58 | cs ',' = ' ' | ||
59 | cs x = x | ||
60 | |||
61 | main = do | ||
62 | putStr "Checking foreign libraries..." | ||
63 | |||
64 | args <- getArgs | ||
65 | Just bInfo <- maybeGetPersistBuildConfig "dist" | ||
66 | |||
67 | let Just lib = library . localPkgDescr $ bInfo | ||
68 | base = unwords . extraLibs . libBuildInfo $ lib | ||
69 | fwks = unwords . frameworks . libBuildInfo $ lib | ||
70 | auxpref = getUserLink args | ||
71 | |||
72 | -- We extract the desired libs from hmatrix.cabal (using a cabal flags) | ||
73 | -- and from a posible --configure-option=link:lib1,lib2,lib3 | ||
74 | -- by default the desired libs are gsl lapack. | ||
75 | |||
76 | let pref = if null (words (base ++ " " ++ auxpref)) then "gsl lapack" else auxpref | ||
77 | fullOpts = map ((pref++" ")++) opts | ||
78 | |||
79 | r <- try base fwks fullOpts | ||
80 | case r of | ||
81 | Nothing -> do | ||
82 | putStrLn " FAIL" | ||
83 | putStrLn " *** Sorry, I can't link gsl and lapack." | ||
84 | putStrLn " *** Please make sure that the appropriate -dev packages are installed." | ||
85 | putStrLn " *** You can also specify the required libraries using" | ||
86 | putStrLn " *** cabal install hmatrix --configure-option=link:lib1,lib2,lib3,etc." | ||
87 | writeFile "hmatrix.buildinfo" ("buildable: False\n") | ||
88 | Just ops -> do | ||
89 | putStrLn " OK" | ||
90 | writeFile "hmatrix.buildinfo" ("extra-libraries: " ++ ops++"\n") | ||