Makefile 12 KB
Newer Older
1
# Copyright (c) 2014-2017 Cryptography Research, Inc.
Michael Hamburg's avatar
Michael Hamburg committed
2 3
# Released under the MIT License.  See LICENSE.txt for license information.

4 5 6 7

UNAME := $(shell uname)
MACHINE := $(shell uname -m)

8 9 10 11
# Subdirectories for objects etc.
# Many of them are mapped to build/obj right now, but could be split later.
# The non-build/obj directories are the public interface.
BUILD_OBJ = build/obj
12 13
BUILD_C   = src/GENERATED/c
BUILD_H   = src/GENERATED/c
14 15
BUILD_PY  = build/obj
BUILD_LIB = build/lib
16
BUILD_INC = src/GENERATED/include
17 18 19 20 21
BUILD_BIN = build/bin
BUILD_IBIN = build/obj/bin
BATBASE=ed448goldilocks_decaf_bats_$(TODAY)
BATNAME=build/$(BATBASE)

22
ifeq ($(UNAME),Darwin)
Michael Hamburg's avatar
Michael Hamburg committed
23
CC = clang
Mike Hamburg's avatar
Mike Hamburg committed
24
CXX = clang++
25 26
else
CC = gcc
Mike Hamburg's avatar
Mike Hamburg committed
27
CXX = g++
28 29
endif
LD = $(CC)
Mike Hamburg's avatar
Mike Hamburg committed
30
LDXX = $(CXX)
31
ASM ?= $(CC)
32

33 34
PYTHON ?= python

Michael Hamburg's avatar
Michael Hamburg committed
35
WARNFLAGS = -pedantic -Wall -Wextra -Werror -Wunreachable-code \
36
	 -Wmissing-declarations -Wunused-function -Wno-overlength-strings $(EXWARN)
37

Michael Hamburg's avatar
Michael Hamburg committed
38 39
INCFLAGS = -Isrc/include -I$(BUILD_INC) -I$(BUILD_H)
PUB_INCFLAGS = -I$(BUILD_INC)
40
LANGFLAGS = -std=c99 -fno-strict-aliasing
Mike Hamburg's avatar
Mike Hamburg committed
41
LANGXXFLAGS = -fno-strict-aliasing
42
GENFLAGS = -ffunction-sections -fdata-sections -fvisibility=hidden -fomit-frame-pointer -fPIC
43
OFLAGS ?= -O2
Michael Hamburg's avatar
Michael Hamburg committed
44

45 46 47 48 49
MACOSX_VERSION_MIN ?= 10.9
ifeq ($(UNAME),Darwin)
GENFLAGS += -mmacosx-version-min=$(MACOSX_VERSION_MIN)
endif

50 51
TODAY = $(shell date "+%Y-%m-%d")

52 53
#FIXME ARCHFLAGS
ARCHFLAGS ?= -maes -mavx2 -mbmi2 #TODO
54 55 56 57 58

ifeq ($(CC),clang)
WARNFLAGS += -Wgcc-compat
endif

