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