Skip to content

Instantly share code, notes, and snippets.

@lbfalvy
Created May 11, 2020 10:30
Show Gist options
  • Select an option

  • Save lbfalvy/f9b5b44d5ae389375ad6f6fa0b8037da to your computer and use it in GitHub Desktop.

Select an option

Save lbfalvy/f9b5b44d5ae389375ad6f6fa0b8037da to your computer and use it in GitHub Desktop.
CXX := -c++
BUILD := bin
APP_DIR := $(BUILD)/apps
OBJ_DIR := $(BUILD)/objects
CXXFLAGS := -Wall -Wextra -Werror \
-Wno-unused-parameter -Wno-missing-field-initializers
CXXFLAGS_EXE := $(CXXFLAGS) -rdynamic
CXXFLAGS_LIB := $(CXXFLAGS) -fPIC -shared
LDFLAGS := -L/usr/lib -lstdc++ -lm -lportaudio
LDFLAGS_EXE := $(LDFLAGS) -ldl
LDFLAGS_LIB := $(LDFLAGS)
INTERFACE := interface/
EXE := program
LIBS := libs
# ./
# src/ - executable source files
# include/ - executable headers
# interface/ - common headers with libs
# libs/ - shared object library sources
# first_library/
# src/ - shared object source files
# include/ - shared object headers
# bin/
# apps/
# program
# libs/
# first_library.so
# objects/
# program/ - object files for executable
# libs/
# first_library/ - object files for shared object library
scandir = $(shell find $(1) -name '$(2)')
# =====================================
# ======== The main executable ========
# =====================================
# Every .cpp file
SOURCES := $(call scandir, src,*.cpp)
# Their respective .o files
OBJECTS := $(SOURCES:src/%.cpp=$(OBJ_DIR)/$(EXE)/%.o)
# Build the exe from the .o files
$(APP_DIR)/$(EXE): $(OBJECTS)
$(CXX) $(CXXFLAGS_EXE) -o $(APP_DIR)/$(EXE) $^ $(LDFLAGS_EXE)
# Build any .o file of the exe
$(OBJ_DIR)/$(EXE)/%.o: src/%.cpp
$(CXX) $(CXXFLAGS_EXE) -I$(INTERFACE) -Iinclude/ -c $< -o $@ $(LDFLAGS_EXE)
# ===========================
# ======== Libraries ========
# ===========================
# List of all library names
LIB_NAMES := $(notdir $(wildcard $(LIBS)/*))
# List of all SO files
SO_FILES := $(foreach lib,$(LIB_NAMES), $(APP_DIR)/libs/$(lib).so)
# Function to get library source files
LIB_SOURCES = $(call scandir, $(LIBS)/$(1)/src,*.cpp)
# Function to get library object files
LIB_OBJECTS = $(LIB_SOURCES:$(LIBS)/$(1)/src/%.cpp=$(OBJ_DIR)/libs/$(1)/%.o)
# Build library $(1)
define LIBRARY_TARGETS
$(APP_DIR)/libs/$(1).so: $(call LIB_OBJECTS,$(1))
$(CXX) $(CXXFLAGS_LIB) -o $(APP_DIR)/libs/$(1).so $(call LIB_OBJECTS,$(1)) $(LDFLAGS_LIB)
# Build any .o from any .cpp within the library
$(OBJ_DIR)/libs/$(1)/%.o: $(LIBS)/$(1)/src/%.cpp
$(CXX) $(CXXFLAGS_LIB) -I$(INTERFACE) -I$(LIBS)/$(1)/include -c $$< -o $$@ $(LDFLAGS_LIB)
endef
# Build all libraries
$(foreach lib,$(LIB_NAMES),$(eval $(call LIBRARY_TARGETS,$(lib))))
# ===================================
# ======== Commands and misc ========
# ===================================
.PHONY: all build clean debug release list_libs
# Build the exe and every .so file
all: build $(APP_DIR)/$(EXE) $(SO_FILES)
build:
@mkdir -p $(APP_DIR)/libs
@mkdir -p $(OBJ_DIR)/$(EXE)
$(foreach lib,$(LIB_NAMES),@mkdir -p $(OBJ_DIR)/libs/$(lib);)
debug: CXXFLAGS += -DDEBUG -g
debug: all
release: CXXFLAGS += -O2
release: all
clean:
-@rm -rvf $(BUILD)/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment