Makefile 8.41 KB
Newer Older
Michael Hamburg's avatar
Michael Hamburg committed
1 2 3
# Copyright (c) 2014 Cryptography Research, Inc.
# 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 12 13 14 15 16 17 18 19 20 21
# 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_ASM = build/obj
BUILD_OBJ = build/obj
BUILD_C   = build/obj
BUILD_PY  = build/obj
BUILD_LIB = build/lib
BUILD_INC = build/include
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
DECAF ?= decaf_fast
34

35
ifneq (,$(findstring x86_64,$(MACHINE)))
36
ARCH ?= arch_x86_64
37 38
else
# no i386 port yet
39
ARCH ?= arch_ref32
40
endif
Michael Hamburg's avatar
Michael Hamburg committed
41

42
FIELD ?= p25519
Michael Hamburg's avatar
Michael Hamburg committed
43 44

WARNFLAGS = -pedantic -Wall -Wextra -Werror -Wunreachable-code \
45
	 -Wmissing-declarations -Wunused-function -Wno-overlength-strings $(EXWARN)
46

47
INCFLAGS = -Isrc/include -Isrc/public_include
48
LANGFLAGS = -std=c99 -fno-strict-aliasing
Mike Hamburg's avatar
Mike Hamburg committed
49
LANGXXFLAGS = -fno-strict-aliasing
50
GENFLAGS = -ffunction-sections -fdata-sections -fvisibility=hidden -fomit-frame-pointer -fPIC
51
OFLAGS ?= -O2
Michael Hamburg's avatar
Michael Hamburg committed
52

53 54
TODAY = $(shell date "+%Y-%m-%d")

55 56 57 58 59 60
ifneq (,$(findstring arm,$(MACHINE)))
ifneq (,$(findstring neon,$(ARCH)))
ARCHFLAGS += -mfpu=neon
else
ARCHFLAGS += -mfpu=vfpv3-d16
endif
61 62
ARCHFLAGS += -mcpu=cortex-a8 # FIXME
GENFLAGS += -DN_TESTS_BASE=1000 # sooooo sloooooow
63
else
64
ARCHFLAGS += -maes -mavx2 -mbmi2 #TODO
65 66 67 68 69 70
endif

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

Mike Hamburg's avatar
Mike Hamburg committed
71
ARCHFLAGS += $(XARCHFLAGS)
72
CFLAGS  = $(LANGFLAGS) $(WARNFLAGS) $(INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCFLAGS)
Mike Hamburg's avatar
Mike Hamburg committed
73
CXXFLAGS = $(LANGXXFLAGS) $(WARNFLAGS) $(INCFLAGS) $(OFLAGS) $(ARCHFLAGS) $(GENFLAGS) $(XCXXFLAGS) 
74
LDFLAGS = $(XLDFLAGS)
Mike Hamburg's avatar
Mike Hamburg committed
75
ASFLAGS = $(ARCHFLAGS) $(XASFLAGS)
Michael Hamburg's avatar
Michael Hamburg committed
76

