source ./configure-lib.sh

variable builddir build

rule copy --command "cp \$in \$out"

rule re2c --command "re2c \$flags -o \$out \$in"

rule lemon --command "lemon -d\$builddir \$in"

DEBUG_FLAGS="--cflag -g"

# TODO, why is this not accepting an argument with strings?
per_platform library ___-tabula \
  --include-directory tabula/include \
  --cflag -Wall \
  --cflag -Werror \
  --cflag -Wno-unused-function \
  --cflag -Wno-unused-value \
  --cflag -pthread \
  --lflag -lpthread \
  $DEBUG_FLAGS \
  tabula/src/tabula.c

per_platform library ___-libuv     --pkg-config libuv
per_platform library ___-sqlite3   --pkg-config sqlite3

library lnx-glfw3 --platform lnx --pkg-config glfw3 --lflag -lGL
library win-glfw3 --platform win --pkg-config glfw3 --lflag -lgdi32 --lflag -lssp

executable alias-gen_geometric_algebra lnx-tabula alias/gen/geometric_algebra.c

rule gen_geometric_algebra --implicit \$builddir/alias-gen_geometric_algebra --command "\$builddir/alias-gen_geometric_algebra \$flags > \$out"

build "\$builddir/include/alias/pga2d.h" gen_geometric_algebra "" --flags "-p 2 -d 1 --binary meet outer_product --binary join regressive_product --code m02"
build "\$builddir/include/alias/pga3d.h" gen_geometric_algebra "" --flags "-p 3 -d 1 --binary meet outer_product --binary join regressive_product --code m024"
build "\$builddir/include/alias/cga2d.h" gen_geometric_algebra "" --flags "-p 3 -n 1 --binary meet outer_product --binary join regressive_product"
build "\$builddir/include/alias/cga3d.h" gen_geometric_algebra "" --flags "-p 4 -n 1 --binary meet outer_product --binary join regressive_product"

phony geometric_algebra_headers \
  --include-directory "\$builddir/include" \
  "\$builddir/include/alias/pga2d.h" \
  "\$builddir/include/alias/pga3d.h" \
  "\$builddir/include/alias/cga2d.h" \
  "\$builddir/include/alias/cga3d.h"

per_platform library ___-alias \
  --include-directory alias/include \
  --include-directory alias/ext/glad \
  --lflag -lm \
  ___-tabula \
  ___-libuv \
  geometric_algebra_headers \
  alias/src/ash.c \
  alias/src/color.c \
  alias/src/ecs_archetype.c \
  alias/src/ecs_component.c \
  alias/src/ecs_entity.c \
  alias/src/ecs_instance.c \
  alias/src/ecs_layer.c \
  alias/src/ecs_memory.c \
  alias/src/ecs_query.c \
  alias/src/ff_qoi.c \
	alias/src/file_system.c \
	alias/src/glob.c \
  alias/src/lexer.c \
  alias/src/log.c \
  alias/src/memory.c \
  alias/src/physics_bvh_2d.c \
  alias/src/physics.c \
  alias/src/random.c \
  alias/src/str.c \
  alias/src/string32.c \
  alias/src/transform.c \
  alias/src/ui.c \
  alias/src/libuv.c \
	alias/src/gl.c

build "\$builddir/algebra-parse.re.c" lemon solver/src/algebra-parse.re.y

build "\$builddir/algebra-parse.c" re2c "\$builddir/algebra-parse.re.c"

per_platform library ___-solver \
  --include-directory solver/include \
  ___-alias \
  solver/src/algebra.c \
  "\$builddir/algebra-parse.c"

per_platform executable ___-solver-test \
  ___-solver \
  solver/test/algebra.c \
  solver/test/main.c

per_platform executable ___-struo \
	___-alias \
	___-solver \
	--include-directory struo/include \
	struo/src/main.c \
	struo/src/options.c

