Merge pull request #3 from CapsCollective/auto-sources

Made Makefile automatically gather compilation sources and implemented conditional compilation
This commit is contained in:
Aryeh 2020-12-03 21:19:24 +11:00 committed by GitHub
commit 193da09dd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 71 deletions

View File

@ -15,12 +15,12 @@ jobs:
- name: make setup - name: make setup
run: make setup run: make setup
- name: make compile - name: make bin/app
run: make compile run: make bin/app
- name: make clean - name: make clean
run: make clean run: make clean
- name: make compile CXX=g++ - name: make bin/app CXX=g++
run: make compile CXX=g++ run: make bin/app CXX=g++
- name: make clean - name: make clean
run: make clean run: make clean

View File

@ -19,12 +19,12 @@ jobs:
- name: make setup - name: make setup
run: make setup run: make setup
- name: make compile - name: make bin/app
run: make compile run: make bin/app
- name: make clean - name: make clean
run: make clean run: make clean
- name: make compile CXX=g++ - name: make bin/app CXX=g++
run: make compile CXX=g++ run: make bin/app CXX=g++
- name: make clean - name: make clean
run: make clean run: make clean

View File

@ -1,4 +1,3 @@
name: Windows name: Windows
on: on:
@ -18,18 +17,18 @@ jobs:
run: mingw32-make setup run: mingw32-make setup
shell: cmd shell: cmd
- name: make compile - name: make bin/app
run: mingw32-make compile run: mingw32-make bin/app
shell: cmd shell: cmd
- name: make clean - name: make clean
run: mingw32-make clean run: mingw32-make clean
shell: cmd shell: cmd
- name: make compile CXX=g++ - name: make bin/app CXX=g++
run: mingw32-make compile CXX=g++ run: mingw32-make bin/app CXX=g++
shell: cmd shell: cmd
- name: make clean - name: make clean
run: mingw32-make clean run: mingw32-make clean
shell: cmd shell: cmd

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
include include
lib lib
build bin
.idea

106
Makefile
View File

