Makefile 13 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
BUILD_BIN = build/bin
BUILD_IBIN = build/obj/bin

20 21
DOXYGEN ?= doxygen

22
ifeq ($(UNAME),Darwin)
23 24
CC ?= clang
CXX ?= clang++
25
else
26 27
CC ?= gcc
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
ARCHFLAGS ?= -march=native
53 54

ifeq ($(CC),clang)
55 56 57 58 59
WARNFLAGS_C += -Wgcc-compat
endif

ifeq ($(CXX),clang++)
WARNFLAGS_CXX += -Wgcc-compat
60 61
endif

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

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

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

76
HEADER_SRCS= $(shell find src/public_include -name "*.h*")
77
HEADER_PRIVATE_SRCS= $(shell find src/include -name "*.tmpl.h*")
78
GEN_CODE_0= $(HEADER_SRCS:src/public_include/%=$(BUILD_INC)/%)
79
GEN_CODE_0+= $(HEADER_PRIVATE_SRCS:src/include/%=$(BUILD_C)/%)
80 81 82
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)
83 84

# components needed by the lib
85 86
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
87

88
BENCHCOMPONENTS = $(BUILD_OBJ)/bench.o $(BUILD_OBJ)/shake.o
89

90
all: lib $(BUILD_IBIN)/test $(BUILD_BIN)/ristretto $(BUILD_IBIN)/bench $(BUILD_BIN)/shakesum
Michael Hamburg's avatar
Michael Hamburg committed
91 92 93 94 95

scan: clean
	scan-build --use-analyzer=`which clang` \
		 -enable-checker deadcode -enable-checker llvm \
		 -enable-checker osx -enable-checker security -enable-checker unix \
96 97 98 99
		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
