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
10# in order to rebuild the uoscore-uedhoc.a and server application call:
11# make oscore_edhoc; make
12
13include ../../../makefile_config.mk
14
15# toolchain
16CXX ?= g++
17CC ?= gcc
18SZ ?= size
19MAKE ?= make
20
21# target
22TARGET = server
23
24# build path
25BUILD_DIR = build
26
27# libusocore-uedhoc path
28USOCORE_UEDHOC_PATH = ../../../
29USOCORE_UEDHOC_BUILD_PATH = $(USOCORE_UEDHOC_PATH)build
30
31# build for 32 bit x68
32# export the variable so that it is available in the uoscore-uedhoc Makefile
33ARCH = -m32
34export ARCH
35
36# debug build?
37DEBUG = 1
38
39# optimization
40OPT = -Og
41
42# CPP sources
43CPP_SOURCES += src/main.cpp
44CPP_SOURCES += ../../../externals/cantcoap/cantcoap.cpp
45
46# C sources
47# rename files available with the same name in different libraries
48$(shell mv ../../../externals/compact25519/src/c25519/sha512.c ../../../externals/compact25519/src/c25519/_sha512.c )
49
50C_SOURCES += src/_entropy.c
51C_SOURCES += $(wildcard ../../common/*.c)
52C_SOURCES += $(wildcard ../../../externals/zcbor/src/*.c)
53
54# Crypto engine dependent source files
55ifeq ($(findstring COMPACT25519,$(CRYPTO_ENGINE)),COMPACT25519)
56C_SOURCES += $(wildcard ../../../externals/compact25519/src/c25519/*.c)
57C_SOURCES += $(wildcard ../../../externals/compact25519/src/*.c)
58endif
59
60ifeq ($(findstring TINYCRYPT,$(CRYPTO_ENGINE)),TINYCRYPT)
61C_SOURCES += $(wildcard ../../../externals/tinycrypt/lib/source/*.c)
62endif
63
64ifeq ($(findstring MBEDTLS,$(CRYPTO_ENGINE)),MBEDTLS)
65C_SOURCES += $(wildcard ../../../externals/mbedtls/library/*.c)
66endif
67
68# C includes
69C_INCLUDES += -I../../../inc/
70C_INCLUDES += -I../../common/
71C_INCLUDES += -I../../../test_vectors/
72C_INCLUDES += -I../../../externals/cantcoap/
73C_INCLUDES += -I../../../externals/zcbor/include
74
75# Crypto engine dependent includes
76ifeq ($(findstring COMPACT25519,$(CRYPTO_ENGINE)),COMPACT25519)
77C_INCLUDES += -I../../../externals/compact25519/src/c25519/
78C_INCLUDES += -I../../../externals/compact25519/src/
79endif
80
81ifeq ($(findstring TINYCRYPT,$(CRYPTO_ENGINE)),TINYCRYPT)
82C_INCLUDES += -I../../../externals/tinycrypt/lib/include
83endif
84
85ifeq ($(findstring MBEDTLS,$(CRYPTO_ENGINE)),MBEDTLS)
86C_INCLUDES += -I../../../externals/mbedtls/library
87C_INCLUDES += -I../../../externals/mbedtls/include
88C_INCLUDES += -I../../../externals/mbedtls/include/mbedtls
89C_INCLUDES += -I../../../externals/mbedtls/include/psa
90endif
91
92# C defines
93# make PRINT_ARRAY macro usable in the main file
94C_DEFS += -DDEBUG_PRINT
95C_DEFS += -DLINUX_SOCKETS
96
97# Linked libraries
98LD_LIBRARY_PATH += -L$(USOCORE_UEDHOC_BUILD_PATH)
99
100LDFLAGS += $(LD_LIBRARY_PATH)
101LDFLAGS += -luoscore-uedhoc
102LDFLAGS += -lstdc++
103LDFLAGS += $(ARCH)
104##########################################
105# CFLAGS
106##########################################
107#general c flags
108CFLAGS +=  $(ARCH) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall
109CXXFLAGS +=  $(ARCH) $(C_DEFS) $(C_INCLUDES) $(OPT)
110
111# have dubug information
112ifeq ($(DEBUG), 1)
113CFLAGS += -g -gdwarf-2
114CXXFLAGS += -Wall -g
115endif
116
117# Generate dependency information
118CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
119CXXFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
120
121# required for gddl-gen library
122CFLAGS += -DZCBOR_CANONICAL
123
124
125
126# use AddressSanitizer to find memory bugs
127# comment this out for better speed
128#CFLAGS += -fsanitize=address -fno-omit-frame-pointer
129#CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer
130#LDFLAGS += -fsanitize=address -static-libasan
131
132###########################################
133# default action: build all
134###########################################
135#list of objects from c files
136OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
137vpath %.c $(sort $(dir $(C_SOURCES)))
138
139# list of objects from c++ file
140OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(CPP_SOURCES:.cpp=.o)))
141vpath %.cpp $(sort $(dir $(CPP_SOURCES)))
142
143USOCORE_UEDHOC_OBJ = $(wildcard $(USOCORE_UEDHOC_PATH)/*.o)
144
145
146
147$(BUILD_DIR)/%.o: %.cpp Makefile | $(BUILD_DIR)
148	$(CXX) -c $(CXXFLAGS) $< -o $@
149
150$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
151	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
152
153$(BUILD_DIR)/$(TARGET): $(OBJECTS) Makefile $(USOCORE_UEDHOC_PATH)/Makefile $(USOCORE_UEDHOC_OBJ)
154	$(MAKE) -C $(USOCORE_UEDHOC_PATH)
155	$(CXX) $(OBJECTS)  $(LDFLAGS) -o $@
156	$(SZ) $@
157
158$(BUILD_DIR):
159	mkdir $@
160
161
162
163oscore_edhoc:
164	$(MAKE) -C $(USOCORE_UEDHOC_PATH)
165
166clean_oscore_edhoc:
167	$(MAKE) -C $(USOCORE_UEDHOC_PATH) clean
168
169clean:
170	-rm -fR $(BUILD_DIR)
171	$(MAKE) -C $(USOCORE_UEDHOC_PATH) clean
172
173#######################################
174# dependencies
175#######################################
176-include $(wildcard $(BUILD_DIR)/*.d)