]> git.cryptolib.org Git - avr-crypto-lib.git/blob - Makefile_main.inc
present looks better now (using unverified testvectors)
[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 "[cc]: $(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.elf \
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
214 #-------------------------------------------------------------------------------
215
216 define Flash_Template
217 $(1)_FLASH: $(2)
218         @echo "[flash]: $(2)"
219         @$(FLASHCMD)$(call first,$(2))
220 endef
221
222 $(foreach algo, $(ALGORITHMS), $(eval $(call Flash_Template, \
223     $(algo), \
224     $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR)main-$(call lc, $(algo))-test.hex \
225 )))
226
227 #-------------------------------------------------------------------------------
228
229 define Speed_Template
230 $(1)_SPEED:  $(1)_FLASH
231         @$(RUBY) $(SPEEDTOOL) -c $(SPEEDCMD) -t $(SPEEDLOG_DIR) -a $(call lc, $(1))
232 endef
233
234 $(foreach algo, $(ALGORITHMS), $(eval $(call Speed_Template, \
235     $(algo), $(algo) \
236 )))
237
238 .PHONY: hash_speed
239 hash_speed: $(foreach algo, $(HASHES), $(algo)_SPEED)
240
241 .PHONY: blockcipher_speed
242 blockcipher_speed: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SPEED)
243 #-------------------------------------------------------------------------------
244
245 define Size_Template
246 $(1):  $(2)
247         @echo "[size] $(3)"
248         @mkdir -p $(dir $(1))
249         @$(SIZE) $(2) > $(1)
250 endef
251
252 $(foreach algo, $(ALGORITHMS), $(eval $(call Size_Template, \
253     $(strip $(SIZE_DIR))$(strip $(call lc, $(algo))).size,$($(algo)_BINOBJ),$(algo) \
254 )))
255
256 define Size_Template
257 $(1)_SIZE:  $(2)
258         @echo "[size] $(1)"
259         @mkdir -p $(dir $(strip $(SIZE_DIR))$(strip $(call lc, $(1))).size)
260         @$(SIZE) $(2) > $(strip $(SIZE_DIR))$(strip $(call lc, $(1))).size
261 endef
262
263 $(foreach algo, $(ALGORITHMS), $(eval $(call GenericTarget_Template, \
264     $(strip $(algo))_SIZE,$(strip $(SIZE_DIR))$(strip $(call lc, $(algo))).size \
265 )))
266
267 .PHONY: hash_size
268 hash_size: $(foreach algo, $(HASHES), $(algo)_SIZE)
269
270 .PHONY: blockcipher_size
271 blockcipher_size: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SIZE)
272
273 .PHONY: size
274 size: $(foreach algo, $(ALGORITHMS), $(algo)_SIZE)
275
276 .PHONY: size_clean
277 size_clean:
278         rm -vf $(strip $(SIZE_DIR))*.size
279
280 #-------------------------------------------------------------------------------
281
282 .PHONY: tests
283 tests: $(foreach a, $(ALGORITHMS), $(a)_TESTBIN)
284
285 #-------------------------------------------------------------------------------
286
287 define TestRun_Template
288 $(1)_TESTRUN: $(1)_FLASH
289         @echo "[reset]"
290         @sleep 3
291         @$(RESETCMD)
292         @sleep 1
293         @echo "[test]: $(1)"
294         $(RUBY) $(GET_TEST) $(TESTPORT) $(TESTPORTBAUDR) 8 1 nessie $(TESTLOG_DIR)$(TESTPREFIX) $(2)
295 endef
296
297 $(foreach algo, $(ALGORITHMS),$(eval $(call TestRun_Template, $(algo), $(call lc,$(algo)) )))
298
299 all_testrun: $(foreach algo, $(ALGORITHMS), $(algo)_TESTRUN)
300
301 #-------------------------------------------------------------------------------
302
303 define Obj_Template
304 $(1)_OBJ: $(2)
305 endef
306
307 $(foreach algo, $(ALGORITHMS), \
308     $(eval $(call Obj_Template, \
309         $(algo), \
310         $($(algo)_BINOBJ)\
311         ))\
312 )
313
314 .PHONY: all
315 all: cores
316
317 .PHONY: reset
318 reset:
319         $(RESETCMD)
320
321 .PHONY: cores
322 cores: $(foreach algo, $(ALGORITHMS), $(algo)_OBJ)
323
324 .PHONY: blockchiphers
325 blockciphers: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_OBJ)
326
327 .PHONY: streamchiphers
328 streamciphers: $(foreach algo, $(STREAM_CIPHERS), $(algo)_OBJ)
329
330 .PHONY: hashes
331 hashes: $(foreach algo, $(HASHES), $(algo)_OBJ)
332
333 .PHONY: macs
334 macs: $(foreach algo, $(MACS), $(algo)_OBJ)
335
336 .PHONY: prngs
337 prngs: $(foreach algo, $(PRNGS), $(algo)_OBJ)
338
339 .PHONY: encodings
340 encodings: $(foreach algo, $(ENCODINGS), $(algo)_OBJ)
341
342 .PHONY: aux
343 aux: $(foreach algo, $(AUX), $(algo)_OBJ)
344
345
346 #-------------------------------------------------------------------------------
347
348
349 .PHONY: help
350 help: info
351 .PHONY: info
352 info:
353         @echo "infos on AVR-Crypto-lib:"
354         @echo "  block ciphers:"
355         @echo "    $(BLOCK_CIPHERS)"
356         @echo "  stream ciphers:"
357         @echo "    $(STREAM_CIPHERS)"
358         @echo "  hash functions:"
359         @echo "    $(HASHES)"
360         @echo "  MAC functions:"
361         @echo "    $(MACS)"
362         @echo "  PRNG functions:"
363         @echo "    $(PRNGS)"
364         @echo "  signature functions:"
365         @echo "    $(SIGNATURE)"
366         @echo "  public key ciphers:"
367         @echo "    $(PK_CIPHERS)"
368         @echo "  encodings:"
369         @echo "    $(ENCODINGS)"
370         @echo "  auxiliary functions:"
371         @echo "    $(AUX)"
372         @echo " targets:"
373         @echo "  all                - all algorithm cores"
374         @echo "  cores              - all algorithm cores"
375         @echo "  listings           - all algorithm core listings"
376         @echo "  tests              - all algorithm test programs"
377         @echo "  size               - all algorithm size statistics"
378         @echo "  blockciphers       - all blockcipher cores"
379         @echo "  streamciphers      - all streamcipher cores"
380         @echo "  hashes             - all hash cores"
381         @echo "  macs               - all MAC cores"
382         @echo "  prngs              - all PRNG cores"
383         @echo "  all_testrun        - testrun all algorithms"
384         @echo "  hash_size          - measure size of all hash functions"
385         @echo "  hash_speed         - measure performance of all hash functions"
386         @echo "  blockcipher_size   - measure size of all blockciphers"
387         @echo "  blockcipher_speed  - measure performance of all blockciphers"
388         @echo "  docu               - build doxygen documentation"
389         @echo "  clean              - remove a lot of builded files"
390         @echo "  depclean           - also remove dependency files"
391         @echo "  *_TESTBIN          - build test program"
392         @echo "  *_TESTRUN          - run nessie test"
393         @echo "  *_OBJ              - build algorithm core"
394         @echo "  *_FLASH            - flash test program"
395         @echo "  *_LIST             - build assembler listing"
396
397
398 #-------------------------------------------------------------------------------
399
400 .PHONY: clean
401 clean:
402         rm -rf $(BIN_DIR)*
403
404 .PHONY: depclean
405 depclean: clean
406         rm -f $(DEP_DIR)*.d
407
408 #-------------------------------------------------------------------------------
409 # dependency inclusion
410 #
411
412 DEPS := $(wildcard $(DEP_DIR)*.d)
413
414 ifneq ($(DEPS),)
415 include $(DEPS)
416 endif
417