1#
2# Copyright (c) 2019-2022, Linaro Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6
7V		?= 0
8BUILD_INFO	?= 1
9DEBUG		:= 0
10ENCTOOL		?= encrypt_fw${BIN_EXT}
11BINARY		:= $(notdir ${ENCTOOL})
12OPENSSL_DIR	:= /usr
13
14
15MAKE_HELPERS_DIRECTORY := ../../make_helpers/
16include ${MAKE_HELPERS_DIRECTORY}build_macros.mk
17include ${MAKE_HELPERS_DIRECTORY}build_env.mk
18include ${MAKE_HELPERS_DIRECTORY}defaults.mk
19
20OBJECTS := src/encrypt.o \
21           src/cmd_opt.o \
22           src/main.o
23
24HOSTCCFLAGS := -Wall -std=c99
25
26# Select OpenSSL version flag according to the OpenSSL build selected
27# from setting the OPENSSL_DIR path.
28$(eval $(call SELECT_OPENSSL_API_VERSION))
29
30ifeq (${DEBUG},1)
31  HOSTCCFLAGS += -g -O0 -DDEBUG -DLOG_LEVEL=40
32else
33ifeq (${BUILD_INFO},1)
34  HOSTCCFLAGS += -O2 -DLOG_LEVEL=20
35else
36  HOSTCCFLAGS += -O2 -DLOG_LEVEL=10
37endif
38endif
39ifeq (${V},0)
40  Q := @
41else
42  Q :=
43endif
44
45HOSTCCFLAGS += ${DEFINES}
46# USING_OPENSSL3 flag will be added to the HOSTCCFLAGS variable with the proper
47# computed value.
48HOSTCCFLAGS += -DUSING_OPENSSL3=$(USING_OPENSSL3)
49
50
51# Make soft links and include from local directory otherwise wrong headers
52# could get pulled in from firmware tree.
53INC_DIR := -I ./include -I ../../include/tools_share -I ${OPENSSL_DIR}/include
54
55# Include library directories where OpenSSL library files are located.
56# For a normal installation (i.e.: when ${OPENSSL_DIR} = /usr or
57# /usr/local), binaries are located under the ${OPENSSL_DIR}/lib/
58# directory. However, for a local build of OpenSSL, the built binaries are
59# located under the main project directory (i.e.: ${OPENSSL_DIR}, not
60# ${OPENSSL_DIR}/lib/).
61LIB_DIR := -L ${OPENSSL_DIR}/lib -L ${OPENSSL_DIR}
62LIB := -lssl -lcrypto
63
64HOSTCC ?= gcc
65
66.PHONY: all clean realclean --openssl
67
68all: ${BINARY}
69
70${BINARY}: --openssl ${OBJECTS} Makefile
71	@echo "  HOSTLD  $@"
72	@echo 'const char build_msg[] = "Built : "__TIME__", "__DATE__;' | \
73                ${HOSTCC} -c ${HOSTCCFLAGS} -xc - -o src/build_msg.o
74	${Q}${HOSTCC} src/build_msg.o ${OBJECTS} ${LIB_DIR} ${LIB} -o $@
75
76%.o: %.c
77	@echo "  HOSTCC  $<"
78	${Q}${HOSTCC} -c ${HOSTCCFLAGS} ${INC_DIR} $< -o $@
79
80--openssl:
81ifeq ($(DEBUG),1)
82	@echo "Selected OpenSSL version: ${OPENSSL_CURRENT_VER}"
83endif
84
85clean:
86	$(call SHELL_DELETE_ALL, src/build_msg.o ${OBJECTS})
87
88realclean: clean
89	$(call SHELL_DELETE,${BINARY})
90