@ -1,45 +1,58 @@
# Set general macros # Define custom functions
buildFile = build/app rwildcard = $(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
platformpth = $(subst /,$(PATHSEP),$1)
# Set global macros
buildDir := bin
executable := app
target := $(buildDir)/$(executable)
sources := $(call rwildcard,src/,*.cpp)
objects := $(patsubst src/%, $(buildDir)/%, $(patsubst %.cpp, %.o, $(sources)))
compileFlags := -std=c++17 -I include
linkFlags = -L lib/$(platform) -l raylib
# Check for Windows # Check for Windows
ifeq ($(OS), Windows_NT) ifeq ($(OS), Windows_NT)
# Set Windows macros # Set Windows macros
platform = Windows platform := Windows
compiler = g++ CXX ?= g++
options = -pthread -lopengl32 -lgdi32 -lwinmm -mwindows linkFlags += -Wl,--allow-multiple-definition -pthread -lopengl32 -lgdi32 -lwinmm -mwindows
THEN = && libGenDir := src
THEN := &&
PATHSEP := \$(BLANK)
MKDIR := -mkdir
RM := -del /q
COPY = -robocopy "$(call platformpth,$1)" "$(call platformpth,$2)" $3
else else
# Check for MacOS/Linux # Check for MacOS/Linux
UNAMEOS = $(shell uname) UNAMEOS := $(shell uname)
ifeq ($(UNAMEOS), Linux) ifeq ($(UNAMEOS), Linux)
# Set Linux macros # Set Linux macros
platform = Linux platform := Linux
compiler = g++ CXX ?= g++
options = -l GL -l m -l pthread -l dl -l rt -l X11 linkFlags += -l GL -l m -l pthread -l dl -l rt -l X11
libGenDirectory = # Empty
endif endif
ifeq ($(UNAMEOS), Darwin) ifeq ($(UNAMEOS), Darwin)
# Set macOS macros # Set macOS macros
platform = macOS platform := macOS
compiler = clang++ CXX ?= clang++
options = -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL linkFlags += -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL
libGenDirectory = src libGenDir := src
endif endif
# Set UNIX macros # Set UNIX macros
THEN = ; THEN := ;
endif PATHSEP := /
MKDIR := mkdir -p
# Explicitly set compiler to platform default if unset RM := rm -rf
ifeq ($(CXX),) COPY = cp $1$(PATHSEP)$3 $2
CXX = $(compiler)
endif endif
# Lists phony targets for Makefile # Lists phony targets for Makefile
.PHONY: all setup submodules compile execute clean .PHONY: all setup submodules execute clean
# Default target, compiles, executes and cleans # Default target, compiles, executes and cleans
all: compile execute clean all: $(target) execute clean
# Sets up the project for compiling, generates includes and libs # Sets up the project for compiling, generates includes and libs
setup: include lib setup: include lib
@ -50,47 +63,30 @@ submodules:
# Copy the relevant header files into includes # Copy the relevant header files into includes
include: submodules include: submodules
ifeq ($(platform), Windows) $(MKDIR) $(call platformpth, ./include)
-mkdir .\include $(call COPY,vendor/raylib-cpp/vendor/raylib/src,./include,raylib.h)
-robocopy "vendor\raylib-cpp\vendor\raylib\src" "include" raylib.h raymath.h $(call COPY,vendor/raylib-cpp/vendor/raylib/src,./include,raymath.h)
-robocopy "vendor\raylib-cpp\include" "include" *.hpp $(call COPY,vendor/raylib-cpp/include,./include,*.hpp)
else
mkdir -p include
cp vendor/raylib-cpp/vendor/raylib/src/raylib.h vendor/raylib-cpp/vendor/raylib/src/raymath.h include
cp vendor/raylib-cpp/include/*.hpp include
endif
# 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
cd vendor/raylib-cpp/vendor/raylib/src $(THEN) "$(MAKE)" PLATFORM=PLATFORM_DESKTOP cd vendor/raylib-cpp/vendor/raylib/src $(THEN) "$(MAKE)" PLATFORM=PLATFORM_DESKTOP
ifeq ($(platform), Windows) $(MKDIR) $(call platformpth, lib/$(platform))
-mkdir lib\$(platform) $(call COPY,vendor/raylib-cpp/vendor/raylib/$(libGenDir),lib/$(platform),libraylib.a)
-robocopy "vendor\raylib-cpp\vendor\raylib\src" "lib\Windows" libraylib.a
else
mkdir -p lib/$(platform)
cp vendor/raylib-cpp/vendor/raylib/$(libGenDirectory)/libraylib.a lib/$(platform)/libraylib.a
endif
# Create the build folder # Link the program and create the executable
build: $(target): $(objects)
ifeq ($(platform), Windows) $(CXX) $(objects) -o $(target) $(linkFlags)
-mkdir build
else
mkdir -p build
endif
# Create the build folder and compile the executable # Compile objects to the build directory
compile: build $(buildDir)/%.o: src/%.cpp
$(CXX) -std=c++17 -I include -L lib/$(platform) src/main.cpp -o $(buildFile) -l raylib $(options) $(MKDIR) $(call platformpth, $(@D))
$(CXX) -c $(compileFlags) $< -o $@
# Run the executable # Run the executable
execute: execute:
$(buildFile) $(target)
# Clean up all relevant files # Clean up all relevant files
clean: clean:
ifeq ($(platform), Windows) $(RM) $(call platformpth, $(buildDir)/*)
-del build\app.exe
else
rm $(buildFile)
endif

View File

@ -1,5 +1,5 @@
# Raylib C++ Starter # Raylib C++ Starter
The Raylib C++ Starter kit is a template project that provides a simple starter template for the [raylib](https://github.com/raysan5/raylib) game tools library incorporating the [raylib-cpp](https://github.com/robloach/raylib-cpp) C++ bindings and using [Make](https://www.gnu.org/software/make/) for building. The starter kit can automatcially clone down raylib and the bindings, compile them, and setup the project for build using a static library. The Raylib C++ Starter kit is a template project that provides a simple starter template for the [raylib](https://github.com/raysan5/raylib) game tools library incorporating the [raylib-cpp](https://github.com/robloach/raylib-cpp) C++ bindings and using [Make](https://www.gnu.org/software/make/) for building. The starter kit can automatcially clone down raylib and the bindings, compile them, and setup the project for conditional compilation using a static library.
> Why static linking? > Why static linking?