]> git.cryptolib.org Git - avr-crypto-lib.git/blob - Makefile_main.inc
turning on software flow control for uart
[avr-crypto-lib.git] / Makefile_main.inc
1 # Makefile for the AVR-Crypto-Lib project
2 #
3 #    This file is part of the AVR-Crypto-Lib.
4 #    Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
5 #
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.
10 #
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.
15 #
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/>.
18
19 BLOCK_CIPHERS  :=
20 STREAM_CIPHERS :=
21 AEAD_CIPHERS   :=
22 HASHES         :=
23 MACS           :=
24 PRNGS          :=
25 ENCODINGS      :=
26 SIGNATURE      :=
27 PK_CIPHERS     :=
28 AUX            :=
29
30
31 # we use the gnu make standard library
32 include gmsl
33
34 GLOBAL_INCDIR := ./ $(TESTSRC_DIR)
35
36 #-------------------------------------------------------------------------------
37 # inclusion of make stubs
38 include $(sort $(wildcard mkfiles/*.mk))
39
40 default: info
41
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))
47
48 #-------------------------------------------------------------------------------
49 # define binary object in $(BIN_DIR)$(ALGO)/<obj>
50 define Assert_Template
51 $(1) = $(2)
52 endef
53
54 $(foreach a, $(ALGORITHMS), $(eval $(call Assert_Template, \
55     $(a)_BINOBJ, \
56     $(addprefix $(BIN_DIR)$(call lc,$(a))/,$($(a)_OBJ)) \
57 )))
58
59 $(foreach a, $(ALGORITHMS), $(eval $(call Assert_Template, \
60     $(a)_TESTBINOBJ, \
61     $(addprefix $(BIN_DIR)$(call lc,$(a))/$(TEST_DIR),$($(a)_TESTBIN)) \
62 )))
63
64
65
66 #-------------------------------------------------------------------------------
67
68 define GenericTarget_Template
69 $(1): $(2)
70 endef
71
72 ifeq ($(V),)
73 define TargetSource_Template
74 $(1): $(2)
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)
78 endef
79 else
80 define TargetSource_Template
81 $(1): $(2)
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)
85 endef
86 endif
87
88
89
90 define TargetSourceList_Template
91 $(1): $(2)
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)
95 endef
96
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 
102 #            $(SOURCE_PATTERN)
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)).*) \
108                      ) \
109                    ) )
110               
111               
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), \
118       $($(a)_DEF), \
119     )) \
120   ) \
121 )
122
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), \
129       $($(a)_DEF) \
130     )) \
131   ) \
132 )
133
134              
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), \
141       $($(a)_DEF), \
142     )) \
143   ) \
144 )
145
146 EXTRALINK_BINOBJ = $(patsubst %, $(BIN_DIR)%, $(EXTRALINK))
147
148 $(foreach a, $(EXTRALINK), \
149   $(eval $(call TargetSource_Template, \
150     $(BIN_DIR)$(a), \
151     $(call find_source_file, $(a), $(GLOBAL_INCDIR) ),\
152     $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
153     $($(a)_DEF), \
154   )) \
155 )
156
157 #-------------------------------------------------------------------------------
158
159 define MainTestElf_Template
160 $(1): $(2) $(3) $(4)
161         @mkdir -p $(dir $(1))
162         @echo "[ld]: $(1)"
163         @$(CC) $(CFLAGS_A) $(LDFLAGS)$(patsubst %.elf,%.map,$(1)) -o \
164         $(1) \
165         $(2) $(3) $(EXTRALINK_BINOBJ)\
166         $(LIBS)
167 endef
168
169 $(foreach a, $(ALGORITHMS), \
170     $(eval $(call MainTestElf_Template,  \
171         $(BIN_DIR)$(call lc, $(a))/$(TEST_DIR)main-$(call lc, $(a))-test.elf, \
172         $($(a)_BINOBJ), \
173         $($(a)_TESTBINOBJ), \
174         $(EXTRALINK_BINOBJ) \
175         )) \
176 )
177
178 #-------------------------------------------------------------------------------
179
180 all: $(foreach algo, $(ALGORITHMS), $($(algo)_BINOBJ))
181
182 #-------------------------------------------------------------------------------
183
184 define MakeDir_TEMPLATE
185 $(1):
186         @echo [mkdir] $(1)
187         @mkdir -p $(1)
188 endef
189
190 $(foreach d, DEP_DIR BIN_DIR TESTSRC_DIR TESTLOG_DIR SPEEDLOG_DIR SIZE_DIR LIST_DIR STAT_DIR, $(eval $(call MakeDir_TEMPLATE, \
191    $($(d)) \
192 )))
193
194 $(foreach algo, $(ALGORITHMS), $(eval $(call MakeDir__TEMPLATE, \
195    $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR) \
196 )))
197
198 define TestBin_TEMPLATE
199 $(1)_TESTBIN: $(2)
200 endef
201
202 $(foreach algo, $(ALGORITHMS), $(eval $(call TestBin_TEMPLATE, \
203     $(algo), \
204     $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR)main-$(call lc, $(algo))-test.hex \
205 )))
206
207 #-------------------------------------------------------------------------------
208
209 define Listing_TEMPLATE
210 $(1)_LIST: $(2)
211 endef
212
213 $(foreach algo, $(ALGORITHMS), $(eval $(call Listing_TEMPLATE, \
214     $(algo), \
215     $(foreach obj,$($(algo)_OBJ), $(LIST_DIR)$(call lc, $(algo))/$(obj:.o=.s)) \
216 )))
217
218 #-------------------------------------------------------------------------------
219
220 %.hex: %.elf
221         @echo "[objcopy]: $@"
222         @$(OBJCOPY) -j .text -j .data -O ihex $< $@
223 ifdef HASH_TOOL
224 ifneq ($(HASH_TOOL),)
225         @echo -n "[$(HASH_TOOL)]: "
226         @$(HASH_TOOL) $@
227 endif
228 endif
229
230 #-------------------------------------------------------------------------------
231
232 define Flash_Template
233 $(1)_FLASH: $(2)
234         @echo "[flash]: $(2)"
235         @$(FLASHCMD)$(call first,$(2))
236 endef
237
238 $(foreach algo, $(ALGORITHMS), $(eval $(call Flash_Template, \
239     $(algo), \
240     $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR)main-$(call lc, $(algo))-test.hex \
241 )))
242
243 #-------------------------------------------------------------------------------
244
245 define Speed_Template
246 $(1)_SPEED:  $(1)_FLASH
247         @$(RUBY) $(SPEEDTOOL) -c $(SPEEDCMD) -t $(SPEEDLOG_DIR) -a $(call lc, $(1))
248 endef
249
250 $(foreach algo, $(ALGORITHMS), $(eval $(call Speed_Template, \
251     $(algo), $(algo) \
252 )))
253
254 .PHONY: hash_speed
255 hash_speed: $(foreach algo, $(HASHES), $(algo)_SPEED)
256
257 .PHONY: blockcipher_speed
258 blockcipher_speed: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SPEED)
259 #-------------------------------------------------------------------------------
260
261 define Size_Template
262 $(1):  $(2)
263         @echo "[size] $(3)"
264         @mkdir -p $(dir $(1))
265         @$(SIZE) $(2) > $(1)
266 endef
267
268 $(foreach algo, $(ALGORITHMS), $(eval $(call Size_Template, \
269     $(strip $(SIZE_DIR))$(strip $(call lc, $(algo))).size,$($(algo)_BINOBJ),$(algo) \
270 )))
271
272 define Size_Template
273 $(1)_SIZE:  $(2)
274         @echo "[size] $(1)"
275         @mkdir -p $(dir $(strip $(SIZE_DIR))$(strip $(call lc, $(1))).size)
276         @$(SIZE) $(2) > $(strip $(SIZE_DIR))$(strip $(call lc, $(1))).size
277 endef
278
279 $(foreach algo, $(ALGORITHMS), $(eval $(call GenericTarget_Template, \
280     $(strip $(algo))_SIZE,$(strip $(SIZE_DIR))$(strip $(call lc, $(algo))).size \
281 )))
282
283 .PHONY: hash_size
284 hash_size: $(foreach algo, $(HASHES), $(algo)_SIZE)
285
286 .PHONY: blockcipher_size
287 blockcipher_size: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SIZE)
288
289 .PHONY: size
290 size: $(foreach algo, $(ALGORITHMS), $(algo)_SIZE)
291
292 .PHONY: size_clean
293 size_clean:
294         rm -vf $(strip $(SIZE_DIR))*.size
295
296 #-------------------------------------------------------------------------------
297
298 .PHONY: tests
299 tests: $(foreach a, $(ALGORITHMS), $(a)_TESTBIN)
300
301 #-------------------------------------------------------------------------------
302
303 define TestRun_Template
304 $(1)_TESTRUN: $(1)_FLASH
305         @echo "[reset]"
306         @sleep 3
307         @$(RESETCMD)
308         @sleep 1
309         @echo "[test]: $(1)"
310         $(RUBY) $(GET_TEST) $(TESTPORT) $(TESTPORTBAUDR) 8 1 nessie $(TESTLOG_DIR)$(TESTPREFIX) $(2)
311 endef
312
313 $(foreach algo, $(ALGORITHMS),$(eval $(call TestRun_Template, $(algo), $(call lc,$(algo)) )))
314
315 all_testrun: $(foreach algo, $(ALGORITHMS), $(algo)_TESTRUN)
316
317 #-------------------------------------------------------------------------------
318
319 define Obj_Template
320 $(1)_OBJ: $(2)
321 endef
322
323 $(foreach algo, $(ALGORITHMS), \
324     $(eval $(call Obj_Template, \
325         $(algo), \
326         $($(algo)_BINOBJ)\
327         ))\
328 )
329
330 .PHONY: all
331 all: cores
332
333 .PHONY: reset
334 reset:
335         $(RESETCMD)
336
337 .PHONY: cores
338 cores: $(foreach algo, $(ALGORITHMS), $(algo)_OBJ)
339
340 .PHONY: blockchiphers
341 blockciphers: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_OBJ)
342
343 .PHONY: streamchiphers
344 streamciphers: $(foreach algo, $(STREAM_CIPHERS), $(algo)_OBJ)
345
346 .PHONY: hashes
347 hashes: $(foreach algo, $(HASHES), $(algo)_OBJ)
348
349 .PHONY: macs
350 macs: $(foreach algo, $(MACS), $(algo)_OBJ)
351
352 .PHONY: prngs
353 prngs: $(foreach algo, $(PRNGS), $(algo)_OBJ)
354
355 .PHONY: encodings
356 encodings: $(foreach algo, $(ENCODINGS), $(algo)_OBJ)
357
358 .PHONY: aux
359 aux: $(foreach algo, $(AUX), $(algo)_OBJ)
360
361
362 #-------------------------------------------------------------------------------
363
364
365 .PHONY: help
366 help: info
367 .PHONY: info
368 info:
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:"
377         @echo "    $(HASHES)"
378         @echo "  MAC functions:"
379         @echo "    $(MACS)"
380         @echo "  PRNG functions:"
381         @echo "    $(PRNGS)"
382         @echo "  signature functions:"
383         @echo "    $(SIGNATURE)"
384         @echo "  public key ciphers:"
385         @echo "    $(PK_CIPHERS)"
386         @echo "  encodings:"
387         @echo "    $(ENCODINGS)"
388         @echo "  auxiliary functions:"
389         @echo "    $(AUX)"
390         @echo " targets:"
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"
414
415
416 #-------------------------------------------------------------------------------
417
418 .PHONY: clean
419 clean:
420         rm -rf $(BIN_DIR)*
421
422 .PHONY: depclean
423 depclean: clean
424         rm -f $(DEP_DIR)*.d
425
426 #-------------------------------------------------------------------------------
427 # dependency inclusion
428 #
429
430 DEPS := $(wildcard $(DEP_DIR)*.d)
431
432 ifneq ($(DEPS),)
433 include $(DEPS)
434 endif
435