summaryrefslogtreecommitdiffstats
path: root/src/spdk/intel-ipsec-mb/Makefile
blob: 2d993bdf48338a06ed5de7f0c0d7b327aecde9f5 (plain)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#
# Copyright (c) 2012-2019, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Intel Corporation nor the names of its contributors
#       may be used to endorse or promote products derived from this software
#       without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

LIB = libIPSec_MB
SHARED ?= y
IMB_HDR = intel-ipsec-mb.h

# Detect library version
IMB_VERSION = $(shell grep -e "IMB_VERSION_STR" $(IMB_HDR) | cut -d'"' -f2)
ifeq ($(IMB_VERSION),)
$(error "Failed to detect library version!")
endif

VERSION = $(shell echo $(IMB_VERSION) | cut -d. -f1-3)
SO_VERSION = $(shell echo $(VERSION) | cut -d. -f1)

PREFIX ?= /usr
LIB_INSTALL_DIR ?= $(PREFIX)/lib
HDR_DIR ?= $(PREFIX)/include
MAN_DIR ?= $(PREFIX)/man/man7
MAN1 = libipsec-mb.7
MAN2 = libipsec-mb-dev.7
NOLDCONFIG ?= n

USE_YASM ?= n
YASM ?= yasm
NASM ?= nasm

OBJ_DIR = obj

INCLUDE_DIRS := include . no-aesni
INCLUDES := $(foreach i,$(INCLUDE_DIRS),-I $i)

CC ?= gcc

CFLAGS := -DLINUX $(EXTRA_CFLAGS) $(INCLUDES) \
	-W -Wall -Wextra -Wmissing-declarations -Wpointer-arith \
	-Wcast-qual -Wundef -Wwrite-strings  \
	-Wformat -Wformat-security \
	-Wunreachable-code -Wmissing-noreturn -Wsign-compare -Wno-endif-labels \
	-Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition \
	-fno-strict-overflow -fno-delete-null-pointer-checks -fwrapv

ASM_INCLUDE_DIRS := .

YASM_INCLUDES := $(foreach i,$(ASM_INCLUDE_DIRS),-I $i)
YASM_FLAGS := -f x64 -f elf64 -X gnu -g dwarf2 -DLINUX -D__linux__ $(YASM_INCLUDES)

NASM_INCLUDES := $(foreach i,$(ASM_INCLUDE_DIRS),-I$i/)
NASM_FLAGS := -felf64 -Xgnu -gdwarf -DLINUX -D__linux__ $(NASM_INCLUDES)

ifeq ($(DEBUG),y)
CFLAGS += -g -DDEBUG
OPT = -O0
LDFLAGS += -g
else
OPT = -O3
CFLAGS += -fstack-protector -D_FORTIFY_SOURCE=2
endif

ifeq ($(SAFE_DATA),y)
CFLAGS += -DSAFE_DATA
NASM_FLAGS += -DSAFE_DATA
YASM_FLAGS += -DSAFE_DATA
endif

ifeq ($(SAFE_PARAM),y)
CFLAGS += -DSAFE_PARAM
NASM_FLAGS += -DSAFE_PARAM
YASM_FLAGS += -DSAFE_PARAM
endif

ifneq ($(SAFE_LOOKUP),n)
CFLAGS += -DSAFE_LOOKUP
NASM_FLAGS += -DSAFE_LOOKUP
YASM_FLAGS += -DSAFE_LOOKUP
endif

# prevent SIMD optimizations for non-aesni modules
CFLAGS_NO_SIMD = $(CFLAGS) -O1
CFLAGS += $(OPT)

# Set architectural optimizations for GCC/CC
ifeq ($(CC),$(filter $(CC),gcc cc))
GCC_VERSION = $(shell $(CC) -dumpversion | cut -d. -f1)
GCC_GE_V5 = $(shell [ $(GCC_VERSION) -ge 5 ] && echo true)
ifeq ($(GCC_GE_V5),true)
OPT_SSE = -march=nehalem -maes
OPT_AVX = -march=sandybridge -maes
OPT_AVX2 = -march=haswell -maes
OPT_AVX512 = -march=broadwell
OPT_NOAESNI = -march=nehalem
endif
endif

