-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMakefile_v1
407 lines (347 loc) · 14.8 KB
/
Makefile_v1
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/make
# Makefile for libsecutils
#
# Copyright (c) Siemens Mobility GmbH, 2021
#
# Authors:
# David von Oheimb <David.von.Oheimb@siemens.com>
#
# This work is licensed under the terms of the Apache Software License 2.0.
# See the COPYING file in the top-level directory.
#
# SPDX-License-Identifier: Apache-2.0
# Optional OPENSSL_DIR defines where to find the OpenSSL installation
# with header files at include/openssl (default: will try, e.g., /usr).
# Optional OPENSSL_LIB defines where to find the OpenSSL libraries
# (default: will try, e.g., OPENSSL_DIR/lib).
# Optional CFLAGS and LDFLAGS are appended by local settings.
# Optional DEBUG_FLAGS may set to prepend to local CFLAGS and LDFLAGS (default see below).
# Builds are done in release mode if optional NDEBUG is defined.
# Optional OUT_DIR defines where to place the resulting library (default: '.').
# Optional DESTDIR defines a prefix for the installation target directories.
# All paths may be absolute or relative to the directory containing this Makefile.
SHELL=bash # This is needed for supporting extended file name globbing
# variables ####################################################################
ROOTFS ?= $(DESTDIR)$(prefix)
ifeq ($(OUT_DIR),)
override OUT_DIR = .
endif
VERSION=2.0
# must be kept in sync with debian/changelog and CMakeLists.txt
# PACKAGENAME=libsecutils
# DIRNAME=$(PACKAGENAME)-$(VERSION)
# https://stackoverflow.com/questions/714100/os-detecting-makefile
ifeq ($(OS),Windows_NT) # strange but apparently this string is used also for all later versions
# so far, we do not support Windows, but trying to continue anyway
override OS=Windows
USERS='^([[:alpha:]]:)?\\Users\\'
EXE=.exe
DLL=.dll
SONAME=
LDD=TODO
OBJ=.obj
LIB=bin
else
EXE=
OBJ=.o
LIB=lib
override OS = $(shell sh -c 'uname 2>/dev/null || echo Unknown')
USERS='^/(home|Users)/'
ifeq ($(shell uname -s),Darwin)
override OS=MacOS
DLL=.dylib
SONAME=install_name,@rpath/
LDD=otool -l
else # assuming other Unix-like
DLL=.so
SONAME=soname,
LDD=ldd
endif
endif
ifneq ($(filter-out doc install uninstall clean clean_config clean_all clean_uta clean_deb,$(MAKECMDGOALS)),)
ifeq ($(OPENSSL_DIR),) # for convenience, use heuristics to determine OPENSSL_DIR
ifeq ($(OS),MacOS)
SYSTEM_INCLUDE_OPENSSL=/opt/homebrew/include/openssl # usually symlink
else # TODO for Windows
SYSTEM_INCLUDE_OPENSSL=/usr/include/openssl
endif
OPENSSL_INCLUDE_DIR = $(realpath $(SYSTEM_INCLUDE_OPENSSL))
override OPENSSL_DIR = $(realpath $(OPENSSL_INCLUDE_DIR)/../..)
endif
ifneq ($(OPENSSL_DIR),) # due to the above, always true
LIB_NAME_PATTERN=libcrypto*$(DLL)*
ifeq ($(realpath $(OPENSSL_DIR)),)
$(error OPENSSL_DIR appears to be an invalid path: $(OPENSSL_DIR))
endif
override OPENSSL_DIR := $(realpath $(OPENSSL_DIR))
ifeq ($(OPENSSL_LIB),) # for convenience, use heuristics to determine OPENSSL_LIB
override OPENSSL_LIB = $(OPENSSL_DIR)/$(LIB)
ifeq ($(wildcard $(OPENSSL_LIB)/$(LIB_NAME_PATTERN)),)
$(warning Warning: cannot find OpenSSL libraries at determined location $(OPENSSL_LIB), now trying $(OPENSSL_DIR))
override OPENSSL_LIB = $(OPENSSL_DIR)
ifeq ($(wildcard $(OPENSSL_LIB)/$(LIB_NAME_PATTERN)),)
ifeq ($(OS),Linux)
ifeq ($(shell echo $(OPENSSL_DIR) | grep -E '^/(home|Users)'),)
override OPENSSL_LIB = $(wildcard /lib/*linux-gnu*)
$(warning Warning: cannot find OpenSSL libraries at $(OPENSSL_DIR), now trying $(OPENSSL_LIB))
endif
endif
endif
endif
else
ifeq ($(wildcard $(OPENSSL_LIB)/$(LIB_NAME_PATTERN)),)
# $(warning Warning: cannot find OpenSSL libraries at given OPENSSL_LIB $(OPENSSL_LIB), now trying OPENSSL_DIR)
override OPENSSL_LIB = $(OPENSSL_DIR)
endif
endif
# ifeq ($(findstring $(USERS),$(OPENSSL_FULL_DIR)),)
# $(warning [DEBUG] OPENSSL_DIR is assumed to be an installation directory)
# else
# $(warning [DEBUG] OPENSSL_DIR is assumed to be a local build directory)
# endif
ifeq ($(wildcard $(OPENSSL_LIB)/$(LIB_NAME_PATTERN)),)
$(error Error: cannot find OpenSSL library $(LIB_NAME_PATTERN) at $(OPENSSL_LIB)/)
endif
override OPENSSL_LIB := $(realpath $(OPENSSL_LIB))
endif
ifeq ($(wildcard $(OPENSSL_DIR)/include/openssl),)
$(error cannot find directory '$(OPENSSL_DIR)/include/openssl', check OPENSSL_DIR variable)
endif
endif # neq ($(filter-out doc install uninstall clean clean_config clean_all clean_uta clean_deb,$(MAKECMDGOALS)),)
################################################################################
# Basic definitions targeted at debugging
#
# Can be overridden by command line arguments (not ENV variables!)
# Note: stuff for testing purposes should go here
################################################################################
ifdef NDEBUG
override DEBUG_FLAGS ?= -O2
override DEBUG_FLAGS += -DNDEBUG=1 -Werror
else
override DEBUG_FLAGS ?= -g -O0 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all # not every compiler(version) supports -Og
endif
override CFLAGS += $(DEBUG_FLAGS) \
-Wall -Woverflow -Wextra -Wswitch -Wmissing-prototypes -Wstrict-prototypes \
-Wformat -Wformat-security -Wtype-limits -Wundef \
-Wsign-compare -Wpointer-arith -Wunused-parameter
# TODO clean up code and re-enable warnings instead:
override CFLAGS += -Wno-conversion -Wno-sign-conversion \
-Wno-shadow -Wno-declaration-after-statement -Wno-vla -Wno-gnu-folding-constant
override CFLAGS += -pedantic -DPEDANTIC
################################################################################
# Obligatory flags
#
# Can not be overriden
################################################################################
CC ?= gcc
override OUTLIB_= libsecutils
OUTLIB=$(OUTLIB_)$(DLL)
ifeq ($(OS),MacOS)
OUTLIBV=$(OUTLIB_).$(VERSION)$(DLL)
else
OUTLIBV=$(OUTLIB).$(VERSION)
override CFLAGS += -D_FORTIFY_SOURCE=2
endif
DEST_PRE=$(ROOTFS)/usr/local
DEST_LIB=$(DEST_PRE)/lib
DEST_INC=$(DEST_PRE)
DEST_DOC=$(DEST_PRE)/share/doc/libsecutils-dev
OUTBIN=icvutil$(EXE)
DEST_BIN=$(DEST_PRE)/bin
LOCAL_CFLAGS= -fPIC # -std=gnu90 TODO clean up code and re-enable flag
override CFLAGS += -isystem $(OPENSSL_DIR)/include# # use of -isystem is critical for selecting wanted OpenSSL version
override CFLAGS += -Isrc/libsecutils/include
override CFLAGS += -Isrc/libsecutils/include/secutils
ifneq ($(SECUTILS_USE_UTA),)
override CFLAGS += -DSECUTILS_USE_UTA=1
endif
ifneq ($(SECUTILS_USE_ICV),)
override CFLAGS += -DSECUTILS_USE_ICV=1
endif
override LDFLAGS += $(DEBUG_FLAGS) # needed for -fsanitize=...
override LDFLAGS += -L $(OPENSSL_LIB)
ifeq ($(DEB_TARGET_ARCH),) # not during Debian packaging
override LDFLAGS += -Wl,-rpath,$(OPENSSL_LIB)
endif
ifneq ($(SECUTILS_USE_UTA),)
override LDFLAGS += -luta
endif
ifneq ($(SECUTILS_NO_TLS),)
override CFLAGS += -DSECUTILS_NO_TLS=1
else
override LDFLAGS += -lssl
endif
override LDFLAGS += -lcrypto
ifeq ($(COMPILE_TYPE), code_coverage)
override CFLAGS += --coverage
override LDFLAGS += --coverage
COV_ENABLED=1
unexport COMPILE_TYPE
endif
################################################################################
# Helper variables
################################################################################
# Directory for object files
BUILDDIR=tmp
# Path for automatic lookup of source files by Make
# Note: sort removes duplicate entries
VPATH := src/libsecutils/src
VPATH += $(sort $(dir $(wildcard src/libsecutils/src/*/)))
################################################################################
# Objects lists
################################################################################
# Target object files lookup in src directory (and mapping to build directory)
OBJS := $(patsubst %.c,$(BUILDDIR)/%$(OBJ),$(notdir $(wildcard src/libsecutils/src/*/*.c)))
################################################################################
# Targets
################################################################################
# building #####################################################################
# Phony (non-file) targets
.PHONY: all doc util build build_only build_all clean clean_config clean_all clean_uta install uninstall deb clean_deb coverage
# Default target
all: build_all doc
$(OUT_DIR):
@mkdir -p $(OUT_DIR)
ifneq ($(findstring build_only,$(MAKECMDGOALS)),)
$(info Build info: source directory is $(PWD))
$(info detected OpenSSL base directory $(OPENSSL_DIR))
$(info detected OpenSSL lib directory $(OPENSSL_LIB))
endif
build_only: $(OUT_DIR)/$(OUTLIB)
build:
ifeq ($(COV_ENABLED), 1)
COMPILE_TYPE=code_coverage
endif
$(MAKE) -f Makefile_v1 COMPILE_TYPE=$(COMPILE_TYPE) build_only
SECUTILS_CONFIG=src/libsecutils/include/secutils/secutils_static_config.h
util: $(SECUTILS_CONFIG)
ifneq ($(SECUTILS_USE_ICV),)
$(MAKE) -C src/util -f Makefile_v1 CFLAGS="$(CFLAGS) $(LOCAL_CFLAGS)" LDFLAGS="$(LDFLAGS)" OUT_DIR="$(OUT_DIR)"
endif
build_all: build util
$(SECUTILS_CONFIG): $(SECUTILS_CONFIG).in # limitation: this is not triggered when any of the env. vars changes
cp $< $@
ifdef SECUTILS_USE_UTA
@ # note that sed -i '' (for not saving a backup file) works on MacOS but not on Linux
@sed -i~ -e 's|#cmakedefine SECUTILS_USE_UTA|#define SECUTILS_USE_UTA|' $@
else
@sed -i~ -e 's|#cmakedefine SECUTILS_USE_UTA|/* #undef SECUTILS_USE_UTA */|' $@
endif
ifdef SECUTILS_USE_ICV
@sed -i~ -e 's|#cmakedefine SECUTILS_USE_ICV|#define SECUTILS_USE_ICV|' $@
else
@sed -i~ -e 's|#cmakedefine SECUTILS_USE_ICV|/* #undef SECUTILS_USE_ICV */|' $@
endif
ifdef SECUTILS_NO_TLS
@sed -i~ -e 's|#cmakedefine SECUTILS_NO_TLS|#define SECUTILS_NO_TLS|' $@
else
@sed -i~ -e 's|#cmakedefine SECUTILS_NO_TLS|/* #undef SECUTILS_NO_TLS */|' $@
endif
# Binary output target
$(OUT_DIR)/$(OUTLIBV): $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) -shared -o $@ -Wl,-$(SONAME)$(OUTLIBV)
$(OUT_DIR)/$(OUTLIB): $(OUT_DIR)/$(OUTLIBV) fix_build_lib
ln -sf $(OUTLIBV) $(OUT_DIR)/$(OUTLIB)
# Individual object targets; also provide dependencies on header files of the project (not on system headers)
$(BUILDDIR)/%$(OBJ): %.c $(SECUTILS_CONFIG)
$(CC) $(CFLAGS) $(LOCAL_CFLAGS) -c $< -o $@
@$(CC) $(CFLAGS) $(LOCAL_CFLAGS) -MM $< -MT $@ -MF $(BUILDDIR)/$*.d
DEPS = $(OBJS:$(OBJ)=.d)
ifeq ($(findstring clean,$(MAKECMDGOALS)),)
-include $(DEPS)
endif
# Build directory generation
$(BUILDDIR):
mkdir -p $(BUILDDIR)
# Order-only dependency for objects on build dir - prevents unnecessary rebuilds
# (directories are flagged as changed on every object build)
$(OBJS): | $(BUILDDIR)
# workaround for using local OpenSSL builds by default expecting that
# its dynamic libs have been installed in ./$(LIB) when using the libs
# see for binaries that dynamically link to OpenSSL the output of $(LDD) <binary>
.PHONY: fix_build_lib
fix_build_lib:
ifneq ($(shell echo $(realpath $(OPENSSL_LIB)) | grep -E $(USERS)),)
ifeq ($(OPENSSL_LIB),$(OPENSSL_DIR))
@cd "$(OPENSSL_DIR)"; if [ ! -e $(LIB) ]; then ln -s . $(LIB); fi
@ # alternative would be to use, e.g.,
@ # install_name_tool -change $(OPENSSL_DIR)/lib/libcrypto.3.dylib $(OPENSSL_DIR)/libcrypto.3.dylib <libname>
endif
endif
@true # prevent warning "Nothing to be done for `fix_build_lib'."
# Debian packaging #############################################################
deb:
debuild -e OPENSSL_DIR="$(OPENSSL_DIR)" -e OPENSSL_LIB="$(OPENSSL_LIB)" \
--preserve-envvar SECUTILS_NO_TLS \
--preserve-envvar SECUTILS_USE_ICV \
--preserve-envvar SECUTILS_USE_UTA -uc -us \
--lintian-opts --profile debian # --fail-on none
# alternative:
# LD_LIBRARY_PATH= dpkg-buildpackage -uc -us # may prepend DH_VERBOSE=1
clean_deb:
rm -rf debian/{.debhelper,tmp,libsecutils{,-dev,-bin}} debian-packaging
rm -f debian/{files,debhelper-build-stamp} debian/*.{log,substvars}
rm -f ../libsecutils*.{deb,dsc,build*,changes,tar.gz}
rm -fr _CPack_Packages changelog.gz
rm -f libsecutils*.{deb,tar.gz,zip}
# installation #################################################################
# installation target - append ROOTFS=<path> to install into virtual root
# filesystem
install: # doc/html $(OUT_DIR)/$(OUTLIB) $(OUT_DIR)/$(OUTBIN)
install -D $(OUT_DIR)/$(OUTLIBV) $(DEST_LIB)/$(OUTLIBV)
ln -sf $(OUTLIBV) $(DEST_LIB)/$(OUTLIB)
#install_headers:
mkdir -p $(DEST_INC)/include/secutils
cd src/libsecutils; find include/secutils -type d -exec install -d '$(DEST_INC)/{}' ';'
cd src/libsecutils; find include/secutils -type f -exec install -Dm 0644 '{}' '$(DEST_INC)/{}' ';'
#install_bins:
ifeq ($(SECUTILS_USE_ICV),)
touch $(OUT_DIR)/$(OUTBIN) # workaround for Debian packaging
else
install -D ./$(OUTBIN) $(DEST_BIN)/$(OUTBIN)
endif
install -D $(OUT_DIR)/$(OUTBIN) $(DEST_BIN)/$(OUTBIN)
#install_doc:
ln -sf doc/html
find html/ -type d -exec install -d '$(DEST_DOC)/{}' ';'
find html/ -type f -exec install -Dm 0644 '{}' '$(DEST_DOC)/{}' ';'
rm html
uninstall:
rm -fr $(DEST_LIB)/$(OUTLIB_)*$(DLL)*
# find include -type f -exec rm '$(DEST_PRE)/{}' ';'
rm -rf $(DEST_INC)/include/secutils
rm -f $(DEST_BIN)/$(OUTBIN)
rm -rf $(DEST_DOC)/doc/html
# cleaning #####################################################################
clean_uta:
rm -fr $(BUILDDIR)/uta_api$(OBJ) $(BUILDDIR)/files_icv$(OBJ) \
$(BUILDDIR)/files_dv$(OBJ) \
$(OUT_DIR){,/src/libsecutils}/$(OUTLIB_)*$(DLL)* $(OUT_DIR){,/src/util}/$(OUTBIN) $(OUT_DIR)/util/icvutil$(OBJ)
clean: clean_config
rm -fr $(OUT_DIR)/$(OUTLIB_)*$(DLL)*
rm -f $(OUT_DIR)/$(OUTBIN)
$(MAKE) -C src/util -f Makefile_v1 clean OUT_DIR="$(OUT_DIR)"
rm -fr $(BUILDDIR)
clean_config:
rm -f $(SECUTILS_CONFIG){,~}
clean_all: clean clean_deb
find . \( -name "*.cmake" -o -name Makefile \) \
-not -path ./src/libsecutils/security-utilities_libraryConfig.cmake \
-not -path ./src/util/security-utilities_icvutilConfig.cmake \
-not -path ./coverage/Makefile \
| xargs rm 2>/dev/null || true
find . -name CMakeFiles | xargs rm -r 2>/dev/null || true
rm -f install_manifest*.txt
rm -fr doc refman.pdf CMakeDoxyfile.in Doxyfile.security-utilities_doxygen Doxyfile.doc *.gcov reports
rm -fr _CPack_Packages Makefile CMakeCache.txt
# documentation ################################################################
doc: $(SECUTILS_CONFIG) doc/html refman.pdf
doc/html: Doxyfile $(wildcard src/libsecutils/include/*/*.h src/libsecutils/include/*/*/*.h)
doxygen Doxyfile 1>/dev/null || mkdir -p doc/html # required packages: doxygen graphviz
refman.pdf: doc/html
mkdir -p doc/latex
@# for producing doc/latex/*, comment out in Doxyfile: GENERATE_LATEX = NO
@# $(MAKE) -C -f Makefile_v1 doc/latex && cp -a doc/latex/refman.pdf . # requires latex
# others #######################################################################
coverage: clean
$(MAKE) -f Makefile_v1 COMPILE_TYPE=code_coverage