Makefile.bsd 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. CC?=cc
  2. AS=$(CC) -c
  3. AR?=ar
  4. # are we running on an X86 machine?
  5. X86_ARCH=no
  6. MACHINE=$(shell uname -m | sed 's/i.86/i386/g')
  7. ifeq ($(MACHINE), i386)
  8. X86_ARCH=yes
  9. endif
  10. ifeq ($(MACHINE), amd64)
  11. X86_ARCH=yes
  12. endif
  13. # SIMD is only supported on X86
  14. ifeq ($(X86_ARCH), no)
  15. SIMD=none
  16. endif
  17. ifeq ($(SIMD), sse)
  18. SIMD=ssse3
  19. endif
  20. # set CFLAGS depending on SIMD
  21. CFLAGS?=-g
  22. CFLAGS+=-Wall -Wextra -Wno-unused-parameter
  23. ifeq ($(SIMD), ssse3)
  24. CFLAGS+=-mssse3
  25. else ifeq ($(SIMD), avx2)
  26. CFLAGS+=-mssse3 -mavx2
  27. else ifneq ($(SIMD), none)
  28. SIMD=auto
  29. CFLAGS+=-DNTRU_DETECT_SIMD
  30. endif
  31. # use -march=native if we're compiling 'bench' for x86 and SIMD=auto
  32. BENCH_ARCH_OPTION=
  33. ifeq ($(SIMD), auto)
  34. BENCH_ARCH_OPTION=-march=native
  35. endif
  36. OPTFLAGS=-O2
  37. bench: OPTFLAGS=-O3 $(BENCH_ARCH_OPTION)
  38. CFLAGS+=$(OPTFLAGS)
  39. ifneq ($(shell uname), OpenBSD)
  40. LIBS+=-lrt
  41. endif
  42. SRCDIR=src
  43. TESTDIR=tests
  44. LIB_OBJS=bitstring.o encparams.o hash.o idxgen.o key.o mgf.o ntru.o poly.o rand.o arith.o sha1.o sha2.o nist_ctr_drbg.o rijndael.o
  45. ifneq ($(SIMD), none)
  46. LIB_OBJS+=sha1-mb-x86_64.o sha256-mb-x86_64.o hash_simd.o poly_ssse3.o
  47. ifneq ($(SIMD), ssse3)
  48. LIB_OBJS+=poly_avx2.o
  49. endif
  50. endif
  51. TEST_OBJS=test_bitstring.o test_hash.o test_idxgen.o test_key.o test_ntruprime.o test_ntru.o test.o test_poly.o test_util.o
  52. VERSION=0.5
  53. INST_PFX=/usr
  54. INST_LIBDIR=$(INST_PFX)/lib
  55. INST_INCLUDE=$(INST_PFX)/include/libntru
  56. INST_DOCDIR=$(INST_PFX)/share/doc/libntru-$(VERSION)
  57. INST_HEADERS=ntru.h types.h key.h encparams.h hash.h rand.h err.h
  58. PERL=/usr/bin/perl
  59. PERLASM_SCHEME=elf
  60. LIB_OBJS_PATHS=$(patsubst %,$(SRCDIR)/%,$(LIB_OBJS))
  61. TEST_OBJS_PATHS=$(patsubst %,$(TESTDIR)/%,$(TEST_OBJS))
  62. DIST_NAME=libntru-$(VERSION)
  63. MAKEFILENAME=$(lastword $(MAKEFILE_LIST))
  64. .PHONY: all lib install uninstall dist test clean distclean
  65. all: lib
  66. lib: libntru.so
  67. static-lib: libntru.a
  68. libntru.so: $(LIB_OBJS_PATHS)
  69. $(CC) $(CFLAGS) $(CPPFLAGS) -shared -Wl,-soname,libntru.so -o libntru.so $(LIB_OBJS_PATHS) $(LDFLAGS) $(LIBS)
  70. libntru.a: $(LIB_OBJS_PATHS)
  71. $(AR) cru libntru.a $(LIB_OBJS_PATHS)
  72. install: install-lib install-doc install-headers
  73. install-lib: lib
  74. test -d "$(DESTDIR)$(INST_LIBDIR)" || mkdir -p "$(DESTDIR)$(INST_LIBDIR)"
  75. install -m 0755 libntru.so "$(DESTDIR)$(INST_LIBDIR)/libntru.so"
  76. install-static-lib: static-lib
  77. test -d "$(DESTDIR)$(INST_LIBDIR)" || mkdir -p "$(DESTDIR)$(INST_LIBDIR)"
  78. install -m 0755 libntru.a "$(DESTDIR)$(INST_LIBDIR)/libntru.a"
  79. install-doc:
  80. test -d "$(DESTDIR)$(INST_DOCDIR)" || mkdir -p "$(DESTDIR)$(INST_DOCDIR)"
  81. install -m 0644 README.md "$(DESTDIR)$(INST_DOCDIR)/README.md"
  82. install-headers:
  83. test -d "$(DESTDIR)$(INST_INCLUDE)" || mkdir -p "$(DESTDIR)$(INST_INCLUDE)"
  84. for header in $(INST_HEADERS) ; do \
  85. install -m 0644 "$(SRCDIR)/$$header" "$(DESTDIR)$(INST_INCLUDE)/" ; \
  86. done
  87. uninstall: uninstall-lib uninstall-doc uninstall-headers
  88. uninstall-lib:
  89. rm -f "$(DESTDIR)$(INST_LIBDIR)/libntru.so"
  90. uninstall-static-lib:
  91. rm -f "$(DESTDIR)$(INST_LIBDIR)/libntru.a"
  92. uninstall-doc:
  93. rm -f "$(DESTDIR)$(INST_DOCDIR)/README.md"
  94. rmdir "$(DESTDIR)$(INST_DOCDIR)/"
  95. uninstall-headers:
  96. for header in $(INST_HEADERS) ; do \
  97. rm "$(DESTDIR)$(INST_INCLUDE)/$$header" ; \
  98. done
  99. rmdir "$(DESTDIR)$(INST_INCLUDE)/"
  100. dist:
  101. rm -rf $(DIST_NAME)
  102. mkdir $(DIST_NAME)
  103. mkdir $(DIST_NAME)/$(SRCDIR)
  104. mkdir $(DIST_NAME)/$(TESTDIR)
  105. cp Makefile Makefile.win Makefile.osx README.md LICENSE PATENTS $(DIST_NAME)
  106. cp $(SRCDIR)/*.c $(DIST_NAME)/$(SRCDIR)
  107. cp $(SRCDIR)/*.h $(DIST_NAME)/$(SRCDIR)
  108. cp $(TESTDIR)/*.c $(DIST_NAME)/$(TESTDIR)
  109. cp $(TESTDIR)/*.h $(DIST_NAME)/$(TESTDIR)
  110. tar cf $(DIST_NAME).tar.xz $(DIST_NAME) --lzma
  111. rm -rf $(DIST_NAME)
  112. test:
  113. $(MAKE) -f $(MAKEFILENAME) testnoham
  114. @echo
  115. @echo Testing patent-reduced build
  116. LD_LIBRARY_PATH=. ./testnoham
  117. $(MAKE) -f $(MAKEFILENAME) testham
  118. @echo
  119. @echo Testing full build
  120. LD_LIBRARY_PATH=. ./testham
  121. testham: clean lib $(TEST_OBJS_PATHS)
  122. @echo CFLAGS=$(CFLAGS)
  123. $(CC) $(CFLAGS) -o testham $(TEST_OBJS_PATHS) -L. -lntru -lm
  124. testnoham: CFLAGS += -DNTRU_AVOID_HAMMING_WT_PATENT
  125. testnoham: clean lib $(TEST_OBJS_PATHS)
  126. @echo CFLAGS=$(CFLAGS)
  127. $(CC) $(CFLAGS) -o testnoham $(TEST_OBJS_PATHS) -L. -lntru -lm
  128. bench: static-lib
  129. $(CC) $(CFLAGS) $(CPPFLAGS) -o bench $(SRCDIR)/bench.c $(LDFLAGS) $(LIBS) -L. -lntru
  130. hybrid: static-lib
  131. $(CC) $(CFLAGS) $(CPPFLAGS) -o hybrid $(SRCDIR)/hybrid.c $(LDFLAGS) $(LIBS) -L. -lntru -lsodium
  132. $(SRCDIR)/%.o: $(SRCDIR)/%.c
  133. $(CC) $(CFLAGS) $(CPPFLAGS) -c -fPIC $< -o $@
  134. $(SRCDIR)/poly_ssse3.o: $(SRCDIR)/poly_ssse3.c
  135. $(CC) $(CFLAGS) $(CPPFLAGS) -mssse3 -c -fPIC $(SRCDIR)/poly_ssse3.c -o $(SRCDIR)/poly_ssse3.o
  136. $(SRCDIR)/poly_avx2.o: $(SRCDIR)/poly_avx2.c
  137. $(CC) $(CFLAGS) $(CPPFLAGS) -mavx2 -c -fPIC $(SRCDIR)/poly_avx2.c -o $(SRCDIR)/poly_avx2.o
  138. $(SRCDIR)/hash_simd.o: $(SRCDIR)/hash_simd.c
  139. $(CC) $(CFLAGS) $(CPPFLAGS) -mssse3 -c -fPIC $(SRCDIR)/hash_simd.c -o $(SRCDIR)/hash_simd.o
  140. $(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
  141. $(SRCDIR)/sha1-mb-x86_64.o: $(SRCDIR)/sha1-mb-x86_64.s
  142. $(AS) $(SRCDIR)/sha1-mb-x86_64.s -o $@
  143. $(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
  144. $(SRCDIR)/sha256-mb-x86_64.o: $(SRCDIR)/sha256-mb-x86_64.s
  145. $(AS) $(SRCDIR)/sha256-mb-x86_64.s -o $@
  146. tests/%.o: tests/%.c
  147. $(CC) $(CFLAGS) $(CPPFLAGS) -fPIC -I$(SRCDIR) -c $< -o $@
  148. clean:
  149. @# also clean files generated on other OSes
  150. rm -f $(SRCDIR)/*.o $(SRCDIR)/*.s $(TESTDIR)/*.o libntru.so libntru.a libntru.dylib libntru.dll testham testnoham testham.exe testnoham.exe bench bench.exe hybrid hybrid.exe
  151. distclean: clean
  152. rm -rf $(DIST_NAME)
  153. rm -f $(DIST_NAME).tar.xz $(DIST_NAME).zip