# so or static build
ifeq ($(SHARED),y)
CFLAGS += -fPIC
LIBNAME = $(LIB).so.$(VERSION)
LIBPERM = 0755
LDFLAGS += -z noexecstack -z relro -z now
else
# SPDK crypto needs shared but -fPIC so to make it clear what
# we are doing, we just comment out the CFLAGS below and
# explicitly set them in our relevant makefile.
#CFLAGS += -fPIE
LIBNAME = $(LIB).a
LIBPERM = 0644
LDFLAGS += -g
endif

# warning messages
SAFE_PARAM_MSG1="SAFE_PARAM option not set."
SAFE_PARAM_MSG2="Input parameters will not be checked."
SAFE_DATA_MSG1="SAFE_DATA option not set."
SAFE_DATA_MSG2="Stack and registers containing sensitive information, \
		such keys or IV will not be cleared \
		at the end of function calls."
SAFE_LOOKUP_MSG1="SAFE_LOOKUP option not set."
SAFE_LOOKUP_MSG2="Lookups which depend on sensitive information \
		are not guaranteed to be done in constant time."

ifeq ($(GCM_BIG_DATA),y)
CFLAGS += -DGCM_BIG_DATA
NASM_FLAGS += -DGCM_BIG_DATA
YASM_FLAGS += -DGCM_BIG_DATA
endif

#
# List of C modules (any origin)
#
c_lib_objs := \
	mb_mgr_avx.o \
	mb_mgr_avx2.o \
	mb_mgr_avx512.o \
	mb_mgr_sse.o \
	mb_mgr_sse_no_aesni.o \
	alloc.o \
	aes_xcbc_expand_key.o \
	md5_one_block.o \
	sha_one_block.o \
	des_key.o \
	des_basic.o \
	version.o \
	cpu_feature.o \
	aesni_emu.o \
	kasumi_avx.o \
	kasumi_iv.o \
	kasumi_sse.o \
	zuc_sse_top.o \
	zuc_avx_top.o \
	zuc_iv.o \
	snow3g_sse.o \
	snow3g_sse_no_aesni.o \
	snow3g_avx.o \
	snow3g_avx2.o \
	snow3g_tables.o \
	snow3g_iv.o

#
# List of ASM modules (root directory/common)
#
asm_generic_lib_objs := \
	aes_keyexp_128.o \
	aes_keyexp_192.o \
	aes_keyexp_256.o \
	aes_cmac_subkey_gen.o \
	save_xmms.o \
	clear_regs_mem_fns.o \
	const.o \
	aes128_ecbenc_x3.o \
	zuc_common.o \
	wireless_common.o \
	constant_lookup.o

#
# List of ASM modules (no-aesni directory)
#
asm_noaesni_lib_objs := \
	aes128_cbc_dec_by4_sse_no_aesni.o \
	aes192_cbc_dec_by4_sse_no_aesni.o \
	aes256_cbc_dec_by4_sse_no_aesni.o \
	aes_cbc_enc_128_x4_no_aesni.o \
	aes_cbc_enc_192_x4_no_aesni.o \
	aes_cbc_enc_256_x4_no_aesni.o \
	aes128_cntr_by4_sse_no_aesni.o \
	aes192_cntr_by4_sse_no_aesni.o \
	aes256_cntr_by4_sse_no_aesni.o \
	aes_ecb_by4_sse_no_aesni.o \
	aes128_cntr_ccm_by4_sse_no_aesni.o \
	pon_sse_no_aesni.o \
	aes_cfb_128_sse_no_aesni.o \
	aes128_cbc_mac_x4_no_aesni.o \
	aes_xcbc_mac_128_x4_no_aesni.o \
	mb_mgr_aes_flush_sse_no_aesni.o \
	mb_mgr_aes_submit_sse_no_aesni.o \
	mb_mgr_aes192_flush_sse_no_aesni.o \
	mb_mgr_aes192_submit_sse_no_aesni.o \
	mb_mgr_aes256_flush_sse_no_aesni.o \
	mb_mgr_aes256_submit_sse_no_aesni.o \
	mb_mgr_aes_cmac_submit_flush_sse_no_aesni.o \
	mb_mgr_aes_ccm_auth_submit_flush_sse_no_aesni.o \
	mb_mgr_aes_xcbc_flush_sse_no_aesni.o \
	mb_mgr_aes_xcbc_submit_sse_no_aesni.o

