1 -----------------------------------------------------------------------------
2 -- $Id: PreProcess.hs,v 1.1 2000/10/11 11:54:58 simonmar Exp $
4 -- Pre-process source files
6 -- (c) The University of Glasgow 2000
8 -----------------------------------------------------------------------------
11 preprocess -- :: FilePath -> IO FilePath
20 -----------------------------------------------------------------------------
21 -- preprocess takes a haskell source file and generates a raw .hs
22 -- file. This involves passing the file through 'unlit', 'cpp', or both.
24 preprocess :: FilePath -> IO FilePath
25 preprocess filename = do
26 let (basename, suffix) = splitFilename filename
28 unlit_file <- unlit filename
29 cpp_file <- cpp unlit_file
32 -------------------------------------------------------------------------------
35 unlit :: FilePath -> IO FilePath
37 | suffix /= unlitInputExt = return input_fn
39 do output_fn <- newTempName cppInputExt
40 unlit <- readIORef pgm_L
41 unlit_flags <- getOpts opt_L
42 run_something "Literate pre-processor"
43 ("echo '# 1 \"" ++input_fn++"\"' > "++output_fn++" && "
44 ++ unlit ++ ' ':input_fn ++ " - >> " ++ output_fn)
47 (filename, suffix) = splitFilename input_fn
49 -------------------------------------------------------------------------------
52 cpp :: FilePath -> IO FilePath
54 = do src_opts <- getOptionsFromSource input_fn
55 _ <- processArgs dynamic_flags src_opts []
57 output_fn <- newTempName hscInputExt
59 do_cpp <- readState cpp_flag
63 cpp <- readIORef pgm_P
64 hscpp_opts <- getOpts opt_P
65 hs_src_cpp_opts <- readIORef hs_source_cpp_opts
67 cmdline_include_paths <- readIORef include_paths
68 pkg_include_dirs <- getPackageIncludePath
69 let include_paths = map (\p -> "-I"++p) (cmdline_include_paths
73 run_something "C pre-processor"
75 (["echo '{-# LINE 1 \"" ++ input_fn ++ "\" -}'", ">", output_fn, "&&",
80 ++ [ "-x", "c", input_fn, ">>", output_fn ]
83 run_something "Ineffective C pre-processor"
84 ("echo '{-# LINE 1 \"" ++ input_fn ++ "\" -}' > "
85 ++ output_fn ++ " && cat " ++ input_fn
86 ++ " >> " ++ output_fn)
89 -----------------------------------------------------------------------------
92 splitFilename :: String -> (String,String)
93 splitFilename f = (reverse (stripDot rev_basename), reverse rev_ext)
94 where (rev_ext, rev_basename) = span ('.' /=) (reverse f)
95 stripDot ('.':xs) = xs