#
# Copyright (C) 2007 Bahadir Balban
#

import os
import glob
import sys
from os.path import join
from string import split
from configure import *

config = configuration_retrieve()

project_root = "../.."
headers_root = join(project_root, "include/l4")
config_h = join(headers_root, "config.h")

#libl4 paths
libl4_headers = join(project_root, "tasks/libl4/include")
libl4_libpath = join(project_root, "tasks/libl4")

mm = "mm"
kmalloc = "kmalloc"
memcache = "memcache"
tests = "tests"

mm_dir = mm
kmalloc_dir = kmalloc
memcache_dir = memcache
tests_dir = tests

test_env = Environment(CC = 'gcc -m32',
		  CCFLAGS = ['-g', '-std=gnu99', '-Wall', '-Werror'],
		  ENV = {'PATH' : os.environ['PATH']},
		  LIBS = ['gcc', 'mm', 'km', 'mc'],
		  LIBPATH = ['#'],
		  CPPPATH = ['#include', join(project_root, "include"), "#", libl4_headers])

env = Environment(CC = config.user_toolchain + 'gcc',
		  CCFLAGS = ['-g', '-nostdlib', '-Wall', '-Werror', '-ffreestanding', '-std=gnu99'],
		  LINKFLAGS = ['-nostdlib'],
		  ENV = {'PATH' : os.environ['PATH']},
		  LIBS = 'gcc',
		  CPPPATH = [join(project_root, "include"), "#", libl4_headers])

if os.path.exists(config_h) is False:
	print "\nThis build requires a valid kernel configuration header."
	print "Please run `scons configure' in the kernel root directory."
	print "Choose the `tests' target to build memory allocator tests,"
	print "or any other target for real use.\n"
	sys.exit()

mm_src = glob.glob("%s/*.c" % mm_dir)
kmalloc_src = glob.glob("%s/*.c" % kmalloc_dir)
memcache_src = glob.glob("%s/*.c" % memcache_dir)
tests_src = glob.glob ("%s/*.c" % tests_dir)

if "tests" in COMMAND_LINE_TARGETS:
	print "WARNING!!! Did you configure the kernel with test target first???"
	libmm = test_env.StaticLibrary(mm, mm_src)
	libkmalloc = test_env.StaticLibrary("km", kmalloc_src)
	libmemcache = test_env.StaticLibrary("mc", memcache_src)
	test_prog = test_env.Program("test", tests_src)
	env.Alias("tests", test_prog)
else:
	libmm = env.StaticLibrary(mm, mm_src)
	libkmalloc = env.StaticLibrary("km", kmalloc_src)
	libmemcache = env.StaticLibrary("mc", memcache_src)

