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 initiator application call:
11# make oscore_edhoc; make
12
13include ../../../makefile_config.mk
14ROOT_DIR := ../../..
15# toolchain
16CXX ?= g++
17CC ?= gcc
18SZ ?= size
19MAKE ?= make
20
21# target
22TARGET = initiator
23
24# build path
25BUILD_DIR = build
26
27# libusocore-uedhoc path
28USOCORE_UEDHOC_PATH = $(ROOT_DIR)
29USOCORE_UEDHOC_BUILD_PATH = $(USOCORE_UEDHOC_PATH)/build
30
31ifeq ($(ARCH_32_ONLY), 1)
32# build for 32 bit x68
33# export the varible so that it is availbale in the uoscore-uedhoc Makefile
34ARCH = -m32
35export ARCH
36endif
37
38# debug build?
39DEBUG = 1
40
41# optimization
42OPT = -Og
43
44
45# C defines
46# make PRINT_ARRAY macro usable in the main file
47C_DEFS += -DDEBUG_PRINT
48C_DEFS += -DLINUX_SOCKETS
49
50C_DEFS += $(ENV_DEFS)
51
52# Linked libraries
53LD_LIBRARY_PATH += -L$(USOCORE_UEDHOC_BUILD_PATH)
54
55LDFLAGS += $(LD_LIBRARY_PATH)
56LDFLAGS += -luoscore-uedhoc
57LDFLAGS += -lstdc++
58LDFLAGS += $(ARCH)
59##########################################
60# CFLAGS
61##########################################
62#general c flags
63CFLAGS +=  $(ARCH) $(C_DEFS) $(INCLUDES) $(OPT) -Wall
64CXXFLAGS +=  $(ARCH) $(C_DEFS) $(INCLUDES) $(OPT)
65
66# have dubug information
67ifeq ($(DEBUG), 1)
68CFLAGS += -g -gdwarf-2
69CXXFLAGS += -Wall -g
70endif
71
72# Generate dependency information
73CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
74CXXFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
75# required for gddl-gen library
76CFLAGS += -DZCBOR_CANONICAL
77
78# use linux sockets
79CFLAGS += -DLINUX_SOCKETS
80CXXFLAGS += -DLINUX_SOCKETS
81
82# use AddressSanitizer to find memory bugs
83# comment this out for better speed
84#CFLAGS += -fsanitize=address -fno-omit-frame-pointer
85#CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer
86#LDFLAGS += -fsanitize=address -static-libasan
87
88USOCORE_UEDHOC_OBJ = $(wildcard $(USOCORE_UEDHOC_PATH)/*.o)
89
90INITIATOR_DIR := ${ROOT_DIR}/samples/linux_edhoc/initiator
91INITIATOR_SOURCES := $(wildcard ${INITIATOR_DIR}/src/*.c) $(wildcard ${INITIATOR_DIR}/src/*.cpp)
92INITIATOR_INCLUDES := -I${ROOT_DIR}/inc -I${ROOT_DIR}/test_vectors
93
94COMMON_DIR := ${ROOT_DIR}/samples/common
95COMMON_SOURCES := $(wildcard ${COMMON_DIR}/*.c)
96COMMON_INCLUDES := -I$(COMMON_DIR)
97
98ZCBOR_DIR := ${ROOT_DIR}/externals/zcbor
99ZCBOR_C_SOURCES += $(wildcard ${ZCBOR_DIR}/src/*.c)
100ZCBOR_INCLUDES := -I${ZCBOR_DIR}/include
101
102MBEDTLS_DIR := ${ROOT_DIR}/externals/mbedtls
103MBEDTLS_SOURCES := $(wildcard ${MBEDTLS_DIR}/library/*.c)
104MBEDTLS_SRC_DIR := ${MBEDTLS_DIR}/library
105MBEDTLS_INCLUDES := -I${MBEDTLS_DIR}/library -I${MBEDTLS_DIR}/include -I${MBEDTLS_DIR}/include/mbedtls -I${MBEDTLS_DIR}/include/psa
106
107COMPACT25519_DIR := ${ROOT_DIR}/externals/compact25519/src
108COMPACT25519_C_SOURCES :=  $(wildcard ${COMPACT25519_DIR}/c25519/*.c) $(wildcard ${COMPACT25519_DIR}/*.c)
109COMPACT25519_INCLUDES := -I../../../externals/compact25519/src/c25519/ -I../../../externals/compact25519/src/
110
111TINYCRYPT_ALL_C_SOURCES += $(wildcard ${TINYCRYPT_DIR}/*.c)
112TINYCRYPT_INCLUDES := -I${ROOT_DIR}/externals/tinycrypt/lib/include
113TINYCRYPT_SOURCES := $(filter-out ${TINYCRYPT_DIR}/sha256.c, $(wildcard ${ROOT_DIR}/externals/tinycrypt/lib/source/*.c))
114
115CANTCOAP_DIR :=  ${ROOT_DIR}/externals/cantcoap
116CANTCOAP_SOURCES := ${CANTCOAP_DIR}/cantcoap.cpp
117CANTCOAP_INCLUDES := -I${CANTCOAP_DIR}
118
119SOURCES := ${INITIATOR_SOURCES}
120SOURCES += ${TINYCRYPT_SOURCES}
121SOURCES += ${COMPACT25519_C_SOURCES}
122SOURCES += ${MBEDTLS_SOURCES}
123SOURCES += ${ZCBOR_C_SOURCES}
124SOURCES += ${COMMON_SOURCES}
125SOURCES += ${CANTCOAP_SOURCES}
126OBJECTS := $(patsubst ${ROOT_DIR}/%.cpp,${BUILD_DIR}/%.o,$(patsubst ${ROOT_DIR}/%.c,${BUILD_DIR}/%.o,$(SOURCES)))
127INCLUDES := ${TINYCRYPT_INCLUDES}
128INCLUDES += ${COMPACT25519_INCLUDES}
129INCLUDES += ${MBEDTLS_INCLUDES}
130INCLUDES += ${ZCBOR_INCLUDES}
131INCLUDES += ${COMMON_INCLUDES}
132INCLUDES += ${INITIATOR_INCLUDES}
133INCLUDES += ${CANTCOAP_INCLUDES}
134###########################################
135# default action: build all
136###########################################
137
138$(BUILD_DIR)/%.o: ${ROOT_DIR}/%.c | build_dirs
139	$(CC) ${CFLAGS} ${INCLUDES} -Wa,-a,-ad,-alms=$(<:$(ROOT_DIR)/%.c=$(BUILD_DIR)/%.lst) -c $< -o $@
140
141$(BUILD_DIR)/%.o: ${ROOT_DIR}/%.cpp | build_dirs
142	$(CXX) ${CXXFLAGS} ${INCLUDES} -c $< -o $@
143
144${BUILD_DIR}/${TARGET}:${OBJECTS} Makefile $(USOCORE_UEDHOC_PATH)/Makefile ${USOCORE_UEDHOC_OBJ}
145	$(MAKE) -C $(USOCORE_UEDHOC_PATH)
146	$(CXX) ${OBJECTS} ${LDFLAGS} -o $@
147	$(SZ) $@
148
149oscore_edhoc:
150	$(MAKE) -C $(USOCORE_UEDHOC_PATH)
151
152clean_oscore_edhoc:
153	$(MAKE) -C $(USOCORE_UEDHOC_PATH) clean
154
155build_dirs:
156	mkdir -p $(sort $(dir ${OBJECTS}))
157
158clean:
159	-rm -fR $(BUILD_DIR)
160	$(MAKE) -C $(USOCORE_UEDHOC_PATH) clean
161#######################################
162# dependencies
163#######################################
164DEPENDENCIES := $(shell find ./$(BUILD_DIR) -name '*.d' -type f)
165-include $(DEPENDENCIES)
166