1 # Makefile for the AVR-Crypto-Lib project
3 # This file is part of the AVR-Crypto-Lib.
4 # Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
31 # we use the gnu make standard library
34 GLOBAL_INCDIR := ./ $(TESTSRC_DIR)
36 #-------------------------------------------------------------------------------
37 # inclusion of make stubs
38 include $(sort $(wildcard mkfiles/*.mk))
42 #-------------------------------------------------------------------------------
43 ALGORITHMS = $(BLOCK_CIPHERS) $(STREAM_CIPHERS) $(AEAD_CIPHERS) $(HASHES) \
44 $(PRNGS) $(MACS) $(ENCODINGS) $(SIGNATURE) $(PK_CIPHERS) $(AUX)
45 ALGORITHMS_OBJ = $(patsubst %,%_OBJ, $(ALGORITHMS))
46 ALGORITHMS_TESTBIN = $(patsubst %,%_TESTBIN, $(ALGORITHMS))
48 #-------------------------------------------------------------------------------
49 # define binary object in $(BIN_DIR)$(ALGO)/<obj>
50 define Assert_Template
54 $(foreach a, $(ALGORITHMS), $(eval $(call Assert_Template, \
56 $(addprefix $(BIN_DIR)$(call lc,$(a))/,$($(a)_OBJ)) \
59 $(foreach a, $(ALGORITHMS), $(eval $(call Assert_Template, \
61 $(addprefix $(BIN_DIR)$(call lc,$(a))/$(TEST_DIR),$($(a)_TESTBIN)) \
66 #-------------------------------------------------------------------------------
68 define GenericTarget_Template
73 define TargetSource_Template
75 @mkdir -p $(dir $(1)) $(DEP_DIR)
76 @echo "[cc]: $(1) <-- $(2)"
77 @$(CC) $(CFLAGS_A) $(addprefix -I./,$(3)) $(addprefix -D, $(4)) -c -o $(1) $(2)
80 define TargetSource_Template
82 @mkdir -p $(dir $(1)) $(DEP_DIR)
83 @echo "[cc]: $(1) <-- $(2)"
84 $(CC) $(CFLAGS_A) $(addprefix -I./,$(3)) $(addprefix -D, $(4)) -c -o $(1) $(2)
90 define TargetSourceList_Template
92 @mkdir -p $(dir $(1)) $(DEP_DIR)
93 @echo "[lst]: $(1) <-- $(2)"
94 @$(CC) $(CFLAGS_A) $(addprefix -I./,$(3)) $(addprefix -D, $(4)) $(LIST_OPT) -c -o /dev/null $(2) > $(1)
97 # ----------------------------------------------------------------------------
98 # Function: find_source_file
99 # Arguments: 1: name of the binary file (.o extension) to search
100 # 2: list of directorys to search for file
101 # Returns: Returns paths to source file (mathing the pattern in
103 # ----------------------------------------------------------------------------
104 SOURCE_PATTERN := %.S %.c
105 find_source_file = $(firstword $(foreach d, $(2), \
106 $(filter $(SOURCE_PATTERN), \
107 $(wildcard $(d)$(notdir $(patsubst %.o,%,$1)).*) \
112 $(foreach a, $(ALGORITHMS), \
113 $(foreach b, $($(a)_OBJ), \
114 $(eval $(call TargetSource_Template, \
115 $(BIN_DIR)$(call lc, $(a))/$(b), \
116 $(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
117 $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
123 $(foreach a, $(ALGORITHMS), \
124 $(foreach b, $($(a)_TESTBIN), \
125 $(eval $(call TargetSource_Template, \
126 $(BIN_DIR)$(call lc, $(a))/$(TEST_DIR)$(b), \
127 $(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
128 $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
135 $(foreach a, $(ALGORITHMS), \
136 $(foreach b, $($(a)_OBJ), \
137 $(eval $(call TargetSourceList_Template, \
138 $(LIST_DIR)$(call lc, $(a))/$(patsubst %.o,%.s,$(b)), \
139 $(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
140 $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
146 EXTRALINK_BINOBJ = $(patsubst %, $(BIN_DIR)%, $(EXTRALINK))
148 $(foreach a, $(EXTRALINK), \
149 $(eval $(call TargetSource_Template, \
151 $(call find_source_file, $(a), $(GLOBAL_INCDIR) ),\
152 $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
157 #-------------------------------------------------------------------------------
159 define MainTestElf_Template
161 @mkdir -p $(dir $(1))
163 @$(CC) $(CFLAGS_A) $(LDFLAGS)$(patsubst %.elf,%.map,$(1)) -o \
165 $(2) $(3) $(EXTRALINK_BINOBJ)\
169 $(foreach a, $(ALGORITHMS), \
170 $(eval $(call MainTestElf_Template, \
171 $(BIN_DIR)$(call lc, $(a))/$(TEST_DIR)main-$(call lc, $(a))-test.elf, \
173 $($(a)_TESTBINOBJ), \
174 $(EXTRALINK_BINOBJ) \
178 #-------------------------------------------------------------------------------
180 all: $(foreach algo, $(ALGORITHMS), $($(algo)_BINOBJ))
182 #-------------------------------------------------------------------------------
184 define MakeDir_TEMPLATE
190 $(foreach d, DEP_DIR BIN_DIR TESTSRC_DIR TESTLOG_DIR SPEEDLOG_DIR SIZE_DIR LIST_DIR STAT_DIR, $(eval $(call MakeDir_TEMPLATE, \
194 $(foreach algo, $(ALGORITHMS), $(eval $(call MakeDir__TEMPLATE, \
195 $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR) \
198 define TestBin_TEMPLATE
202 $(foreach algo, $(ALGORITHMS), $(eval $(call TestBin_TEMPLATE, \
204 $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR)main-$(call lc, $(algo))-test.hex \
207 #-------------------------------------------------------------------------------
209 define Listing_TEMPLATE
213 $(foreach algo, $(ALGORITHMS), $(eval $(call Listing_TEMPLATE, \
215 $(foreach obj,$($(algo)_OBJ), $(LIST_DIR)$(call lc, $(algo))/$(obj:.o=.s)) \
218 #-------------------------------------------------------------------------------
221 @echo "[objcopy]: $@"
222 @$(OBJCOPY) -j .text -j .data -O ihex $< $@
224 ifneq ($(HASH_TOOL),)
225 @echo -n "[$(HASH_TOOL)]: "
230 #-------------------------------------------------------------------------------
232 define Flash_Template
234 @echo "[flash]: $(2)"
235 @$(FLASHCMD)$(call first,$(2))
238 $(foreach algo, $(ALGORITHMS), $(eval $(call Flash_Template, \
240 $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR)main-$(call lc, $(algo))-test.hex \
243 #-------------------------------------------------------------------------------
245 define Speed_Template
246 $(1)_SPEED: $(1)_FLASH
247 @$(RUBY) $(SPEEDTOOL) -c $(SPEEDCMD) -t $(SPEEDLOG_DIR) -a $(call lc, $(1))
250 $(foreach algo, $(ALGORITHMS), $(eval $(call Speed_Template, \
255 hash_speed: $(foreach algo, $(HASHES), $(algo)_SPEED)
257 .PHONY: blockcipher_speed
258 blockcipher_speed: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SPEED)
259 #-------------------------------------------------------------------------------
264 @mkdir -p $(dir $(1))
268 $(foreach algo, $(ALGORITHMS), $(eval $(call Size_Template, \
269 $(strip $(SIZE_DIR))$(strip $(call lc, $(algo))).size,$($(algo)_BINOBJ),$(algo) \
275 @mkdir -p $(dir $(strip $(SIZE_DIR))$(strip $(call lc, $(1))).size)
276 @$(SIZE) $(2) > $(strip $(SIZE_DIR))$(strip $(call lc, $(1))).size
279 $(foreach algo, $(ALGORITHMS), $(eval $(call GenericTarget_Template, \
280 $(strip $(algo))_SIZE,$(strip $(SIZE_DIR))$(strip $(call lc, $(algo))).size \
284 hash_size: $(foreach algo, $(HASHES), $(algo)_SIZE)
286 .PHONY: blockcipher_size
287 blockcipher_size: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SIZE)
290 size: $(foreach algo, $(ALGORITHMS), $(algo)_SIZE)
294 rm -vf $(strip $(SIZE_DIR))*.size
296 #-------------------------------------------------------------------------------
299 tests: $(foreach a, $(ALGORITHMS), $(a)_TESTBIN)
301 #-------------------------------------------------------------------------------
303 define TestRun_Template
304 $(1)_TESTRUN: $(1)_FLASH
310 $(RUBY) $(GET_TEST) $(TESTPORT) $(TESTPORTBAUDR) 8 1 nessie $(TESTLOG_DIR)$(TESTPREFIX) $(2)
313 $(foreach algo, $(ALGORITHMS),$(eval $(call TestRun_Template, $(algo), $(call lc,$(algo)) )))
315 all_testrun: $(foreach algo, $(ALGORITHMS), $(algo)_TESTRUN)
317 #-------------------------------------------------------------------------------
323 $(foreach algo, $(ALGORITHMS), \
324 $(eval $(call Obj_Template, \
338 cores: $(foreach algo, $(ALGORITHMS), $(algo)_OBJ)
340 .PHONY: blockchiphers
341 blockciphers: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_OBJ)
343 .PHONY: streamchiphers
344 streamciphers: $(foreach algo, $(STREAM_CIPHERS), $(algo)_OBJ)
347 hashes: $(foreach algo, $(HASHES), $(algo)_OBJ)
350 macs: $(foreach algo, $(MACS), $(algo)_OBJ)
353 prngs: $(foreach algo, $(PRNGS), $(algo)_OBJ)
356 encodings: $(foreach algo, $(ENCODINGS), $(algo)_OBJ)
359 aux: $(foreach algo, $(AUX), $(algo)_OBJ)
362 #-------------------------------------------------------------------------------
369 @echo "infos on AVR-Crypto-lib:"
370 @echo " block ciphers:"
371 @echo " $(BLOCK_CIPHERS)"
372 @echo " stream ciphers:"
373 @echo " $(STREAM_CIPHERS)"
374 @echo " AEAD ciphers:"
375 @echo " $(AEAD_CIPHERS)"
376 @echo " hash functions:"
378 @echo " MAC functions:"
380 @echo " PRNG functions:"
382 @echo " signature functions:"
383 @echo " $(SIGNATURE)"
384 @echo " public key ciphers:"
385 @echo " $(PK_CIPHERS)"
387 @echo " $(ENCODINGS)"
388 @echo " auxiliary functions:"
391 @echo " all - all algorithm cores"
392 @echo " cores - all algorithm cores"
393 @echo " listings - all algorithm core listings"
394 @echo " tests - all algorithm test programs"
395 @echo " size - all algorithm size statistics"
396 @echo " blockciphers - all blockcipher cores"
397 @echo " streamciphers - all streamcipher cores"
398 @echo " hashes - all hash cores"
399 @echo " macs - all MAC cores"
400 @echo " prngs - all PRNG cores"
401 @echo " all_testrun - testrun all algorithms"
402 @echo " hash_size - measure size of all hash functions"
403 @echo " hash_speed - measure performance of all hash functions"
404 @echo " blockcipher_size - measure size of all blockciphers"
405 @echo " blockcipher_speed - measure performance of all blockciphers"
406 @echo " docu - build doxygen documentation"
407 @echo " clean - remove a lot of builded files"
408 @echo " depclean - also remove dependency files"
409 @echo " *_TESTBIN - build test program"
410 @echo " *_TESTRUN - run nessie test"
411 @echo " *_OBJ - build algorithm core"
412 @echo " *_FLASH - flash test program"
413 @echo " *_LIST - build assembler listing"
416 #-------------------------------------------------------------------------------
426 #-------------------------------------------------------------------------------
427 # dependency inclusion
430 DEPS := $(wildcard $(DEP_DIR)*.d)