Last active
August 21, 2022 22:56
-
-
Save yannvanhalewyn/5d8a776e7e4a1bc13832 to your computer and use it in GitHub Desktop.
A generic makefile to generate a Mac OSX style .app package with resources (or compile a relatively big c++ project). All you need is code in a src/ dir, and eventually a Resources directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Compiler | |
CC = g++ | |
# Flags | |
CFLAGS = -c -Wall -std=c++11 | |
LDLFLAGS = # -lSDL2 -lSDL2_mixer -lSDL2_ttf -framework OpenGL -framework CoreFoundation | |
# Directories | |
SRCDIR = src | |
OBJDIR = obj | |
BINDIR = bin | |
RSCDIR = Resources | |
# Executables | |
EXEC = main | |
EXEC := $(addprefix $(BINDIR)/, $(EXEC)) | |
# Files | |
SOURCES := $(shell find $(SRCDIR) -name '*.cpp') | |
HEADERS := $(shell find $(SRCDIR) -name '*.hpp') | |
OBJECTS := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o) | |
RESOURCES := $(shell find $(RSCDIR) -type f) | |
# Package | |
PKGNAME = MyApp | |
PKG := $(PKGNAME).app | |
PKGCONTENTS := $(PKG)/Contents | |
PKGEXEC := $(PKGCONTENTS)/MacOS/$(PKGNAME) | |
PKGRSCDIR := $(PKGCONTENTS)/Resources | |
PKGRESOURCES := $(RESOURCES:$(RSCDIR)/%=$(PKGRSCDIR)/%) | |
# ====== | |
# Making | |
# ====== | |
# Depends on the resources and the executable | |
$(PKG): $(PKGRESOURCES) $(PKGEXEC) | |
# Copy all resources not copied yet or updated after last copy, that's what | |
# make's make (pun intended) | |
$(PKGRESOURCES): $(PKGRSCDIR)/%: $(RSCDIR)/% | |
@[ -d $(dir $@) ] || mkdir -p $(dir $@) | |
cp $< $@ | |
# Copy the executable | |
$(PKGEXEC): $(EXEC) | |
@[ -d $(dir $@) ] || mkdir -p $(dir $@) | |
cp $(EXEC) $(PKGEXEC) | |
# Link the objects to create a binary executable | |
$(EXEC): $(OBJECTS) | |
@[ -d $(BINDIR) ] || mkdir $(BINDIR) | |
$(CC) $(LDLFLAGS) $^ -o $@ | |
# Compile all sources into objects | |
$(OBJECTS): $(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(HEADERS) | |
@[ -d $(dir $@) ] || mkdir -p $(dir $@) | |
$(CC) $(CFLAGS) $< -o $@ | |
# ======= | |
# Helpers | |
# ======= | |
clean: | |
rm -f $(EXEC) $(OBJECTS) | |
rm -rf $(PKG) | |
print_vars: | |
@echo SOURCES: $(SOURCES) | |
@echo OBJECTS: $(OBJECTS) | |
@echo HEADERS: $(HEADERS) | |
@echo EXEC: $(EXEC) | |
@echo RESOURCES: $(RESOURCES) "\n" | |
@echo PKGNAME: $(PKGNAME) "\n" | |
@echo PKGCONTENTS: $(PKGCONTENTS) "\n" | |
@echo PKGEXEDIR: $(PKGEXEDIR) "\n" | |
@echo PKGRSCDIR: $(PKGRSCDIR) "\n" | |
@echo RESOURCES: $(RESOURCES) "\n" | |
@echo PKGRESOURCES: $(PKGRESOURCES) "\n" | |
# Create/Update and run the binary (Will not create/update the package) | |
run: $(EXEC) | |
@./$(EXEC) | |
# Create/Update and run the package | |
run_package: $(PKG) | |
@open $(PKG) | |
.PHONY: clean clean_all print_vars run run_package |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment