corelib/Makefile.main
Emmanuel BENOîT 30d8d3057e Windows "compatibility"
* Makefile changes allowing for cross-compilation
* SRDData: had to rename an enum member from ERROR to ERR because
apparently creating a macro called ERROR is a thing in the windows
headers
* DynLib: ported
* Strings: fixes in toOSString, untested for now
* Fixes in some tests
* TODO list update
2018-06-17 12:39:02 +02:00

172 lines
3.8 KiB
Text

include $(CONFIG)
BUILD_FOR ?= linux
# Select compiler (g++ for linux, mingw-w64/posix for windows)
ifeq ($(BUILD_FOR),linux)
CXX=x86_64-linux-gnu-g++
else
ifeq ($(BUILD_FOR),win)
CXX=x86_64-w64-mingw32-g++-posix
else
$(error Invalid target "$(BUILD_FOR)")
endif
endif
# FIXME: temporarily used for Windows build
ifeq ($(BUILD_FOR),win)
CPPFLAGS += -I/tmp/cppunit/include
LDFLAGS += -L/tmp/cppunit/src/cppunit/.libs/
endif
CPPFLAGS += -Iinclude -I$(OUTDIR) -I$(OUTDIR)/data -DEBCL_BUILD -D_USE_MATH_DEFINES
CXXFLAGS += -std=c++14 -Wall -Wextra
LDFLAGS += -L$(OUTDIR) -pthread
ifeq ($(OPTIMIZE),y)
CXXFLAGS += -O2
else
CXXFLAGS += -O0
endif
ifeq ($(DEBUG),y)
CXXFLAGS += -g
endif
# Libs for either Linux or Windows
ifeq ($(BUILD_FOR),linux)
LIBS_LIB = -ldl -latomic
else
ifeq ($(BUILD_FOR),win)
LIBS_LIB = -latomic
else
$(error Invalid target "$(BUILD_FOR)")
endif
endif
# Library and progam suffixes
ifeq ($(BUILD_FOR),linux)
SUFFIX_LIB=.so
SUFFIX_EXE=
else
ifeq ($(BUILD_FOR),win)
SUFFIX_LIB=.dll
SUFFIX_EXE=.exe
else
$(error Invalid target "$(BUILD_FOR)")
endif
endif
CXXFLAGS_LIB = $(CXXFLAGS)
LIB_DYNAMIC = $(OUTDIR)/libebcorelib$(SUFFIX_LIB)
LIB_STATIC = $(OUTDIR)/libebcorelib.a
LIB_TARGETS =
ifeq ($(BUILD_DYNAMIC_LIB),y)
CXXFLAGS_LIB += -fPIC
LIB_TARGETS += $(LIB_DYNAMIC)
endif
ifeq ($(BUILD_STATIC_LIB),y)
LIB_TARGETS += $(LIB_STATIC)
endif
include src/files.mk
LIB_OBJS = $(addprefix $(OUTDIR)/,$(addsuffix .o, $(subst src/,, $(basename $(LIB_SOURCES)))))
include tests/list.mk
TEST_SUITES := $(addsuffix $(SUFFIX_EXE), $(addprefix $(OUTDIR)/test-,$(TESTS)))
TEST_ALL := $(OUTDIR)/run-all-tests$(SUFFIX_EXE)
TEST_MAIN := $(OUTDIR)/tests/run-test.o
TEST_OBJS := $(addsuffix .o,$(addprefix $(OUTDIR)/tests/,$(TESTS))) $(TEST_MAIN)
LIBS_TESTS = -lcppunit
ifeq ($(BUILD_DYNAMIC_LIB),y)
LIBS_TESTS += -lebcorelib
TEST_NEEDS = $(LIB_DYNAMIC)
else
LIBS_TESTS += $(OUTDIR)/libebcorelib.a $(LIBS_LIB)
TEST_NEEDS = $(LIB_STATIC)
endif
ALL_OBJS = $(LIB_OBJS)
ifeq ($(BUILD_TESTS),y)
ALL_OBJS += $(TEST_OBJS)
endif
DEPS := $(addsuffix .d,$(ALL_OBJS))
TARGETS = $(LIB_TARGETS)
ifeq ($(BUILD_TESTS),y)
TARGETS += tests
endif
all: $(TARGETS)
run: tests
@if [ -z "$(E)" ]; then \
echo "\n\nPlease define E\n\n"; \
exit 1; \
else \
LD_LIBRARY_PATH=$(OUTDIR) $(OUTDIR)/$(E); \
fi
debug: tests
@if [ -z "$(E)" ]; then \
echo "\n\nPlease define E\n\n"; \
exit 1; \
else \
LD_LIBRARY_PATH=$(OUTDIR) debug $(OUTDIR)/$(E); \
fi
run-tests: tests
LD_LIBRARY_PATH=$(OUTDIR) ./$(TEST_ALL)
clean:
rm -f $(TEST_SUITES) $(TEST_ALL) $(TEST_OBJS) \
$(LIB_OBJS) $(LIB_TARGETS)
fullclean: clean
rm -f $(DEPS)
-rmdir $(sort $(dir $(TEST_OBJS)) $(dir $(LIB_OBJS)))
-rmdir $(OUTDIR)
tests: $(TEST_OBJS) $(TEST_SUITES) $(TEST_ALL)
.PHONY: all run debug run-tests clean fullclean tests
#------------------------------------------------------------------------------
# Actual building
-include $(DEPS)
# Library
$(LIB_DYNAMIC): $(LIB_OBJS)
$(CXX) $(LDFLAGS) -shared -Wl,-soname,$(notdir $(LIB_DYNAMIC)) \
-o $(LIB_DYNAMIC) \
$(LIB_OBJS) $(LIBS_LIB)
$(LIB_STATIC): $(LIB_OBJS)
ar crD $(LIB_STATIC) $(LIB_OBJS)
# Tests
$(TEST_SUITES): $(OUTDIR)/test-%$(SUFFIX_EXE): $(OUTDIR)/tests/%.o $(TEST_MAIN) $(TEST_NEEDS)
$(CXX) $(LDFLAGS) -o $@ $< $(TEST_MAIN) $(LIBS_TESTS)
$(TEST_ALL): $(TEST_OBJS)
$(CXX) $(LDFLAGS) -o $@ $(TEST_OBJS) $(LIBS_TESTS)
#------------------------------------------------------------------------------
# .cc -> .o
$(LIB_OBJS): $(OUTDIR)/%.o: src/%.cc
@if [ ! -d "$(dir $@)" ]; then mkdir -p "$(dir $@)"; fi
$(CXX) $(CPPFLAGS) $(CXXFLAGS_LIB) -c -o $@ $<
$(CXX) $(CPPFLAGS) $(CXXFLAGS_LIB) -M -MF $@.d -MT $@ $<
$(TEST_OBJS): $(OUTDIR)/%.o: %.cc
@if [ ! -d "$(dir $@)" ]; then mkdir -p "$(dir $@)"; fi
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -M -MF $@.d -MT $@ $<