#
# List of ASM modules (sse directory)
#
asm_sse_lib_objs := \
	aes128_cbc_dec_by4_sse.o \
	aes192_cbc_dec_by4_sse.o \
	aes256_cbc_dec_by4_sse.o \
	aes_cbc_enc_128_x4.o \
	aes_cbc_enc_192_x4.o \
	aes_cbc_enc_256_x4.o \
	pon_sse.o \
	aes128_cntr_by4_sse.o \
	aes192_cntr_by4_sse.o \
	aes256_cntr_by4_sse.o \
	aes_ecb_by4_sse.o \
	aes128_cntr_ccm_by4_sse.o \
	aes_cfb_128_sse.o \
	aes128_cbc_mac_x4.o \
	aes_xcbc_mac_128_x4.o \
	md5_x4x2_sse.o \
	sha1_mult_sse.o \
	sha1_one_block_sse.o \
	sha224_one_block_sse.o \
	sha256_one_block_sse.o \
	sha384_one_block_sse.o \
	sha512_one_block_sse.o \
	sha512_x2_sse.o \
	sha_256_mult_sse.o \
	sha1_ni_x2_sse.o \
	sha256_ni_x2_sse.o \
	zuc_sse.o \
	mb_mgr_aes_flush_sse.o \
	mb_mgr_aes_submit_sse.o \
	mb_mgr_aes192_flush_sse.o \
	mb_mgr_aes192_submit_sse.o \
	mb_mgr_aes256_flush_sse.o \
	mb_mgr_aes256_submit_sse.o \
	mb_mgr_aes_cmac_submit_flush_sse.o \
	mb_mgr_aes_ccm_auth_submit_flush_sse.o \
	mb_mgr_aes_xcbc_flush_sse.o \
	mb_mgr_aes_xcbc_submit_sse.o \
	mb_mgr_hmac_md5_flush_sse.o \
	mb_mgr_hmac_md5_submit_sse.o \
	mb_mgr_hmac_flush_sse.o \
	mb_mgr_hmac_submit_sse.o \
	mb_mgr_hmac_sha_224_flush_sse.o \
	mb_mgr_hmac_sha_224_submit_sse.o \
	mb_mgr_hmac_sha_256_flush_sse.o \
	mb_mgr_hmac_sha_256_submit_sse.o \
	mb_mgr_hmac_sha_384_flush_sse.o \
	mb_mgr_hmac_sha_384_submit_sse.o \
	mb_mgr_hmac_sha_512_flush_sse.o \
	mb_mgr_hmac_sha_512_submit_sse.o \
	mb_mgr_hmac_flush_ni_sse.o \
	mb_mgr_hmac_submit_ni_sse.o \
	mb_mgr_hmac_sha_224_flush_ni_sse.o \
	mb_mgr_hmac_sha_224_submit_ni_sse.o \
	mb_mgr_hmac_sha_256_flush_ni_sse.o \
	mb_mgr_hmac_sha_256_submit_ni_sse.o

#
# List of ASM modules (avx directory)
#
asm_avx_lib_objs := \
	aes_cbc_enc_128_x8.o \
	aes_cbc_enc_192_x8.o \
	aes_cbc_enc_256_x8.o \
	aes128_cbc_dec_by8_avx.o \
	aes192_cbc_dec_by8_avx.o \
	aes256_cbc_dec_by8_avx.o \
	pon_avx.o \
	aes128_cntr_by8_avx.o \
	aes192_cntr_by8_avx.o \
	aes256_cntr_by8_avx.o \
	aes128_cntr_ccm_by8_avx.o \
	aes_ecb_by4_avx.o \
	aes_cfb_128_avx.o \
	aes128_cbc_mac_x8.o \
	aes_xcbc_mac_128_x8.o \
	md5_x4x2_avx.o \
	sha1_mult_avx.o \
	sha1_one_block_avx.o \
	sha224_one_block_avx.o \
	sha256_one_block_avx.o \
	sha_256_mult_avx.o \
	sha384_one_block_avx.o \
	sha512_one_block_avx.o \
	sha512_x2_avx.o \
	zuc_avx.o \
	mb_mgr_aes_flush_avx.o \
	mb_mgr_aes_submit_avx.o \
	mb_mgr_aes192_flush_avx.o \
	mb_mgr_aes192_submit_avx.o \
	mb_mgr_aes256_flush_avx.o \
	mb_mgr_aes256_submit_avx.o \
	mb_mgr_aes_cmac_submit_flush_avx.o\
	mb_mgr_aes_ccm_auth_submit_flush_avx.o \
	mb_mgr_aes_xcbc_flush_avx.o \
	mb_mgr_aes_xcbc_submit_avx.o \
	mb_mgr_hmac_md5_flush_avx.o \
	mb_mgr_hmac_md5_submit_avx.o \
	mb_mgr_hmac_flush_avx.o \
	mb_mgr_hmac_submit_avx.o \
	mb_mgr_hmac_sha_224_flush_avx.o \
	mb_mgr_hmac_sha_224_submit_avx.o \
	mb_mgr_hmac_sha_256_flush_avx.o \
	mb_mgr_hmac_sha_256_submit_avx.o \
	mb_mgr_hmac_sha_384_flush_avx.o \
	mb_mgr_hmac_sha_384_submit_avx.o \
	mb_mgr_hmac_sha_512_flush_avx.o \
	mb_mgr_hmac_sha_512_submit_avx.o

