
-- | Compiler configuration for the freebsd-x86 target
--	Everything specific to this target should be in this file.
--
module Config.Config where
import DDC.Main.Arg


-- | Pointer size in bits and bytes.
pointerBytes, pointerBits :: Int
pointerBytes = 4
pointerBits = pointerBytes * 8


-- | The extension to use for a shared object
extSharedObject
	= "so"


-- | The system command to compile a Sea source file.
makeSeaCompileCmd
	:: [Arg]		-- ^ ddc command line args
	-> FilePath		-- ^ path to source c file
	-> FilePath		-- ^ path to output o file
	-> FilePath		-- ^ path to runtime system
	-> FilePath		-- ^ path to base libraries
	-> [FilePath]		-- ^ extra include dirs
	-> [String]		-- ^ extra flags to compile with, from .build file
	-> String		-- ^ the compile command

makeSeaCompileCmd
	args pathC pathO pathRuntime pathLibrary
	extraIncludeDirs extraCompileFlags

	=  "gcc"
	++ " -Werror"
	++ " -std=c99"
	++ " -D BITS=" ++ show pointerBits

	++ " -c " ++ pathC
	++ " -o " ++ pathO

	++ (if elem Debug args
		then " -g"
		else "")

	++ (if elem OptAll args
		then " -O3"
		else "")

	++ (if elem Profile args
		then " -pg"
		else "")

	++ " -I."
	++ " -I/usr/local/include"
	++ " -I"  ++ (take (length pathRuntime - length "/runtime") pathRuntime)
	++ " -I"  ++ pathLibrary

	++ concat [ " -I" ++ p | p <- extraIncludeDirs ]
	++ concat [ " "   ++ f | f <- extraCompileFlags ]


-- | The system command to link objects into an executable.
makeLinkCmd
	:: [Arg]		-- ^ ddc command line args
	-> [FilePath]		-- ^ objects to link
	-> FilePath		-- ^ path of output binary
	-> FilePath		-- ^ path to the runtime system
	-> [FilePath]		-- ^ extra libs to link with
	-> [FilePath]		-- ^ extra lib dirs to search
	-> String

makeLinkCmd
	args objects outFile pathRuntime extraLibs extraLibDirs

	= "gcc"
	++ " -std=c99"

	++ " -o " ++ outFile

	++ (if elem Profile args
		then " -pg"
		else "")

	++ " " ++ concat [" " ++ obj | obj <- objects]

	++ (if elem StaticRuntime args
		then " " ++ pathRuntime ++ "/libddc-runtime.a"
		else " " ++ pathRuntime ++ "/libddc-runtime.so")

	++ " -lm"
	++ " -L/usr/local/lib"
	++ " " ++ concat [" -L" ++ dir | dir <- extraLibDirs]
	++ " " ++ concat [" -l" ++ lib | lib <- extraLibs]


-- | Command to run the LLVM compiler
llcCommand :: String
llcCommand = "llc -march=x86"


-- | Command to assemble a file.
asCommand :: String
asCommand  = "as "

