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 build
lib
bin
.cache .cache
compile_commands.json compile_commands.json

157
Makefile
View File

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

2
compile_flags.txt Normal file
View File

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