Mike Hamburg's avatar
Mike Hamburg committed
59
ARCHFLAGS += $(XARCHFLAGS)
60
CFLAGS  = $(LANGFLAGS) $(WARNFLAGS) $(INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
61
PUB_CFLAGS  = $(LANGFLAGS) $(WARNFLAGS) $(PUB_INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
62
CXXFLAGS = $(LANGXXFLAGS) $(WARNFLAGS) $(INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCXXFLAGS)
63
LDFLAGS = $(XLDFLAGS)
Mike Hamburg's avatar
Mike Hamburg committed
64
ASFLAGS = $(ARCHFLAGS) $(XASFLAGS)
Michael Hamburg's avatar
Michael Hamburg committed
65

66 67 68
SAGE ?= sage
SAGES= $(shell ls test/*.sage)
BUILDPYS= $(SAGES:test/%.sage=$(BUILD_PY)/%.py)
Michael Hamburg's avatar
Michael Hamburg committed
69

70
.PHONY: clean all test test_ct bench todo doc lib bat sage sagetest gen_code
71
.PRECIOUS: $(BUILD_C)/*/%.c $(BUILD_H)/*/%.h  $(BUILD_H)/%.h  $(BUILD_H)/%.hxx $(BUILD_H)/*/%.hxx $(BUILD_IBIN)/%
72

73
HEADER_SRCS= $(shell find src/public_include -name "*.h*")
74
HEADER_PRIVATE_SRCS= $(shell find src/include -name "*.tmpl.h*")
75
GEN_CODE_0= $(HEADER_SRCS:src/public_include/%=$(BUILD_INC)/%)
76
GEN_CODE_0+= $(HEADER_PRIVATE_SRCS:src/include/%=$(BUILD_C)/%)
77 78 79
GEN_CODE_1= $(GEN_CODE_0:%.tmpl.h=%.h)
GEN_CODE= $(GEN_CODE_1:%.tmpl.hxx=%.hxx)
HEADERS= Makefile $(shell find src test -name "*.h") $(BUILD_OBJ)/timestamp $(GEN_CODE)
80 81

# components needed by the lib
82 83
LIBCOMPONENTS = $(BUILD_OBJ)/utils.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o $(BUILD_OBJ)/spongerng.o
# and per-field components
Michael Hamburg's avatar
Michael Hamburg committed
84

85
BENCHCOMPONENTS = $(BUILD_OBJ)/bench.o $(BUILD_OBJ)/shake.o
86

87
all: lib $(BUILD_IBIN)/test $(BUILD_IBIN)/bench $(BUILD_BIN)/shakesum
Michael Hamburg's avatar
Michael Hamburg committed
88 89 90 91 92

scan: clean
	scan-build --use-analyzer=`which clang` \
		 -enable-checker deadcode -enable-checker llvm \
		 -enable-checker osx -enable-checker security -enable-checker unix \
93 94 95 96
		make all

# Internal test programs, which are not part of the final build/bin directory.
$(BUILD_IBIN)/test: $(BUILD_OBJ)/test_decaf.o lib
Mike Hamburg's avatar
Mike Hamburg committed
97
ifeq ($(UNAME),Darwin)
98
	$(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
Mike Hamburg's avatar
Mike Hamburg committed
99
else
100
	$(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
Mike Hamburg's avatar
Mike Hamburg committed
101
endif
102

103 104 105 106 107 108 109 110
# Internal test programs, which are not part of the final build/bin directory.
$(BUILD_IBIN)/test_ct: $(BUILD_OBJ)/test_ct.o lib
ifeq ($(UNAME),Darwin)
	$(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
else
	$(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
endif

111
$(BUILD_IBIN)/bench: $(BUILD_OBJ)/bench_decaf.o lib
112
ifeq ($(UNAME),Darwin)
113
	$(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
114
else
115
	$(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
116 117
endif

118 119
# Create all the build subdirectories
$(BUILD_OBJ)/timestamp:
120
	mkdir -p $(BUILD_OBJ) $(BUILD_C) $(BUILD_PY) \
121
		$(BUILD_LIB) $(BUILD_INC) $(BUILD_BIN) $(BUILD_IBIN) $(BUILD_H) $(BUILD_INC)/decaf \
122
		$(PER_OBJ_DIRS) $(BUILD_C)/decaf
Michael Hamburg's avatar
Michael Hamburg committed
123 124
	touch $@

125
gen_code: $(GEN_CODE)
126 127 128 129

$(BUILD_INC)/%: src/public_include/% $(BUILD_OBJ)/timestamp
	cp -f $< $@
	
130
$(BUILD_INC)/%.h: src/public_include/%.tmpl.h src/generator/*
131
	$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
132
	
133
$(BUILD_C)/%.h: src/include/%.tmpl.h src/generator/*
134
	$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $<
135
	
136
$(BUILD_INC)/%.hxx: src/public_include/%.tmpl.hxx src/generator/*
137
	$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
138 139
	
$(BUILD_C)/%.hxx: src/include/%.tmpl.hxx src/generator/*
140
	$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $<
141

142 143 144 145
################################################################
# Per-field code: call with field, arch
################################################################
define define_field
146
ARCH_FOR_$(1) ?= $(2)
147 148
COMPONENTS_OF_$(1) = $$(BUILD_OBJ)/$(1)/f_impl.o $$(BUILD_OBJ)/$(1)/f_arithmetic.o $$(BUILD_OBJ)/$(1)/f_generic.o
HEADERS_OF_$(1) = $(HEADERS) $$(BUILD_H)/$(1)/f_field.h
149
LIBCOMPONENTS += $$(COMPONENTS_OF_$(1))
150
PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
151

152
$$(BUILD_C)/$(1)/%.c: src/per_field/%.tmpl.c src/generator/* Makefile
153
	$(PYTHON) -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
154
	
155
$$(BUILD_H)/$(1)/%.h: src/per_field/%.tmpl.h src/generator/* Makefile
156
	$(PYTHON) -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
157

158
$$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
159 160
	$$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
	-I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
161
	-c -o $$@ $$<
162

163
$$(BUILD_OBJ)/$(1)/%.o: src/$(1)/%.c $$(HEADERS_OF_$(1))
164 165
	$$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
	-I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
166
	-c -o $$@ $$<
167

168
$$(BUILD_OBJ)/$(1)/%.o: src/$(1)/$$(ARCH_FOR_$(1))/%.c $$(HEADERS_OF_$(1))
169
	$$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
170 171
		-I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
		-c -o $$@ $$<
172 173 174 175 176 177
endef

################################################################
# Per-field, per-curve code: call with curve, field
################################################################
define define_curve
178

179
LIBCOMPONENTS += $$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/elligator.o $$(BUILD_OBJ)/$(1)/scalar.o \
180
	 $$(BUILD_OBJ)/$(1)/eddsa.o $$(BUILD_OBJ)/$(1)/decaf_tables.o
181
PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
182
GLOBAL_HEADERS_OF_$(1) = $(BUILD_INC)/decaf/point_$(3).h $(BUILD_INC)/decaf/point_$(3).hxx \
183
		$(BUILD_INC)/decaf/ed$(3).h $(BUILD_INC)/decaf/ed$(3).hxx
184 185
HEADERS_OF_$(1) = $$(HEADERS_OF_$(2)) $$(GLOBAL_HEADERS_OF_$(1))
HEADERS += $$(GLOBAL_HEADERS_OF_$(1))
186

187
$$(BUILD_C)/$(1)/%.c: src/per_curve/%.tmpl.c src/generator/* Makefile
188
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
189
	
190
$$(BUILD_H)/$(1)/%.h: src/per_curve/%.tmpl.h src/generator/* Makefile
191
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
192
	
193
$$(BUILD_INC)/decaf/point_$(3).%: src/per_curve/point.tmpl.% src/generator/* Makefile
194
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
195
	
196
$$(BUILD_INC)/decaf/ed$(3).%: src/per_curve/eddsa.tmpl.% src/generator/* Makefile
197
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
198
	
199
$$(BUILD_INC)/decaf/elligator_$(3).%: src/per_curve/elligator.tmpl.% src/generator/* Makefile
200
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
201
	
202
$$(BUILD_INC)/decaf/scalar_$(3).%: src/per_curve/scalar.tmpl.% src/generator/* Makefile
203
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
204 205

$$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/$(1)/decaf_gen_tables.o \
206
		$$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/scalar.o $$(BUILD_OBJ)/utils.o \
207 208 209
		$$(COMPONENTS_OF_$(2))
	$$(LD) $$(LDFLAGS) -o $$@ $$^

210
$$(BUILD_C)/$(1)/decaf_tables.c: $$(BUILD_IBIN)/decaf_gen_tables_$(1)
211 212
	./$$< > $$@ || (rm $$@; exit 1)

213 214
$$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
	$$(CC) $$(CFLAGS) -c -o $$@ $$< \
215
		-I build/obj/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
216
		-I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2))
217

218
$$(BUILD_OBJ)/decaf_gen_tables_$(1).o: src/decaf_gen_tables.c $$(HEADERS_OF_$(1))
219
	$$(CC) $$(CFLAGS) \
220
		-I build/obj/curve_$(1) -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
221
		-I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2)) \
222
		-c -o $$@ $$<
223 224 225 226 227
endef

################################################################
# call code above to generate curves and fields
$(eval $(call define_field,p25519,arch_x86_64))
228
$(eval $(call define_curve,curve25519,p25519,255))
229
$(eval $(call define_field,p448,arch_x86_64))
230
$(eval $(call define_curve,ed448goldilocks,p448,448))
231

232
# The shakesum utility is in the public bin directory.
Michael Hamburg's avatar
Michael Hamburg committed
233
$(BUILD_BIN)/shakesum: $(BUILD_OBJ)/shakesum.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o $(BUILD_OBJ)/utils.o
234
	$(LD) $(LDFLAGS) -o $@ $^
235 236 237 238 239 240 241 242 243 244

# The main decaf library, and its symlinks.
lib: $(BUILD_LIB)/libdecaf.so

$(BUILD_LIB)/libdecaf.so: $(BUILD_LIB)/libdecaf.so.1
	ln -sf `basename $^` $@

$(BUILD_LIB)/libdecaf.so.1: $(LIBCOMPONENTS)
	rm -f $@
ifeq ($(UNAME),Darwin)
245
	libtool -macosx_version_min $(MACOSX_VERSION_MIN) -dynamic -dead_strip -lc -x -o $@ \
246
		  $(LIBCOMPONENTS)
247 248 249
else ifeq ($(UNAME),SunOS)
	$(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -o $@ $(LIBCOMPONENTS)
	strip --discard-all $@
250 251 252 253 254 255 256
else
	$(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -Wl,--gc-sections -o $@ $(LIBCOMPONENTS)
	strip --discard-all $@
endif



257 258
$(BUILD_OBJ)/%.o: src/%.c $(HEADERS)
	$(CC) $(CFLAGS) -c -o $@ $<
Mike Hamburg's avatar
Mike Hamburg committed
259
	
260 261
$(BUILD_OBJ)/%.o: test/%.c $(HEADERS)
	$(CC) $(PUB_CFLAGS) -c -o $@ $<
Michael Hamburg's avatar
Michael Hamburg committed
262

263 264
$(BUILD_OBJ)/%.o: test/%.cxx $(HEADERS)
	$(CXX) $(CXXFLAGS) -c -o $@ $<
Mike Hamburg's avatar
Mike Hamburg committed
265

266
# The sage test scripts
267 268 269
sage: $(BUILDPYS)

sagetest: sage lib
270
	$(SAGE) $(BUILD_PY)/test_decaf.sage
271

272 273 274
$(BUILDPYS): $(SAGES) $(BUILD_OBJ)/timestamp
	cp -f $(SAGES) $(BUILD_PY)/
	$(SAGE) --preparse $(SAGES:test/%.sage=$(BUILD_PY)/%.sage)
275
	# some sage versions compile to .sage.py
276
	for f in $(SAGES:test/%.sage=$(BUILD_PY)/%); do \
277 278 279 280
		 if [ -e $$f.sage.py ]; then \
		 	 mv $$f.sage.py $$f.py; \
		 fi; \
	  done
281

282 283 284
# The documentation files
$(BUILD_DOC)/timestamp:
	mkdir -p `dirname $@`
285
	touch $@
286
#
287 288
doc: Doxyfile $(BUILD_OBJ)/timestamp $(HEADERS)
	doxygen > /dev/null
289
	
290
# Finds todo items in .h and .c files
291
TODO_TYPES ?= HACK TODO @todo FIXME BUG XXX PERF FUTURE REMOVE MAGIC UNIFY
292
TODO_LOCATIONS ?= src/*.c src/include src/p* src/generator test Makefile Doxyfile
293
todo::
294
	@(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep --color=auto -w \
Mike Hamburg's avatar
Mike Hamburg committed
295
		`echo $(TODO_TYPES) | tr ' ' '|'`
296
	@echo '============================='
Mike Hamburg's avatar
Mike Hamburg committed
297
	@(for i in $(TODO_TYPES); do \
298
	  (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i > /dev/null || continue; \
299
	  /bin/echo -n $$i'       ' | head -c 10; \
300
	  (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i| wc -l; \
301 302 303
	done)
	@echo '============================='
	@echo -n 'Total     '
304
	@(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w \
Mike Hamburg's avatar
Mike Hamburg committed
305
		`echo $(TODO_TYPES) | tr ' ' '|'` | wc -l
Michael Hamburg's avatar
Michael Hamburg committed
306

307
bench: $(BUILD_IBIN)/bench
Michael Hamburg's avatar
Michael Hamburg committed
308
	./$<
309

310 311
test: $(BUILD_IBIN)/test
	./$<
312 313

test_ct: $(BUILD_IBIN)/test_ct
314
	# NB: you must compile with XCFLAGS=-DNDEBUG or you will get lots of extra warnings due to assert(thing that is always true).
315
	valgrind ./$<
Mike Hamburg's avatar
Mike Hamburg committed
316
	
317
microbench: $(BUILD_IBIN)/bench
318
	./$< --micro
Michael Hamburg's avatar
Michael Hamburg committed
319 320

clean:
321
	rm -fr build
322

323
clean_generated: clean
324
	rm -fr $(BUILD_C)/* $(BUILD_H)/* $(BUILD_INC)/*