Add support for release builds with debug info and using lib externally

This commit is contained in:
2025-08-31 14:17:04 +01:00
parent 81e3ab2c67
commit e26bf613a5
3 changed files with 153 additions and 60 deletions

131
Makefile
View File

@@ -1,31 +1,41 @@
CC = clang
CXX = clang++
AR = ar
BUILD_TYPE = debug
CSTD = -std=gnu11
CXXSTD = -std=gnu++11
CFLAGS = -Wall -Wextra -Werror -pedantic -Isrc
LIBFLAGS = -fPIC
KERNEL = $(shell uname -s)
MACHINE = $(shell uname -m)
PLATFORM = $(KERNEL)_$(MACHINE)
TEST_INCLUDE = -Isrc $(shell find tests -type d | xargs -I{} echo -n "-I{} ")
TEST_SRC = $(shell find tests -type f -name "*.c" | xargs -I{} echo -n "{} ")
TEST_C_SRC = src/wapp.c $(TEST_SRC)
TEST_CXX_SRC = $(shell find tests -type f -name "*.cc" | xargs -I{} echo -n "{} ")
BUILD_DIR = libwapp-build/$(PLATFORM)-$(BUILD_TYPE)
LIB_NAME = wapp
OBJ_OUT = $(BUILD_DIR)/$(LIB_NAME).o
LIB_OUT = $(BUILD_DIR)/lib$(LIB_NAME).a
TEST_C_OUT = $(BUILD_DIR)/wapptest
TEST_CXX_OUT = $(BUILD_DIR)/wapptestcc
# External variables
CC ?= clang
CXX ?= clang++
AR ?= ar
BUILD_TYPE ?= Debug
BUILD_DIR ?= libwapp-build/$(PLATFORM)-$(BUILD_TYPE)
INSTALL_PREFIX ?= dist
ifeq ($(BUILD_TYPE),debug)
# Internal variables
override CFLAGS = -Wall -Wextra -Werror -pedantic -Isrc
override LIBFLAGS = -fPIC
override ABS_INSTALL_PREFIX != readlink -f $(INSTALL_PREFIX)
override CSTD := -std=gnu11
override CXXSTD := -std=gnu++11
override KERNEL := $(shell uname -s)
override MACHINE := $(shell uname -m)
override PLATFORM := $(KERNEL)_$(MACHINE)
override TEST_INCLUDE := -Isrc $(shell find tests -type d | xargs -I{} echo -n "-I{} ")
override TEST_SRC := $(shell find tests -type f -name "*.c" | xargs -I{} echo -n "{} ")
override TEST_C_SRC := src/wapp.c $(TEST_SRC)
override TEST_CXX_SRC := $(shell find tests -type f -name "*.cc" | xargs -I{} echo -n "{} ")
override LIB_NAME := wapp
override OBJ_OUT := $(BUILD_DIR)/$(LIB_NAME).o
override LIB_OUT := $(BUILD_DIR)/lib$(LIB_NAME).a
override TEST_C_OUT := $(BUILD_DIR)/wapptest
override TEST_CXX_OUT := $(BUILD_DIR)/wapptestcc
override INCLUDE_INSTALL := $(ABS_INSTALL_PREFIX)/include/$(LIB_NAME)
override LIB_INSTALL := $(ABS_INSTALL_PREFIX)/lib
override HEADER_INSTALL_CMD := scripts/header_install.sh
ifeq ($(BUILD_TYPE),Debug)
CFLAGS += -g -fsanitize=address,undefined -DWAPP_DEBUG_ASSERT
else ifeq ($(BUILD_TYPE),release)
else ifeq ($(BUILD_TYPE),RelWithDebInfo)
CFLAGS += -g -O2 -fsanitize=address,undefined -DWAPP_DEBUG_ASSERT
else ifeq ($(BUILD_TYPE),Release)
CFLAGS += -O3
else
$(error Invalid BUILD type '$(BUILD_TYPE)'. Use 'debug' or 'release')
$(error Invalid BUILD type '$(BUILD_TYPE)'. Use 'Debug', 'RelWithDebInfo' or 'Release')
endif
ifeq ($(CC),gcc)
@@ -33,13 +43,56 @@ ifeq ($(CC),gcc)
export ASAN_OPTIONS=verify_asan_link_order=0
endif
.PHONY: all clean builddir build-test run-test codegen build-lib full prng testing uuid core primitives
.PHONY: help full prng testing uuid core primitives all clean builddir build-test run-test codegen install build-lib
all: clean builddir codegen run-c-test full run-cc-test
help:
@echo "Available build variables:"
@echo " CC C compiler to use (Default: clang)."
@echo " CXX C++ compiler to use (Default: clang++)."
@echo " AR Archiving utility to use for building static libraries (Default: ar)."
@echo " BUILD_TYPE Build type. Choose from \`Debug\`, \`RelWithDebInfo\` or \`Release\` (Default: Debug)."
@echo " BUILD_DIR Directory where build files will be written."
@echo " INSTALL_PREFIX Prefix where library and include files will be installed."
@echo
@echo "Available targets:"
@echo " make full Build and install the full wapp library."
@echo " make core Build and install only the \`core\` component of the wapp library with all its dependencies."
@echo " make prng Build and install only the \`prng\` component of the wapp library with all its dependencies."
@echo " make uuid Build and install only the \`uuid\` component of the wapp library with all its dependencies."
@echo " make testing Build and install only the \`testing\` component of the wapp library with all its dependencies."
@echo " make primitives Build and install only the \`primitives\` component of the wapp library with all its dependencies."
@echo " make clean Clean build directory."
@echo " make help Print this help message and exit."
full: LIB_SRC = src/wapp.c
full: INCLUDES = common core primitives prng testing uuid
full: install
prng: LIB_SRC = src/prng/wapp_prng.c
prng: INCLUDES = common prng
prng: install
testing: LIB_SRC = src/testing/wapp_testing.c
testing: INCLUDES = common core testing
testing: install
uuid: LIB_SRC = src/uuid/wapp_uuid.c
uuid: INCLUDES = common primitives prng
uuid: install
core: LIB_SRC = src/core/wapp_core.c
core: INCLUDES = common core primitives
core: install
primitives: LIB_SRC = src/core/wapp_primitives.c
primitives: INCLUDES = common primitives
primitives: install
clean:
@rm -rf $(BUILD_DIR)
all: clean builddir codegen run-c-test full run-cc-test
builddir:
@mkdir -p $(BUILD_DIR)
@@ -62,25 +115,13 @@ run-cc-test: build-cc-test
codegen:
python3 -m codegen
build-lib:
install: build-lib
@mkdir -p $(LIB_INSTALL)
@cp -v $(LIB_OUT) $(LIB_INSTALL)
@mkdir -p $(INCLUDE_INSTALL)
@bash $(HEADER_INSTALL_CMD) $(LIB_SRC) $(INCLUDE_INSTALL) $(INCLUDES)
build-lib: builddir
$(CC) -c $(CSTD) $(CFLAGS) $(LIBFLAGS) $(LIB_SRC) -o $(OBJ_OUT)
$(AR) r $(LIB_OUT) $(OBJ_OUT)
@rm $(OBJ_OUT)
full: LIB_SRC = src/wapp.c
full: build-lib
prng: LIB_SRC = src/prng/wapp_prng.c
prng: build-lib
testing: LIB_SRC = src/testing/wapp_testing.c
testing: build-lib
uuid: LIB_SRC = src/uuid/wapp_uuid.c
uuid: build-lib
core: LIB_SRC = src/core/wapp_core.c
core: build-lib
primitives: LIB_SRC = src/core/wapp_primitives.c
primitives: build-lib