Import('context')

import os

SConscript('all.scons', exports = ['context']);

# config = env.Configure()

# env = config.Finish()

flags = [ '-g', '-Wall', '-Wno-unused', '-O2', '-funroll-loops', '-ffast-math', '-fomit-frame-pointer' ]
includes = [ "include", "include/allegro", "." ]
# defines = [ "-DALLEGRO_SRC", "-DALLEGRO_NO_ASM", "-DALLEGRO_LIB_BUILD" ]
defines = [ "ALLEGRO_SRC", "ALLEGRO_LIB_BUILD" ]

env = context.getLibraryEnv()

env.Append(CCFLAGS = flags)
env.Append(CPPDEFINES = defines)
env.Append(CPPPATH = includes)

# assembler = Builder(action = 'gcc --x assembler-with-cpp $CCFLAGS $SOURCES -c -o $TARGET', src_suffic = '.s', suffic = '.o')

AssemblerAction = Action( 'gcc -x assembler-with-cpp $CCFLAGS $_CPPINCFLAGS $SOURCES -c -o $TARGET' )

import SCons.Tool

static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
# static_obj.src_builder.append('assembler')
# shared_obj.src_builder.append('assembler')
# shared_obj.add_action('.s', SCons.Defaults.ASAction )
shared_obj.add_action('.s', AssemblerAction )
shared_obj.add_emitter('.s', SCons.Defaults.SharedObjectEmitter )

# env.Append(BUILDERS = { 'Assembler' : assembler })

env.Append(LIBS = [ 'kernel32', 'user32', 'gdi32', 'comdlg32', 'ole32', 'dinput', 'ddraw', 'dxguid', 'winmm', 'dsound' ])

# files += sourceFiles('c', ['cmiscs.s'])
# f = sourceFiles('c', ['cmiscs.s'])
# for i in f:
#	files += [env.Assembler(i)]

context.addFiles('c',Split("""
"""));
# cmiscs.s

context.addFiles('i386', Split("""
iblit8.s
iblit16.s
iblit24.s
iblit32.s
icpu.c
icpus.s
icsprite.c
igfx8.s
igfx15.s
igfx16.s
igfx24.s
igfx32.s
imisc.s
iscan.s
iscanmmx.s
ispr8.s
ispr15.s
ispr16.s
ispr24.s
ispr32.s
istretch.c
izbuf.s
"""));

context.addFiles('misc',Split("""
icolconv.s
"""));

context.addFiles('win', Split("""
asmlock.s
gdi.c
wddaccel.c
wddbmp.c
wddbmpl.c
wddfull.c
wddlock.c
wddmode.c
wddovl.c
wddraw.c
wddwin.c
wdispsw.c
wdsinput.c
wdsndmix.c
wdsound.c
wdxver.c
wfile.c
wgdi.c
wgfxdrv.c
winput.c
wjoydrv.c
wjoydxnu.c
wkeybdnu.c
wmidi.c
wmouse.c
wsnddrv.c
wsndwo.c
wsystem.c
wthread.c
wtime.c
wtimernu.c
wwnd.c
wxthread.c
"""));

## crappy hack
file = open('obj/mingw32/asmcapa.h', "w")
file.write("#define ALLEGRO_GENERATED_BY_MAKEFILE_TST\n")
file.write("#define ALLEGRO_MMX\n")
file.write("#define ALLEGRO_SSE\n")
file.close()

cat = Builder(action = 'cat $SOURCES > $TARGET')
env.Append(BUILDERS = {'Cat' : cat})

context.setExampleEnv(env)