#
# List of ASM modules (avx2 directory)
#
asm_avx2_lib_objs := \
	md5_x8x2_avx2.o \
	sha1_x8_avx2.o \
	sha256_oct_avx2.o \
	sha512_x4_avx2.o \
	mb_mgr_hmac_md5_flush_avx2.o \
	mb_mgr_hmac_md5_submit_avx2.o \
	mb_mgr_hmac_flush_avx2.o \
	mb_mgr_hmac_submit_avx2.o \
	mb_mgr_hmac_sha_224_flush_avx2.o \
	mb_mgr_hmac_sha_224_submit_avx2.o \
	mb_mgr_hmac_sha_256_flush_avx2.o \
	mb_mgr_hmac_sha_256_submit_avx2.o \
	mb_mgr_hmac_sha_384_flush_avx2.o \
	mb_mgr_hmac_sha_384_submit_avx2.o \
	mb_mgr_hmac_sha_512_flush_avx2.o \
	mb_mgr_hmac_sha_512_submit_avx2.o

#
# List of ASM modules (avx512 directory)
#
asm_avx512_lib_objs := \
	sha1_x16_avx512.o \
	sha256_x16_avx512.o \
	sha512_x8_avx512.o \
	des_x16_avx512.o \
	cntr_vaes_avx512.o \
	aes_cbc_dec_vaes_avx512.o \
	aes_cbc_enc_vaes_avx512.o \
	mb_mgr_aes_submit_avx512.o \
	mb_mgr_aes_flush_avx512.o \
	mb_mgr_aes192_submit_avx512.o \
	mb_mgr_aes192_flush_avx512.o \
	mb_mgr_aes256_submit_avx512.o \
	mb_mgr_aes256_flush_avx512.o \
	mb_mgr_hmac_flush_avx512.o \
	mb_mgr_hmac_submit_avx512.o \
	mb_mgr_hmac_sha_224_flush_avx512.o \
	mb_mgr_hmac_sha_224_submit_avx512.o \
	mb_mgr_hmac_sha_256_flush_avx512.o \
	mb_mgr_hmac_sha_256_submit_avx512.o \
	mb_mgr_hmac_sha_384_flush_avx512.o \
	mb_mgr_hmac_sha_384_submit_avx512.o \
	mb_mgr_hmac_sha_512_flush_avx512.o \
	mb_mgr_hmac_sha_512_submit_avx512.o \
	mb_mgr_des_avx512.o

#
# GCM object file lists
#

c_gcm_objs := gcm.o

asm_noaesni_gcm_objs := \
	gcm128_sse_no_aesni.o gcm192_sse_no_aesni.o gcm256_sse_no_aesni.o

asm_sse_gcm_objs := \
	gcm128_sse.o gcm192_sse.o gcm256_sse.o

asm_avx_gcm_objs := \
	gcm128_avx_gen2.o gcm192_avx_gen2.o gcm256_avx_gen2.o

asm_avx2_gcm_objs := \
	gcm128_avx_gen4.o gcm192_avx_gen4.o gcm256_avx_gen4.o

