# Q3 ui building

# qvm building against native:
# only native has ui_syscalls.c
# qvm uses a ui_syscalls.asm with equ stubs
# qvm has additional bg_lib.c

Import qw( BASE_CFLAGS TARGET_DIR INSTALL_DIR NO_VM NO_SO );

$env = new cons(
  # the code has the very bad habit of doing things like #include "../ui/ui_shared.h"
  # this seems to confuse the dependency analysis, explicit toplevel includes seem to fix
  CPPPATH => '#cgame:#game:#q3_ui:#ui',
  CC => 'gcc',
  CFLAGS => $BASE_CFLAGS . '-fPIC',
  LDFLAGS => '-shared -ldl -lm'
);

# for TA, use -DMISSIONPACK
%ta_env_hash = $env->copy(
  CPPPATH => '#cgame:#game:#q3_ui:#ui'
  );
$ta_env_hash{CFLAGS} = '-DMISSIONPACK ' . $ta_env_hash{CFLAGS};
$ta_env = new cons(%ta_env_hash);


# qvm building
# we heavily customize the cons environment
$vm_env = new cons(
  # the code has the very bad habit of doing things like #include "../ui/ui_shared.h"
  # this seems to confuse the dependency analysis, explicit toplevel includes seem to fix
  CPPPATH => '#cgame:#game:#q3_ui:#ui',
  CC => 'q3lcc',
  CCCOM => '%CC %CFLAGS %_IFLAGS -c %< -o %>',
  SUFOBJ => '.asm',
  LINK => 'q3asm',
  CFLAGS => '-DQ3_VM -S -Wf-target=bytecode -Wf-g',
  ENV => { PATH => $ENV{PATH} . ":/usr/local/bin", },
);

# TA qvm building
%vm_ta_env_hash = $vm_env->copy(
  CPPPATH => '#cgame:#game:#q3_ui:#ui'
  );
$vm_ta_env_hash{CFLAGS} = '-DMISSIONPACK ' . $vm_ta_env_hash{CFLAGS};
$vm_ta_env = new cons(%vm_ta_env_hash);

# the file with vmMain function MUST be the first one of the list
@FILES = qw(
ui_main.c
ui_cdkey.c
ui_ingame.c
ui_players.c
ui_serverinfo.c
ui_confirm.c
ui_setup.c
../game/bg_misc.c
../game/q_math.c
../game/q_shared.c
ui_gameinfo.c
ui_dynamicmenu.c
ui_ingame_mapvote.c
ui_startserver_bot.c
ui_startserver_botsel.c
ui_startserver_common.c
ui_startserver_custommaps.c
ui_startserver_data.c
ui_startserver_items.c
ui_startserver_items_old.c
ui_startserver_map.c
ui_startserver_mapsel.c
ui_startserver_script.c
ui_startserver_server.c
ui_startserver_battle.c
ui_loadconfig.c
ui_saveconfig.c
ui_atoms.c
ui_connect.c
ui_controls2.c
ui_demo2.c
ui_mfield.c
ui_credits.c
ui_menu.c
ui_options.c
ui_display.c
ui_sound.c
ui_network.c
ui_playermodel.c
ui_playersettings.c
ui_preferences.c
ui_qmenu.c
ui_servers2.c
ui_sparena.c
ui_specifyserver.c
ui_splevel.c
ui_sppostgame.c
ui_team.c
ui_video.c
ui_cinematics.c
ui_spskill.c
ui_addbots.c
ui_removebots.c
ui_teamorders.c
ui_mods.c
bg_ui.c
  );
$FILESREF = \@FILES;
# FIXME CPU string?
# NOTE: $env $ta_env and $vm_env $vm_ta_env may not be necessary
#   we could alter the $env and $ta_env based on $TARGET_DIR
#   doing it this way to ensure homogeneity with cgame building
if ($TARGET_DIR eq 'Q3')
{
	if ($NO_SO eq 0)
	{
		Program $env 'uii386.so', @$FILESREF, 'ui_syscalls.c';
		Install $env $INSTALL_DIR, 'uii386.so';
	}
	if ($NO_VM eq 0)
	{
		Program $vm_env 'ui.qvm', @$FILESREF, '../game/bg_lib.c', '../ui/ui_syscalls.asm';
		Install $vm_env $INSTALL_DIR . '/vm', 'ui.qvm';
	}
}
else
{
	if ($NO_SO eq 0)
	{
		Program $ta_env 'uii386.so', @$FILESREF, 'ui_syscalls.c';
		Install $ta_env $INSTALL_DIR, 'uii386.so';
	}
	if ($NO_VM eq 0)
	{
		Program $vm__ta_env 'ui.qvm', @$FILESREF, '../game/bg_lib.c', '../ui/ui_syscalls.asm';
		Install $vm_ta_env $INSTALL_DIR . '/vm', 'ui.qvm';
	}
}
