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