asm_avx512_gcm_objs := \
	gcm128_vaes_avx512.o gcm192_vaes_avx512.o gcm256_vaes_avx512.o \
	gcm128_avx512.o gcm192_avx512.o gcm256_avx512.o

#
# build object files lists for GCM and NO-GCM variants
#
ifeq ($(NO_GCM), y)
CFLAGS += -DNO_GCM
asm_obj_files := $(asm_generic_lib_objs) $(asm_noaesni_lib_objs) \
	$(asm_sse_lib_objs) $(asm_avx_lib_objs) \
	$(asm_avx2_lib_objs) $(asm_avx512_lib_objs)
c_obj_files := $(c_lib_objs)
else
asm_obj_files := $(asm_generic_lib_objs) \
	$(asm_noaesni_lib_objs) $(asm_noaesni_gcm_objs) \
	$(asm_sse_lib_objs) $(asm_sse_gcm_objs) \
	$(asm_avx_lib_objs) $(asm_avx_gcm_objs) \
	$(asm_avx2_lib_objs) $(asm_avx2_gcm_objs) \
	$(asm_avx512_lib_objs) $(asm_avx512_gcm_objs)
c_obj_files := $(c_lib_objs) $(c_gcm_objs)
endif

#
# aggregate all objects files together and prefix with OBJDIR
#
lib_obj_files := $(asm_obj_files) $(c_obj_files)
target_obj_files := $(lib_obj_files:%=$(OBJ_DIR)/%)

#
# create a list of dependency files for assembly modules
# create a list of dependency files for c modules then
# prefix these with OBJDIR
#
asm_dep_files := $(asm_obj_files:%.o=%.d)

c_dep_files := $(c_obj_files:%.o=%.d)
c_dep_target_files := $(c_dep_files:%=$(OBJ_DIR)/%)

#
# aggregate all dependency files together and prefix with OBJDIR
#
dep_files := $(asm_dep_files) $(c_dep_files)
dep_target_files := $(dep_files:%=$(OBJ_DIR)/%)

all: $(LIBNAME)

$(LIBNAME): $(target_obj_files)
ifeq ($(SHARED),y)
	$(CC) -shared -Wl,-soname,$(LIB).so.$(SO_VERSION) -o $(LIBNAME) $^ -lc
	ln -f -s $(LIBNAME) $(LIB).so.$(SO_VERSION)
	ln -f -s $(LIB).so.$(SO_VERSION) $(LIB).so
else
	$(AR) -qcs $@ $^
endif
ifneq ($(SAFE_PARAM), y)
	@echo "NOTE:" $(SAFE_PARAM_MSG1) $(SAFE_PARAM_MSG2)
endif
ifneq ($(SAFE_DATA), y)
	@echo "NOTE:" $(SAFE_DATA_MSG1) $(SAFE_DATA_MSG2)
endif
ifeq ($(SAFE_LOOKUP), n)
	@echo "NOTE:" $(SAFE_LOOKUP_MSG1) $(SAFE_LOOKUP_MSG2)
endif

.PHONY: install
install: $(LIBNAME)
	install -d $(HDR_DIR)
	install -m 0644 $(IMB_HDR) $(HDR_DIR)
	install -d $(LIB_INSTALL_DIR)
	install -s -m $(LIBPERM) $(LIBNAME) $(LIB_INSTALL_DIR)
	install -d $(MAN_DIR)
	install -m 0444 $(MAN1) $(MAN_DIR)
	install -m 0444 $(MAN2) $(MAN_DIR)
ifeq ($(SHARED),y)
	cd $(LIB_INSTALL_DIR); \
		ln -f -s $(LIB).so.$(VERSION) $(LIB).so.$(SO_VERSION); \
		ln -f -s $(LIB).so.$(SO_VERSION) $(LIB).so
ifneq ($(NOLDCONFIG),y)
	ldconfig
endif
endif

.PHONY: uninstall
uninstall: $(LIBNAME)
	-rm -f $(HDR_DIR)/$(IMB_HDR)
	-rm -f $(LIB_INSTALL_DIR)/$(LIBNAME)
	-rm -f $(MAN_DIR)/$(MAN1)
	-rm -f $(MAN_DIR)/$(MAN2)
ifeq ($(SHARED),y)
	-rm -f $(LIB_INSTALL_DIR)/$(LIB).so.$(SO_VERSION)
	-rm -f $(LIB_INSTALL_DIR)/$(LIB).so
