# SConscript for Altogether
# $Id: SConscript 421 2004-06-16 08:09:49Z eric $
# Copyright 2004, 2005, 2007 Eric L. Smith <eric@brouhaha.com>

# Altogether is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.  Note that I am not
# granting permission to redistribute or modify Altogether under the
# terms of any later version of the General Public License.

# Altogether 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 (in the file "COPYING"); if not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111, USA.

Import('build_env native_env')
Import('source_release_dir snapshot_dir')

#-----------------------------------------------------------------------------
# cross-platform
#-----------------------------------------------------------------------------

build_target_only = build_env ['build_target_only']

cross = build_env ['PLATFORM'] != build_env ['target']

ming = build_env ['target'] == 'win32'

if cross and ming and build_target_only:
    pkg_config_cmd = 'PKG_CONFIG_PATH=/usr/local/gtkwin/lib/pkgconfig pkg-config --define-variable=includedir=/usr/local/gtkwin/include --define-variable=libdir=/usr/local/gtkwin/lib'
else:
    pkg_config_cmd = 'pkg-config'
pkg_config_cmd += ' --cflags --libs '

build_env.Append (CCFLAGS = ['-std=gnu99', '-Wall', '-Wextra'])

build_env.Append (CPPDEFINES = {'NATIVE_DISPLAY_LSB_LEFT' : 1})
build_env.Append (CPPDEFINES = {'NATIVE_DISPLAY_MIN_IS_BLACK' : 1})

if build_env ['debug']:
        build_env.Append (CCFLAGS = ['-g'])
else:
        build_env.Append (CCFLAGS = ['-O2'])

if cross and ming and build_target_only:
        build_env.Replace (CC = '/usr/local/bin/i386-mingw32-gcc')
        build_env.Append (CCFLAGS = ['-mms-bitfields'])

if ming and (build_target_only or not cross):
    build_env.Append (CPPDEFINES = [('MINGW', 1),
                                    ('MS_WINDOWS', 1)])

#-----------------------------------------------------------------------------
# common components
#-----------------------------------------------------------------------------

srcs = Split ("""main.c gui.c
                 priority_queue.c scheduler.c
		 debug.c
                 cpu.c emulator.c main_mem.c mem_refresh.c ram_related.c
		 display.c
		 disk.c
                 ethernet.c breath.c
                 util.c""")

packages = "gtk+-2.0 libgsf-1"

build_env.ParseConfig (pkg_config_cmd + packages)

build_env.Append (CPPDEFINES = [('DEFAULT_PATH', build_env ['libdir'])])

if build_env ['use_tcl']:
    build_env.Append (CPPDEFINES = {'USE_TCL': 1})
    build_env.Append (LIBS = 'tcl')

if build_env ['use_readline']:
    build_env.Append (CPPDEFINES = {'USE_READLINE': 1})
    build_env.Append (LIBS = Split ("readline history termcap"))

if build_env ['use_gui']:
    build_env.Append (CPPDEFINES = {'USE_GUI' : 1})

if build_env ['use_benchmark']:
    build_env.Append (CPPDEFINES = {'USE_BENCHMARK' : 1})

objs = [build_env.Object (src) for src in srcs]

altogether = build_env.Program (target = 'altogether',
                                source = objs)

#-----------------------------------------------------------------------------
# default targets
#-----------------------------------------------------------------------------

default_targets = [altogether]

if (not cross) or build_target_only:
    Default (default_targets)

#-----------------------------------------------------------------------------
# install
#-----------------------------------------------------------------------------

if build_env ['build_target_only'] == 0:
    native_env.Alias (target = 'install',
                      source = native_env.Install (dir = native_env ['destdir'] + native_env ['bindir'],
                                                   source = default_targets))

#-----------------------------------------------------------------------------
# source tarball
#-----------------------------------------------------------------------------

if build_env ['build_target_only'] == 0:
    headers = Split ("""alto.h breath.h cpu.h debug.h disk.h display.h
                        emulator.h ethernet.h gui.h main.h main_mem.h
                        mem_refresh.h priority_queue.h ram_related.h
                        scheduler.h util.h""")

    misc = Split ("""SConscript""")

    dist_files = (misc + headers + srcs)

    native_env.Distribute (source_release_dir, dist_files)

    native_env.Distribute (snapshot_dir, dist_files)