per_platform library ___-engine \
	___-glfw3 \
	___-struo \
  --include-directory engine/include \
  --include-directory engine/ext/stb \
	engine/src/engine.c \
	engine/src/stbi.c

per_platform executable ___-engine-test \
  ___-engine \
	engine/integration_test/src/main.c

per_platform executable ___-game_01 \
	___-engine \
	game_01/src/main.c

# ---------------------------------------------------------------------------------------------------------------------

rule configure --command "./configure.sh > build.ninja" --implicit "configure-lib.sh configure.sh" --generator
build build.ninja configure

#rule gen_build_graph --command "ninja -t graph | dot -Tpng -o \$out" --implicit build.ninja
#build build.ninja.png gen_build_graph

# ---------------------------------------------------------------------------------------------------------------------

# TODO add_subdirectory(ext/mir)

per_platform library ___-quake2-shared \
  ___-alias \
  ___-sqlite3 \
  --include-directory quake2 \
  quake2/game/q_shared.c \
  quake2/game/monster/m_flash.c

per_platform library ___-quake2-game \
  ___-quake2-shared \
  --define-private GAME_HARD_LINKED=1 \
  quake2/game/player/p_weapon.c \
  quake2/game/player/p_view.c \
  quake2/game/player/p_trail.c \
  quake2/game/player/p_hud.c \
  quake2/game/player/p_client.c \
  quake2/game/monster/m_tank.c \
  quake2/game/monster/m_supertank.c \
  quake2/game/monster/m_soldier.c \
  quake2/game/monster/m_parasite.c \
  quake2/game/monster/m_mutant.c \
  quake2/game/monster/m_move.c \
  quake2/game/monster/m_medic.c \
  quake2/game/monster/m_insane.c \
  quake2/game/monster/m_infantry.c \
  quake2/game/monster/m_hover.c \
  quake2/game/monster/m_gunner.c \
  quake2/game/monster/m_gladiator.c \
  quake2/game/monster/m_flyer.c \
  quake2/game/monster/m_float.c \
  quake2/game/monster/m_flipper.c \
  quake2/game/monster/m_chick.c \
  quake2/game/monster/m_brain.c \
  quake2/game/monster/m_boss32.c \
  quake2/game/monster/m_boss31.c \
  quake2/game/monster/m_boss3.c \
  quake2/game/monster/m_boss2.c \
  quake2/game/monster/m_berserk.c \
  quake2/game/monster/m_actor.c \
  quake2/game/g_weapon.c \
  quake2/game/g_utils.c \
  quake2/game/g_turret.c \
  quake2/game/g_trigger.c \
  quake2/game/g_target.c \
  quake2/game/g_svcmds.c \
  quake2/game/g_spawn.c \
  quake2/game/g_save.c \
  quake2/game/g_phys.c \
  quake2/game/g_monster.c \
  quake2/game/g_misc.c \
  quake2/game/g_main.c \
  quake2/game/g_items.c \
  quake2/game/g_func.c \
  quake2/game/g_combat.c \
  quake2/game/g_cmds.c \
  quake2/game/g_chase.c \
  quake2/game/g_ai.c

per_platform library ___-quake2-common \
  ___-quake2-shared \
  quake2/qcommon/cmd.c \
  quake2/qcommon/cmodel.c \
  quake2/qcommon/common.c \
  quake2/qcommon/crc.c \
  quake2/qcommon/cvar.c \
  quake2/qcommon/files.c \
  quake2/qcommon/md4.c \
  quake2/qcommon/net_chan.c \
  quake2/qcommon/pmove.c \
  quake2/qcommon/sql.c

per_platform library ___-quake2-client \
  ___-quake2-common \
  ___-glfw3 \
  quake2/client/cl_cin.c \
  quake2/client/cl_ents.c \
  quake2/client/cl_fx.c \
  quake2/client/cl_input.c \
  quake2/client/cl_inv.c \
  quake2/client/cl_main.c \
  quake2/client/cl_newfx.c \
  quake2/client/cl_parse.c \
  quake2/client/cl_pred.c \
  quake2/client/cl_scrn.c \
  quake2/client/cl_tent.c \
  quake2/client/cl_view.c \
  quake2/client/console.c \
  quake2/client/keys.c \
  quake2/client/menu.c \
  quake2/client/qmenu.c \
  quake2/client/snd_dma.c \
  quake2/client/snd_mem.c \
  quake2/client/snd_mix.c \
  quake2/client/x86.c \
  quake2/client/in_glfw.c \
  quake2/client/snd_miniaudio.c \
  quake2/client/snd_miniaudio_imp.c

per_platform library ___-quake2-server \
  ___-quake2-common \
  quake2/server/sv_ccmds.c \
  quake2/server/sv_ents.c \
  quake2/server/sv_game.c \
  quake2/server/sv_init.c \
  quake2/server/sv_main.c \
  quake2/server/sv_send.c \
  quake2/server/sv_user.c \
  quake2/server/sv_world.c

per_platform library ___-quake2-refresh \
  ___-quake2-common \
  ___-glfw3 \
  --define-private REF_HARD_LINKED=1 \
  quake2/ref_gl/gl_beam.c \
  quake2/ref_gl/gl_compute.c \
  quake2/ref_gl/gl_draw.c \
  quake2/ref_gl/gl_light.c \
  quake2/ref_gl/gl_model.c \
  quake2/ref_gl/gl_particle.c \
  quake2/ref_gl/gl_rmisc.c \
  quake2/ref_gl/gl_warp.c \
  quake2/ref_gl/gl_image.c \
  quake2/ref_gl/gl_mesh.c \
  quake2/ref_gl/gl_model_not_found.c \
  quake2/ref_gl/gl_rmain.c \
  quake2/ref_gl/gl_rsurf.c \
  quake2/ref_gl/gl_sky.c \
  quake2/ref_gl/gl_sprite.c \
  quake2/ref_gl/glimp_glfw.c \
  quake2/ref_gl/qgl_glfw.c \
  quake2/ref_gl/render_mesh.c

per_platform executable ___-quake2 \
  ___-quake2-client \
  ___-quake2-server \
  ___-quake2-refresh \
  ___-quake2-game \
  quake2/main.c

per_platform executable ___-q2tools \
  --include-directory quake2/tools/common \
  --lflag -lm \
  quake2/tools/main.c \
  quake2/tools/4bsp/4bsp.c \
  quake2/tools/4bsp/brushbsp.c \
  quake2/tools/4bsp/csg.c \
  quake2/tools/4bsp/faces.c \
  quake2/tools/4bsp/leakfile.c \
  quake2/tools/4bsp/map.c \
  quake2/tools/4bsp/portals.c \
  quake2/tools/4bsp/prtfile.c \
  quake2/tools/4bsp/textures.c \
  quake2/tools/4bsp/tree.c \
  quake2/tools/4bsp/writebsp.c \
  quake2/tools/4rad/4rad.c \
  quake2/tools/4rad/lightmap.c \
  quake2/tools/4rad/patches.c \
  quake2/tools/4rad/trace.c \
  quake2/tools/4vis/4vis.c \
  quake2/tools/4vis/flow.c \
  quake2/tools/common/bspfile.c \
  quake2/tools/common/cmdlib.c \
  quake2/tools/common/l3dslib.c \
  quake2/tools/common/lbmlib.c \
  quake2/tools/common/llwolib.c \
  quake2/tools/common/mathlib.c \
  quake2/tools/common/mdfour.c \
  quake2/tools/common/polylib.c \
  quake2/tools/common/scriplib.c \
  quake2/tools/common/threads.c \
  quake2/tools/common/trilib.c

  #quake2/tools/4data/4data.c \
  #quake2/tools/4data/images.c \
  #quake2/tools/4data/models.c \
  #quake2/tools/4data/sprites.c \
  #quake2/tools/4data/tables.c \
  #quake2/tools/4data/video.c \