#
#	SConstruct file for Kane Utilities 2
#
#	Sat Nov 11 13:57:52 2006
#		J. Anton, Copyright 2006
#

###
#	Initialisation
##
env = Environment(CPPPATH = ["."])
mm_defines = []
com_defines = []

###
#	System settings
##
sysset = ARGUMENTS.get("system", "auto")

###
#	Platform depended variables and actions
###
if env["PLATFORM"] == "win32" and (sysset == "vc" or sysset == "auto"):
	env.Append(CCFLAGS = ["/W3", "/MD", "/Wp64"], \
		CPPDEFINES = ["_CRT_SECURE_NO_DEPRECATE", "WIN32", "VCPP"])
		#LINKFLAGS = ["/SUBSYSTEM:WINDOWS"])
	debug_ccflags = ["/ZI"]
	release_ccflags = ["/O2"]
	release_linkflags = []
	sdl_includes = ["C:\Devel\Microsoft Visual Studio 8\VC\include\SDL"]
elif env["PLATFORM"] == "win32" and sysset == "mingw":
	env.Tool("mingw")
	env.Append(CCFLAGS = ["-Wall"])
	debug_ccflags = ["-g"]
	release_ccflags = ["-O2"]
	release_linkflags = ["-s"]
	sdl_includes = ["C:\Devel\MSYS\mingw\include\SDL"]
elif env["PLATFORM"] == "posix" and sysset == "auto":
	env.Append(CCFLAGS = ["-Wall"])
	debug_ccflags = ["-g"]
	release_ccflags = ["-O2"]
	release_linkflags = ["-s"]
	sdl_includes = ["/usr/include/SDL"]
else:
	print "Unsupported platform: " + env["PLATFORM"] + " (" + sysset + ")"
	Exit()

###
#	Checking header files
###
conf = Configure(env)
if not conf.CheckCHeader("ctype.h"):
	Exit()
if not conf.CheckCHeader("errno.h"):
	Exit()
if not conf.CheckCHeader("libintl.h"):
	env.Append(CPPDEFINES = ["NO_LIBINTL_H"])
if not conf.CheckCHeader("limits.h"):
	Exit()
if not conf.CheckCHeader("stdarg.h"):
	Exit()
if not conf.CheckCHeader("stdint.h"):
	env.Append(CPPDEFINES = ["NO_STDINT_H"])
	if not conf.CheckCHeader("inttypes.h"):
		env.Append(CPPDEFINES = ["NO_INTTYPES_H"])
if not conf.CheckCHeader("stdio.h"):
	Exit()
if not conf.CheckCHeader("stdlib.h"):
	Exit()
if not conf.CheckCHeader("string.h"):
	Exit()
if not conf.CheckCHeader("sys/timeb.h"):
	Exit()
if not conf.CheckCHeader("time.h"):
	Exit()

#	Headers for OPENGL module:
if not conf.CheckCHeader("nvidia/GL/gl.h"):
	mm_defines += ["NO_NVIDIA_GL_H"]
	if not conf.CheckCHeader("GL/gl.h"):
		mm_defines += ["NO_GL_H"]

#	Headers for COM module:
if env["PLATFORM"] == "win32":
	if not conf.CheckCHeader("winsock2"):
		com_defines += ["NO_WINSOCK2_H"]
env = conf.Finish()

###
#	Getting building mode
#	Possible modes:
#		debug - build with debugging
#		release - build with optimisations and witout debuggin
###
mode = ARGUMENTS.get("mode", "debug")
if mode == "debug":
	env.Append(CCFLAGS = debug_ccflags, CPPDEFINES = ["DEBUG"])
	debug = ARGUMENTS.get("debug", "memory,log")
	if "memory" in debug:
		env.Append(CPPDEFINES = ["DEBUG_MEMORY"])
	if "log" in debug:
		env.Append(CPPDEFINES = ["DEBUG_LOG"])
elif mode == "release":
	env.Append(CCFLAGS = release_ccflags)
	env.Append(LINKFLAGS = release_linkflags)
	debug = ""
else:
	print "Invalid mode ( debug|release )"
	Exit()

###
#	Getting list of add-ons to build with.
#	Possible add-ons:
#		gettext - use libintl support
###
use = ARGUMENTS.get("use", "")
notuse = ARGUMENTS.get("notuse", "")
if "gettext" in use and not "gettext" in notuse:
	env.Append(CPPDEFINES = ["USE_GETTEXT"])

###
#	Kane Utilities 2
###
sources = Split("""
	ku2/ku2.c
	other/other.c
	io/log/log.c
	io/cfgreader/cfg.c
	io/dialogue/dialogue.c
	ds/list/dl_list.c
	ds/abtree/abtree.c
	ds/stack/stack.c
	dp/resmanager/res.c
	""")

ku2lib = env.Library("ku2", sources)

###
#	Kane Utilities 2 SDL/OpenGL GUI and multimedia module
###
mm_sources = Split("""
	modules/opengl/gfx/gfx.c
	modules/opengl/gfx/image.c
	modules/opengl/gfx/font.c
	modules/opengl/gui/gui.c
	modules/opengl/widgets/frame.c
	modules/opengl/widgets/button.c
	modules/opengl/widgets/texed.c
	modules/opengl/widgets/textline.c
	""")

env4mm = env.Copy()
env4mm.Append(CPPPATH = sdl_includes, CPPDEFINES = mm_defines)

ku2mm = env4mm.Library("ku2mm", mm_sources)

###
#	Kane Utilities 2 communication module
###
com_sources = Split("""
	modules/com/channel/channel.c
	modules/com/ttp/ttp.c
	""")

env4com = env.Copy()
env4com.Append(CPPDEFINES = com_defines)

ku2com = env4com.Library("ku2com", com_sources)

###
#	Kane Utilities 2 Test programme
###
test_sources = Split("""
	test/ku.c
	""")

#	SDL
#	SDL_image
#	SDL_ttf
#	ku2mm
test_libs = Split("""
	ku2com
	ku2
	""")

env4test = env4mm.Copy()
env4test.Append(LIBS = test_libs, LIBPATH = ["."])

ku2test = env4test.Program("ku2test", test_sources)

###
#	Getting list of modules to build
#	Possible modules:
#		core - build core library (default)
#		mm - build multimedia module (default)
#		com - build communication module
#		test - build the test programme
###
build = ARGUMENTS.get("build", "core,mm,com")
notbuild = ARGUMENTS.get("notbuild", "")
if "core" in build and not "core" in notbuild:
	Default(ku2lib)
if "mm" in build and not "mm" in notbuild:
	Default(ku2mm)
if "com" in build and not "com" in notbuild:
	Default(ku2com)
if "test" in build and not "test" in notbuild:
	Default(ku2test)
