raylib-cpp-template/Makefile
2023-07-20 03:15:54 +03:00

133 lines
3.4 KiB
Makefile

# ------------------------ Config -----------------------
# PLATFORM can be 'web' or 'desktop'
PLATFORM ?= desktop
SHELL := /bin/zsh
TOP_BUILD_DIR := build
EXECUTABLE := app
WEB_SHELL := src/shell.html
SUBMODULES_PATH := depends
COMPILER_FLAGS := -std=c++17
LINKER_FLAGS := -lraylib
SOURCES := $(wildcard src/*.cpp)
# ----------------- Prepare variables for targets ------------------
EXT :=
EMSDK_PATH := $(SUBMODULES_PATH)/emsdk
RAYLIB_PLATFORM := PLATFORM_DESKTOP
COMPILER_FLAGS += -I$(SUBMODULES_PATH)/raylib-cpp/include
COMPILER_FLAGS += -I$(SUBMODULES_PATH)/raylib/src
ifeq ($(OS), Windows_NT)
CXX ?= g++
THEN := &&
MKDIR := -mkdir -p
RM := -del /q
COPY = -robocopy "$1" "$2" $3
EXT = .exe
MOVE := move
OS_NAME := windows
LINKER_FLAGS += -Wl,--allow-multiple-definition -pthread -lopengl32
LINKER_FLAGS += -lgdi32 -lwinmm -mwindows -static -static-libgcc -static-libstdc++
else
CXX ?= g++
THEN := ;
MKDIR := mkdir -p
RM := rm -rf
COPY = cp $1/$3 $2
MOVE := mv
OS_NAME := linux
LINKER_FLAGS += -lGL -lm -lpthread -ldl -lrt -lX11
endif
BUILD_NAME := $(OS_NAME)-$(PLATFORM)
BUILD_DIR := $(TOP_BUILD_DIR)/$(BUILD_NAME)
RAYLIB_RELEASE_PATH := $(TOP_BUILD_DIR)/raylib-$(BUILD_NAME)
OBJECTS := $(patsubst src/%, $(BUILD_DIR)/%, $(patsubst %.cpp, %.o, $(SOURCES)))
OBJECT_DEPENDS := $(patsubst %.o, %.d, $(OBJECTS))
LIB_DEPENDENCIES :=
ifeq ($(PLATFORM), web)
export EMSDK_QUIET=1
RAYLIB_PLATFORM := PLATFORM_WEB
CXX := source $(SUBMODULES_PATH)/emsdk/emsdk_env.sh $(THEN) emcc
EXT = .html
EMSCRIPTEN_PATH ?= $(EMSDK_PATH)/upstream/emscripten
COMPILER_FLAGS += -I$(EMSCRIPTEN_PATH)/cache/sysroot/include
LINKER_FLAGS += -s USE_GLFW=3
LINKER_FLAGS += -s FORCE_FILESYSTEM=1
LINKER_FLAGS += $(RAYLIB_RELEASE_PATH)/libraylib.a
LINKER_FLAGS += --shell-file $(WEB_SHELL)
LIB_DEPENDENCIES += emsdk
endif
LINKER_FLAGS += -L$(RAYLIB_RELEASE_PATH)
MAIN_TARGET := $(BUILD_DIR)/$(EXECUTABLE)$(EXT)
# ------------------------ Targets -----------------------
# Add all rules from dependency files
-include $(OBJECT_DEPENDS)
.DEFAULT_GOAL := all
# Lists phony targets for Makefile
.PHONY: all setup submodules run clean emsdk
# Default target, compiles, runss and cleans
all: clean $(MAIN_TARGET) run
# Sets up the project for compiling, generates includes and libs
setup: lib
# Pull and update the the build submodules
submodules:
git submodule update --init --recursive
# Build the raylib static library file and copy it into lib
lib: submodules $(LIB_DEPENDENCIES)
$(MKDIR) $(RAYLIB_RELEASE_PATH)
cd $(SUBMODULES_PATH)/raylib/src $(THEN) \
"$(MAKE)" \
PLATFORM=$(RAYLIB_PLATFORM) \
EMSDK_PATH=$(EMSDK_PATH) \
RAYLIB_RELEASE_PATH=../../../$(RAYLIB_RELEASE_PATH) -B
# Install and activate emscripten
emsdk: submodules
cd $(SUBMODULES_PATH)/emsdk $(THEN) ./emsdk install latest
cd $(SUBMODULES_PATH)/emsdk $(THEN) ./emsdk activate latest
# Link the program and create the executable
$(MAIN_TARGET): $(OBJECTS)
$(CXX) $(OBJECTS) -o $(MAIN_TARGET) $(LINKER_FLAGS) -D$(RAYLIB_PLATFORM)
ifeq ($(PLATFORM), web)
$(MOVE) $(MAIN_TARGET) $(BUILD_DIR)/index.html
endif
# Compile objects to the build directory
$(BUILD_DIR)/%.o: src/%.cpp Makefile
$(MKDIR) $(BUILD_DIR)
$(CXX) -MMD -MP -c $(COMPILER_FLAGS) $< -o $@ $(CXXFLAGS) -D$(RAYLIB_PLATFORM)
# Run the executable
run:
ifeq ($(PLATFORM), web)
python -m http.server 8080 -d $(BUILD_DIR)
else
$(MAIN_TARGET) $(ARGS)
endif
# Clean up all relevant files
clean:
$(RM) $(BUILD_DIR)