1# Copyright (c) 2021 Fraunhofer AISEC. See the COPYRIGHT
2# file at the top-level directory of this distribution.
3
4# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7# option. This file may not be copied, modified, or distributed
8# except according to those terms.
9
10LIB_NAME = libuoscore-uedhoc.a
11
12include makefile_config.mk
13
14$(info    CC is $(CC))
15# $(info    LIB_NAME is $(LIB_NAME))
16# $(info    PREFIX is $(PREFIX))
17# $(info    SOURCE_DIR is $(SOURCE_DIR))
18# $(info    BINARY_DIR is $(BINARY_DIR))
19# $(info    CC is $(CC))
20# $(info    CFLAGS is $(CFLAGS))
21# $(info    OPT is $(OPT))
22# $(info    CRYPTO_ENGINE is $(CRYPTO_ENGINE))
23################################################################################
24# Build directory
25PREFIX ?= build
26DIR = $(PREFIX)
27$(shell mkdir -p $(DIR))
28################################################################################
29# C Source files
30
31C_SOURCES += $(wildcard src/edhoc/*.c)
32C_SOURCES += $(wildcard src/oscore/*.c)
33C_SOURCES += $(wildcard src/common/*.c)
34C_SOURCES += $(wildcard src/cbor/*.c)
35
36
37#$(info    \n C_SOURCES is $(C_SOURCES))
38vpath %.c $(sort $(dir $(C_SOURCES)))
39
40################################################################################
41# CFLAGS
42################################################################################
43FILTERED_CFLAGS = -Os
44EXTENDED_CFLAGS = $(filter-out $(FILTERED_CFLAGS), $(CFLAGS))
45
46#add options form configuration file
47EXTENDED_CFLAGS += $(FEATURES)
48EXTENDED_CFLAGS += $(FEATURES)
49EXTENDED_CFLAGS += $(ARCH)
50EXTENDED_CFLAGS += $(OPT)
51EXTENDED_CFLAGS += $(DEBUG_PRINT)
52EXTENDED_CFLAGS += $(CBOR_ENGINE)
53EXTENDED_CFLAGS += $(OSCORE_NVM_SUPPORT)
54EXTENDED_CFLAGS += $(CRYPTO_ENGINE)
55EXTENDED_CFLAGS += $(UNIT_TEST)
56
57# Unit tests require NVM support regardless of user settings
58ifeq ($(findstring UNIT_TEST,$(DUNIT_TEST)),UNIT_TEST)
59EXTENDED_CFLAGS += -DOSCORE_NVM_SUPPORT
60endif
61
62#generate debug symbols
63EXTENDED_CFLAGS += -g3 -gdwarf-4
64
65# Generate dependency information
66EXTENDED_CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
67
68# Generate stack usage information
69EXTENDED_CFLAGS += -fstack-usage
70
71# use C11
72EXTENDED_CFLAGS += -std=c11
73
74#GCC warning flags
75ifeq ($(findstring cc,$(CC)),cc)
76EXTENDED_CFLAGS += -Waddress
77EXTENDED_CFLAGS += -Waggregate-return
78EXTENDED_CFLAGS += -Wformat-nonliteral
79EXTENDED_CFLAGS += -Wformat-security
80EXTENDED_CFLAGS += -Wformat
81EXTENDED_CFLAGS += -Winit-self
82EXTENDED_CFLAGS += -Wmissing-include-dirs
83EXTENDED_CFLAGS += -Wno-multichar
84EXTENDED_CFLAGS += -Wno-parentheses
85EXTENDED_CFLAGS += -Wno-type-limits
86EXTENDED_CFLAGS += -Wno-unused-parameter
87EXTENDED_CFLAGS += -Wunreachable-code
88EXTENDED_CFLAGS += -Wwrite-strings
89EXTENDED_CFLAGS += -Wpointer-arith
90EXTENDED_CFLAGS += -Wall
91EXTENDED_CFLAGS += -Wextra
92EXTENDED_CFLAGS += -Wcast-qual
93#EXTENDED_CFLAGS += -Wstack-usage=9000
94EXTENDED_CFLAGS += -Wconversion
95EXTENDED_CFLAGS += -Wpedantic
96#EXTENDED_CFLAGS += -Werror
97
98#Clang warning flags
99else ifeq ($(findstring clang,$(CC)),clang)
100EXTENDED_CFLAGS += -Wcast-qual
101EXTENDED_CFLAGS += -Wconversion
102EXTENDED_CFLAGS += -Wexit-time-destructors
103EXTENDED_CFLAGS += -Wglobal-constructors
104EXTENDED_CFLAGS += -Wmissing-noreturn
105EXTENDED_CFLAGS += -Wmissing-prototypes
106EXTENDED_CFLAGS += -Wno-missing-braces
107EXTENDED_CFLAGS += -Wold-style-cast
108EXTENDED_CFLAGS += -Wshadow
109EXTENDED_CFLAGS += -Wweak-vtables
110EXTENDED_CFLAGS += -Wall
111EXTENDED_CFLAGS += -Wextra
112EXTENDED_CFLAGS += -Wpedantic
113EXTENDED_CFLAGS += -Wstack-exhausted
114EXTENDED_CFLAGS += -Wconversion
115#EXTENDED_CFLAGS += -Werror
116endif
117
118ifeq ($(findstring ASAN,$(ASAN)),ASAN)
119EXTENDED_CFLAGS += -fsanitize=address -fomit-frame-pointer
120endif
121
122ifeq ($(findstring ASAN,$(ASAN)),ASAN)
123EXTENDED_CFLAGS += -fsanitize=address -fomit-frame-pointer
124endif
125
126################################################################################
127# C includes
128################################################################################
129# Set a default crypto engine if non is provided in CRYPTO_ENGINE or CC
130ifneq ($(findstring TINYCRYPT,$(EXTENDED_CFLAGS)),TINYCRYPT)
131ifneq ($(findstring MBEDTLS,$(EXTENDED_CFLAGS)),MBEDTLS)
132EXTENDED_CFLAGS += -DTINYCRYPT
133endif
134endif
135
136C_INCLUDES += -Iinc
137
138# Crypto engine
139ifeq ($(findstring COMPACT25519,$(EXTENDED_CFLAGS)),COMPACT25519)
140C_INCLUDES += -Iexternals/compact25519/src/c25519/
141C_INCLUDES += -Iexternals/compact25519/src/
142endif
143
144ifeq ($(findstring TINYCRYPT,$(EXTENDED_CFLAGS)),TINYCRYPT)
145C_INCLUDES += -Iexternals/tinycrypt/lib/include
146endif
147
148ifeq ($(findstring MBEDTLS,$(EXTENDED_CFLAGS)),MBEDTLS)
149C_INCLUDES += -Iexternals/mbedtls/library
150C_INCLUDES += -Iexternals/mbedtls/include
151C_INCLUDES += -Iexternals/mbedtls/include/mbedtls
152C_INCLUDES += -Iexternals/mbedtls/include/psa
153endif
154
155# CBOR engine
156ifeq ($(findstring ZCBOR,$(EXTENDED_CFLAGS)),ZCBOR)
157C_INCLUDES += -Iexternals/zcbor/include
158endif
159
160#add include paths
161EXTENDED_CFLAGS += $(C_INCLUDES)
162
163$(info    EXTENDED_CFLAGS are $(EXTENDED_CFLAGS))
164################################################################################
165# build the library
166################################################################################
167OBJ = $(addprefix $(DIR)/,$(notdir $(C_SOURCES:.c=.o)))
168#$(info    \n OBJ is $(OBJ))
169
170
171$(DIR)/$(LIB_NAME): $(OBJ)
172	@echo "[Link (Static)]"
173	@$(AR) -rcs $@ $^
174
175$(DIR)/%.o: %.c Makefile makefile_config.mk
176	@echo [Compile] $<
177	@$(CC) -c $(EXTENDED_CFLAGS) $< -o $@
178
179clean:
180	rm -fR $(DIR)