Skip to content

Commit

Permalink
Remove some of the duplicate logic in our Makefiles (enigma-dev#1876)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundies authored and JoshDreamland committed Jan 23, 2020
1 parent c6849f6 commit 9eefc9a
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 256 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/ENIGMA/
/emake
/test-runner
/emake-tests
/gm2egm
/LinuxExtra/
/android/
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
include:
- stage: ENIGMA CI
env: TEST_HARNESS=true AUDIO=OpenAL
script: ./CI/build_and_run_game.sh
script: make emake-tests && ./emake-tests && ./CI/build_and_run_game.sh
- stage: ENIGMA CI
env: WORKER=0
- stage: ENIGMA CI
Expand Down
68 changes: 19 additions & 49 deletions CommandLine/emake/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
BINARY := ../../emake
SRC_DIR := .
OBJ_DIR := .eobjs
include ../../Config.mk

SHARED_SRC_DIR := ../../shared
include $(SHARED_SRC_DIR)/Makefile
include $(SHARED_SRC_DIR)/eyaml/Makefile
include $(SHARED_SRC_DIR)/event_reader/Makefile

OS := $(shell uname -s)
PROTO_DIR := $(SHARED_SRC_DIR)/protos/.eobjs

ifeq ($(OS), Linux)
OS_LIBS=-lboost_system -Wl,--no-as-needed -Wl,-rpath,./ -lboost_program_options -lboost_iostreams -lboost_filesystem -lboost_system -lpthread -ldl
else ifeq ($(OS), Darwin)
Expand All @@ -16,60 +15,31 @@ else
OS_LIBS=-lboost_system-mt -Wl,--no-as-needed -Wl,-rpath,./ -lboost_program_options-mt -lboost_iostreams-mt -lboost_filesystem-mt -lboost_system-mt -lpthread
endif

PROTO_DIR := $(SHARED_SRC_DIR)/protos
CXXFLAGS := -I$(SRC_DIR) -I../../CompilerSource -I$(SHARED_SRC_DIR) -I$(PROTO_DIR)/.eobjs -std=c++11 -Wall -Wextra -Wpedantic -g
LDFLAGS := $(OS_LIBS) -L../../ -lProtocols -lprotobuf
CXXFLAGS += -I../../CompilerSource -I$(PROTO_DIR)
LDFLAGS += $(OS_LIBS) -L../../ -lProtocols -lprotobuf

rwildcard=$(wildcard $1/$2) $(foreach d,$(wildcard $1/*),$(call rwildcard,$d,$2))
SOURCES := $(call rwildcard,$(SRC_DIR),*.cpp)
ifeq ($(TESTS), TRUE)
TARGET=../../emake-tests
SOURCES := $(call rwildcard, ../libEGM-test,*.cpp)
LDFLAGS += -lgtest -lpthread -lgtest_main
else
TARGET = ../../emake
SOURCES := $(call rwildcard,$(SRC_DIR),*.cpp)
endif

#ON BY DEFAULT
ifneq ($(CLI_ENABLE_EGM), FALSE)
override CXXFLAGS += -DCLI_ENABLE_EGM
override CXXFLAGS += -I../libEGM -I../libEGM
override LDFLAGS += -lEGM
CXXFLAGS += -DCLI_ENABLE_EGM
CXXFLAGS += -I../libEGM -I../libEGM
LDFLAGS += -lEGM
endif

#OFF BY DEFAULT
ifeq ($(CLI_ENABLE_SERVER), TRUE)
override CXXFLAGS += -DCLI_ENABLE_SERVER
override LDFLAGS += -lgrpc++
CXXFLAGS += -DCLI_ENABLE_SERVER
LDFLAGS += -lgrpc++
else
SOURCES := $(filter-out ./Server.cpp, $(SOURCES))
endif

OBJECTS := $(patsubst $(SRC_DIR)/%, $(OBJ_DIR)/%, $(patsubst %.cpp, %.o, $(SOURCES)))
DEPENDS := $(OBJECTS:.o=.d)

CXXFLAGS += -I$(SHARED_SRC_DIR) $(addprefix -I$(SHARED_SRC_DIR)/, $(SHARED_INCLUDES))
SOURCES += $(addprefix $(SHARED_SRC_DIR),$(SHARED_SOURCES))
OBJECTS += $(addprefix $(OBJ_DIR)/shared/,$(SHARED_SOURCES:.cpp=.o))
DEPENDS += $(addprefix $(OBJ_DIR)/shared/,$(SHARED_SOURCES:.cpp=.d))

OBJDIRS := $(sort $(OBJ_DIR) $(dir $(OBJECTS)))

$(BINARY): $(OBJECTS)
g++ $^ $(LDFLAGS) -o $@

.PHONY: all clean obj_dirs

all: $(BINARY)

clean:
rm -rf $(BINARY) $(OBJ_DIR)

# Create the object directories
$(OBJDIRS):
mkdir -p $@
# Old make has a bug which requires this nonsense...
obj_dirs: $(OBJDIRS)

# Generate rules for new (unbuilt) files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | obj_dirs
$(CXX) $(CXXFLAGS) -MMD -c -o $@ $<
$(OBJ_DIR)/shared/%.o: $(SHARED_SRC_DIR)/%.cpp | obj_dirs
$(CXX) $(CXXFLAGS) -MMD -c -o $@ $<

# Include rules for known (previously-built) files
-include $(DEPENDS)
.SUFFIXES:
include ../../Default.mk
9 changes: 9 additions & 0 deletions CommandLine/gm2egm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include ../../Config.mk

TARGET := ../../gm2egm

CXXFLAGS := -I../../shared/protos/ -I../../shared/protos/.eobjs -I../libEGM/
LDFLAGS := -Wl,-rpath,. -L../../ -lEGM -lProtocols -lpthread
SOURCES := main.cpp

include ../../Default.mk
64 changes: 10 additions & 54 deletions CommandLine/libEGM/Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
OS := $(shell uname -s)
ifeq ($(OS), Linux)
EXT := .so
else ifeq ($(OS), Darwin)
EXT := .dylib
else
EXT := .dll
endif
include ../../Config.mk

GCCVER := $(shell gcc -dumpversion | cut -c 1)
SHARED_SOURCES := ../../shared
LIBRARY := ../../libEGM$(EXT)
PROTO_DIR := ../../shared/protos
SRC_DIR := .
OBJ_DIR := .eobjs

CXXFLAGS := -I$(SRC_DIR) -I../ $(shell pkg-config --cflags pugixml) -I$(PROTO_DIR)/.eobjs -I$(SHARED_SOURCES) -I$(SHARED_SOURCES)/libpng-util -Wall -Wextra -Wpedantic -g -fPIC
LDFLAGS := -lz $(shell pkg-config --libs-only-L pugixml) -lpugixml -lyaml-cpp -L../../ -lProtocols -lprotobuf -lstdc++fs -L$(SHARED_SOURCES)/libpng-util -lpng-util -lpng
TARGET := ../../libEGM$(LIB_EXT)
SOURCES := $(call rwildcard,$(SRC_DIR),*.cpp)
SHARED_SRC_DIR := ../../shared
PROTO_DIR := $(SHARED_SRC_DIR)/protos/.eobjs

#if gcc >= 8 use std::filesystem instead of boost
ifeq ($(shell expr $(GCCVER) \>= 8), 1)
override CXXFLAGS += -std=c++17
CXXFLAGS += -std=c++17
FS_LIBS=-lstdc++fs
else
override CXXFLAGS += -std=c++14 -DUSE_BOOST_FS
CXXFLAGS += -std=c++14 -DUSE_BOOST_FS
ifeq ($(OS), Linux)
FS_LIBS=-lboost_system -lboost_filesystem
else ifeq ($(OS), Darwin)
Expand All @@ -32,39 +20,7 @@ else
endif
endif

LDFLAGS += $(FS_LIBS)

rwildcard=$(wildcard $1/$2) $(foreach d,$(wildcard $1/*),$(call rwildcard,$d,$2))

SOURCES := $(call rwildcard,$(SRC_DIR),*.cpp)
OBJECTS := $(patsubst $(SRC_DIR)/%, $(OBJ_DIR)/%, $(patsubst %.cpp, %.o, $(SOURCES)))

OBJDIRS = $(sort $(OBJ_DIR) $(dir $(OBJECTS)))
DEPENDS = $(OBJECTS:.o=.d)

lib:
$(MAKE) $(LIBRARY)

# Old make has a bug which requires this nonsense...
obj_dirs: $(OBJDIRS)

clean:
rm -rf $(LIBRARY) $(OBJ_DIR)

# --libs-only-L is specified because the Trusty Tahr package for pugixml does not have a proper pkg-config
# hence we only use pkg-config for the lib dirs and just always link pugixml whether in travis, on ubuntu, or in msys2
$(LIBRARY): $(OBJECTS)
$(CXX) $(CXXFLAGS) -shared -o $@ $^ $(LDFLAGS)

# Create the object directories
$(OBJDIRS):
mkdir -p $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | obj_dirs
$(CXX) $(CXXFLAGS) -MMD -c -o $@ $<

.PHONY: lib clean obj_dirs
CXXFLAGS += -I../ $(shell pkg-config --cflags pugixml) -I$(PROTO_DIR) -I$(SHARED_SRC_DIR) -I$(SHARED_SRC_DIR)/libpng-util -fPIC
LDFLAGS += -shared -lz $(shell pkg-config --libs-only-L pugixml) -lpugixml -lyaml-cpp -L../../ -lProtocols -lprotobuf -lstdc++fs -L$(SHARED_SRC_DIR)/libpng-util -lpng-util -lpng $(FS_LIBS)

# Include rules for known (previously-built) files
-include $(DEPENDS)
.SUFFIXES:
include ../../Default.mk
39 changes: 5 additions & 34 deletions CommandLine/testing/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
BINARY := ../../test-runner
SRC_DIR := .
OBJ_DIR := .eobjs
include ../../Config.mk

TARGET := ../../test-runner
TESTCASES_DIR := $(SRC_DIR)/Tests

OS := $(shell uname -s)
ifeq ($(OS), Linux)
OS_SRCS=$(wildcard ./Platform/*-X11.cpp)
OS_LIBS=-lX11 -lboost_filesystem -lboost_system
Expand All @@ -15,35 +14,7 @@ else
OS_LIBS=-lboost_filesystem-mt -lboost_system-mt
endif

CXXFLAGS := -I$(SRC_DIR) -Wall -Wextra -Wpedantic -g -std=c++11
LDFLAGS := $(OS_LIBS) -lgtest -lpthread -lgtest_main

SOURCES := $(wildcard $(SRC_DIR)/*.cpp) $(wildcard $(TESTCASES_DIR)/*.cpp) $(OS_SRCS)
OBJECTS := $(patsubst $(SRC_DIR)/%, $(OBJ_DIR)/%, $(patsubst %.cpp, %.o, $(SOURCES)))
OBJDIRS := $(sort $(OBJ_DIR) $(dir $(OBJECTS)))
DEPENDS := $(OBJECTS:.o=.d)

$(BINARY): $(OBJECTS) .FORCE
g++ $(filter-out .FORCE, $^) -o $@ $(LDFLAGS)

.PHONY: all clean obj_dirs .FORCE

.FORCE:

all: $(BINARY)
clean:
rm -rf $(BINARY) $(OBJ_DIR)

# Create the object directories
$(OBJDIRS):
mkdir -p $@
# Old make has a bug which requires this nonsense...
obj_dirs: $(OBJDIRS)

# Generate rules for new (unbuilt) files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | obj_dirs
$(CXX) $(CXXFLAGS) $(CXXFLAGS) -MMD -c -o $(OBJ_DIR)/$*.o $<
LDFLAGS += $(OS_LIBS) -lgtest -lpthread -lgtest_main

# Include rules for known (previously-built) files
-include $(DEPENDS)
.SUFFIXES:
include ../../Default.mk
69 changes: 6 additions & 63 deletions CompilerSource/Makefile
Original file line number Diff line number Diff line change
@@ -1,82 +1,25 @@
include ../Config.mk

# "Shared" includes.
SHARED_SRC_DIR := ../shared
include $(SHARED_SRC_DIR)/Makefile
include $(SHARED_SRC_DIR)/eyaml/Makefile
include $(SHARED_SRC_DIR)/event_reader/Makefile
include $(SHARED_SRC_DIR)/rectpacker/Makefile

#################
# configuration #
#################
# TODO: better config process based on features rather than OS

BASE = compileEGMf
OS := $(shell uname -s)
ifeq ($(OS), Linux)
TARGET := ../lib$(BASE).so
CXXFLAGS += -fPIC
MKDIR := mkdir
else ifeq ($(OS), Darwin)
TARGET := ../lib$(BASE).dylib
CXXFLAGS += -fPIC
MKDIR := mkdir
else
TARGET := ../$(BASE).dll
LDFLAGS += -static-libgcc
MKDIR := mkdir.exe
endif

###########
# options #
###########

CXX := g++
CXXFLAGS += -std=c++11 -Wall -O3 -g -I./JDI/src
LDFLAGS += -shared -O3 -g -L../ -Wl,-rpath,./
LDLIBS += -lProtocols -lprotobuf -lz -L$(SHARED_SRC_DIR)/libpng-util -lpng-util -lpng

# This implements a recursive wildcard allowing us to iterate in subdirs
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

SOURCES := $(filter-out ./standalone_main.cpp, $(call rwildcard,./,*.cpp))
OBJECTS := $(addprefix .eobjs/,$(SOURCES:.cpp=.o))
DEPENDS := $(OBJECTS:.o=.d)
PROTO_DIR := $(SHARED_SRC_DIR)/protos
CXXFLAGS += -fPIC -I./JDI/src -I$(SHARED_SRC_DIR) -I$(SHARED_SRC_DIR)/libpng-util -I$(PROTO_DIR)/.eobjs $(addprefix -I$(SHARED_SRC_DIR)/, $(SHARED_INCLUDES))
LDFLAGS += -shared -g -L../ -Wl,-rpath,./ -lProtocols -lprotobuf -lz -L$(SHARED_SRC_DIR)/libpng-util -lpng-util -lpng
SOURCES := $(filter-out ./standalone_main.cpp, $(call rwildcard,.,*.cpp))

CXXFLAGS += -I$(SHARED_SRC_DIR) -I$(SHARED_SRC_DIR)/libpng-util -I$(PROTO_DIR)/.eobjs $(addprefix -I$(SHARED_SRC_DIR)/, $(SHARED_INCLUDES))
SOURCES += $(addprefix $(SHARED_SRC_DIR),$(SHARED_SOURCES))
OBJECTS += $(addprefix .eobjs/shared/,$(SHARED_SOURCES:.cpp=.o))
DEPENDS += $(addprefix .eobjs/shared/,$(SHARED_SOURCES:.cpp=.d))

# sort has the nice side effect of removing duplicates. this may or may not be faster.
OBJDIRS := $(sort $(dir $(OBJECTS)))
override CPPFLAGS += -I.

############
# building #
############

.PHONY: all clean

all: $(TARGET)

clean:
$(RM) $(TARGET) $(OBJECTS) $(DEPENDS)

$(TARGET): $(OBJECTS)
$(CXX) $(LDFLAGS) -o $@ $(OBJECTS) $(LDLIBS)

# GCC will figure out dependencies and write out makefile rules in %.d when they change
# -MMD outputs dependencies to %.d as a side effect of compilation, ignoring system headers
# -MP gives phony rules for non-target files, avoiding problems with missing files
.eobjs/%.o .eobjs/%.d: %.cpp | $(OBJDIRS)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -MMD -MP -c -o .eobjs/$*.o $<
.eobjs/shared/%.o .eobjs/shared/%.d: $(SHARED_SRC_DIR)/%.cpp | $(OBJDIRS)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -MMD -MP -c -o .eobjs/shared/$*.o $<

$(OBJDIRS):
$(MKDIR) -p $@

ifneq ($(MAKECMDGOALS),clean)
-include $(DEPENDS)
endif
include ../Default.mk
29 changes: 29 additions & 0 deletions Config.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
GCCVER := $(shell gcc -dumpversion | cut -c 1)

OS := $(shell uname -s)
ifeq ($(OS), Linux)
LIB_EXT := .so
BIN_EXT :=
else ifeq ($(OS), Darwin)
LIB_EXT := .dylib
BIN_EXT :=
else
LIB_EXT := .dll
BIN_EXT := .exe
endif

# Global g++ flags
CXXFLAGS := -Wall -Wextra -Wpedantic -g -I.
LDFLAGS := -g

# For older travis instances
ifeq ($(shell expr $(GCCVER) \<= 6), 1)
CXXFLAGS += -std=c++11
endif

# These will be relative to the file that includes this Makefile
SRC_DIR := .
OBJ_DIR := .eobjs

# This implements a recursive wildcard allowing us to iterate in subdirs
rwildcard=$(wildcard $1/$2) $(foreach d,$(wildcard $1/*),$(call rwildcard,$d,$2))
Loading

0 comments on commit 9eefc9a

Please sign in to comment.