#! /usr/bin/env python
# encoding: utf-8
# mittorn, 2018

VERSION='0.99'
APPNAME='xash3d-fwgs'

top = '.'

def options(opt):
	opt.load('compiler_c')
#	opt.load('msvs')
	opt.add_option('--dedicated', action = 'store_true', help = 'build dedicated server only' )
	opt.add_option('--64bits', dest = 'amd64', action = 'store_true', help = 'build 64-bit engine on x86_64' )


        
def configure(conf):
	conf.load('compiler_c')
	conf.env.append_unique('CFLAGS', '-O2')
	conf.env.append_unique('DEFINES', 'SINGLE_BINARY')
	conf.env.append_value('LINKFLAGS', '-ldl')
	conf.env.append_value('LINKFLAGS', '-lm')
	conf.env.append_value('LINKFLAGS', '-pthread')

	# check for 64-bit builds
	# TODO: check 32-bit toolchain and dependencies
	if conf.env.DEST_CPU == 'x86_64' and not conf.options.amd64:
		conf.env.append_value('LINKFLAGS', '-m32')
		conf.env.append_value('CFLAGS', '-m32')
		print('NOTE: will build engine with 64-bit toolchain using -m32')
	else:
		print('Warning: 64bit engine may be unstable')

	# check for dedicated server build
	if conf.options.dedicated:
		conf.env.append_unique('DEFINES', 'XASH_DEDICATED')
		conf.env.dedicated = True
	else:
		# TODO: add way to specify SDL2 path, move to separate function
		try:
			conf.check_cfg(path='sdl2-config', args='--cflags --libs', package='', msg='Checking for SDL2', uselib_store='SDL2')
		except conf.errors.ConfigurationError:
			conf.fatal('SDL2 not availiable! If you want to build dedicated server, specify --dedicated')
		conf.env.append_unique('DEFINES', 'XASH_SDL')

def build(bld):
	# basic build: dedicated only, no dependencies
	use = [];
	source = bld.path.ant_glob([
		'common/*.c',
		'common/imagelib/*.c',
		'common/soundlib/*.c',
		'common/soundlib/libmpg/*.c',
		'server/*.c'])

	# add client files and sdl2 library
	if not bld.env.dedicated:
		use += ['SDL2']
		source += bld.path.ant_glob([
			'client/*.c',
			'client/vgui/*.c',
			'client/avi/*.c',
			'platform/sdl/*.c'])


	bld(
		target = 'xash',
		features = 'c cprogram',
		includes = ['../common', '../pm_shared', 'common', 'server', 'client', 'client/vgui', '.' ],
		source = source,
		use = use
	)
