]> git.cryptolib.org Git - arm-crypto-lib.git/blob - Makefile_main.inc
fixing bugs reported by Christian Dernehl
[arm-crypto-lib.git] / Makefile_main.inc
1 # vim:set ft=make
2 # Makefile for the AVR-Crypto-Lib project
3 #
4 #    This file is part of the AVR-Crypto-Lib.
5 #    Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 BLOCK_CIPHERS  :=
21 STREAM_CIPHERS :=
22 HASHES         :=
23 MACS           :=
24 PRNGS          :=
25 ENCODINGS      :=
26 SIGNATURE      :=
27 PK_CIPHERS     :=
28 AUX            :=
29
30 LIB_ALGOS      :=
31
32 # we use the gnu make standard library
33 include gmsl
34
35 GLOBAL_INCDIR := ./ $(TESTSRC_DIR)
36
37 #-------------------------------------------------------------------------------
38 # inclusion of make stubs
39 include $(sort $(wildcard mkfiles/*.mk))
40
41 #-------------------------------------------------------------------------------
42 # define binary object in $(BIN_DIR)$(ALGO)/<obj>
43 define Assert_Template
44 $(1) = $(2)
45 endef
46
47 define Add_Template
48 $(1) += $(2)
49 endef
50
51 #-------------------------------------------------------------------------------
52 ALGORITHMS = $(BLOCK_CIPHERS) $(STREAM_CIPHERS) $(HASHES) $(PRNGS) $(MACS) \
53                          $(ENCODINGS) $(SIGNATURE) $(PK_CIPHERS) $(AUX)
54 ALGORITHMS_OBJ = $(patsubst %,%_OBJ, $(ALGORITHMS))
55 ALGORITHMS_TESTBIN = $(patsubst %,%_TESTBIN, $(ALGORITHMS))
56
57 LIB_OBJECTS := 
58 LIB_SRCDIRS := 
59 LIB_DEFINES :=
60
61 $(foreach a, $(LIB_ALGOS), $(eval $(call Add_Template, \
62   LIB_OBJECTS, \
63   $($(a)_OBJ)  \
64 )))
65 LIB_OBJECTS := $(addprefix $(BIN_DIR)$(LIB_DIR), $(sort $(LIB_OBJECTS)))
66
67 $(foreach a, $(LIB_ALGOS), $(eval $(call Add_Template, \
68   LIB_SRCDIRS, \
69   $($(a)_DIR)  \
70 )))
71
72 $(foreach a, $(LIB_ALGOS), $(eval $(call Add_Template, \
73   LIB_SRCDIRS, \
74   $($(a)_INCDIR)  \
75 )))
76 LIB_SRCDIRS := $(sort $(LIB_SRCDIRS)) 
77
78 $(foreach a, $(LIB_ALGOS), $(eval $(call Add_Template, \
79   LIB_DEFINES, \
80   $($(a)_DEF)  \
81 )))
82 LIB_DEFINES := $(sort $(LIB_DEFINES)) 
83
84
85 #-------------------------------------------------------------------------------
86 # define binary object in $(BIN_DIR)$(ALGO)/<obj>
87 $(foreach a, $(ALGORITHMS), $(eval $(call Assert_Template, \
88     $(a)_BINOBJ, \
89     $(addprefix $(BIN_DIR)$(call lc,$(a))/,$($(a)_OBJ)) \
90 )))
91
92 $(foreach a, $(ALGORITHMS), $(eval $(call Assert_Template, \
93     $(a)_TESTBINOBJ, \
94     $(addprefix $(BIN_DIR)$(call lc,$(a))/$(TEST_DIR),$($(a)_TESTBIN)) \
95 )))
96
97
98
99 #-------------------------------------------------------------------------------
100
101 define TargetSource_Template
102 $(1): $(2)
103         @mkdir -p  $(dir $(1)) $(DEP_DIR)
104         @echo '[cc]: $(1) <-- $(2)'
105         @$(CC) $(CFLAGS_A) $(addprefix -I./,$(3)) $(addprefix -D, $(4)) -c -o $(1) $(2)
106 endef
107
108
109
110 define TargetSourceList_Template
111 $(1): $(2)
112         @mkdir -p $(dir $(1)) $(DEP_DIR)
113         @echo '[cc]: $(1) <-- $(2)'
114         @$(CC) $(CFLAGS_A) $(addprefix -I./,$(3)) $(addprefix -D, $(4)) $(LIST_OPT) -c -o /dev/null $(2) > $(1)
115 endef
116
117 # ----------------------------------------------------------------------------
118 # Function:  find_source_file
119 # Arguments: 1: name of the binary file (.o extension) to search
120 #            2: list of directorys to search for file
121 # Returns:   Returns paths to source file (mathing the pattern in 
122 #            $(SOURCE_PATTERN)
123 # ----------------------------------------------------------------------------
124 SOURCE_PATTERN := %.S %.c 
125 find_source_file = $(firstword $(foreach d, $(2), \
126                      $(filter $(SOURCE_PATTERN),  \
127                        $(wildcard $(d)$(notdir $(patsubst %.o,%,$1)).*) \
128                      ) \
129                    ) )
130               
131               
132 $(foreach a, $(ALGORITHMS), \
133   $(foreach b, $($(a)_OBJ), \
134     $(eval $(call TargetSource_Template, \
135       $(BIN_DIR)$(call lc, $(a))/$(b), \
136       $(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
137       $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
138       $($(a)_DEF), \
139     )) \
140   ) \
141 )
142
143 $(foreach a, $(ALGORITHMS), \
144   $(foreach b, $($(a)_TESTBIN), \
145     $(eval $(call TargetSource_Template, \
146       $(BIN_DIR)$(call lc, $(a))/$(TEST_DIR)$(b), \
147       $(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
148       $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
149       $($(a)_DEF) \
150     )) \
151   ) \
152 )
153
154              
155 $(foreach a, $(ALGORITHMS), \
156   $(foreach b, $($(a)_OBJ), \
157     $(eval $(call TargetSourceList_Template, \
158       $(LIST_DIR)$(call lc, $(a))/$(patsubst %.o,%.s,$(b)), \
159       $(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
160       $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
161       $($(a)_DEF), \
162     )) \
163   ) \
164 )
165
166 EXTRALINK_BINOBJ = $(patsubst %, $(BIN_DIR)%, $(EXTRALINK))
167
168 $(foreach a, $(EXTRALINK), \
169   $(eval $(call TargetSource_Template, \
170     $(BIN_DIR)$(a), \
171     $(call find_source_file, $(a), $(GLOBAL_INCDIR) ),\
172     $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
173     $($(a)_DEF), \
174   )) \
175 )
176
177
178 $(foreach a, $(LIB_OBJECTS), \
179   $(eval $(call TargetSource_Template, \
180     $(a), \
181     $(call find_source_file, $(notdir $(a)), $(LIB_SRCDIRS) ),\
182     $(LIB_SRCDIRS) $(GLOBAL_INCDIR), \
183     $(LIB_DEFINES) \
184     ) \
185   ) \
186 )
187
188 #-------------------------------------------------------------------------------
189
190 define MainTestElf_Template
191 $(1): $(2) $(3) $(4)
192         @mkdir -p $(dir $(1))
193         @echo '[ld]: $(1)'
194         @$(CC) $(CFLAGS_A) $(LDFLAGS)$(patsubst %.elf,%.map,$(1)) -o \
195         $(1) \
196         $(2) $(3) $(EXTRALINK_BINOBJ)\
197         $(addprefix -l,$(LIBS))
198 endef
199
200 $(foreach a, $(ALGORITHMS), \
201     $(eval $(call MainTestElf_Template,  \
202         $(BIN_DIR)$(call lc, $(a))/$(TEST_DIR)main-$(call lc, $(a))-test.elf, \
203         $($(a)_BINOBJ), \
204         $($(a)_TESTBINOBJ), \
205         $(EXTRALINK_BINOBJ) \
206         )) \
207 )
208
209 #-------------------------------------------------------------------------------
210
211 all: $(foreach algo, $(ALGORITHMS), $($(algo)_BINOBJ))
212
213 #-------------------------------------------------------------------------------
214
215 define MakeDir_TEMPLATE
216 $(1):
217         @echo [mkdir] $(1)
218         @mkdir -p $(1)
219 endef
220
221 $(foreach d, DEP_DIR BIN_DIR TESTSRC_DIR TESTLOG_DIR SPEEDLOG_DIR SIZE_DIR LIST_DIR STAT_DIR AUTOASM_DIR, $(eval $(call MakeDir_TEMPLATE, \
222    $($(d)) \
223 )))
224
225 $(foreach algo, $(ALGORITHMS), $(eval $(call MakeDir__TEMPLATE, \
226    $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR) \
227 )))
228
229 define TestBin_TEMPLATE
230 $(1)_TESTBIN: $(2)
231 endef
232
233 $(foreach algo, $(ALGORITHMS), $(eval $(call TestBin_TEMPLATE, \
234     $(algo), \
235     $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR)main-$(call lc, $(algo))-test.elf \
236 )))
237
238 #-------------------------------------------------------------------------------
239
240 define Listing_TEMPLATE
241 $(1)_LIST: $(2)
242 endef
243
244 $(foreach algo, $(ALGORITHMS), $(eval $(call Listing_TEMPLATE, \
245     $(algo), \
246     $(foreach obj,$($(algo)_OBJ), $(LIST_DIR)$(call lc, $(algo))/$(obj:.o=.s)) \
247 )))
248
249 #-------------------------------------------------------------------------------
250 %.bin: %.elf
251         @echo '[objcopy]: $@'
252         @$(OBJCOPY) -O binary $< $@
253
254 %.hex: %.elf
255         @echo '[objcopy]: $@'
256         @$(OBJCOPY) -j .text -j .data -O ihex $< $@
257
258 #-------------------------------------------------------------------------------
259
260 define Flash_Template
261 $(1)_FLASH: $(2)
262         @echo '[flash]: $(2)'
263         @$(call FLASHCMD, $(call first,$(2)))
264 endef
265
266 $(foreach algo, $(ALGORITHMS), $(eval $(call Flash_Template, \
267     $(algo), \
268     $(BIN_DIR)$(call lc, $(algo))/$(TEST_DIR)main-$(call lc, $(algo))-test.bin \
269 )))
270
271 #-------------------------------------------------------------------------------
272
273 define Speed_Template
274 $(1)_SPEED:  $(1)_FLASH
275         @$(RUBY) $(SPEEDTOOL) -c $(SPEEDCMD) -t $(SPEEDLOG_DIR) -a $(call lc, $(1))
276 endef
277
278 $(foreach algo, $(ALGORITHMS), $(eval $(call Speed_Template, \
279     $(algo), $(algo) \
280 )))
281
282 .PHONY: hash_speed
283 hash_speed: $(foreach algo, $(HASHES), $(algo)_SPEED)
284
285 .PHONY: blockcipher_speed
286 blockcipher_speed: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SPEED)
287 #-------------------------------------------------------------------------------
288
289
290 define Size_Template
291 $(1)_SIZE:  $(2)
292         @echo '[size] $(1)'
293         $(SIZE) $(2) > $(strip $(SIZE_DIR))$(strip $(call lc, $(1))).size
294 endef
295
296 $(foreach algo, $(ALGORITHMS), $(eval $(call Size_Template, \
297     $(strip $(algo)), $($(algo)_BINOBJ) \
298 )))
299
300 .PHONY: hash_size
301 hash_size: $(foreach algo, $(HASHES), $(algo)_SIZE)
302
303 .PHONY: blockcipher_size
304 blockcipher_size: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SIZE)
305
306 #-------------------------------------------------------------------------------
307
308 .PHONY: tests
309 tests: $(foreach a, $(ALGORITHMS), $(a)_TESTBIN)
310
311 #-------------------------------------------------------------------------------
312
313 define TestRun_Template
314 $(1)_TESTRUN: $(1)_FLASH
315         @echo '[reset]'
316         @sleep 3
317         @$(RESETCMD)
318         @sleep 1
319         @echo '[test]: $(1)'
320         $(RUBY) $(GET_TEST) $(TESTPORT) $(TESTPORTBAUDR) 8 1 nessie $(TESTLOG_DIR)$(TESTPREFIX) $(2)
321 endef
322
323 $(foreach algo, $(ALGORITHMS),$(eval $(call TestRun_Template, $(algo), $(call lc,$(algo)) )))
324
325 all_testrun: $(foreach algo, $(ALGORITHMS), $(algo)_TESTRUN)
326
327 #-------------------------------------------------------------------------------
328
329 define Obj_Template
330 $(1)_OBJ: $(2)
331 endef
332
333 $(foreach algo, $(ALGORITHMS), \
334     $(eval $(call Obj_Template, \
335         $(algo), \
336         $($(algo)_BINOBJ)\
337         ))\
338 )
339
340 .PHONY: all
341 all: cores
342
343 .PHONY: reset
344 reset:
345         $(RESETCMD)
346
347 .PHONY: cores
348 cores: $(foreach algo, $(ALGORITHMS), $(algo)_OBJ)
349
350 .PHONY: blockchiphers
351 blockciphers: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_OBJ)
352
353 .PHONY: streamchiphers
354 streamciphers: $(foreach algo, $(STREAM_CIPHERS), $(algo)_OBJ)
355
356 .PHONY: hashes
357 hashes: $(foreach algo, $(HASHES), $(algo)_OBJ)
358
359 .PHONY: macs
360 macs: $(foreach algo, $(MACS), $(algo)_OBJ)
361
362 .PHONY: prngs
363 prngs: $(foreach algo, $(PRNGS), $(algo)_OBJ)
364
365 .PHONY: encodings
366 encodings: $(foreach algo, $(ENCODINGS), $(algo)_OBJ)
367
368 .PHONY: aux
369 aux: $(foreach algo, $(AUX), $(algo)_OBJ)
370
371 .PHONY: lib_info
372 lib_info:
373         @echo 'LIB_ALGOS ='
374         @echo  $(foreach a, $(LIB_ALGOS), '\t$(a)\n')
375         @echo 'LIB_OBJECTS ='
376         @echo  $(foreach a, $(LIB_OBJECTS), '\t$(a)\n')
377         @echo 'LIB_SRCDIRS ='
378         @echo  $(foreach a, $(LIB_SRCDIRS), '\t$(a)\n')
379
380 $(BIN_DIR)$(LIB_DIR)$(LIB_NAME): $(LIB_OBJECTS)
381         @echo '[rm]:  old $@'
382         @$(RM) -f $@
383         @echo '[chmod]: <objects>'
384         @$(CHMOD) 644 $^
385 #       $(CHGRP) root $^
386 #       $(CHOWN) root $^
387         @echo '[ar]:  $@ <-- <objects>'
388         @$(AR) qc $@ $^
389
390 .PHONY: lib
391 lib: $(BIN_DIR)$(LIB_DIR)$(LIB_NAME)
392
393 #-------------------------------------------------------------------------------
394
395
396 .PHONY: help
397 help: info
398 .PHONY: info
399 info:
400         @echo 'infos on ARM-Crypto-lib:'
401         @echo '  block ciphers:'
402         @echo '    $(BLOCK_CIPHERS)'
403         @echo '  stream ciphers:'
404         @echo '    $(STREAM_CIPHERS)'
405         @echo '  hash functions:'
406         @echo '    $(HASHES)'
407         @echo '  MAC functions:'
408         @echo '    $(MACS)'
409         @echo '  PRNG functions:'
410         @echo '    $(PRNGS)'
411         @echo '  signature functions:'
412         @echo '    $(SIGNATURE)'
413         @echo '  public key ciphers:'
414         @echo '    $(PK_CIPHERS)'
415         @echo '  encodings:'
416         @echo '    $(ENCODINGS)'
417         @echo '  auxiliary functions:'
418         @echo '    $(AUX)'
419         @echo ' targets:'
420         @echo '  all                - all algorithm cores'
421         @echo '  cores              - all algorithm cores'
422         @echo '  listings           - all algorithm core listings'
423         @echo '  tests              - all algorithm test programs'
424         @echo '  stats              - all algorithm size statistics'
425         @echo '  blockciphers       - all blockcipher cores'
426         @echo '  streamciphers      - all streamcipher cores'
427         @echo '  hashes             - all hash cores'
428         @echo '  macs               - all MAC cores'
429         @echo '  prngs              - all PRNG cores'
430         @echo '  lib                - create library
431         @echo '  all_testrun        - testrun all algorithms'
432         @echo '  hash_size          - measure size of all hash functions'
433         @echo '  hash_speed         - measure performance of all hash functions'
434         @echo '  blockcipher_size   - measure size of all blockciphers'
435         @echo '  blockcipher_speed  - measure performance of all blockciphers'
436         @echo '  docu               - build doxygen documentation'
437         @echo '  clean              - remove a lot of builded files'
438         @echo '  depclean           - also remove dependency files'
439         @echo '  *_TESTBIN          - build test program'
440         @echo '  *_TESTRUN          - run nessie test'
441         @echo '  *_OBJ              - build algorithm core'
442         @echo '  *_FLASH            - flash test program'
443         @echo '  *_LIST             - build assembler listing'
444
445
446 #-------------------------------------------------------------------------------
447
448 .PHONY: clean
449 clean:
450         @echo '[rm]:  $(BIN_DIR)*'
451         rm -rf $(BIN_DIR)*
452
453 .PHONY: depclean
454 depclean: clean
455         @echo '[rm]:  $(DEP_DIR)*.d'
456         rm -f $(DEP_DIR)*.d
457
458 #-------------------------------------------------------------------------------
459 # dependency inclusion
460 #
461
462 DEPS := $(wildcard $(DEP_DIR)*.d)
463
464 ifneq ($(DEPS),)
465 include $(DEPS)
466 endif
467