refactor Makefile

This commit is contained in:
Rokas Puzonas 2023-07-19 20:56:37 +03:00
parent 6a7266b524
commit ef426d2df0
3 changed files with 88 additions and 75 deletions

4
.gitignore vendored
View File

@ -1,5 +1,3 @@
include
lib
bin
build
.cache
compile_commands.json

157
Makefile
View File

@ -1,114 +1,127 @@
# Copyright (c) 2020 Jonathan Moallem (@J-Mo63) & Aryeh Zinn (@Raelr)
#
# This code is released under an unmodified zlib license.
# For conditions of distribution and use, please see:
# https://opensource.org/licenses/Zlib
# ------------------------ Config -----------------------
# Define custom functions
rwildcard = $(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
platformpth = $(subst /,$(PATHSEP),$1)
# PLATFORM can be 'web' or 'desktop'
PLATFORM ?= desktop
# Set global macros
buildDir := bin
executable := boids-playground
target := $(buildDir)/$(executable)
sources := $(call rwildcard,src/,*.cpp)
# objects := $(patsubst src/%, $(buildDir)/%, $(patsubst %.cpp, %.o, $(sources)))
objects := $(buildDir)/main.o
depends := $(patsubst %.o, %.d, $(objects))
compileFlags := -std=c++17 -I include
linkFlags = -lraylib
emsdk_path = /usr/lib/emscripten
ext =
TOP_BUILD_DIR := build
EXECUTABLE := boids-playground
WEB_SHELL := src/shell.html
SUBMODULES_PATH := depends
# PLATFORM_DESKTOP or PLATFORM_WEB
PLATFORM ?= PLATFORM_DESKTOP
COMPILER_FLAGS := -std=c++17
LINKER_FLAGS := -lraylib
# SOURCES := $(wildcard src/*.cpp)
SOURCES := src/main.cpp
# ----------------- Prepare variables for targets ------------------
EXT :=
EMSDK_PATH := $(SUBMODULES_PATH)/emsdk
RAYLIB_PLATFORM := PLATFORM_DESKTOP
COMPILER_FLAGS += -I$(SUBMODULES_PATH)/raylib/src
COMPILER_FLAGS += -I$(SUBMODULES_PATH)/raylib-cpp/include
# Check for Windows
ifeq ($(OS), Windows_NT)
# Set Windows macros
osName := windows
CXX ?= g++
linkFlags += -Wl,--allow-multiple-definition -pthread -lopengl32 -lgdi32 -lwinmm -mwindows -static -static-libgcc -static-libstdc++
THEN := &&
PATHSEP := \$(BLANK)
MKDIR := -mkdir -p
RM := -del /q
COPY = -robocopy "$(call platformpth,$1)" "$(call platformpth,$2)" $3
else
# Set Linux macros
osName := linux
CXX ?= g++
linkFlags += -lGL -lm -lpthread -ldl -lrt -lX11
COPY = -robocopy "$1" "$2" $3
EXT = .exe
# Set UNIX macros
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 := ;
PATHSEP := /
MKDIR := mkdir -p
RM := rm -rf
COPY = cp $1$(PATHSEP)$3 $2
COPY = cp $1/$3 $2
OS_NAME := linux
LINKER_FLAGS += -lGL -lm -lpthread -ldl -lrt -lX11
endif
RAYLIB_RELEASE_PATH = lib/$(osName)
ifeq ($(PLATFORM), PLATFORM_WEB)
RAYLIB_RELEASE_PATH = lib/$(osName)-web
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)
RAYLIB_PLATFORM := PLATFORM_WEB
CXX := emcc
EXT = .html
compileFlags += -I$(emscripten_path)/cache/sysroot/include
emscripten_path ?= $(emsdk_path)/upstream/emscripten
linkFlags += -s USE_GLFW=3 -s FORCE_FILESYSTEM=1 $(RAYLIB_RELEASE_PATH)/libraylib.a --shell-file src/shell.html
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
linkFlags += -L$(RAYLIB_RELEASE_PATH)
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
.PHONY: all setup submodules run clean emsdk
# Default target, compiles, runss and cleans
all: $(target) run clean
all: clean $(MAIN_TARGET) run
# Sets up the project for compiling, generates includes and libs
setup: include lib
setup: lib
# Pull and update the the build submodules
submodules:
git submodule update --init --recursive
# Copy the relevant header files into includes
include: submodules
$(MKDIR) $(call platformpth, ./include)
$(call COPY,depends/raylib/src,./include,raylib.h)
$(call COPY,depends/raylib/src,./include,raymath.h)
$(call COPY,depends/raylib/src,./include,rlgl.h)
$(call COPY,depends/raylib-cpp/include,./include,*.hpp)
# Build the raylib static library file and copy it into lib
lib: submodules
$(MKDIR) $(call platformpth, $(RAYLIB_RELEASE_PATH))
cd depends/raylib/src $(THEN) "$(MAKE)" PLATFORM=$(PLATFORM) EMSDK_PATH=$(emsdk_path) RAYLIB_RELEASE_PATH=../../../$(RAYLIB_RELEASE_PATH) -B
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
EMSDK_QUIET=1 source $(SUBMODULES_PATH)/emsdk/emsdk_env.sh
# Link the program and create the executable
$(target): $(objects)
$(CXX) $(objects) -o $(target)$(ext) -DPLATFORM=$(PLATFORM) $(linkFlags)
# Add all rules from dependency files
-include $(depends)
$(MAIN_TARGET): $(OBJECTS)
$(CXX) $(OBJECTS) -o $(MAIN_TARGET) $(LINKER_FLAGS) -D$(RAYLIB_PLATFORM)
# Compile objects to the build directory
$(buildDir)/%.o: src/%.cpp Makefile
$(MKDIR) $(call platformpth, $(@D))
$(CXX) -MMD -MP -c $(compileFlags) $< -o $@ $(CXXFLAGS) -D$(PLATFORM)
$(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), PLATFORM_WEB)
serve $(buildDir) $(ARGS)
ifeq ($(PLATFORM), web)
serve $(BUILD_DIR) $(ARGS)
else
$(target) $(ARGS)
$(MAIN_TARGET) $(ARGS)
endif
# Clean up all relevant files
clean:
$(RM) $(call platformpth, $(buildDir)/*)
# emcc -o bin/game.html bin/main.o bin/raycast.o -std=c99 -Wall -Wno-missing-braces -Wunused-result -D_DEFAULT_SOURCE -Os -I. -I../raylib/src -I../raylib/src/external -I../raylib/src/extras -I/cache/sysroot/include -L. -L../depends/raylib/src -L../depends/raylib/src -s USE_GLFW=3 -s TOTAL_MEMORY=134217728 -s FORCE_FILESYSTEM=1 ~/code/fun/raylib-game-template/raylib/src/libraylib.a -DPLATFORM_WEB -s ASYNCIFY
$(RM) $(BUILD_DIR)

2
compile_flags.txt Normal file
View File

@ -0,0 +1,2 @@
-Idepends/raylib-cpp/include/
-Idepends/raylib/src/