]> git.cryptolib.org Git - avr-crypto-lib.git/blob - Makefile_main.inc
34aa6257fee98c9555cd064a3b4f8d06fd031220
[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) 2010 Daniel Otte (daniel.otte@rub.de)
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 HASHES         :=
22 MACS           :=
23 PRNGS          :=
24 ENCODINGS      :=
25 SIGNATURE      :=
26 PK_CIPHERS     :=
27 AUX            :=
28
29
30 # we use the gnu make standard library
31 include gmsl
32
33 GLOBAL_INCDIR := ./ $(TESTSRC_DIR)
34
35 #-------------------------------------------------------------------------------
36 # inclusion of make stubs
37 include $(sort $(wildcard mkfiles/*.mk))
38
39 default: info
40
41 #-------------------------------------------------------------------------------
42 ALGORITHMS = $(BLOCK_CIPHERS) $(STREAM_CIPHERS) $(HASHES) $(PRNGS) $(MACS) \
43                          $(ENCODINGS) $(SIGNATURE) $(PK_CIPHERS) $(AUX)
44 ALGORITHMS_OBJ = $(patsubst %,%_OBJ, $(ALGORITHMS))
45 ALGORITHMS_TESTBIN = $(patsubst %,%_TESTBIN, $(ALGORITHMS))
46
47 #-------------------------------------------------------------------------------
48 # define binary object in $(BIN_DIR)$(ALGO)/<obj>
49 define Assert_Template
50 $(1) = $(2)
51 endef
52
53 $(foreach a, $(ALGORITHMS), $(eval $(call Assert_Template, \
54     $(a)_BINOBJ, \
55     $(addprefix $(BIN_DIR)$(call lc,$(a))/,$($(a)_OBJ)) \
56 )))
57
58 $(foreach a, $(ALGORITHMS), $(eval $(call Assert_Template, \
59     $(a)_TESTBINOBJ, \
60     $(addprefix $(BIN_DIR)$(call lc,$(a))/$(TEST_DIR),$($(a)_TESTBIN)) \
61 )))
62
63
64
65 #-------------------------------------------------------------------------------
66
67 define GenericTarget_Template
68 $(1): $(2)
69 endef
70
71 define TargetSource_Template
72 $(1): $(2)
73         @mkdir -p  $(dir $(1)) $(DEP_DIR)
74         @echo "[cc]: $(1) <-- $(2)"
75         @$(CC) $(CFLAGS_A) $(addprefix -I./,$(3)) $(addprefix -D, $(4)) -c -o $(1) $(2)
76 endef
77
78
79
80 define TargetSourceList_Template
81 $(1): $(2)
82         @mkdir -p $(dir $(1)) $(DEP_DIR)
83         @echo "[lst]: $(1) <-- $(2)"
84         @$(CC) $(CFLAGS_A) $(addprefix -I./,$(3)) $(addprefix -D, $(4)) $(LIST_OPT) -c -o /dev/null $(2) > $(1)
85 endef
86
87 # ----------------------------------------------------------------------------
88 # Function:  find_source_file
89 # Arguments: 1: name of the binary file (.o extension) to search
90 #            2: list of directorys to search for file
91 # Returns:   Returns paths to source file (mathing the pattern in 
92 #            $(SOURCE_PATTERN)
93 # ----------------------------------------------------------------------------
94 SOURCE_PATTERN := %.S %.c 
95 find_source_file = $(firstword $(foreach d, $(2), \
96                      $(filter $(SOURCE_PATTERN),  \
97                        $(wildcard $(d)$(notdir $(patsubst %.o,%,$1)).*) \
98                      ) \
99                    ) )
100               
101               
102 $(foreach a, $(ALGORITHMS), \
103   $(foreach b, $($(a)_OBJ), \
104     $(eval $(call TargetSource_Template, \
105       $(BIN_DIR)$(call lc, $(a))/$(b), \
106       $(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
107       $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
108       $($(a)_DEF), \
109     )) \
110   ) \
111 )
112
113 $(foreach a, $(ALGORITHMS), \
114   $(foreach b, $($(a)_TESTBIN), \
115     $(eval $(call TargetSource_Template, \
116       $(BIN_DIR)$(call lc, $(a))/$(TEST_DIR)$(b), \
117       $(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
118       $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
119       $($(a)_DEF) \
120     )) \
121   ) \
122 )
123
124              
125 $(foreach a, $(ALGORITHMS), \
126   $(foreach b, $($(a)_OBJ), \
127     $(eval $(call TargetSourceList_Template, \
128       $(LIST_DIR)$(call lc, $(a))/$(patsubst %.o,%.s,$(b)), \
129       $(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
130       $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
131       $($(a)_DEF), \
132     )) \
133   ) \
134 )
135
136 EXTRALINK_BINOBJ = $(patsubst %, $(BIN_DIR)%, $(EXTRALINK))
137
138 $(foreach a, $(EXTRALINK), \
139   $(eval $(call TargetSource_Template, \
140     $(BIN_DIR)$(a), \
141     $(call find_source_file, $(a), $(GLOBAL_INCDIR) ),\
142     $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
143     $($(a)_DEF), \
144   )) \
145 )
146
147 #-------------------------------------------------------------------------------
148
149 define MainTestElf_Template
150 $(1): $(2) $(3) $(4)
151         @mkdir -p $(dir $(1))
152         @echo "[ld]: $(1)"
153         @$(CC) $(CFLAGS_A) $(LDFLAGS)$(patsubst %.elf,%.map,$(1)) -o \
154         $(1) \
155         $(2) $(3) $(EXTRALINK_BINOBJ)\
156         $(LIBS)
157 endef
158
159 $(foreach a, $(ALGORITHMS), \
160     $(eval $(call MainTestElf_Template,  \
161         $(BIN_DIR)$(call lc, $(a))/$(TEST_DIR)main-$(call lc, $(a))-test.elf, \
162         $($(a)_BINOBJ), \
163         $($(a)_TESTBINOBJ), \
164         $(EXTRALINK_BINOBJ) \
165         )) \
166 )
167
168 #-------------------------------------------------------------------------------
169
170 all: $(foreach algo, $(ALGORITHMS), $($(algo)_BINOBJ))
171
172 #-------------------------------------------------------------------------------
173
174 define MakeDir_TEMPLATE
175 $(1):
176         @echo [mkdir] $(1)
177         @mkdir -p $(1)
178 endef
179
180 $(foreach d, DEP_DIR BIN_DIR TESTSRC_DIR TESTLOG_DIR SPEEDLOG_DIR SIZE_DIR LIST_DIR STAT_DIR, $(eval $(call MakeDir_TEMPLATE, \
181    $($(d)) \
182 )))
183
184 $(foreach algo, $(ALGORITHMS), $(eval $(call MakeDir__TEMPLATE, \
185    $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR) \
186 )))
187
188 define TestBin_TEMPLATE
189 $(1)_TESTBIN: $(2)
190 endef
191
192 $(foreach algo, $(ALGORITHMS), $(eval $(call TestBin_TEMPLATE, \
193     $(algo), \
194     $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR)main-$(call lc, $(algo))-test.hex \
195 )))
196
197 #-------------------------------------------------------------------------------
198
199 define Listing_TEMPLATE
200 $(1)_LIST: $(2)
201 endef
202
203 $(foreach algo, $(ALGORITHMS), $(eval $(call Listing_TEMPLATE, \
204     $(algo), \
205     $(foreach obj,$($(algo)_OBJ), $(LIST_DIR)$(call lc, $(algo))/$(obj:.o=.s)) \
206 )))
207
208 #-------------------------------------------------------------------------------
209
210 %.hex: %.elf
211         @echo "[objcopy]: $@"
212         @$(OBJCOPY) -j .text -j .data -O ihex $< $@
213 ifdef HASH_TOOL
214 ifneq ($(HASH_TOOL),)
215         @echo -n "[$(HASH_TOOL)]: "
216         @$(HASH_TOOL) $@
217 endif
218 endif
219
220 #-------------------------------------------------------------------------------
221
222 define Flash_Template
223 $(1)_FLASH: $(2)
224         @echo "[flash]: $(2)"
225         @$(FLASHCMD)$(call first,$(2))
226 endef
227
228 $(foreach algo, $(ALGORITHMS), $(eval $(call Flash_Template, \
229     $(algo), \
230     $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR)main-$(call lc, $(algo))-test.hex \
231 )))
232
233 #-------------------------------------------------------------------------------
234
235 define Speed_Template
236 $(1)_SPEED:  $(1)_FLASH
237         @$(RUBY) $(SPEEDTOOL) -c $(SPEEDCMD) -t $(SPEEDLOG_DIR) -a $(call lc, $(1))
238 endef
239
240 $(foreach algo, $(ALGORITHMS), $(eval $(call Speed_Template, \
241     $(algo), $(algo) \
242 )))
243
244 .PHONY: hash_speed
245 hash_speed: $(foreach algo, $(HASHES), $(algo)_SPEED)
246
247 .PHONY: blockcipher_speed
248 blockcipher_speed: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SPEED)
249 #-------------------------------------------------------------------------------
250
251 define Size_Template
252 $(1):  $(2)
253         @echo "[size] $(3)"
254         @mkdir -p $(dir $(1))
255         @$(SIZE) $(2) > $(1)
256 endef
257
258 $(foreach algo, $(ALGORITHMS), $(eval $(call Size_Template, \
259     $(strip $(SIZE_DIR))$(strip $(call lc, $(algo))).size,$($(algo)_BINOBJ),$(algo) \
260 )))
261
262 define Size_Template
263 $(1)_SIZE:  $(2)
264         @echo "[size] $(1)"
265         @mkdir -p $(dir $(strip $(SIZE_DIR))$(strip $(call lc, $(1))).size)
266         @$(SIZE) $(2) > $(strip $(SIZE_DIR))$(strip $(call lc, $(1))).size
267 endef
268
269 $(foreach algo, $(ALGORITHMS), $(eval $(call GenericTarget_Template, \
270     $(strip $(algo))_SIZE,$(strip $(SIZE_DIR))$(strip $(call lc, $(algo))).size \
271 )))
272
273 .PHONY: hash_size
274 hash_size: $(foreach algo, $(HASHES), $(algo)_SIZE)
275
276 .PHONY: blockcipher_size
277 blockcipher_size: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SIZE)
278
279 .PHONY: size
280 size: $(foreach algo, $(ALGORITHMS), $(algo)_SIZE)
281
282 .PHONY: size_clean
283 size_clean:
284         rm -vf $(strip $(SIZE_DIR))*.size
285
286 #-------------------------------------------------------------------------------
287
288 .PHONY: tests
289 tests: $(foreach a, $(ALGORITHMS), $(a)_TESTBIN)
290
291 #-------------------------------------------------------------------------------
292
293 define TestRun_Template
294 $(1)_TESTRUN: $(1)_FLASH
295         @echo "[reset]"
296         @sleep 3
297         @$(RESETCMD)
298         @sleep 1
299         @echo "[test]: $(1)"
300         $(RUBY) $(GET_TEST) $(TESTPORT) $(TESTPORTBAUDR) 8 1 nessie $(TESTLOG_DIR)$(TESTPREFIX) $(2)
301 endef
302
303 $(foreach algo, $(ALGORITHMS),$(eval $(call TestRun_Template, $(algo), $(call lc,$(algo)) )))
304
305 all_testrun: $(foreach algo, $(ALGORITHMS), $(algo)_TESTRUN)
306
307 #-------------------------------------------------------------------------------
308
309 define Obj_Template
310 $(1)_OBJ: $(2)
311 endef
312
313 $(foreach algo, $(ALGORITHMS), \
314     $(eval $(call Obj_Template, \
315         $(algo), \
316         $($(algo)_BINOBJ)\
317         ))\
318 )
319
320 .PHONY: all
321 all: cores
322
323 .PHONY: reset
324 reset:
325         $(RESETCMD)
326
327 .PHONY: cores
328 cores: $(foreach algo, $(ALGORITHMS), $(algo)_OBJ)
329
330 .PHONY: blockchiphers
331 blockciphers: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_OBJ)
332
333 .PHONY: streamchiphers
334 streamciphers: $(foreach algo, $(STREAM_CIPHERS), $(algo)_OBJ)
335
336 .PHONY: hashes
337 hashes: $(foreach algo, $(HASHES), $(algo)_OBJ)
338
339 .PHONY: macs
340 macs: $(foreach algo, $(MACS), $(algo)_OBJ)
341
342 .PHONY: prngs
343 prngs: $(foreach algo, $(PRNGS), $(algo)_OBJ)
344
345 .PHONY: encodings
346 encodings: $(foreach algo, $(ENCODINGS), $(algo)_OBJ)
347
348 .PHONY: aux
349 aux: $(foreach algo, $(AUX), $(algo)_OBJ)
350
351
352 #-------------------------------------------------------------------------------
353
354
355 .PHONY: help
356 help: info
357 .PHONY: info
358 info:
359         @echo "infos on AVR-Crypto-lib:"
360         @echo "  block ciphers:"
361         @echo "    $(BLOCK_CIPHERS)"
362         @echo "  stream ciphers:"
363         @echo "    $(STREAM_CIPHERS)"
364         @echo "  hash functions:"
365         @echo "    $(HASHES)"
366         @echo "  MAC functions:"
367         @echo "    $(MACS)"
368         @echo "  PRNG functions:"
369         @echo "    $(PRNGS)"
370         @echo "  signature functions:"
371         @echo "    $(SIGNATURE)"
372         @echo "  public key ciphers:"
373         @echo "    $(PK_CIPHERS)"
374         @echo "  encodings:"
375         @echo "    $(ENCODINGS)"
376         @echo "  auxiliary functions:"
377         @echo "    $(AUX)"
378         @echo " targets:"
379         @echo "  all                - all algorithm cores"
380         @echo "  cores              - all algorithm cores"
381         @echo "  listings           - all algorithm core listings"
382         @echo "  tests              - all algorithm test programs"
383         @echo "  size               - all algorithm size statistics"
384         @echo "  blockciphers       - all blockcipher cores"
385         @echo "  streamciphers      - all streamcipher cores"
386         @echo "  hashes             - all hash cores"
387         @echo "  macs               - all MAC cores"
388         @echo "  prngs              - all PRNG cores"
389         @echo "  all_testrun        - testrun all algorithms"
390         @echo "  hash_size          - measure size of all hash functions"
391         @echo "  hash_speed         - measure performance of all hash functions"
392         @echo "  blockcipher_size   - measure size of all blockciphers"
393         @echo "  blockcipher_speed  - measure performance of all blockciphers"
394         @echo "  docu               - build doxygen documentation"
395         @echo "  clean              - remove a lot of builded files"
396         @echo "  depclean           - also remove dependency files"
397         @echo "  *_TESTBIN          - build test program"
398         @echo "  *_TESTRUN          - run nessie test"
399         @echo "  *_OBJ              - build algorithm core"
400         @echo "  *_FLASH            - flash test program"
401         @echo "  *_LIST             - build assembler listing"
402
403
404 #-------------------------------------------------------------------------------
405
406 .PHONY: clean
407 clean:
408         rm -rf $(BIN_DIR)*
409
410 .PHONY: depclean
411 depclean: clean
412         rm -f $(DEP_DIR)*.d
413
414 #-------------------------------------------------------------------------------
415 # dependency inclusion
416 #
417
418 DEPS := $(wildcard $(DEP_DIR)*.d)
419
420 ifneq ($(DEPS),)
421 include $(DEPS)
422 endif
423