# vim: filetype=python

#  Network Simulation Cradle
#  Copyright (C) 2003-2005 Sam Jansen
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
#
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
#  more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc., 59
#  Temple Place, Suite 330, Boston, MA 02111-1307 USA

# $Id: SConscript 1665 2008-01-10 20:42:26Z stj2 $
import glob, os
import os.path

curdir = Dir('.').path + '/'

# Input files and dirs 
input_files = [] #glob.glob('support/*.c')

#input_files = [x for x in input_files if x != 'support/globals.c'
#                                      and x != 'support/globals.parsed.c']

dirs_to_process = [ 'support', 'kern', 'netinet', 'net', 'netinet6', 
    'netkey', 'crypto', 'crypto/des', 'crypto/cast128', 'crypto/blowfish',
    'crypto/rc4', 'crypto/rijndael', 'crypto/sha2',
    'freebsd/usr/obj/usr/src/sys/GENERIC',
    ]

# ----
# Find files to process: all the *.c files in dirs_to_process minus the
# files ending in .parsed.c
processed_files = []
for i in dirs_to_process: processed_files += glob.glob(i + '/*.c')
processed_files = [x for x in processed_files if x[-8:] != 'parsed.c']

processed_files.remove('support/malloc.c')
input_files.append('support/malloc.c')


FREEBSD_SRC_ROOT = 'freebsd'
include_path = [ 
    '#/-',
    'override', '.',
    FREEBSD_SRC_ROOT + '/usr/src/sys',
    FREEBSD_SRC_ROOT + '/usr/src/sys/dev',
    FREEBSD_SRC_ROOT + '/usr/src/sys/contrib/dev/acpica',
    FREEBSD_SRC_ROOT + '/usr/src/sys/contrib/ipfilter',
    FREEBSD_SRC_ROOT + '/usr/obj/usr/src/sys/GENERIC',
    FREEBSD_SRC_ROOT + '/usr/obj/usr/src/sys/VEGAS-SMP',
    FREEBSD_SRC_ROOT + '/usr/obj/usr/src/sys/VEGAS',
    FREEBSD_SRC_ROOT + '/usr/include',
    '../sim',
    '#//usr/include' ]

cflags = '-Dmalloc=nsc_malloc -Dfree=nsc_free -Drealloc=nsc_realloc ' \
    + '-nostdinc -Dcalloc=nsc_calloc -Dlog=nsc_log -Dprintf=nsc_printf ' \
    + '-D__FreeBSD__=5 ' \
    + '-D_KERNEL -include ' + curdir + FREEBSD_SRC_ROOT \
    + '/usr/obj/usr/src/sys/GENERIC/opt_global.h ' \
    + '-fno-common -ffreestanding -Wall '

# Removed:
#   -mpreferred-stack-boundary=2 

# For FreeBSD 5.3: requires c99
cflags += ' -std=c99 '

ext_cflags = ''
linkflags = '-Wl,-O1  '

uname = os.uname()

if uname[4] == 'x86_64':
    pass
    #ext_cflags += '-m32 '
    #linkflags += '-m32 '

if uname[0] == 'Linux':
    cflags += '-DLINUX_PORT ';

cc34 = 'gcc-3.4'
cxx34 = 'g++-3.4'

cflags += ext_cflags

cxxflags = '-Wall ' + ext_cflags

pflags = ' -O '
if ARGUMENTS.has_key('optimise') and int(ARGUMENTS['optimise']):pflags += ' -O'
if ARGUMENTS.has_key('profile') and int(ARGUMENTS['profile']): pflags += ' -pg'
if ARGUMENTS.has_key('debug') and not int(ARGUMENTS['debug']): pass
else: pflags += ' -g -O '

cflags += pflags + ' '
cxxflags += pflags

Import('default_env')

env = default_env.Copy(CCFLAGS = cflags, CPPPATH = include_path, CC = cc34, 
        CXX = cxx34, GLB_LIST = curdir + '/global_list_pre.txt')
cppenv = Environment(CCFLAGS = cxxflags, CPPPATH = [ '../sim' ], CC = cc34, CXX = cxx34)

# Tried gcc-3.0 on debian woody, but it seemed buggy?
#env['CC'] = 'gcc-3.0'

parser_srcs = []
for i in processed_files:
    if 1:
        output = os.path.splitext(i)[0]+'.parsed.c'
        parser_srcs.append( env.Parser(output, i) )
        # Make sure the parser is built so we can actually create the output
        # file.
        env.Depends(output, '#' + default_env['GLOBALISER'])
    else:
        parser_srcs.append( i )

# The sim_support.cpp file is not built in the normal 'kernel' build
# environment. It provides the interface between simulation and the 'kernel'
# stuff.
# To give it different flags we use the cppenv construction environment
sim_support = cppenv.SharedObject('support/sim_support.cpp')

# The final output: shared library
so = env.SharedLibrary('freebsd5.3', 
    input_files + parser_srcs + [sim_support], 
    CPPPATH = include_path,
    CXXFLAGS = cxxflags,
    LINKFLAGS = linkflags,
    CC = cc34, CXX = cxx34
)

# We can create a program here for testing, it will allow us to see a full
# list of undefined references. This is useful when updating to a new version
# of FreeBSD.
#prog = env.Program('freebsd5', 
#    input_files + parser_srcs + [sim_support], 
#    CPPPATH = include_path,
#    CXXFLAGS = cxxflags,
#    LINKFLAGS = "-Wl,-O1 -ldl -m32 "
#)

env.Install(dir = '..', source = so)