endif

.PHONY: build_c_dep_target_files
build_c_dep_target_files: $(c_dep_target_files)

$(target_obj_files): | $(OBJ_DIR) build_c_dep_target_files
$(dep_target_files): | $(OBJ_DIR)

#
# dependency file build recipies
#

$(OBJ_DIR)/%.d:%.c
	$(CC) -MM -MP -MF $@ $(CFLAGS) $<

$(OBJ_DIR)/%.d:sse/%.c
	$(CC) -MM -MP -MF $@ $(CFLAGS) $<

$(OBJ_DIR)/%.d:avx/%.c
	$(CC) -MM -MP -MF $@ $(CFLAGS) $<

$(OBJ_DIR)/%.d:avx2/%.c
	$(CC) -MM -MP -MF $@ $(CFLAGS) $<

$(OBJ_DIR)/%.d:avx512/%.c
	$(CC) -MM -MP -MF $@ $(CFLAGS) $<

$(OBJ_DIR)/%.d:no-aesni/%.c
	$(CC) -MM -MP -MF $@ $(CFLAGS) $<

#
# object file build recipies
#

$(OBJ_DIR)/%.o:%.c
	$(CC) -c $(CFLAGS) $< -o $@

$(OBJ_DIR)/%.o:%.asm
ifeq ($(USE_YASM),y)
	$(YASM) $(YASM_FLAGS) $< -o $@
else
	$(NASM) -MD $(@:.o=.d) -MT $@ -o $@ $(NASM_FLAGS) $<
endif

$(OBJ_DIR)/%.o:sse/%.c
	$(CC) $(OPT_SSE) -c $(CFLAGS) $< -o $@

$(OBJ_DIR)/%.o:sse/%.asm
ifeq ($(USE_YASM),y)
	$(YASM) $(YASM_FLAGS) $< -o $@
else
	$(NASM) -MD $(@:.o=.d) -MT $@ -o $@ $(NASM_FLAGS) $<
endif

$(OBJ_DIR)/%.o:avx/%.c
	$(CC) $(OPT_AVX) -c $(CFLAGS) $< -o $@

$(OBJ_DIR)/%.o:avx/%.asm
ifeq ($(USE_YASM),y)
	$(YASM) $(YASM_FLAGS) $< -o $@
else
	$(NASM) -MD $(@:.o=.d) -MT $@ -o $@ $(NASM_FLAGS) $<
endif

$(OBJ_DIR)/%.o:avx2/%.c
	$(CC) $(OPT_AVX2) -c $(CFLAGS) $< -o $@

$(OBJ_DIR)/%.o:avx2/%.asm
ifeq ($(USE_YASM),y)
	$(YASM) $(YASM_FLAGS) $< -o $@
else
	$(NASM) -MD $(@:.o=.d) -MT $@ -o $@ $(NASM_FLAGS) $<
endif

$(OBJ_DIR)/%.o:avx512/%.c
	$(CC) $(OPT_AVX512) -c $(CFLAGS) $< -o $@

$(OBJ_DIR)/%.o:avx512/%.asm
ifeq ($(USE_YASM),y)
	$(YASM) $(YASM_FLAGS) $< -o $@
else
	$(NASM) -MD $(@:.o=.d) -MT $@ -o $@ $(NASM_FLAGS) $<
endif

$(OBJ_DIR)/%.o:include/%.asm
ifeq ($(USE_YASM),y)
	$(YASM) $(YASM_FLAGS) $< -o $@
else
	$(NASM) -MD $(@:.o=.d) -MT $@ -o $@ $(NASM_FLAGS) $<
endif

$(OBJ_DIR)/%.o:no-aesni/%.c
	$(CC) $(OPT_NOAESNI) -c $(CFLAGS_NO_SIMD) $< -o $@

$(OBJ_DIR)/%.o:no-aesni/%.asm
ifeq ($(USE_YASM),y)
	$(YASM) $(YASM_FLAGS) $< -o $@
else
	$(NASM) -MD $(@:.o=.d) -MT $@ -o $@ $(NASM_FLAGS) $<
endif

$(OBJ_DIR):
	mkdir $(OBJ_DIR)

.PHONY: TAGS
TAGS:
	find ./ -name '*.[ch]' | etags -
	find ./ -name '*.asm'  | etags -a -
	find ./ -name '*.inc'  | etags -a -

