import subprocess

cflags   = ARGUMENTS.get('CFLAGS', '-Wall -Wextra -O2 -fomit-frame-pointer')
cxxflags = ARGUMENTS.get('CXXFLAGS', cflags + ' -fno-exceptions -fno-rtti')
vars = Variables()
vars.Add('CC')
vars.Add('CXX')

env = Environment(CPPPATH = ['src', '../libgambatte/include', '../common'],
                  CFLAGS = cflags,
                  CXXFLAGS = cxxflags,
                  CPPDEFINES = [ 'HAVE_STDINT_H', None ],
                  variables = vars)
env.ParseConfig('sdl-config --cflags --libs')

sourceFiles = Split('''
			src/audiosink.cpp
			src/blitterwrapper.cpp
			src/parser.cpp
			src/sdlblitter.cpp
			src/str_to_sdlkey.cpp
			src/usec.cpp
			../common/adaptivesleep.cpp
			../common/resample/src/chainresampler.cpp
			../common/resample/src/i0.cpp
			../common/resample/src/kaiser50sinc.cpp
			../common/resample/src/kaiser70sinc.cpp
			../common/resample/src/makesinckernel.cpp
			../common/resample/src/resamplerinfo.cpp
			../common/resample/src/u48div.cpp
			../common/rateest.cpp
			../common/skipsched.cpp
			../common/videolink/rgb32conv.cpp
			../common/videolink/vfilterinfo.cpp
			../common/videolink/vfilters/catrom2x.cpp
			../common/videolink/vfilters/catrom3x.cpp
			../common/videolink/vfilters/kreed2xsai.cpp
			../common/videolink/vfilters/maxsthq2x.cpp
			../common/videolink/vfilters/maxsthq3x.cpp
			../libgambatte/libgambatte.a
		   ''')

conf = env.Configure()
conf.CheckLib('z')
conf.Finish()

version_str_def = []
if Dir('../.git').exists():
	try:
		git_revno = subprocess.check_output("git rev-list HEAD --count", shell=True).strip()
		version_str_def = [ 'GAMBATTE_SDL_VERSION_STR', r'\"r' + git_revno + r'\"' ]
	except subprocess.CalledProcessError:
		pass

env.Program('gambatte_sdl',
            [env.Object('src/gambatte_sdl.cpp',
                        CPPDEFINES = env['CPPDEFINES'] + [version_str_def])]
            + sourceFiles)
