nstool/makefile

197 lines
5.8 KiB
Makefile
Raw Permalink Normal View History

2019-02-17 07:21:19 +00:00
# C++/C Recursive Project Makefile
# (c) Jack
2021-11-10 07:12:01 +00:00
# Version 6 (20211110)
2019-02-17 07:21:19 +00:00
2019-02-27 05:51:06 +00:00
# Project Name
2019-01-31 09:10:19 +00:00
PROJECT_NAME = nstool
2019-02-17 07:21:19 +00:00
# Project Relative Paths
PROJECT_PATH = $(CURDIR)
PROJECT_SRC_PATH = src
PROJECT_SRC_SUBDIRS = $(PROJECT_SRC_PATH)
#PROJECT_INCLUDE_PATH = include
#PROJECT_TESTSRC_PATH = test
#PROJECT_TESTSRC_SUBDIRS = $(PROJECT_TESTSRC_PATH)
PROJECT_BIN_PATH = bin
#PROJECT_DOCS_PATH = docs
2019-02-17 07:21:19 +00:00
#PROJECT_DOXYFILE_PATH = Doxyfile
# Determine if the root makefile has been established, and if not establish this makefile as the root makefile
2019-01-31 09:10:19 +00:00
ifeq ($(ROOT_PROJECT_NAME),)
export ROOT_PROJECT_NAME = $(PROJECT_NAME)
export ROOT_PROJECT_PATH = $(PROJECT_PATH)
export ROOT_PROJECT_DEPENDENCY_PATH = $(ROOT_PROJECT_PATH)/deps
endif
2019-02-17 07:21:19 +00:00
# Shared Library Definitions
2019-02-27 05:51:06 +00:00
PROJECT_SO_VER_MAJOR = 0
PROJECT_SO_VER_MINOR = 1
PROJECT_SO_VER_PATCH = 0
PROJECT_SONAME = $(PROJECT_NAME).so.$(PROJECT_SO_VER_MAJOR)
PROJECT_SO_FILENAME = $(PROJECT_SONAME).$(PROJECT_SO_VER_MINOR).$(PROJECT_SO_VER_PATCH)
2019-02-17 07:21:19 +00:00
# Project Dependencies
2022-06-29 13:19:36 +00:00
PROJECT_DEPEND = pietendo toolchain fmt lz4 mbedtls
PROJECT_DEPEND_LOCAL_DIR = libpietendo libtoolchain libfmt liblz4 libmbedtls
2019-02-17 07:21:19 +00:00
# Generate compiler flags for including project include path
2019-01-31 09:10:19 +00:00
ifneq ($(PROJECT_INCLUDE_PATH),)
INC += -I"$(PROJECT_INCLUDE_PATH)"
endif
2019-02-17 07:21:19 +00:00
# Generate compiler flags for local included dependencies
ifneq ($(PROJECT_DEPEND_LOCAL_DIR),)
LIB += $(foreach dep,$(PROJECT_DEPEND_LOCAL_DIR), -L"$(ROOT_PROJECT_DEPENDENCY_PATH)/$(dep)/bin")
INC += $(foreach dep,$(PROJECT_DEPEND_LOCAL_DIR), -I"$(ROOT_PROJECT_DEPENDENCY_PATH)/$(dep)/include")
2019-01-31 09:10:19 +00:00
endif
2019-02-17 07:21:19 +00:00
# Generate compiler flags for external dependencies
ifneq ($(PROJECT_DEPEND),)
LIB += $(foreach dep,$(PROJECT_DEPEND), -l$(dep))
2019-02-17 07:21:19 +00:00
endif
# Detect Platform
ifeq ($(PROJECT_PLATFORM),)
ifeq ($(OS), Windows_NT)
export PROJECT_PLATFORM = WIN32
2019-01-31 09:10:19 +00:00
else
2019-02-17 07:21:19 +00:00
UNAME = $(shell uname -s)
ifeq ($(UNAME), Darwin)
export PROJECT_PLATFORM = MACOS
else
export PROJECT_PLATFORM = GNU
endif
2019-01-31 09:10:19 +00:00
endif
endif
2019-02-17 07:21:19 +00:00
2021-11-10 07:12:01 +00:00
# Detect Architecture
ifeq ($(PROJECT_PLATFORM_ARCH),)
ifeq ($(PROJECT_PLATFORM), WIN32)
export PROJECT_PLATFORM_ARCH = x86_64
else ifeq ($(PROJECT_PLATFORM), GNU)
export PROJECT_PLATFORM_ARCH = $(shell uname -m)
else ifeq ($(PROJECT_PLATFORM), MACOS)
export PROJECT_PLATFORM_ARCH = $(shell uname -m)
else
export PROJECT_PLATFORM_ARCH = x86_64
endif
endif
2019-02-17 07:21:19 +00:00
# Generate platform specific compiler flags
ifeq ($(PROJECT_PLATFORM), WIN32)
# Windows Flags/Libs
2019-01-31 09:10:19 +00:00
CC = x86_64-w64-mingw32-gcc
CXX = x86_64-w64-mingw32-g++
2019-02-17 07:21:19 +00:00
WARNFLAGS = -Wall -Wno-unused-value -Wno-unused-but-set-variable
2021-11-10 07:12:01 +00:00
ARCHFLAGS =
2019-02-17 07:21:19 +00:00
INC +=
LIB += -static
ARFLAGS = cr -o
else ifeq ($(PROJECT_PLATFORM), GNU)
# GNU/Linux Flags/Libs
#CC =
#CXX =
WARNFLAGS = -Wall -Wno-unused-value -Wno-unused-but-set-variable
2021-11-10 07:12:01 +00:00
ARCHFLAGS =
2019-02-17 07:21:19 +00:00
INC +=
LIB +=
ARFLAGS = cr -o
else ifeq ($(PROJECT_PLATFORM), MACOS)
# MacOS Flags/Libs
#CC =
#CXX =
WARNFLAGS = -Wall -Wno-unused-value -Wno-unused-private-field
2021-11-10 07:12:01 +00:00
ARCHFLAGS = -arch $(PROJECT_PLATFORM_ARCH)
2019-02-17 07:21:19 +00:00
INC +=
LIB +=
ARFLAGS = rc
2019-01-31 09:10:19 +00:00
endif
2019-02-17 07:21:19 +00:00
# Compiler Flags
2021-11-10 07:12:01 +00:00
CXXFLAGS = -std=c++11 $(INC) $(WARNFLAGS) $(ARCHFLAGS) -fPIC
CFLAGS = -std=c11 $(INC) $(WARNFLAGS) $(ARCHFLAGS) -fPIC
2019-02-17 07:21:19 +00:00
2019-01-31 09:10:19 +00:00
# Object Files
2021-11-10 07:12:01 +00:00
SRC_OBJ = $(foreach dir,$(PROJECT_SRC_SUBDIRS),$(subst .cpp,.o,$(wildcard $(dir)/*.cpp))) $(foreach dir,$(PROJECT_SRC_SUBDIRS),$(subst .cc,.o,$(wildcard $(dir)/*.cc))) $(foreach dir,$(PROJECT_SRC_SUBDIRS),$(subst .c,.o,$(wildcard $(dir)/*.c)))
TESTSRC_OBJ = $(foreach dir,$(PROJECT_TESTSRC_SUBDIRS),$(subst .cpp,.o,$(wildcard $(dir)/*.cpp))) $(foreach dir,$(PROJECT_TESTSRC_SUBDIRS),$(subst .cc,.o,$(wildcard $(dir)/*.cc))) $(foreach dir,$(PROJECT_TESTSRC_SUBDIRS),$(subst .c,.o,$(wildcard $(dir)/*.c)))
2019-01-31 09:10:19 +00:00
# all is the default, user should specify what the default should do
# - 'static_lib' for building static library
# - 'shared_lib' for building shared library
# - 'program' for building the program
# - 'test_program' for building the test program
# These can typically be used together however *_lib and program should not be used together
all: program
2019-02-17 07:21:19 +00:00
clean: clean_object_files remove_binary_dir
# Object Compile Rules
%.o: %.c
@echo CC $<
@$(CC) $(CFLAGS) -c $< -o $@
%.o: %.cpp
@echo CXX $<
@$(CXX) $(CXXFLAGS) -c $< -o $@
2019-01-31 09:10:19 +00:00
2021-11-10 07:12:01 +00:00
%.o: %.cc
@echo CXX $<
@$(CXX) $(CXXFLAGS) -c $< -o $@
2019-01-31 09:10:19 +00:00
# Binary Directory
2019-02-17 07:21:19 +00:00
.PHONY: create_binary_dir
create_binary_dir:
2019-01-31 09:10:19 +00:00
@mkdir -p "$(PROJECT_BIN_PATH)"
2019-02-17 07:21:19 +00:00
.PHONY: remove_binary_dir
remove_binary_dir:
ifneq ($(PROJECT_BIN_PATH),)
2019-01-31 09:10:19 +00:00
@rm -rf "$(PROJECT_BIN_PATH)"
2019-02-17 07:21:19 +00:00
endif
2019-01-31 09:10:19 +00:00
2019-02-17 07:21:19 +00:00
.PHONY: clean_object_files
clean_object_files:
@rm -f $(SRC_OBJ) $(TESTSRC_OBJ)
2019-01-31 09:10:19 +00:00
# Build Library
2019-02-17 07:21:19 +00:00
static_lib: $(SRC_OBJ) create_binary_dir
@echo LINK $(PROJECT_BIN_PATH)/$(PROJECT_NAME).a
@ar $(ARFLAGS) "$(PROJECT_BIN_PATH)/$(PROJECT_NAME).a" $(SRC_OBJ)
2019-01-31 09:10:19 +00:00
2019-02-17 07:21:19 +00:00
shared_lib: $(SRC_OBJ) create_binary_dir
@echo LINK $(PROJECT_BIN_PATH)/$(PROJECT_SO_FILENAME)
@gcc -shared -Wl,-soname,$(PROJECT_SONAME) -o "$(PROJECT_BIN_PATH)/$(PROJECT_SO_FILENAME)" $(SRC_OBJ)
2019-01-31 09:10:19 +00:00
# Build Program
2019-02-17 07:21:19 +00:00
program: $(SRC_OBJ) create_binary_dir
@echo LINK $(PROJECT_BIN_PATH)/$(PROJECT_NAME)
2021-11-10 07:40:19 +00:00
@$(CXX) $(ARCHFLAGS) $(SRC_OBJ) $(LIB) -o "$(PROJECT_BIN_PATH)/$(PROJECT_NAME)"
2019-01-31 09:10:19 +00:00
# Build Test Program
2019-02-17 07:21:19 +00:00
test_program: $(TESTSRC_OBJ) $(SRC_OBJ) create_binary_dir
ifneq ($(PROJECT_TESTSRC_PATH),)
@echo LINK $(PROJECT_BIN_PATH)/$(PROJECT_NAME)_test
2021-11-10 07:40:19 +00:00
@$(CXX) $(ARCHFLAGS) $(TESTSRC_OBJ) $(SRC_OBJ) $(LIB) -o "$(PROJECT_BIN_PATH)/$(PROJECT_NAME)_test"
2019-02-17 07:21:19 +00:00
endif
2019-01-31 09:10:19 +00:00
# Documentation
2019-02-17 07:21:19 +00:00
.PHONY: docs
2019-01-31 09:10:19 +00:00
docs:
2019-02-17 07:21:19 +00:00
ifneq ($(PROJECT_DOCS_PATH),)
doxygen "$(PROJECT_DOXYFILE_PATH)"
endif
2019-01-31 09:10:19 +00:00
.PHONY: clean_docs
clean_docs:
2019-02-17 07:21:19 +00:00
ifneq ($(PROJECT_DOCS_PATH),)
@rm -rf "$(PROJECT_DOCS_PATH)"
endif
2019-01-31 09:10:19 +00:00
# Dependencies
2019-02-17 07:21:19 +00:00
.PHONY: deps
deps:
@$(foreach lib,$(PROJECT_DEPEND_LOCAL_DIR), cd "$(ROOT_PROJECT_DEPENDENCY_PATH)/$(lib)" && $(MAKE) static_lib && cd "$(PROJECT_PATH)";)
2019-01-31 09:10:19 +00:00
.PHONY: clean_deps
clean_deps:
@$(foreach lib,$(PROJECT_DEPEND_LOCAL_DIR), cd "$(ROOT_PROJECT_DEPENDENCY_PATH)/$(lib)" && $(MAKE) clean && cd "$(PROJECT_PATH)";)