-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMakefile
More file actions
146 lines (120 loc) · 4.77 KB
/
Copy pathMakefile
File metadata and controls
146 lines (120 loc) · 4.77 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright (c) 2023-2026, Nubificus LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License
# Path variables
#
# Use absolute paths just for sanity
#? BUILD_DIR Directory to place produced binaries (default: ${CWD}/dist)
BUILD_DIR ?= ${CURDIR}/dist
#? PREFIX Directory to install urunit (default: /usr/local/bin)
PREFIX ?= /usr/local/bin
# Binary variables
URUNIT_BIN_DYNAMIC := $(BUILD_DIR)/urunit_dynamic
URUNIT_BIN_DYNAMIC_DEBUG := $(BUILD_DIR)/urunit_dynamic_debug
URUNIT_BIN_STATIC := $(BUILD_DIR)/urunit_static
URUNIT_BIN_STATIC_DEBUG := $(BUILD_DIR)/urunit_static_debug
# Compiler variables
#? CC the compiler to use (default: gcc)
CC ?= gcc
CFLAGS := -Werror -Wextra -Wall -pedantic-errors -Wformat
CFLAGS += -std=gnu99 -O2 -fstack-protector --param=ssp-buffer-size=4
# Linking FLAGS
LDFLAGS := -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-s
STATIC_LDFLAGS := -Wl,--no-export-dynamic -static
# Source files variables
SOURCE_DIR := src
URUNIT_SRC := ${SOURCE_DIR}/main.c ${SOURCE_DIR}/common.c
# Header files
URUNIT_HDR := ${SOURCE_DIR}/common.h
# Install dependencies variables
#
# If we have already built either static or dynamic version of urunit
# we do not have to rebuild it, but instead we can install whichever
# version is available. However, the dynamic version
# has always a preference.
INSTALL_DEPS = $(shell test -e $(URUNIT_BIN_STATIC) \
&& echo $(URUNIT_BIN_STATIC) && exit \
|| test -e $(URUNIT_BIN_DYNAMIC) \
&& echo $(URUNIT_BIN_DYNAMIC) \
|| echo $(URUNIT_BIN_STATIC))
# ---------------------------------------------------------------
# Target OS detection
#
# Priority:
# 1. Explicit TARGET_OS variable (make TARGET_OS=FreeBSD)
# 2. Compiler's target triple (gcc -dumpmachine)
# 3. Host OS via uname (native build fallback)
# ---------------------------------------------------------------
#? TARGET_OS Target OS for cross-compilation (default: auto-detected)
CC_TARGET := $(shell $(CC) -dumpmachine 2>/dev/null)
ifndef TARGET_OS
ifneq ($(findstring linux,$(CC_TARGET)),)
TARGET_OS := Linux
else
TARGET_OS := $(shell uname -s)
endif
endif
ifeq ($(TARGET_OS),Linux)
URUNIT_SRC += ${SOURCE_DIR}/linux.c
endif
ifeq ($(TARGET_OS),FreeBSD)
URUNIT_SRC += ${SOURCE_DIR}/freebsd.c
endif
# Main Building rules
#
# By default we opt to build static binaries targeting the host archiotecture.
## default Build urunit statically for host arch.(default).
.PHONY: default
default: $(URUNIT_BIN_STATIC)
## all Build urunit statically and dynamically for host arch.
.PHONY: all
all: $(URUNIT_BIN_STATIC) $(URUNIT_BIN_DYNAMIC)
# Just an alias for $(BUILD_DIR) for easier invocation
## prepare alias for creating the build directory.
prepare: $(BUILD_DIR)
## static Build urunit statically-linked for host arch.
.PHONY: static
static: $(URUNIT_BIN_STATIC)
## static_debug Build urunit statically-linked for host arch enabling debug logs.
.PHONY: static_debug
static_debug: $(URUNIT_BIN_STATIC_DEBUG)
## dynamic Build urunit dynamically-linked for host arch.
.PHONY: dynamic
dynamic: $(URUNIT_BIN_DYNAMIC)
## dynamic_debug Build urunit dynamically-linked for host arch enabling debug logs.
.PHONY: dynamic_debug
dynamic_debug: $(URUNIT_BIN_DYNAMIC_DEBUG)
$(BUILD_DIR):
mkdir -p $@
$(URUNIT_BIN_DYNAMIC): $(URUNIT_SRC) $(URUNIT_HDR) | prepare
$(CC) $(CFLAGS) $(URUNIT_SRC) $(LDFLAGS) -o $@
$(URUNIT_BIN_DYNAMIC_DEBUG): $(URUNIT_SRC) $(URUNIT_HDR) | prepare
$(CC) $(CFLAGS) -DDEBUG $(URUNIT_SRC) $(LDFLAGS) -o $@
$(URUNIT_BIN_STATIC): $(URUNIT_SRC) $(URUNIT_HDR) | prepare
$(CC) $(CFLAGS) $(URUNIT_SRC) $(LDFLAGS) $(STATIC_LDFLAGS) -o $@
$(URUNIT_BIN_STATIC_DEBUG): $(URUNIT_SRC) $(URUNIT_HDR) | prepare
$(CC) $(CFLAGS) -DDEBUG $(URUNIT_SRC) $(LDFLAGS) $(STATIC_LDFLAGS) -o $@
## install Install urunit in PREFIX
.PHONY: install
install: $(INSTALL_DEPS)
install -m 0755 $^ $(PREFIX)/urunit
clean:
rm -f $(BIN) $(BIN_STATIC)
rm -rf $(BUILD_DIR)
## help Show this help message
help:
@echo 'Usage: make <target> <flags>'
@echo 'Targets:'
@grep -w "^##" $(MAKEFILE_LIST) | sed -n 's/^## /\t/p' | sed -n 's/ /\@/p' | column -s '\@' -t
@echo 'Flags:'
@grep -w "^#?" $(MAKEFILE_LIST) | sed -n 's/^#? /\t/p' | sed -n 's/ /\@/p' | column -s '\@' -t