import os
import SCons

Import('context')

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

env = context.getLibraryEnv()

context.setLibraryDir("lib/macosx")

file = open( "include/allegro/platform/alplatf.h", "w" )
file.write( "#define ALLEGRO_MACOSX" )
file.close()

## These lines tell SCons to treat .m files like .cpp files
static_obj, shared_obj = SCons.Tool.createObjBuilders( env )
shared_obj.add_action( '.m', SCons.Defaults.ShCAction )
shared_obj.add_emitter( '.m', SCons.Defaults.SharedObjectEmitter )

env[ 'SHCCFLAGS' ] = SCons.Util.CLVar( '$CCFLAGS' )

context.addFiles('macosx/', Split("""
cadigi.m
camidi.m
drivers.m
hidjoy.m
hidman.m
keybd.m
pcpu.m
qtmidi.m
quartz.m
qzfull.m
qzmouse.m
qzwindow.m
soundman.m
system.m
"""));

context.addFiles("unix", Split("""
ufile.c
usystem.c
uthreads.c
utime.c
utimernu.c
uxthread.c
"""));


context.addFiles('c/', Split("""
cblit16.c
cblit24.c
cblit32.c
cblit8.c
ccpu.c
ccsprite.c
cgfx15.c
cgfx16.c
cgfx24.c
cgfx32.c
cgfx8.c
cmisc.c
cscan15.c
cscan16.c
cscan24.c
cscan32.c
cscan8.c
cspr15.c
cspr16.c
cspr24.c
cspr32.c
cspr8.c
cstretch.c
czscan15.c
czscan16.c
czscan24.c
czscan32.c
czscan8.c
""" ));

#mainEnv = Environment()
#mainLibrary = mainEnv.StaticLibrary( 'alleg-main', 'src/macosx/main.m' )
#mainEnv.Install(context.getLibraryDir(),mainLibrary)

context.addLibrary("alleg-main")

# gcc -DALLEGRO_SRC -DALLEGRO_LIB_BUILD -Wall -Wno-unused -Wno-long-double  -O2 -funroll-loops -ffast-math -fomit-frame-pointer -fno-common -pipe -dynamic -DALLEGRO_NO_ASM -I. -I./include -o obj/macosx/alleg/main.o -c src/macosx/main.m
# ar rs lib/macosx/liballeg-main.a obj/macosx/alleg/main.o

def buildMain(env,appendDir,buildDir,libDir):
    fenv = env.Copy()
    fenv.Replace(LIBS = [])
    # fenv.Append(CCFLAGS = ['-DALLEGRO_SRC'])
    fenv.Append(CPPDEFINES = 'ALLEGRO_SRC')
    lib = fenv.StaticLibrary('alleg-main',appendDir(buildDir + '/macosx/', ['main.m']))
    fenv.Install(libDir, lib)
    return lib

#env = context.getLibraryEnv()
#mainLib = env.StaticLibrary( 'alleg-main', 'build/release/macosx/main.m' )

context.addExtra(buildMain)

exampleEnv = Environment()

flags = [ "-Wall", "-Wno-unused", "-Wno-long-double", "-O2", "-funroll-loops", "-ffast-math", "-fomit-frame-pointer", "-fno-common", "-pipe", "-dynamic" ]
defines = [ "ALLEGRO_NO_ASM", "ALLEGRO_LIB_BUILD" ]
includes = [ "include", "include/allegro" ]
linkflags = ["-prebind", "-framework", "Cocoa"]
libpath = [ "lib/macosx" ]
libs = [ 'alleg-main' ]

# gcc -s -prebind -o examples/exupdate obj/macosx/alleg/exupdate.o lib/macosx/liballeg-4.3.0.dylib lib/macosx/liballeg-main.a -framework Cocoa

exampleEnv.Append(CCFLAGS = flags)
exampleEnv.Append(CPPDEFINES = defines)
exampleEnv.Append(CPPPATH = includes)
# exampleEnv.Append(LIBPATH = libpath)
exampleEnv.Append(LINKFLAGS = linkflags)
exampleEnv.Append(LIBS = libs)

context.setExampleEnv( exampleEnv )

# gcc -DALLEGRO_LIB_BUILD -Wall -Wno-unused -Wno-long-double  -O2 -funroll-loops -ffast-math -fomit-frame-pointer -fno-common -pipe -dynamic -DALLEGRO_NO_ASM -I. -I./include -o obj/macosx/alleg/exupdate.o -c examples/exupdate.c

flags = ["-g", "-Wall", "-Wno-unused", "-O2", "-funroll-loops", "-fno-common", "-ffast-math", "-fomit-frame-pointer", "-Wno-long-double", "-pipe", "-dynamic" ]
includes = ["include", "include/allegro", "."]
defines = ["-DHAVE_CONFIG_H", "-DALLEGRO_NO_ASM", "-DALLEGRO_LIB_BUILD", "-DALLEGRO_SHARED", "-DALLEGRO_SRC" ]
linkflags = [ "-prebind", "-seg1addr", "0x30000000", "-compatibility_version", str(context.getAllegroVersion()), "-current_version", str(context.getAllegroVersion()), "-install_name", "liballeg-" + str(context.getAllegroVersion()) + ".dylib" ]


def addFramework( name ):
	linkflags.append( "-framework" )
	linkflags.append( name )

addFramework( "Cocoa" )
addFramework( "Carbon" )
addFramework( "IOKit" )
addFramework( "System" )
addFramework( "CoreAudio" )
addFramework( "AudioUnit" )
addFramework( "AudioToolbox" )
addFramework( "QuickTime" )	

env.Append(CCFLAGS = flags)
env.Append(CCFLAGS = defines)
env.Append(CPPPATH = includes)
env.Append(LINKFLAGS = linkflags)

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