Skip to content

Instantly share code, notes, and snippets.

@HellRok
Created April 20, 2024 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HellRok/abf9c6c78ab1743e98403e756574a93e to your computer and use it in GitHub Desktop.
Save HellRok/abf9c6c78ab1743e98403e756574a93e to your computer and use it in GitHub Desktop.
A simple makefile for using Zig to compile C++ for Linux, Windows, and OSX
# To use this your file structure should look like this
#
# project_root/
# src/main.cpp
# include/*.hpp
.DEFAULT_GOAL = run-native
CXX := zig c++
CXXFLAGS := -std=c++2a -Wall -Wextra -pedantic-errors
INCLUDES := -I ./include/
SRC := $(shell find ./src/**/ -name "*.cpp")
OBJECTS := $(SRC:%.cpp=./build/%.o)
BINARY := sys_mon
release-all: clean-all
make clean
make release-linux-x64
make clean
make release-linux-arm
make clean
make release-osx-x64
make clean
make release-osx-arm
make clean
make release-windows-x64
make clean
make release-windows-arm
%-native: CXX = g++
%-native: PLATFORM = native
run-native: build-native
run-native: run
build-native: build
test-native: test
test-native: run-test
release-native: CXXFLAGS += -O2
release-native: build-native
%-linux-x64: TARGET = -target x86_64-linux
%-linux-x64: PLATFORM = linux-x64
run-linux-x64: build-linux-x64
run-linux-x64: run
build-linux-x64: build
test-linux-x64: test
test-linux-x64: run-test
release-linux-x64: CXXFLAGS += -O2
release-linux-x64: build-linux-x64
%-linux-arm: TARGET = -target aarch64-linux
%-linux-arm: PLATFORM = linux-arm
run-linux-arm: build-linux-arm
run-linux-arm: run
build-linux-arm: build
test-linux-arm: test
test-linux-arm: run-test
release-linux-arm: CXXFLAGS += -O2
release-linux-arm: build-linux-arm
%-osx-arm: TARGET = -target aarch64-macos
%-osx-arm: PLATFORM = osx-arm
run-osx-arm: build-osx-arm
run-osx-arm: run
build-osx-arm: build
test-osx-arm: test
test-osx-arm: run-test
release-osx-arm: CXXFLAGS += -O2
release-osx-arm: build-osx-arm
%-osx-x64: TARGET = -target x86_64-macos
%-osx-x64: PLATFORM = osx-x64
run-osx-x64: build-osx-x64
run-osx-x64: run
build-osx-x64: build
test-osx-x64: test
test-osx-x64: run-test
release-osx-x64: CXXFLAGS += -O2
release-osx-x64: build-osx-x64
%-windows-x64: BINARY := $(BINARY).exe
%-windows-x64: TARGET = -target x86_64-windows
%-windows-x64: PLATFORM = windows-x64
run-windows-x64: build-windows-x64
run-windows-x64: run
build-windows-x64: build
test-windows-x64: test
test-windows-x64: run-test
# -fno-lto is currently required due to this issue.
# https://github.com/ziglang/zig/issues/15958
release-windows-x64: CXXFLAGS += -O2 -fno-lto
release-windows-x64: build-windows-x64
%-windows-arm: BINARY := $(BINARY).exe
%-windows-arm: TARGET = -target aarch64-windows
%-windows-arm: PLATFORM = windows-arm
run-windows-arm: build-windows-arm
run-windows-arm: run
build-windows-arm: build
test-windows-arm: test
test-windows-arm: run-test
# -fno-lto is currently required due to this issue.
# https://github.com/ziglang/zig/issues/15958
release-windows-x64: CXXFLAGS += -O2 -fno-lto
release-windows-arm: build-windows-arm
./build/%.o: %.cpp
@mkdir -p $(@D)
$(CXX) $(TARGET) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
build: src/main.cpp $(OBJECTS)
mkdir -p ./dist/$(PLATFORM)
$(CXX) \
$(TARGET) \
-o ./dist/$(PLATFORM)/$(BINARY) \
$(CXXFLAGS) \
$(INCLUDES) \
$^
test: src/test.cpp $(OBJECTS)
mkdir -p ./dist/$(PLATFORM)
$(CXX) \
$(TARGET) \
-o ./dist/$(PLATFORM)/test-$(BINARY) \
$(CXXFLAGS) \
$(INCLUDES) \
$^
run:
./dist/$(PLATFORM)/$(BINARY)
run-test:
./dist/$(PLATFORM)/test-$(BINARY)
clean:
rm -rf ./build/
clean-all: clean
rm -rf ./dist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment