aboutsummaryrefslogtreecommitdiff
path: root/makefile
blob: ab6da6aeef241d96e3f4da2d781fc31c331bca03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
# SPDX-License-Identifier: MIT

CC = gcc
TG = ctags

BUILD = ${CURDIR}/build
SRC = ${CURDIR}/src
INC = ${CURDIR}/inc
SRCS = $(wildcard $(SRC)/*.c) $(wildcard $(SRC)/*/*.c) $(SRC)/cmdline.c
OBJS = $(patsubst $(SRC)/%.c, $(BUILD)/%.o, $(SRCS))

CFLAGS_DEBUG = -fsanitize=leak,undefined,address -g -O0
CFLAGS_WARNINGS = -Wall -Wextra -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wunreachable-code -Wundef -Wold-style-definition -pedantic -Wno-switch-enum
CFLAGS = $(CFLAGS_WARNINGS) -std=c99 -Ofast -I$(INC)

ifdef DEBUG # TODO: Somehow clean automagically
CFLAGS += $(CFLAGS_DEBUG)
endif

ifeq ($(PREFIX),)
    PREFIX := /usr/local
endif

all: compile

full: all sync

compile: $(BUILD) $(OBJS) $(BUILD)/blocade

clean:
	@rm -rf $(BUILD)/*
	@rm -f $(SRC)/cmdline.*

install:
	@install -m 755 $(BUILD)/blocade $(DESTDIR)$(PREFIX)/bin/

sync: # Ugly hack
	@$(MAKE) $(BUILD)/blocade --always-make --dry-run | grep -wE 'gcc|g\+\+' | grep -w '\-c' | jq -nR '[inputs|{directory:".", command:., file: match(" [^ ]+$$").string[1:]}]' >compile_commands.json
	@$(TG) -R --exclude=.git --exclude=build .

$(SRC)/cmdline.c:
	@gengetopt -i ${CURDIR}/options.ggo -G --output-dir=$(BUILD)
	@printf '%s\n%s\n%s\n%s' '#pragma GCC diagnostic push' '#pragma GCC diagnostic ignored "-Wcast-qual"' "$$(cat $(BUILD)/cmdline.c)" '#pragma GCC diagnostic pop' >$(SRC)/cmdline.c
	@cp $(BUILD)/cmdline.h $(SRC)/cmdline.h

$(BUILD)/%.o: $(SRC)/%.c | $(SRC)/cmdline.c
	@mkdir -p $(@D)
	@$(CC) -c -o $@ $(CFLAGS) $<

$(BUILD)/blocade: $(OBJS)
	@$(CC) -o $@ $(CFLAGS) $^

.PHONY: all compile clean sync

$(BUILD):
	@mkdir -p $@