git @ Cat's Eye Technologies Collapsiv / master Makefile
master

Tree @master (Download .tar.gz)

Makefile @masterraw · history · blame

# SPDX-FileCopyrightText: Chris Pressey, the creator of this work, has dedicated it to the public domain.
# For more information, please refer to <https://unlicense.org/>
# SPDX-License-Identifier: Unlicense

# Compiler and flags
CC ?= gcc
AR ?= ar
INSTALL ?= install
CFLAGS ?= -Wall -Wextra -pedantic
CFLAGS += -fPIC -I$(SRCDIR)

# Installation directories (can be overridden)
PREFIX ?= /usr/local
LIBDIR ?= $(PREFIX)/lib
INCDIR ?= $(PREFIX)/include
BINDIR ?= $(PREFIX)/bin

# Project structure
SRCDIR = src
BUILDDIR = build
LIBNAME = libcollapsiv
STATIC_LIB = $(BUILDDIR)/$(LIBNAME).a
SHARED_LIB = $(LIBNAME).so
SHARED_LIB_MAJ = $(SHARED_LIB).0
SHARED_LIB_MIN = $(SHARED_LIB_MAJ).1
SHARED_LIB_REL = $(SHARED_LIB_MIN).0

# Source files
LIB_SRCS = $(SRCDIR)/collapsiv.c
TEST_SRCS = $(SRCDIR)/test_collapsiv.c
LIB_OBJS = $(LIB_SRCS:$(SRCDIR)/%.c=$(BUILDDIR)/%.o)
TEST_OBJS = $(TEST_SRCS:$(SRCDIR)/%.c=$(BUILDDIR)/%.o)

# Targets
.PHONY: all clean install uninstall test

all: $(BUILDDIR) $(STATIC_LIB) $(BUILDDIR)/$(SHARED_LIB_REL) $(BUILDDIR)/test_collapsiv

$(BUILDDIR):
	mkdir -p $(BUILDDIR)

# Compile source files
$(BUILDDIR)/%.o: $(SRCDIR)/%.c
	$(CC) $(CFLAGS) -c $< -o $@

# Create static library
$(STATIC_LIB): $(LIB_OBJS)
	$(AR) rcs $@ $^

# Create shared library
$(BUILDDIR)/$(SHARED_LIB_REL): $(LIB_OBJS)
	$(CC) -shared -Wl,-soname,$(SHARED_LIB_MAJ) -o $@ $^
	cd $(BUILDDIR) && \
		ln -sf $(SHARED_LIB_REL) $(SHARED_LIB_MIN) && \
		ln -sf $(SHARED_LIB_MIN) $(SHARED_LIB_MAJ) && \
		ln -sf $(SHARED_LIB_MAJ) $(SHARED_LIB)

# Build test program
$(BUILDDIR)/test_collapsiv: $(TEST_OBJS) $(STATIC_LIB)
	$(CC) $(CFLAGS) -o $@ $^ -L$(BUILDDIR) -lcollapsiv

# Install the library and headers
install: all
	$(INSTALL) -d $(DESTDIR)$(LIBDIR)
	$(INSTALL) -d $(DESTDIR)$(INCDIR)
	$(INSTALL) -m 644 $(STATIC_LIB) $(DESTDIR)$(LIBDIR)
	$(INSTALL) -m 755 $(BUILDDIR)/$(SHARED_LIB_REL) $(DESTDIR)$(LIBDIR)
	cd $(DESTDIR)$(LIBDIR) && \
		ln -sf $(SHARED_LIB_REL) $(SHARED_LIB_MIN) && \
		ln -sf $(SHARED_LIB_MIN) $(SHARED_LIB_MAJ) && \
		ln -sf $(SHARED_LIB_MAJ) $(SHARED_LIB)
	$(INSTALL) -m 644 $(SRCDIR)/collapsiv.h $(DESTDIR)$(INCDIR)
	-ldconfig

# Remove installed files
uninstall:
	rm -f $(DESTDIR)$(LIBDIR)/$(STATIC_LIB)
	rm -f $(DESTDIR)$(LIBDIR)/$(SHARED_LIB)*
	rm -f $(DESTDIR)$(INCDIR)/collapsiv.h

# Run tests
test: all
	cd $(BUILDDIR) && LD_LIBRARY_PATH=. ./test_collapsiv

# Clean build artifacts
clean:
	rm -rf $(BUILDDIR)