-
Notifications
You must be signed in to change notification settings - Fork 52
/
Makefile
173 lines (129 loc) · 3.94 KB
/
Makefile
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#
# Copyright 2022 Google LLC
#
# 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.
#
V ?= @
BUILD_DIR := build
BIN_DIR := bin
#
# Set `gcc` as default compiler
#
CC := $(if $(CC)=cc,gcc,$(CC))
AS := $(if $(AS)=as,$(CC),$(AS))
LD := $(if $(LD)=ld,$(CC),$(LD))
CFLAGS := $(if $(DEBUG),-O0 -g,-O3)
LDFLAGS := $(if $(DEBUG),-O0 -g,-O3)
CFLAGS += -std=c11 -Wall -Wextra -Wdouble-promotion -Wvla -pedantic
TARGET = $(lastword $(shell $(CC) -v 2>&1 | grep "Target: "))
LIB_SHARED := true
LIB_SUFFIX := so
ifeq ($(TARGET),wasm32)
LIB_SHARED := false
LIB_SUFFIX := wasm
CFLAGS += -Iwasm -mbulk-memory
LDFLAGS += -nostdlib -Wl,--no-entry -Wl,--export-dynamic
endif
ifneq ($(LC3_PLUS),)
DEFINE += LC3_PLUS=$(LC3_PLUS)
endif
ifneq ($(LC3_PLUS_HR),)
DEFINE += LC3_PLUS_HR=$(LC3_PLUS_HR)
endif
#
# Declarations
#
lib_list :=
bin_list :=
define add-lib
$(eval $(1)_bin ?= $(1).$(LIB_SUFFIX))
$(eval $(1)_bin := $(addprefix $(BIN_DIR)/,$($(1)_bin)))
lib_list += $(1)
LIB += $($(1)_bin)
endef
define add-bin
$(eval $(1)_bin ?= $(1))
$(eval $(1)_bin := $(addprefix $(BIN_DIR)/,$($(1)_bin)))
$($(1)_bin): LDLIBS += $(if $(filter $(LIBC),bionic),\
$(filter-out rt pthread,$($(1)_ldlibs)),$($(1)_ldlibs))
$($(1)_bin): LDFLAGS += $($(1)_ldflags)
bin_list += $(1)
BIN += $($(1)_bin)
endef
define set-target
$(eval $(1)_obj ?= $(patsubst %.c,%.o,$(filter %.c,$($(1)_src))) \
$(patsubst %.s,%.o,$(filter %.s,$($(1)_src))) \
$(patsubst %.cc,%.o,$(filter %.cc,$($(1)_src))))
$(eval $(1)_obj := $(addprefix $(BUILD_DIR)/,$($(1)_obj)))
$($(1)_obj): INCLUDE += $($(1)_include)
$($(1)_obj): DEFINE += $($(1)_define)
$($(1)_obj): CFLAGS += $($(1)_cflags)
$($(1)_obj): CXXFLAGS += $($(1)_cxxflags)
-include $($(1)_obj:.o=.d)
$($(1)_bin): $($(1)_obj)
$($(1)_bin): $($(1)_dependencies)
.PHONY: $(1)
$(1): $($(1)_bin)
endef
.PHONY: default
default:
INCLUDE += include
SRC_DIR = src
include $(SRC_DIR)/makefile.mk
TOOLS_DIR = tools
-include $(TOOLS_DIR)/makefile.mk
TEST_DIR := test
-include $(TEST_DIR)/makefile.mk
FUZZ_DIR := fuzz
-include $(FUZZ_DIR)/makefile.mk
#
# Rules
#
MAKEFILE_DEPS := $(MAKEFILE_LIST)
$(foreach lib, $(lib_list), $(eval $(call set-target,$(lib))))
$(foreach bin, $(bin_list), $(eval $(call set-target,$(bin))))
$(BUILD_DIR)/%.o: %.c $(MAKEFILE_DEPS)
@echo " CC $(notdir $<)"
$(V)mkdir -p $(dir $@)
$(V)$(CC) $< -c $(CFLAGS) \
$(addprefix -I,$(INCLUDE)) \
$(addprefix -D,$(DEFINE)) -MMD -MF $(@:.o=.d) -o $@
$(BUILD_DIR)/%.o: %.s $(MAKEFILE_DEPS)
@echo " AS $(notdir $<)"
$(V)mkdir -p $(dir $@)
$(V)$(AS) $< -c $(CFLAGS) \
$(addprefix -I,$(INCLUDE)) \
$(addprefix -D,$(DEFINE)) -MMD -MF $(@:.o=.d) -o $@
$(BUILD_DIR)/%.o: %.cc $(MAKEFILE_DEPS)
@echo " CXX $(notdir $<)"
$(V)mkdir -p $(dir $@)
$(V)$(CXX) $< -c $(CXXFLAGS) \
$(addprefix -I,$(INCLUDE)) \
$(addprefix -D,$(DEFINE)) -MMD -MF $(@:.o=.d) -o $@
ifeq ($(LIB_SHARED),true)
$(LIB): CFLAGS += -fvisibility=hidden -flto -fPIC
$(LIB): LDFLAGS += -flto -shared
endif
$(LIB): $(MAKEFILE_DEPS)
@echo " LD $(notdir $@)"
$(V)mkdir -p $(dir $@)
$(V)$(LD) $(filter %.o,$^) $(LDFLAGS) -o $@
$(BIN): $(MAKEFILE_DEPS)
@echo " LD $(notdir $@)"
$(V)mkdir -p $(dir $@)
$(V)$(LD) $(filter %.o,$^) \
$(LDFLAGS) -L $(BIN_DIR) $(addprefix -l,$(LDLIBS)) -o $@
clean:
$(V)rm -rf $(BUILD_DIR)
$(V)rm -rf $(BIN_DIR)
clean-all: clean