100
ifeq ($(UNAME),Darwin)
101
	$(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
Mike Hamburg's avatar
Mike Hamburg committed
102
else
103
	$(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
Mike Hamburg's avatar
Mike Hamburg committed
104
endif
105

106 107 108 109 110 111 112
$(BUILD_BIN)/ristretto: $(BUILD_OBJ)/ristretto.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

113 114 115 116 117 118 119 120
# 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

121
$(BUILD_IBIN)/bench: $(BUILD_OBJ)/bench_decaf.o lib
122
ifeq ($(UNAME),Darwin)
123
	$(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
124
else
125
	$(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
126 127
endif

128 129
# Create all the build subdirectories
$(BUILD_OBJ)/timestamp:
130
	mkdir -p $(BUILD_OBJ) $(BUILD_C) $(BUILD_PY) \
131
		$(BUILD_LIB) $(BUILD_INC) $(BUILD_BIN) $(BUILD_IBIN) $(BUILD_H) $(BUILD_INC)/decaf \
132
		$(PER_OBJ_DIRS) $(BUILD_C)/decaf
Michael Hamburg's avatar
Michael Hamburg committed
133 134
	touch $@

135 136 137
$(BUILD_INC)/%: src/public_include/% $(BUILD_OBJ)/timestamp
	cp -f $< $@
	
138
$(BUILD_INC)/%.h: src/public_include/%.tmpl.h src/generator/*
139
	$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
140
	
141
$(BUILD_C)/%.h: src/include/%.tmpl.h src/generator/*
142
	$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $<
143
	
144
$(BUILD_INC)/%.hxx: src/public_include/%.tmpl.hxx src/generator/*
145
	$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $<
146 147
	
$(BUILD_C)/%.hxx: src/include/%.tmpl.hxx src/generator/*
148
	$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $<
149

150 151 152
################################################################
# Per-field code: call with field, arch
################################################################
153 154
PER_FIELD_C = $(wildcard src/per_field/*.tmpl.c)
PER_FIELD_H = $(wildcard src/per_field/*.tmpl.h*)
155
define define_field
156
ARCH_FOR_$(1) ?= $(2)
157 158
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
159
LIBCOMPONENTS += $$(COMPONENTS_OF_$(1))
160 161 162
GEN_CODE_FOR_$(1)  = $$(patsubst src/per_field/%,$(BUILD_C)/$(1)/%,$(patsubst %.tmpl.c,%.c,$(PER_FIELD_C)))
GEN_CODE_FOR_$(1) += $$(patsubst src/per_field/%,$(BUILD_H)/$(1)/%,$(patsubst %.tmpl.h,%.h,$(PER_FIELD_H)))
GEN_CODE += $$(GEN_CODE_FOR_$(1))
163
PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
164

165
$$(BUILD_C)/$(1)/%.c: src/per_field/%.tmpl.c src/generator/* Makefile
166
	$(PYTHON) -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
167
	
168
$$(BUILD_H)/$(1)/%.h: src/per_field/%.tmpl.h src/generator/* Makefile
169
	$(PYTHON) -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$<
170

171
$$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
172 173
	$$(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)) \
174
	-c -o $$@ $$<
175

176
$$(BUILD_OBJ)/$(1)/%.o: src/$(1)/%.c $$(HEADERS_OF_$(1))
177 178
	$$(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)) \
179
	-c -o $$@ $$<
180

181
$$(BUILD_OBJ)/$(1)/%.o: src/$(1)/$$(ARCH_FOR_$(1))/%.c $$(HEADERS_OF_$(1))
182
	$$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \
183 184
		-I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \
		-c -o $$@ $$<
185 186 187 188 189
endef

################################################################
# Per-field, per-curve code: call with curve, field
################################################################
190
PER_CURVE_C = $(wildcard src/per_curve/*.tmpl.c)
191
define define_curve
192

193
LIBCOMPONENTS += $$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/elligator.o $$(BUILD_OBJ)/$(1)/scalar.o \
194
	 $$(BUILD_OBJ)/$(1)/eddsa.o $$(BUILD_OBJ)/$(1)/decaf_tables.o
195
PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1)
196
GLOBAL_HEADERS_OF_$(1) = $(BUILD_INC)/decaf/point_$(3).h $(BUILD_INC)/decaf/point_$(3).hxx \
197
		$(BUILD_INC)/decaf/ed$(3).h $(BUILD_INC)/decaf/ed$(3).hxx
198 199
HEADERS_OF_$(1) = $$(HEADERS_OF_$(2)) $$(GLOBAL_HEADERS_OF_$(1))
HEADERS += $$(GLOBAL_HEADERS_OF_$(1))
200

201 202 203 204 205
GEN_CODE_FOR_$(1)  = $$(patsubst src/per_curve/%,$(BUILD_C)/$(1)/%,$(patsubst %.tmpl.c,%.c,$(PER_CURVE_C)))
GEN_CODE_FOR_$(1) += $$(GLOBAL_HEADERS_OF_$(1))
GEN_CODE_P2 += $(BUILD_C)/$(1)/decaf_tables.c
GEN_CODE += $$(GEN_CODE_FOR_$(1))

206
$$(BUILD_C)/$(1)/%.c: src/per_curve/%.tmpl.c src/generator/* Makefile
207
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
208
	
209
$$(BUILD_H)/$(1)/%.h: src/per_curve/%.tmpl.h src/generator/* Makefile
210
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$<
211
	
212
$$(BUILD_INC)/decaf/point_$(3).%: src/per_curve/point.tmpl.% src/generator/* Makefile
213
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
214
	
215
$$(BUILD_INC)/decaf/ed$(3).%: src/per_curve/eddsa.tmpl.% src/generator/* Makefile
216
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
217
	
218
$$(BUILD_INC)/decaf/elligator_$(3).%: src/per_curve/elligator.tmpl.% src/generator/* Makefile
219
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
220
	
221
$$(BUILD_INC)/decaf/scalar_$(3).%: src/per_curve/scalar.tmpl.% src/generator/* Makefile
222
	$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$<
223 224

$$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/$(1)/decaf_gen_tables.o \
225
		$$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/scalar.o $$(BUILD_OBJ)/utils.o \
226 227 228
		$$(COMPONENTS_OF_$(2))
	$$(LD) $$(LDFLAGS) -o $$@ $$^

229
$$(BUILD_C)/$(1)/decaf_tables.c: $$(BUILD_IBIN)/decaf_gen_tables_$(1)
230 231
	./$$< > $$@ || (rm $$@; exit 1)

232 233
$$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1))
	$$(CC) $$(CFLAGS) -c -o $$@ $$< \
234
		-I build/obj/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
235
		-I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2))
236

237
$$(BUILD_OBJ)/decaf_gen_tables_$(1).o: src/decaf_gen_tables.c $$(HEADERS_OF_$(1))
238
	$$(CC) $$(CFLAGS) \
239
		-I build/obj/curve_$(1) -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \
240
		-I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2)) \
241
		-c -o $$@ $$<
242 243 244 245 246
endef

################################################################
# call code above to generate curves and fields
$(eval $(call define_field,p25519,arch_x86_64))
247
$(eval $(call define_curve,curve25519,p25519,255))
248
$(eval $(call define_field,p448,arch_x86_64))
249
$(eval $(call define_curve,ed448goldilocks,p448,448))
250

251
# The shakesum utility is in the public bin directory.
Michael Hamburg's avatar
Michael Hamburg committed
252
$(BUILD_BIN)/shakesum: $(BUILD_OBJ)/shakesum.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o $(BUILD_OBJ)/utils.o
253
	$(LD) $(LDFLAGS) -o $@ $^
254 255 256 257 258 259 260 261 262 263

# 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)
264
	libtool -macosx_version_min $(MACOSX_VERSION_MIN) -dynamic -dead_strip -lc -x -o $@ \
265
		  $(LIBCOMPONENTS)
266 267 268
else ifeq ($(UNAME),SunOS)
	$(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -o $@ $(LIBCOMPONENTS)
	strip --discard-all $@
269 270 271 272 273 274
else
	$(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -Wl,--gc-sections -o $@ $(LIBCOMPONENTS)
	strip --discard-all $@
endif


275 276
$(BUILD_OBJ)/%.o: src/%.c $(HEADERS)
	$(CC) $(CFLAGS) -c -o $@ $<
Mike Hamburg's avatar
Mike Hamburg committed
277
	
278 279
$(BUILD_OBJ)/%.o: test/%.c $(HEADERS)
	$(CC) $(PUB_CFLAGS) -c -o $@ $<
Michael Hamburg's avatar
Michael Hamburg committed
280

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

284
# The sage test scripts
285 286 287
sage: $(BUILDPYS)

sagetest: sage lib
288
	$(SAGE) $(BUILD_PY)/test_decaf.sage
289

290 291 292
$(BUILDPYS): $(SAGES) $(BUILD_OBJ)/timestamp
	cp -f $(SAGES) $(BUILD_PY)/
	$(SAGE) --preparse $(SAGES:test/%.sage=$(BUILD_PY)/%.sage)
293
	# some sage versions compile to .sage.py
294
	for f in $(SAGES:test/%.sage=$(BUILD_PY)/%); do \
295 296 297 298
		 if [ -e $$f.sage.py ]; then \
		 	 mv $$f.sage.py $$f.py; \
		 fi; \
	  done
299

300 301 302
# The documentation files
$(BUILD_DOC)/timestamp:
	mkdir -p `dirname $@`
303
	touch $@
304
#
305 306
doc: Doxyfile $(BUILD_OBJ)/timestamp gen_code_static
	$(DOXYGEN) > /dev/null
307 308 309 310

gen_code_static: $(GEN_CODE)
gen_code: gen_code_static $(GEN_CODE_P2)

311
# Finds todo items in .h and .c files
312
TODO_TYPES ?= HACK TODO @todo FIXME BUG XXX PERF FUTURE REMOVE MAGIC UNIFY
313
TODO_LOCATIONS ?= src/*.c src/include src/p* src/generator test Makefile Doxyfile
314
todo::
315
	@(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
316
		`echo $(TODO_TYPES) | tr ' ' '|'`
317
	@echo '============================='
Mike Hamburg's avatar
Mike Hamburg committed
318
	@(for i in $(TODO_TYPES); do \
319
	  (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i > /dev/null || continue; \
320
	  /bin/echo -n $$i'       ' | head -c 10; \
321
	  (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i| wc -l; \
322 323 324
	done)
	@echo '============================='
	@echo -n 'Total     '
325
	@(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
326
		`echo $(TODO_TYPES) | tr ' ' '|'` | wc -l
Michael Hamburg's avatar
Michael Hamburg committed
327

328
bench: $(BUILD_IBIN)/bench
Michael Hamburg's avatar
Michael Hamburg committed
329
	./$<
330

331 332
test: $(BUILD_IBIN)/test
	./$<
333 334

test_ct: $(BUILD_IBIN)/test_ct
335
	# NB: you must compile with XCFLAGS=-DNDEBUG or you will get lots of extra warnings due to assert(thing that is always true).
336
	valgrind ./$<
Mike Hamburg's avatar
Mike Hamburg committed
337
	
338
microbench: $(BUILD_IBIN)/bench
339
	./$< --micro
Michael Hamburg's avatar
Michael Hamburg committed
340 341

clean:
342
	rm -fr build
343

344
clean_generated: clean
345
	rm -fr $(BUILD_C)/* $(BUILD_H)/* $(BUILD_INC)/*