From 2002bb961bbd2408dc4298aad2a5de2dff7240cb Mon Sep 17 00:00:00 2001 From: Jonathan Moallem Date: Tue, 1 Dec 2020 14:15:35 +1100 Subject: [PATCH 1/3] Made Makefile automatically gather compilation sources from src directory --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3fb79f9..3193ab2 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ ifeq ($(OS), Windows_NT) platform = Windows compiler = g++ options = -pthread -lopengl32 -lgdi32 -lwinmm -mwindows + sources = src/main.cpp THEN = && else # Check for MacOS/Linux @@ -27,6 +28,7 @@ else endif # Set UNIX macros + sources = $(shell find src -type f -name '*.cpp') THEN = ; endif @@ -81,7 +83,7 @@ endif # Create the build folder and compile the executable compile: build - $(CXX) -std=c++17 -I include -L lib/$(platform) src/main.cpp -o $(buildFile) -l raylib $(options) + $(CXX) -std=c++17 -I include -L lib/$(platform) $(sources) -o $(buildFile) -l raylib $(options) # Run the executable execute: From 828d38f16eddbdf59632f07d1515a342009af48c Mon Sep 17 00:00:00 2001 From: Jonathan Moallem Date: Thu, 3 Dec 2020 12:47:08 +1100 Subject: [PATCH 2/3] Updated Makefile for conditional compilation and recursive pattern searching --- .github/workflows/macOS.yml | 8 ++--- .github/workflows/ubuntu.yml | 8 ++--- .github/workflows/windows.yml | 11 +++--- .gitignore | 3 +- Makefile | 66 ++++++++++++++++++----------------- README.md | 2 +- 6 files changed, 50 insertions(+), 48 deletions(-) diff --git a/.github/workflows/macOS.yml b/.github/workflows/macOS.yml index 3cd5cdd..1428ca5 100644 --- a/.github/workflows/macOS.yml +++ b/.github/workflows/macOS.yml @@ -15,12 +15,12 @@ jobs: - name: make setup run: make setup - - name: make compile - run: make compile + - name: make bin/app + run: make bin/app - name: make clean run: make clean - - name: make compile CXX=g++ - run: make compile CXX=g++ + - name: make bin/app CXX=g++ + run: make bin/app CXX=g++ - name: make clean run: make clean \ No newline at end of file diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 0c95815..a96f2e1 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -19,12 +19,12 @@ jobs: - name: make setup run: make setup - - name: make compile - run: make compile + - name: make bin/app + run: make bin/app - name: make clean run: make clean - - name: make compile CXX=g++ - run: make compile CXX=g++ + - name: make bin/app CXX=g++ + run: make bin/app CXX=g++ - name: make clean run: make clean \ No newline at end of file diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index a9f8d9c..7f48b6a 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -1,4 +1,3 @@ - name: Windows on: @@ -18,18 +17,18 @@ jobs: run: mingw32-make setup shell: cmd - - name: make compile - run: mingw32-make compile + - name: make bin/app + run: mingw32-make bin/app shell: cmd - name: make clean run: mingw32-make clean shell: cmd - - name: make compile CXX=g++ - run: mingw32-make compile CXX=g++ + - name: make bin/app CXX=g++ + run: mingw32-make bin/app CXX=g++ shell: cmd - name: make clean run: mingw32-make clean - shell: cmd + shell: cmd \ No newline at end of file diff --git a/.gitignore b/.gitignore index 8908f5c..5148772 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ include lib -build \ No newline at end of file +bin +.idea \ No newline at end of file diff --git a/Makefile b/Makefile index 3193ab2..5f3fc1b 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,21 @@ -# Set general macros -buildFile = build/app +# Define custom functions +rwildcard = $(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)) + +# 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 ifeq ($(OS), Windows_NT) # Set Windows macros platform = Windows - compiler = g++ - options = -pthread -lopengl32 -lgdi32 -lwinmm -mwindows - sources = src/main.cpp + CXX ?= g++ + linkFlags += -pthread -lopengl32 -lgdi32 -lwinmm -mwindows THEN = && else # Check for MacOS/Linux @@ -15,33 +23,26 @@ else ifeq ($(UNAMEOS), Linux) # Set Linux macros platform = Linux - compiler = g++ - options = -l GL -l m -l pthread -l dl -l rt -l X11 - libGenDirectory = # Empty + CXX ?= g++ + linkFlags += -l GL -l m -l pthread -l dl -l rt -l X11 endif ifeq ($(UNAMEOS), Darwin) # Set macOS macros platform = macOS - compiler = clang++ - options = -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL - libGenDirectory = src + CXX ?= clang++ + linkFlags += -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL + libGenDir = src endif # Set UNIX macros - sources = $(shell find src -type f -name '*.cpp') THEN = ; endif -# Explicitly set compiler to platform default if unset -ifeq ($(CXX),) - CXX = $(compiler) -endif - # Lists phony targets for Makefile -.PHONY: all setup submodules compile execute clean +.PHONY: all setup submodules execute clean # Default target, compiles, executes and cleans -all: compile execute clean +all: $(target) execute clean # Sets up the project for compiling, generates includes and libs setup: include lib @@ -70,29 +71,30 @@ ifeq ($(platform), Windows) -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 + cp vendor/raylib-cpp/vendor/raylib/$(libGenDir)/libraylib.a lib/$(platform)/libraylib.a endif -# Create the build folder -build: +# Link the program and create the executable +$(target): $(objects) + $(CXX) $(objects) -o $(target) $(linkFlags) + +# Compile objects to the build directory +$(buildDir)/%.o: src/%.cpp ifeq ($(platform), Windows) - -mkdir build + -mkdir $(@D) else - mkdir -p build + mkdir -p $(@D) endif - -# Create the build folder and compile the executable -compile: build - $(CXX) -std=c++17 -I include -L lib/$(platform) $(sources) -o $(buildFile) -l raylib $(options) + $(CXX) -c $(compileFlags) $< -o $@ # Run the executable execute: - $(buildFile) + $(target) # Clean up all relevant files clean: ifeq ($(platform), Windows) - -del build\app.exe + -del /S $(buildDir)\* else - rm $(buildFile) -endif + rm -rf $(buildDir)/* +endif \ No newline at end of file diff --git a/README.md b/README.md index 5e20084..e535381 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # 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? From e29d85f0ee978df781aa964569001911eaccdd8f Mon Sep 17 00:00:00 2001 From: Jonathan Moallem Date: Thu, 3 Dec 2020 21:13:24 +1100 Subject: [PATCH 3/3] Added platform-generic commands to Makefile and fixed Windows linking issues --- Makefile | 72 +++++++++++++++++++++++++------------------------------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 5f3fc1b..c81c1c4 100644 --- a/Makefile +++ b/Makefile @@ -1,41 +1,51 @@ # Define custom functions 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 +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 ifeq ($(OS), Windows_NT) # Set Windows macros - platform = Windows + platform := Windows CXX ?= g++ - linkFlags += -pthread -lopengl32 -lgdi32 -lwinmm -mwindows - THEN = && + linkFlags += -Wl,--allow-multiple-definition -pthread -lopengl32 -lgdi32 -lwinmm -mwindows + libGenDir := src + THEN := && + PATHSEP := \$(BLANK) + MKDIR := -mkdir + RM := -del /q + COPY = -robocopy "$(call platformpth,$1)" "$(call platformpth,$2)" $3 else # Check for MacOS/Linux - UNAMEOS = $(shell uname) + UNAMEOS := $(shell uname) ifeq ($(UNAMEOS), Linux) # Set Linux macros - platform = Linux + platform := Linux CXX ?= g++ linkFlags += -l GL -l m -l pthread -l dl -l rt -l X11 endif ifeq ($(UNAMEOS), Darwin) # Set macOS macros - platform = macOS + platform := macOS CXX ?= clang++ linkFlags += -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL - libGenDir = src + libGenDir := src endif # Set UNIX macros - THEN = ; + THEN := ; + PATHSEP := / + MKDIR := mkdir -p + RM := rm -rf + COPY = cp $1$(PATHSEP)$3 $2 endif # Lists phony targets for Makefile @@ -53,26 +63,16 @@ submodules: # Copy the relevant header files into includes include: submodules -ifeq ($(platform), Windows) - -mkdir .\include - -robocopy "vendor\raylib-cpp\vendor\raylib\src" "include" raylib.h raymath.h - -robocopy "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 + $(MKDIR) $(call platformpth, ./include) + $(call COPY,vendor/raylib-cpp/vendor/raylib/src,./include,raylib.h) + $(call COPY,vendor/raylib-cpp/vendor/raylib/src,./include,raymath.h) + $(call COPY,vendor/raylib-cpp/include,./include,*.hpp) # Build the raylib static library file and copy it into lib lib: submodules cd vendor/raylib-cpp/vendor/raylib/src $(THEN) "$(MAKE)" PLATFORM=PLATFORM_DESKTOP -ifeq ($(platform), Windows) - -mkdir lib\$(platform) - -robocopy "vendor\raylib-cpp\vendor\raylib\src" "lib\Windows" libraylib.a -else - mkdir -p lib/$(platform) - cp vendor/raylib-cpp/vendor/raylib/$(libGenDir)/libraylib.a lib/$(platform)/libraylib.a -endif + $(MKDIR) $(call platformpth, lib/$(platform)) + $(call COPY,vendor/raylib-cpp/vendor/raylib/$(libGenDir),lib/$(platform),libraylib.a) # Link the program and create the executable $(target): $(objects) @@ -80,11 +80,7 @@ $(target): $(objects) # Compile objects to the build directory $(buildDir)/%.o: src/%.cpp -ifeq ($(platform), Windows) - -mkdir $(@D) -else - mkdir -p $(@D) -endif + $(MKDIR) $(call platformpth, $(@D)) $(CXX) -c $(compileFlags) $< -o $@ # Run the executable @@ -93,8 +89,4 @@ execute: # Clean up all relevant files clean: -ifeq ($(platform), Windows) - -del /S $(buildDir)\* -else - rm -rf $(buildDir)/* -endif \ No newline at end of file + $(RM) $(call platformpth, $(buildDir)/*) \ No newline at end of file