77 78 79
SAGE ?= sage
SAGES= $(shell ls test/*.sage)
BUILDPYS= $(SAGES:test/%.sage=$(BUILD_PY)/%.py)
Michael Hamburg's avatar
Michael Hamburg committed
80

81
.PHONY: clean all test bench todo doc lib bat sage sagetest
Michael Hamburg's avatar
Michael Hamburg committed
82 83
.PRECIOUS: $(BUILD_ASM)/%.s $(BUILD_ASM)/%_impl.s $(BUILD_ASM)/$(DECAF)_%.s $(BUILD_ASM)/decaf_tables_%.c \
	$(BUILD_IBIN)/decaf_gen_tables_%
Michael Hamburg's avatar
Michael Hamburg committed
84

85
HEADERS= Makefile $(shell find src test -name "*.h") $(shell find . -name "*.hxx") $(BUILD_OBJ)/timestamp
86

87 88 89 90 91 92 93 94
# components needed by the table generators
GENCOMPONENTS=  \
	$(BUILD_OBJ)/$(DECAF)_ed25519.o $(BUILD_OBJ)/p25519_impl.o  $(BUILD_OBJ)/p25519_arithmetic.o \
	$(BUILD_OBJ)/utils.o \
	#$(BUILD_OBJ)/p448_impl.o $(BUILD_OBJ)/p448_arithmetic.o

# components needed by the lib
DECAFCOMPONENTS= $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/decaf_crypto.o $(GENCOMPONENTS)
95
ifeq ($(DECAF),decaf_fast)
96
DECAFCOMPONENTS += $(BUILD_OBJ)/decaf_tables_ed25519.o
97
endif
Michael Hamburg's avatar
Michael Hamburg committed
98

99
BENCHCOMPONENTS = $(BUILD_OBJ)/bench.o $(BUILD_OBJ)/shake.o
100

101
all: lib $(BUILD_IBIN)/test $(BUILD_IBIN)/bench $(BUILD_BIN)/shakesum
Michael Hamburg's avatar
Michael Hamburg committed
102 103 104 105 106

scan: clean
	scan-build --use-analyzer=`which clang` \
		 -enable-checker deadcode -enable-checker llvm \
		 -enable-checker osx -enable-checker security -enable-checker unix \
107 108 109 110 111 112 113 114
		make all
		
# The shakesum utility is in the public bin directory.
$(BUILD_BIN)/shakesum: $(BUILD_OBJ)/shakesum.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/utils.o
	$(LD) $(LDFLAGS) -o $@ $^

# The main decaf library, and its symlinks.
lib: $(BUILD_LIB)/libdecaf.so
Michael Hamburg's avatar
Michael Hamburg committed
115

116 117 118 119 120
$(BUILD_LIB)/libdecaf.so: $(BUILD_LIB)/libdecaf.so.1
	ln -sf `basename $^` $@

$(BUILD_LIB)/libdecaf.so.1: $(DECAFCOMPONENTS)
	rm -f $@
Mike Hamburg's avatar
Mike Hamburg committed
121
ifeq ($(UNAME),Darwin)
122 123
	libtool -macosx_version_min 10.6 -dynamic -dead_strip -lc -x -o $@ \
		  $(DECAFCOMPONENTS)
Mike Hamburg's avatar
Mike Hamburg committed
124
else
125 126
	$(LD) $(LDFLAGS) -shared -Wl,-soname,`basename $@` -Wl,--gc-sections -o $@ $(DECAFCOMPONENTS)
	strip --discard-all $@
Mike Hamburg's avatar
Mike Hamburg committed
127
endif
Mike Hamburg's avatar
Mike Hamburg committed
128

129 130
# 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
131
ifeq ($(UNAME),Darwin)
132
	$(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
Mike Hamburg's avatar
Mike Hamburg committed
133
else
134
	$(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
Mike Hamburg's avatar
Mike Hamburg committed
135
endif
136

137
$(BUILD_IBIN)/bench: $(BUILD_OBJ)/bench_decaf.o lib
138
ifeq ($(UNAME),Darwin)
139
	$(LDXX) $(LDFLAGS) -o $@ $< -L$(BUILD_LIB) -ldecaf
140
else
141
	$(LDXX) $(LDFLAGS) -Wl,-rpath,`pwd`/$(BUILD_LIB) -o $@ $< -L$(BUILD_LIB) -ldecaf
142 143
endif

144 145 146 147
# Create all the build subdirectories
$(BUILD_OBJ)/timestamp:
	mkdir -p $(BUILD_ASM) $(BUILD_OBJ) $(BUILD_C) $(BUILD_PY) \
		$(BUILD_LIB) $(BUILD_INC) $(BUILD_BIN) $(BUILD_IBIN) $(BUILD_INC)/decaf
Michael Hamburg's avatar
Michael Hamburg committed
148 149
	touch $@

150
$(BUILD_OBJ)/%.o: $(BUILD_ASM)/%.s
151
	$(ASM) $(ASFLAGS) -c -o $@ $<
Michael Hamburg's avatar
Michael Hamburg committed
152

153 154 155 156 157
# I don't know why this rule is necessary... bug in make, or obscure pattern matching rule?
$(BUILD_OBJ)/decaf_gen_tables_%.o: $(BUILD_ASM)/decaf_gen_tables_%.s
	$(ASM) $(ASFLAGS) -c -o $@ $<

$(BUILD_IBIN)/decaf_gen_tables_%: $(BUILD_OBJ)/decaf_gen_tables_%.o $(GENCOMPONENTS)
158 159
	$(LD) $(LDFLAGS) -o $@ $^
	
160
$(BUILD_C)/decaf_tables_%.c: $(BUILD_IBIN)/decaf_gen_tables_%
161 162
	./$< > $@
	
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
$(BUILD_ASM)/decaf_tables_%.s: $(BUILD_C)/decaf_tables_%.c $(HEADERS)
	$(CC) $(CFLAGS) -S -c -o $@ $< \
		-I src/curve_$*/ -I src/curve_$*/field -I src/curve_$*/field/$(ARCH) \
	
$(BUILD_ASM)/decaf_gen_tables_%.s: src/decaf_gen_tables.c $(HEADERS)
	$(CC) $(CFLAGS) \
		-I src/curve_$*/ -I src/curve_$*/field -I src/curve_$*/field/$(ARCH) \
		-S -c -o $@ $<
	
$(BUILD_ASM)/decaf_fast_%.s: src/decaf_fast.c $(HEADERS)
	$(CC) $(CFLAGS) \
		-I src/curve_$*/ -I src/curve_$*/field -I src/curve_$*/field/$(ARCH) \
		-S -c -o $@ $<
	
$(BUILD_ASM)/%_arithmetic.s: src/%/f_arithmetic.c $(HEADERS)
	$(CC) $(CFLAGS) \
		-I src/$* -I src/$*/$(ARCH) \
		-S -c -o $@ $<
	
$(BUILD_ASM)/%_impl.s: src/%/$(ARCH)/f_impl.c $(HEADERS)
	$(CC) $(CFLAGS) \
		-I src/$* -I src/$*/$(ARCH) \
		-S -c -o $@ $<
186
	
187
$(BUILD_ASM)/%.s: src/%.c $(HEADERS)
Michael Hamburg's avatar
Michael Hamburg committed
188
	$(CC) $(CFLAGS) -S -c -o $@ $<
Mike Hamburg's avatar
Mike Hamburg committed
189
	
190
$(BUILD_ASM)/%.s: src/%.cxx $(HEADERS)
Mike Hamburg's avatar
Mike Hamburg committed
191
	$(CXX) $(CXXFLAGS) -S -c -o $@ $<
Michael Hamburg's avatar
Michael Hamburg committed
192

193
$(BUILD_ASM)/%.s: test/%.c $(HEADERS)
Michael Hamburg's avatar
Michael Hamburg committed
194 195
	$(CC) $(CFLAGS) -S -c -o $@ $<

196
$(BUILD_ASM)/%.s: test/%.cxx $(HEADERS)
Mike Hamburg's avatar
Mike Hamburg committed
197 198
	$(CXX) $(CXXFLAGS) -S -c -o $@ $<

199
# The sage test scripts
200 201 202
sage: $(BUILDPYS)

sagetest: sage lib
203
	LD_LIBRARY_PATH=$(BUILD_LIB) sage $(BUILD_PY)/test_decaf.sage
204

205 206 207
$(BUILDPYS): $(SAGES) $(BUILD_OBJ)/timestamp
	cp -f $(SAGES) $(BUILD_PY)/
	$(SAGE) --preparse $(SAGES:test/%.sage=$(BUILD_PY)/%.sage)
208
	# some sage versions compile to .sage.py
209
	for f in $(SAGES:test/%.sage=$(BUILD_PY)/%); do \
210 211 212 213
		 if [ -e $$f.sage.py ]; then \
		 	 mv $$f.sage.py $$f.py; \
		 fi; \
	  done
214

215 216 217
# The documentation files
$(BUILD_DOC)/timestamp:
	mkdir -p `dirname $@`
218
	touch $@
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
#
# doc: Doxyfile $(BUILD_OBJ)/timestamp $(HEADERS) src/*.c src/$(FIELD)/$(ARCH)/*.c src/$(FIELD)/$(ARCH)/*.h
# 	doxygen > /dev/null

# # The eBATS benchmarking script
# bat: $(BATNAME)
#
# $(BATNAME): include/* src/* src/*/* test/batarch.map $(BUILD_C)/decaf_tables.c # TODO tables some other way
# 	rm -fr $@
# 	for prim in dh sign; do \
#           targ="$@/crypto_$$prim/ed448goldilocks_decaf"; \
# 	  (while read arch where; do \
# 	    mkdir -p $$targ/`basename $$arch`; \
# 	    cp include/*.h $(BUILD_C)/decaf_tables.c src/decaf_fast.c src/decaf_crypto.c src/shake.c src/include/*.h src/bat/$$prim.c src/p448/$$where/*.c src/p448/$$where/*.h src/p448/*.c src/p448/*.h $$targ/`basename $$arch`; \
# 	    cp src/bat/api_$$prim.h $$targ/`basename $$arch`/api.h; \
# 	    perl -p -i -e 's/SYSNAME/'`basename $(BATNAME)`_`basename $$arch`'/g' $$targ/`basename $$arch`/api.h;  \
# 	    perl -p -i -e 's/__TODAY__/'$(TODAY)'/g' $$targ/`basename $$arch`/api.h;  \
# 	    done \
# 	  ) < test/batarch.map; \
# 	  echo 'Mike Hamburg' > $$targ/designers; \
# 	  echo 'Ed448-Goldilocks Decaf sign and dh' > $$targ/description; \
#         done
# 	(cd $(BATNAME)/.. && tar czf $(BATBASE).tgz $(BATBASE) )
242
	
243
# Finds todo items in .h and .c files
Mike Hamburg's avatar
Mike Hamburg committed
244
TODO_TYPES ?= HACK TODO FIXME BUG XXX PERF FUTURE REMOVE MAGIC
245
todo::
246
	@(find * -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep --color=auto -w \
Mike Hamburg's avatar
Mike Hamburg committed
247
		`echo $(TODO_TYPES) | tr ' ' '|'`
248
	@echo '============================='
Mike Hamburg's avatar
Mike Hamburg committed
249
	@(for i in $(TODO_TYPES); do \
250
	  (find * -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w $$i > /dev/null || continue; \
251
	  /bin/echo -n $$i'       ' | head -c 10; \
252
	  (find * -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w $$i| wc -l; \
253 254 255
	done)
	@echo '============================='
	@echo -n 'Total     '
256
	@(find * -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w \
Mike Hamburg's avatar
Mike Hamburg committed
257
		`echo $(TODO_TYPES) | tr ' ' '|'` | wc -l
Michael Hamburg's avatar
Michael Hamburg committed
258

259
bench: $(BUILD_IBIN)/bench
Michael Hamburg's avatar
Michael Hamburg committed
260
	./$<
261

262 263
test: $(BUILD_IBIN)/test
	./$<
Mike Hamburg's avatar
Mike Hamburg committed
264
	
265
microbench: $(BUILD_IBIN)/bench
266
	./$< --micro
Michael Hamburg's avatar
Michael Hamburg committed
267 268

clean:
269
	rm -fr build $(BATNAME)