Makefile and prepare-depends improvements

- Refactor CXXFLAGS so that feature flags and optimization flags can be easily overriden from command line
- Add NO_SUPERCOP=1 option
- Avoid need to manually create ./depinst directory when ./prepare-depends is not needed (i.e., when BN128 and SUPERCOP are not used)
- Fix the flags for static build
- Build executables that need GTest even if we used an existing GTest rather than compiling it
- Minor cleanups and comments
This commit is contained in:
Eran Tromer 2015-10-01 00:02:33 +03:00 committed by Madars Virza
parent f403f084dc
commit 998a23735a
4 changed files with 59 additions and 46 deletions

View File

@ -6,24 +6,34 @@
#* @copyright MIT license (see LICENSE file)
#*******************************************************************************/
CXXFLAGS += -O2 -Wall -Wextra -Wno-unused-parameter -Wno-comment -march=native -mtune=native -std=c++11 -Wfatal-errors
# To override these, use "make OPTFLAGS=..." etc.
CURVE = BN128
OPTFLAGS = -O2 -march=native -mtune=native
FEATUREFLAGS = -DUSE_ASM -DMONTGOMERY_OUTPUT
# Initialize this using "CXXFLAGS=... make". The makefile appends to that.
CXXFLAGS += -std=c++11 -Wall -Wextra -Wno-unused-parameter -Wno-comment -Wfatal-errors $(OPTFLAGS) $(FEATUREFLAGS) -DCURVE_$(CURVE)
DEPSRC=depsrc
DEPINST=depinst
LDFLAGS += -L $(DEPINST)/lib -Wl,-rpath $(DEPINST)/lib
LDLIBS += -lgmpxx -lgmp -lboost_program_options -lsupercop -lcrypto
CXXFLAGS += -I $(DEPINST)/include -I src -DUSE_ASM -DMONTGOMERY_OUTPUT
DEFAULT_CURVE=BN128
LDLIBS += -lgmpxx -lgmp -lboost_program_options
LDLIBS += -lcrypto -ldl -lz # OpenSSL and its dependencies (needed explicitly for static builds)
CXXFLAGS += -I $(DEPINST)/include -I src
ifneq ($(NO_GTEST),1)
GTESTDIR=/usr/src/gtest
# Recompile GTest, if we can (e.g., Ubuntu). Otherwise use precompiled one (e.g., Fedora).
# Compile GTest from sourcecode if we can (e.g., Ubuntu). Otherwise use precompiled one (e.g., Fedora).
# See https://code.google.com/p/googletest/wiki/FAQ#Why_is_it_not_recommended_to_install_a_pre-compiled_copy_of_Goog .
COMPILE_GTEST:=$(shell test -d $(GTESTDIR) && echo 1) # Found GTest sourcecode?
COMPILE_GTEST:=$(shell test -d $(GTESTDIR) && echo -n 1)
GTEST_LDLIBS += -lgtest -lpthread
endif
ifneq ($(NO_SUPERCOP),1)
SUPERCOP_LDLIBS += -lsupercop
endif
SRCS = \
src/algebra/curves/alt_bn128/alt_bn128_g1.cpp \
src/algebra/curves/alt_bn128/alt_bn128_g2.cpp \
@ -74,11 +84,6 @@ SRCS = \
src/relations/ram_computations/rams/fooram/fooram_aux.cpp \
src/relations/ram_computations/rams/tinyram/tinyram_aux.cpp
ifeq ($(CURVE),)
CURVE = $(DEFAULT_CURVE)
endif
CXXFLAGS += -DCURVE_$(CURVE)
ifeq ($(CURVE),BN128)
SRCS += \
src/algebra/curves/bn128/bn128_g1.cpp \
@ -127,8 +132,7 @@ EXECUTABLES = \
src/zk_proof_systems/ppzksnark/uscs_ppzksnark/profiling/profile_uscs_ppzksnark \
src/zk_proof_systems/ppzksnark/uscs_ppzksnark/tests/test_uscs_ppzksnark \
src/zk_proof_systems/zksnark/ram_zksnark/profiling/profile_ram_zksnark \
src/zk_proof_systems/zksnark/ram_zksnark/tests/test_ram_zksnark \
src/zk_proof_systems/ppzkadsnark/r1cs_ppzkadsnark/examples/demo_r1cs_ppzkadsnark
src/zk_proof_systems/zksnark/ram_zksnark/tests/test_ram_zksnark
ifneq ($(NO_GTEST),1)
@ -137,6 +141,11 @@ ifneq ($(NO_GTEST),1)
src/gadgetlib2/tests/gadgetlib2_test
endif
ifneq ($(NO_SUPERCOP),1)
EXECUTABLES_WITH_SUPERCOP = \
src/zk_proof_systems/ppzkadsnark/r1cs_ppzkadsnark/examples/demo_r1cs_ppzkadsnark
endif
DOCS= README.html
# For documentation of the following options, see README.md .
@ -151,16 +160,17 @@ ifeq ($(LOWMEM),1)
CXXFLAGS += -DLOWMEM
endif
ifeq ($(PROFILE_OP_COUNTS),1)
STATIC = 1
CXXFLAGS += -DPROFILE_OP_COUNTS
endif
ifeq ($(STATIC),1)
CXXFLAGS += -static -DSTATIC
else
CXXFLAGS += -fPIC
endif
ifeq ($(PROFILE_OP_COUNTS),1)
CXXFLAGS += -static -DPROFILE_OP_COUNTS
endif
ifeq ($(MULTICORE),1)
CXXFLAGS += -DMULTICORE -fopenmp
endif
@ -175,29 +185,32 @@ ifeq ($(DEBUG),1)
endif
ifeq ($(PERFORMANCE),1)
CXXFLAGS += -flto -fuse-linker-plugin
CXXFLAGS += -march=native -mtune=native
# OPTFLAGS could be changed here, but the default set above is already OK
CXXFLAGS += -DNDEBUG
CXXFLAGS += -flto -fuse-linker-plugin # enable link-time optimization
LDFLAGS += -flto
endif
OBJS=$(patsubst %.cpp,%.o,$(SRCS))
ifeq ($(strip $(COMPILE_GTEST)),1)
all: libgtest.a $(EXECUTABLES) $(EXECUTABLES_WITH_GTEST) doc
else
all: $(EXECUTABLES) doc
endif
all: $(if $(NO_GTEST),,$(if $(COMPILE_GTEST),libgtest.a) $(EXECUTABLES_WITH_GTEST)) \
$(EXECUTABLES) \
$(if $(NO_SUPERCOP),,$(EXECUTABLES_WITH_SUPERCOP)) \
$(if $(NO_DOCS),,doc)
doc: $(DOCS)
deplib:
# Placeholder. Some make settings (including the default) require actually running ./prepare-depends
mkdir -p $(DEPINST)/lib
# In order to detect changes to #include dependencies. -MMD below generates a .d file for .cpp file. Include the .d file.
-include $(SRCS:.cpp=.d)
$(OBJS): %.o: %.cpp
$(CXX) -o $@ $< -c -MMD $(CXXFLAGS)
libgtest.a: $(GTESTDIR)/src/gtest-all.cc
libgtest.a: $(GTESTDIR)/src/gtest-all.cc deplib
$(CXX) -I $(GTESTDIR) -c -isystem $(GTESTDIR)/include $< $(CXXFLAGS) -o $(DEPINST)/lib/gtest-all.o
$(AR) -rv $(DEPINST)/lib/libgtest.a $(DEPINST)/lib/gtest-all.o
@ -209,11 +222,14 @@ src/gadgetlib2/tests/gadgetlib2_test: \
src/gadgetlib2/tests/protoboard_UTEST.cpp \
src/gadgetlib2/tests/variable_UTEST.cpp
$(EXECUTABLES): %: %.o $(OBJS)
$(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS) $(LDLIBS)
$(EXECUTABLES): %: %.o $(OBJS) deplib
$(CXX) -o $@ $@.o $(OBJS) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS)
$(EXECUTABLES_WITH_GTEST): %: %.o $(OBJS)
$(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) $(GTEST_LDLIBS)
$(EXECUTABLES_WITH_GTEST): %: %.o $(OBJS) deplib
$(CXX) -o $@ $@.o $(OBJS) $(CXXFLAGS) $(LDFLAGS) $(GTEST_LDLIBS) $(LDLIBS)
$(EXECUTABLES_WITH_SUPERCOP): %: %.o $(OBJS) deplib
$(CXX) -o $@ $@.o $(OBJS) $(CXXFLAGS) $(LDFLAGS) $(SUPERCOP_LDLIBS) $(LDLIBS)
ifeq ($(STATIC),1)
libsnark.a: $(OBJS)
@ -229,11 +245,9 @@ lib: $(LIBOBJ)
$(DOCS): %.html: %.md
ifneq ($(NO_DOCS),1)
markdown_py -f $@ $^ -x toc -x extra --noisy
# TODO: Would be nice to enable "-x smartypants" but Ubuntu 12.04 doesn't support that.
# TODO: switch to redcarpet, to produce same output as GitHub's processing of README.md. But what about TOC?
endif
ifeq ($(PREFIX),)
install:
@ -246,7 +260,7 @@ $(HEADERS_DEST): $(PREFIX)/include/libsnark/%: src/%
mkdir -p $(shell dirname $@)
cp $< $@
install: lib $(HEADERS_DEST)
install: lib $(HEADERS_DEST) deplib
mkdir -p $(PREFIX)/lib
cp $(LIBOBJ) $(PREFIX)/lib/$(LIBOBJ)
cp -rv $(DEPINST)/lib $(PREFIX)
@ -270,6 +284,6 @@ clean:
# Clean all, including locally-compiled dependencies
clean-all: clean
rm -fr $(DEPSRC) $(DEPINST)
$(RM) -fr $(DEPSRC) $(DEPINST)
.PHONY: all clean clean-all doc doxy lib
.PHONY: all clean clean-all doc doxy lib deplib