.PHONY: clean
clean:
	rm -Rf $(target_obj_files)
	rm -Rf $(dep_target_files)
	rm -f $(LIB).a $(LIB).so*

.PHONY: help
help:
	@echo "Available build options:"
	@echo "DEBUG=n (default)"
	@echo "          - this option will produce library not fit for debugging"
	@echo "SHARED=y (default)"
	@echo "          - this option will produce shared library"
	@echo "DEBUG=y   - this option will produce library fit for debugging"
	@echo "SHARED=n  - this option will produce static library"
	@echo "SAFE_DATA=n (default)"
	@echo "          - Sensitive data not cleared from registers and memory"
	@echo "            at operation end"
	@echo "SAFE_DATA=y"
	@echo "          - Sensitive data cleared from registers and memory"
	@echo "            at operation end"
	@echo "SAFE_PARAM=n (default)"
	@echo "          - API input parameters not checked"
	@echo "SAFE_PARAM=y"
	@echo "          - API input parameters checked"
	@echo "SAFE_LOOKUP=n"
	@echo "          - Lookups depending on sensitive data might not be constant time"
	@echo "SAFE_LOOKUP=y (default)"
	@echo "          - Lookups depending on sensitive data are constant time"
	@echo "GCM_BIG_DATA=n (default)"
	@echo "          - Smaller GCM key structure with good performance level (VAES)"
	@echo "            for packet processing applications (buffers size < 2K)"
	@echo "          - 8 ghash keys used on SSE, AVX, AVX2 and AVX512"
	@echo "          - 48 ghash keys used on AVX512 with VAES and VPCLMULQDQ"
	@echo "GCM_BIG_DATA=y"
	@echo "          - Better performing VAES GCM on big buffers using more ghash keys."
	@echo "            This option results in a much bigger gcm_key structure (>2K)."
	@echo "            It only takes effect on platforms with VAES and VPCLMULQDQ."


CHECKPATCH ?= checkpatch.pl
# checkpatch ignore settings:
#   SPACING - produces false positives with tyepdefs and *
#   CONSTANT_COMPARISON - forbids defensive programming technique
#   USE_FUNC - produces false positives for Windows target
#   INITIALISED_STATIC, LEADING_SPACE, SPLIT_STRING, CODE_INDENT,
#   PREFER_ALIGNED, UNSPECIFIED_INT, ARRAY_SIZE, GLOBAL_INITIALISERS,
#   NEW_TYPEDEFS, AVOID_EXTERNS, COMPLEX_MACRO, BLOCK_COMMENT_STYLE
#     - found obsolete in this project
#
# NOTE: these flags cannot be broken into multiple lines due to
#       spaces injected by make
CHECKPATCH_FLAGS = --no-tree --no-signoff --emacs --no-color --ignore CODE_INDENT,INITIALISED_STATIC,LEADING_SPACE,SPLIT_STRING,UNSPECIFIED_INT,ARRAY_SIZE,BLOCK_COMMENT_STYLE,GLOBAL_INITIALISERS,NEW_TYPEDEFS,AVOID_EXTERNS,COMPLEX_MACRO,PREFER_ALIGNED,USE_FUNC,CONSTANT_COMPARISON,SPACING

%.c_style_check : %.c
	$(CHECKPATCH) $(CHECKPATCH_FLAGS) -f $<

%.h_style_check : %.h
	$(CHECKPATCH) $(CHECKPATCH_FLAGS) -f $<

%.asm_style_check : %.asm
	$(CHECKPATCH) $(CHECKPATCH_FLAGS) -f $<

%.inc_style_check : %.inc
	$(CHECKPATCH) $(CHECKPATCH_FLAGS) -f $<

SOURCES_DIRS := . sse avx avx2 avx512 include no-aesni
SOURCES := $(foreach dir,$(SOURCES_DIRS),$(wildcard $(dir)/*.[ch]) $(wildcard $(dir)/*.asm) $(wildcard $(dir)/*.inc))
SOURCES_STYLE := $(foreach infile,$(SOURCES),$(infile)_style_check)

.PHONY: style
style: $(SOURCES_STYLE)

# if target not clean or rinse then make dependencies
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),style)
-include $(dep_target_files)
endif
endif