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