View File

@ -209,10 +209,6 @@ compile it. (Required only when using the default bn128 curve.)
$ ./prepare-depends.sh
If not using bn128 option the depinst directory, referred to in Makefile, needs to be created manually:
$ mkdir -p depinst/include depinst/src
Then, to compile the library, tests, profiling harness and documentation, run:
$ make

View File

@ -1,5 +1,5 @@
#!/bin/sh
# This script fetches, builds and locally installs the external ate-pairing library (and its dependency, xbyak)
# This script fetches, builds and locally installs external dependencies.
set -x -e
@ -10,19 +10,22 @@ DEPINST=./depinst
mkdir -p $DEPINST
mkdir -p $DEPSRC
# ate-pairing library, and its dependency, xbyak (needed for BN128 curve)
cd $DEPSRC
[ ! -d xbyak ] && git clone git://github.com/herumi/xbyak.git
[ ! -d ate-pairing ] && git clone git://github.com/herumi/ate-pairing.git
[ ! -d libsnark-supercop ] && git clone git://github.com/mbbarbosa/libsnark-supercop.git
cd ate-pairing
make -j SUPPORT_SNARK=1
cd ..
cd libsnark-supercop
sh "do"
cd ..
cd ..
cd ../..
cp -rv $DEPSRC/ate-pairing/include $DEPINST/
cp -rv $DEPSRC/ate-pairing/lib $DEPINST/
# SUPERCOP library (optimized crypto implementations, used by ADSNARK)
cd $DEPSRC
[ ! -d libsnark-supercop ] && git clone git://github.com/mbbarbosa/libsnark-supercop.git
cd libsnark-supercop
sh "do"
cd ../..
mkdir -p $DEPINST/include/supercop
cp -v $DEPSRC/libsnark-supercop/include/* $DEPINST/include/supercop
cp -rv $DEPSRC/libsnark-supercop/lib $DEPINST/

View File

@ -9,7 +9,7 @@
#define BN128_INIT_HPP_
#include "algebra/curves/public_params.hpp"
#include "algebra/fields/fp.hpp"
#include "bn.h"
#include "bn.h" // If you're missing this file, run libsnark's ./prepare-depends.sh
namespace libsnark {