From: <cod...@go...> - 2008-12-08 17:25:00
|
Author: jam...@us... Date: Mon Dec 8 09:22:06 2008 New Revision: 364 Modified: branches/objc2/hoc/HOC.cabal branches/objc2/hoc/Setup.hs Log: Added a cabal flag "ObjC2" and rewrote a bit of Setup.hs to use a build hook rather than a prebuild hook for building the c bits. The changes to Setup.hs allow us to actually use the preprocessor options specified in the cabal file, so that the option triggered by the ObjC2 flag can actually have an effect. Modified: branches/objc2/hoc/HOC.cabal ============================================================================== --- branches/objc2/hoc/HOC.cabal (original) +++ branches/objc2/hoc/HOC.cabal Mon Dec 8 09:22:06 2008 @@ -14,6 +14,10 @@ description: build test cases default: False +Flag ObjC2 + description: build for Objective-C 2.0 + default: False + Library build-depends: base, template-haskell, unix @@ -55,12 +59,16 @@ extra-libraries: objc, ffi if os(darwin) + include-dirs: /usr/include/ffi frameworks: Foundation cpp-options: -DMACOSX else -- paths are inserted by Setup.hs extra-libraries: gnustep-base cpp-options: -DGNUSTEP + + if flag(ObjC2) + cpp-options: -D__OBJC2__ Executable hoc-ifgen Modified: branches/objc2/hoc/Setup.hs ============================================================================== --- branches/objc2/hoc/Setup.hs (original) +++ branches/objc2/hoc/Setup.hs Mon Dec 8 09:22:06 2008 @@ -1,6 +1,8 @@ import Distribution.Simple import Distribution.PackageDescription +import Distribution.Simple.Build import Distribution.Simple.Setup +import Distribution.Simple.PreProcess import Distribution.Simple.Configure import Distribution.Simple.LocalBuildInfo import System.Cmd( system ) @@ -13,9 +15,18 @@ main = defaultMainWithHooks $ simpleUserHooks { confHook = customConfig, - preBuild = customPreBuild + buildHook = customBuild } - + +-- You probably don't need to change this, but if you do beware that +-- it will not be sanitized for the shell. +cbitsObjectFile = "dist/build/HOC_cbits.o" + +needsCBitsWhileBuilding :: Executable -> Bool +needsCBitsWhileBuilding e + | exeName e == "hoc-test" = True + | otherwise = False + backquote :: String -> IO String backquote cmd = do (inp,out,err,pid) <- runInteractiveCommand cmd @@ -52,48 +63,56 @@ return lbi -customPreBuild :: Args -> BuildFlags -> IO HookedBuildInfo -customPreBuild args buildFlags = do - putStrLn "Compiling HOC_cbits..." - system "mkdir -p dist/build/" +customBuild :: PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO () +customBuild pd lbi hooks buildFlags = do + let Just libInfo = library pd - (cflags, paths, extralibs) <- - if System.Info.os == "darwin" - then do - return ("-I/usr/include/ffi -DMACOSX", [], ["-framework Foundation"]) - else do - (gcclibdir, system_libs, system_headers) <- gnustepPaths - ffi_cflags <- backquote "pkg-config libffi --cflags" - return ("-I" ++ system_headers ++ " -DGNUSTEP" ++ " " ++ ffi_cflags, - ["-L" ++ gcclibdir, "-L" ++ system_libs], - ["-lgnustep-base"]) + extraFlags <- buildCBits (libBuildInfo libInfo) + + let hooked_pd = pd + { library = Just $ libInfo + { libBuildInfo = addCompilerFlags extraFlags + (libBuildInfo libInfo) + } + , executables = alterExecutable needsCBitsWhileBuilding + (\exe -> exe {buildInfo = addCompilerFlags extraFlags (buildInfo exe)}) + (executables pd) + } + + build hooked_pd lbi buildFlags knownSuffixHandlers +-- |Build HOC_cbits.o using the flags specified in the configuration +-- stage, and return a list of flags to add to support usage of +-- template-haskell while compiling (for both the library and the +-- hoc-test executable) +buildCBits :: BuildInfo -> IO [(CompilerFlavor, [String])] +buildCBits buildInfo = do + putStrLn "Compiling HOC_cbits..." + system ("mkdir -p " ++ takeDirectory cbitsObjectFile) + + let cflags = cppOptions buildInfo ++ ccOptions buildInfo + ++ ["-I" ++ dir | dir <- includeDirs buildInfo] + extraGHCflags = [cbitsObjectFile] + ++ ["-l" ++ lib | lib <- extraLibs buildInfo] + ++ ["-framework " ++ fw | fw <- frameworks buildInfo] + exitCode <- system $ "gcc -r -nostdlib -I`ghc --print-libdir`/include " - ++ cflags ++ " HOC_cbits/*.m -o dist/build/HOC_cbits.o" - + ++ unwords cflags + ++ " HOC_cbits/*.m -o " ++ cbitsObjectFile + case exitCode of ExitSuccess -> return () _ -> fail "Failed in C compilation." - -- system "cp dist/build/HOC_cbits.o dist/build/HOC_cbits.dyn_o" - system "cp dist/build/HOC_cbits.o dist/build/hoc-test/hoc-test-tmp/" - - let buildInfo = emptyBuildInfo { - options = [ (GHC, ["dist/build/HOC_cbits.o" ] - ++ paths ++ - ["-lobjc", - "-lffi"] - ++ extralibs) ], - cSources = ["HOC_cbits.o"] - } - buildInfo2 = emptyBuildInfo { - options = [ (GHC, ["dist/build/hoc-test/hoc-test-tmp/HOC_cbits.o" ] - ++ paths ++ - ["-lobjc", - "-lffi"] - ++ extralibs) ]{-, - cSources = ["HOC_cbits.o"]-} - } - - return (Just buildInfo, [("hoc-test", buildInfo2)]) + return [(GHC, extraGHCflags)] + +-- TODO: check whether it's OK for the options field to have multiple +-- entries for the same "compiler flavor" +addCompilerFlags :: [(CompilerFlavor,[String])] -> BuildInfo -> BuildInfo +addCompilerFlags flags buildInfo = buildInfo { + options = flags ++ options buildInfo + } +alterExecutable :: (Executable -> Bool) -> (Executable -> Executable) + -> [Executable] -> [Executable] +alterExecutable p f exes = [if p exe then f exe else exe | exe <- exes] \ No newline at end of file |