Skip to content

Instantly share code, notes, and snippets.

@yannvanhalewyn
Last active August 21, 2022 22:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yannvanhalewyn/5d8a776e7e4a1bc13832 to your computer and use it in GitHub Desktop.
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.
# 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