medfall

A super great game engine
Log | Files | Refs

commit d90002161cf78dad5a18241456774e59f5039a41
parent 6eb7c01ce5308bdcd7149f02da026d1ecda08b7a
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun May  7 22:39:02 +0300

New build system

- Uses Lua to generate a Makefile
- Puts all build objects/libs under build directory
- Puts release bins in release directory
- Supports multiple configs (debug/asan/release)
- Will be easy to add Windows support...
- ...but it can't parse the output of cl /showIncludes
- Will be easy to switch to something that isn't gmake
- No OSX/OpenBSD support for now but that's easy to readd

Diffstat:
.gitignore | 16++++------------
Makefile | 53-----------------------------------------------------
libs/glfw.mk | 13-------------
libs/imgui.mk | 12------------
libs/monocypher.mk | 6------
libs/squish.mk | 11-----------
libs/tinyobjloader.mk | 6------
make.lua | 42++++++++++++++++++++++++++++++++++++++++++
os.mk | 10----------
rules.mk | 40----------------------------------------
scripts/gen_makefile.lua | 205+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11 files changed, 251 insertions(+), 163 deletions(-)
diff --git a/.gitignore b/.gitignore @@ -1,3 +1,7 @@ +build +release +gen.mk + medfall pp sound @@ -14,18 +18,6 @@ models secret_key.h -*.o -*.a -*.so - -*.pdb -*.ilk -*.lib -*.obj -*.exe -*.exp -*.dll - *.bsp *.parts *.xoj diff --git a/Makefile b/Makefile @@ -1,53 +0,0 @@ -BINS := medfall sm bsp btt pp sound srv launcher/launcher utils/genkeys/genkeys utils/genkeys/sign -STBS := truetype image image_write perlin -VISITORS := visitors/linear_algebra.h - -all: $(VISITORS) $(BINS) test_lockfree - -# Binary dependencies -medfall: gl.o renderer.o immediate.o work_queue.o text_renderer.o stb_truetype.o glad.o libglfw.a libsquish.a -pp: pp.o stb_image.o lz4.o lz4hc.o heightmap.o libsquish.a -sound: audio.o mixer.o wave.o platform_audio_output.o -srv: server/main.o -launcher/launcher: launcher/main.o memory_arena.o http.o work_queue.o sha2.o gl.o glad.o libimgui.a stb_truetype.o libglfw.a libmonocypher.a - -utils/genkeys/genkeys: utils/genkeys/genkeys.o libmonocypher.a -utils/genkeys/sign: utils/genkeys/sign.o libmonocypher.a - -test_lockfree: relacy.cc - $(CXX) relacy.cc -o test_lockfree $(CXXFLAGS) $(LDFLAGS) -std=c++98 -fexceptions -frtti -Wno-missing-field-initializers - -# Common dependencies -BIN_COMMON_OBJS := log.o memory_arena.o ggformat.o strlcpy.o strlcat.o strtonum.o profiler.o stats.o rng/well512.o -DLL_COMMON_OBJS := log.o memory_arena.o work_queue.o immediate.o profiler.o text_renderer.o stb_truetype.o renderer.o ggformat.o strlcat.o strlcpy.o strtonum.o stats.o rng/well512.o libsquish.a glad.o main.o gl.o libglfw.a - -# Module dependencies -medfall: hm.o heightmap.o terrain_manager.o lz4.o btt.o gpubtt.o skybox.o http.o $(DLL_COMMON_OBJS) -bsp: bsp.o bsp_renderer.o $(DLL_COMMON_OBJS) -btt: mod_btt.o btt.o gpubtt.o heightmap.o skybox.o stb_image.o lz4.o $(DLL_COMMON_OBJS) -sm: shadow_map.o libtinyobjloader.a $(DLL_COMMON_OBJS) - -# Special CXXFLAGS/LDFLAGS -medfall: LDFLAGS += -Wl,--export-dynamic -lz4.o: CXXFLAGS += -O3 -lz4hc.o: CXXFLAGS += -O3 -xxhash.o: CXXFLAGS += -O3 - -# Visitor headers -visitors/linear_algebra.h: linear_algebra.h - -# Compiler flags -WARNINGS := -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wconversion -Wshadow -Wcast-align -Wstrict-overflow -Werror=switch -CXXFLAGS += -std=c++11 -msse2 -fno-exceptions -fno-rtti -I. \ - -O0 \ - -ggdb3 -fno-omit-frame-pointer -fno-strict-aliasing -fno-strict-overflow \ - -DSTATIC_GAME -LDFLAGS += -lm - -include os.mk -include rules.mk - -include libs/glfw.mk -include libs/imgui.mk -include libs/monocypher.mk -include libs/tinyobjloader.mk diff --git a/libs/glfw.mk b/libs/glfw.mk @@ -1,13 +0,0 @@ -libglfw.a: libs/glfw/src/context.o libs/glfw/src/init.o libs/glfw/src/input.o \ - libs/glfw/src/monitor.o libs/glfw/src/vulkan.o libs/glfw/src/window.o \ - libs/glfw/src/x11_init.o libs/glfw/src/x11_monitor.o libs/glfw/src/x11_window.o \ - libs/glfw/src/xkb_unicode.o libs/glfw/src/linux_joystick.o libs/glfw/src/posix_time.o \ - libs/glfw/src/posix_tls.o libs/glfw/src/glx_context.o libs/glfw/src/egl_context.o - -libs/glfw/src/%.o: CXXFLAGS = -x c -O2 -D_GLFW_X11 -libs/glfw/src/%.o: WARNINGS = -Wall - -clean: clean-glfw - -clean-glfw: - rm -f libs/glfw/src/*.o diff --git a/libs/imgui.mk b/libs/imgui.mk @@ -1,12 +0,0 @@ -libimgui.a: libs/imgui/imgui.o libs/imgui/imgui_draw.o libs/imgui/imgui_impl_glfw_gl3.o - -#libs/imgui/imgui.o: CXXFLAGS += -DIMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION -#libs/imgui/imgui_draw.o: CXXFLAGS += -DIMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION -#libs/imgui/imgui_impl_glfw_gl3.o: CXXFLAGS += -DIMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION - -libs/imgui/imgui_draw.o: CXXFLAGS += -Wno-maybe-uninitialized - -clean: clean-imgui - -clean-imgui: - rm -f libs/imgui/*.o diff --git a/libs/monocypher.mk b/libs/monocypher.mk @@ -1,6 +0,0 @@ -libmonocypher.a: libs/monocypher/monocypher.o - -clean: clean-monocypher - -clean-monocypher: - rm -f libs/monocypher/*.o diff --git a/libs/squish.mk b/libs/squish.mk @@ -1,11 +0,0 @@ -libsquish.a: libs/squish/alpha.o libs/squish/colourfit.o libs/squish/rangefit.o \ - libs/squish/clusterfit.o libs/squish/colourset.o libs/squish/singlecolourfit.o \ - libs/squish/colourblock.o libs/squish/maths.o libs/squish/squish.o - -libs/squish/%.o: CXXFLAGS = -O3 -libs/squish/%.o: WARNINGS = -Wall - -clean: clean-squish - -clean-squish: - rm -f libs/squish/*.o diff --git a/libs/tinyobjloader.mk b/libs/tinyobjloader.mk @@ -1,6 +0,0 @@ -libtinyobjloader.a: libs/tinyobjloader/tinyobjloader.o - -clean: clean-tinyobjloader - -clean-tinyobjloader: - rm -f libs/tinyobjloader/*.o diff --git a/make.lua b/make.lua @@ -0,0 +1,42 @@ +require( "scripts.gen_makefile" ) + +local common_objs = { "memory_arena", "log", "ggformat", "strlcpy", "strlcat", "strtonum", "profiler", "stats", "rng/well512" } +local game_objs = { "main", "work_queue", "renderer", "gl", "glad", "immediate", "text_renderer", "stb_truetype", common_objs } +local game_libs = { "glfw", "squish", "tinyobjloader" } + +bin( "medfall", { "hm", "heightmap", "terrain_manager", "lz4", "btt", "gpubtt", "skybox", "http", game_objs }, { game_libs } ) +bin( "bsp", { "bsp", "bsp_renderer", game_objs }, { game_libs } ) +-- TODO: fix btt build +-- bin( "btt", { "mod_btt", "btt", "heightmap", "skybox", "stb_image", "lz4", game_objs }, { game_libs } ) +bin( "sm", { "shadow_map", game_objs }, { game_libs } ) + +bin_ldflags( "medfall", "-lX11 -lXrandr -lXinerama -lXcursor" ) +bin_ldflags( "bsp", "-lX11 -lXrandr -lXinerama -lXcursor" ) +bin_ldflags( "btt", "-lX11 -lXrandr -lXinerama -lXcursor" ) +bin_ldflags( "sm", "-lX11 -lXrandr -lXinerama -lXcursor" ) + +bin( "sound", { "audio", "mixer", "wave", "platform_audio_output", common_objs } ) + +bin( "srv", { "server/main", common_objs } ) + +bin( "pp", { "pp", "lz4", "lz4hc", "heightmap", "stb_image", common_objs }, { "squish" } ) + +bin( "utils/genkeys/genkeys", { "utils/genkeys/genkeys", common_objs }, { "monocypher" } ) +bin( "utils/genkeys/sign", { "utils/genkeys/sign", common_objs }, { "monocypher" } ) + +obj( "stb_image", "stb_image.h", "-DSTB_IMAGE_IMPLEMENTATION -Wno-shift-negative-value" ) +obj( "stb_image_write", "stb_image_write.h", "-DSTB_IMAGE_WRITE_IMPLEMENTATION" ) +obj( "stb_perlin", "stb_perlin.h", "-DSTB_PERLIN_IMPLEMENTATION" ) +obj( "stb_truetype", "stb_truetype.h", "-DSTB_TRUETYPE_IMPLEMENTATION" ) + +obj_cxxflags( "lz4", "-O3 -g0" ) +obj_cxxflags( "lz4hc", "-O3 -g0" ) + +bin( "test_lockfree", { "relacy" } ) +obj_cxxflags( "relacy", "-std=c++98 -fexceptions -frtti -Wno-shadow -Wno-missing-field-initializers" ) + +require( "libs/imgui" ) +require( "libs/glfw" ) +require( "libs/monocypher" ) +require( "libs/squish" ) +require( "libs/tinyobjloader" ) diff --git a/os.mk b/os.mk @@ -1,10 +0,0 @@ -# Makefile OS detection -ifeq ($(shell uname -s),Linux) - LDFLAGS += -ldl -lpthread -lX11 -lXrandr -lXinerama -lXcursor -else ifeq ($(shell uname -s),Darwin) - LDFLAGS += -framework OpenGL -lglfw3 -else ifeq ($(shell uname -s),OpenBSD) - CXX = eg++ - LDFLAGS += -lpthread -endif - diff --git a/rules.mk b/rules.mk @@ -1,40 +0,0 @@ -# This file contains Makefile rules to actually build things - -# Create the binaries -$(BINS): %: $(BIN_COMMON_OBJS) - @printf "\e[1;31mbuilding $@\e[0m\n" - @$(CXX) -o $@ $^ $(LDFLAGS) - -# Create stb_*.cc and then stb_*.o -STB_OBJS := $(addprefix stb_,$(addsuffix .o,$(STBS))) - -# The STB libraries need you to pass -DSTB_*_IMPLEMENTATION to get the actual code -STB_CXXFLAGS := $(shell echo $(addprefix -DSTB_,$(addsuffix _IMPLEMENTATION,$(STBS))) | tr a-z A-Z) - -$(STB_OBJS): %.o: %.h - @printf "\e[1;32mbuilding $<\e[0m\n" - @$(CXX) $(STB_CXXFLAGS) $(CXXFLAGS) -x c++ -c -o $@ $< - -# Build *.o from *.cc -# The -M* stuff is for generating dependency lists -%.o: %.cc - @printf "\e[1;32mbuilding $<\e[0m\n" - @mkdir -p .deps - @$(CXX) $(CXXFLAGS) -c -o $@ $< -MMD -MP -MF .deps/$(subst /,_,$*).d $(WARNINGS) - -# Build *.a from *.o -%.a: - @printf "\e[1;35mbuilding $@\e[0m\n" - @$(AR) rs $@ $^ - -# Generate visitor headers -visitors/%.h: - @mkdir -p visitors - scripts/gen_visitors.lua $^ > $@ - -clean: - rm -f $(BINS) *.o rng/*.o server/*.o *.a *.so test_lockfree - rm -rf visitors - rm -rf .deps - --include .deps/*.d diff --git a/scripts/gen_makefile.lua b/scripts/gen_makefile.lua @@ -0,0 +1,205 @@ +local configs = { + [ "windows" ] = { + bin_suffix = ".exe", + obj_suffix = ".obj", + lib_suffix = ".lib", + + cxx = "...", + lib = "...", + + cxxflags = "/I . /Z7 /Zo /Oi /Gm- /GR- /EHa- /nologo /DNOMINMAX /D_USE_MATH_DEFINES /DWIN32_LEAN_AND_MEAN /DSTATIC_GAME", + -- ldflags = "user32.lib shell32.lib opengl32.lib gdi32.lib Ws2_32.lib", + ldflags = "user32.lib shell32.lib", + warnings = "/W4 /wd4100 /wd4201 /wd4189 /wd4351 /wd4505 /wd4127 /wd4350 /wd4702 /D_CRT_SECURE_NO_WARNINGS", + }, + + [ "windows-32" ] = { }, + + [ "windows-debug" ] = { + cxxflags = "/Od /MTd", + }, + [ "windows-release" ] = { + cxxflags = "/O2 /MT", + bin_prefix = "release/", + }, + + [ "linux" ] = { + obj_suffix = ".o", + lib_prefix = "lib", + lib_suffix = ".a", + + cxxflags = "-I . -c -x c++ -std=c++11 -msse2 -fno-exceptions -fno-rtti -fno-strict-aliasing -fno-strict-overflow -D_USE_MATH_DEFINES -DSTATIC_GAME -fPIC", + ldflags = "-lm -lpthread -ldl", + warnings = "-Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wshadow -Wcast-align -Wstrict-overflow ", -- -Wconversion + }, + + [ "linux-64" ] = { }, + + [ "linux-debug" ] = { + cxxflags = "-O0 -ggdb3 -fno-omit-frame-pointer", + }, + [ "linux-asan" ] = { + bin_suffix = "-asan", + cxxflags = "-O0 -ggdb3 -fno-omit-frame-pointer -fsanitize=address", + ldflags = "-fsanitize=address", + }, + [ "linux-release" ] = { + cxxflags = "-O2", + bin_prefix = "release/", + }, + + -- TODO: mingw? + -- TODO: OSX +} + +local function identify_host() + local dll_ext = package.cpath:match( "(%a+)$" ) + + if dll_ext == "dll" then + return "windows", "32" + end + + if dll_ext == "so" then + return "linux", "64" + end +end + +local OS, arch = identify_host() +local config = arg[ 1 ] or "debug" + +local double_arch = OS .. "-" .. arch +local double_config = OS .. "-" .. config +local triple = OS .. "-" .. arch .. "-" .. config + +if not configs[ double_arch ] or not configs[ double_config ] then + io.stderr:write( "bad config: " .. triple .. "\n" ) + os.exit( 1 ) +end + +local function concat( key ) + return "" + .. ( ( configs[ OS ] and configs[ OS ][ key ] ) or "" ) + .. " " + .. ( ( configs[ double_arch ] and configs[ double_arch ][ key ] ) or "" ) + .. " " + .. ( ( configs[ double_config ] and configs[ double_config ][ key ] ) or "" ) +end + +local function rightmost( key ) + return nil + or ( configs[ double_config ] and configs[ double_config ][ key ] ) + or ( configs[ double_arch ] and configs[ double_arch ][ key ] ) + or ( configs[ OS ] and configs[ OS ][ key ] ) + or "" +end + +local bin_prefix = rightmost( "bin_prefix" ) +local bin_suffix = rightmost( "bin_suffix" ) +local obj_suffix = rightmost( "obj_suffix" ) +local lib_prefix = rightmost( "lib_prefix" ) +local lib_suffix = rightmost( "lib_suffix" ) +local cxxflags = concat( "cxxflags" ) .. " " .. concat( "warnings" ) +local ldflags = concat( "ldflags" ) + +local dir = "build/" .. triple +local output = { } + +local function join( names, suffix, prefix ) + if not names then + return "" + end + + prefix = prefix or "" + local res = { } + for i, name in ipairs( names ) do + if type( name ) == "table" then + res[ i ] = join( name, suffix, prefix ) + else + res[ i ] = dir .. "/" .. prefix .. names[ i ] .. suffix + end + end + return table.concat( res, " " ) +end + +local function printh( form, ... ) + print( form:format( ... ) ) +end + +local function printf( form, ... ) + table.insert( output, form:format( ... ) ) +end + +function bin( bin_name, objs, libs ) + assert( type( objs ) == "table", "objs should be a table" ) + assert( not libs or type( libs ) == "table", "libs should be a table or nil" ) + local bin_path = ( "%s%s%s" ):format( bin_prefix, bin_name, bin_suffix ) + printh( "BINS += %s", bin_path ) + printf( "%s: %s %s", bin_path, join( objs, obj_suffix ), join( libs, lib_suffix, lib_prefix ) ) + printf( "clean::\n\trm -f %s", bin_path ) +end + +function obj( obj_name, source, cxxflags ) + local obj_path = ( "%s/%s%s" ):format( dir, obj_name, obj_suffix ) + printh( "OBJS += %s", obj_path ) + printf( "%s: %s", obj_path, source ) + printf( "%s: CXXFLAGS += %s", obj_path, cxxflags ) +end + +function lib( lib_name, objs ) + assert( type( objs ) == "table", "objs should be a table" ) + printf( "%s/%s%s%s: %s", dir, lib_prefix, lib_name, lib_suffix, join( objs, obj_suffix ) ) +end + +function bin_ldflags( bin_name, ldflags ) + local bin_path = ( "%s%s%s" ):format( bin_prefix, bin_name, bin_suffix ) + printf( "%s: LDFLAGS += %s", bin_path, ldflags ) +end + +function obj_cxxflags( obj_name, cxxflags ) + printf( "%s/%s%s: CXXFLAGS += %s", dir, obj_name, obj_suffix, cxxflags ) +end + +function obj_replace_cxxflags( obj_name, cxxflags ) + printf( "%s/%s%s: CXXFLAGS := %s", dir, obj_name, obj_suffix, cxxflags ) +end + +printf( "CXXFLAGS := %s", cxxflags ) +printf( "LDFLAGS := %s", ldflags ) +printf( "MAKEFLAGS += -r" ) + +printf( "" ) +printf( "all: $(BINS)" ) +printf( "" ) + +printf( [[ +$(BINS): %%: + @printf "\e[1;31mbuilding $@\e[0m\n" + @mkdir -p "$(@D)" + $(CXX) -o $@ $^ $(LDFLAGS) +]] ) +printf( [[ +%s/%%%s: %%.cc + @printf "\e[1;32mbuilding $<\e[0m\n" + @mkdir -p "$(@D)" + $(CXX) $(CXXFLAGS) -o $@ $< -MMD -MP +]], dir, obj_suffix ) +printf( [[ +$(OBJS): %%: + @printf "\e[1;32mbuilding $<\e[0m\n" + @mkdir -p "$(@D)" + $(CXX) $(CXXFLAGS) -o $@ $< -MMD -MP +]] ) +printf( [[ +%%%s: + @printf "\e[1;35mbuilding $@\e[0m\n" + @mkdir -p "$(@D)" + @$(AR) rs $@ $^ +]], lib_suffix ) + +printf( "clean::\n\trm -rf build release" ) + +automatically_print_output_at_exit = setmetatable( { }, { + __gc = function() + print( table.concat( output, "\n" ) ) + end +} )