CXXFLAGS += -DNDEBUG -g2 -Os -fPIC -pipe

# The following options reduce code size, but breaks link or makes link very slow on some systems
# CXXFLAGS += -ffunction-sections -fdata-sections
# LDFLAGS += -Wl,--gc-sections
CXXFLAGS += -Wno-unused-function -Wno-unused-parameter -Wno-unused-variable
ARFLAGS = -cr	# ar needs the dash on OpenBSD
RANLIB ?= ranlib
CP = cp
MKDIR = mkdir
EGREP = egrep
CHMOD = chmod

CLANG_COMPILER = $(shell $(CXX) --version 2>&1 | $(EGREP) -i -c "clang")

IS_X86=0
IS_LINUX=0
IS_MINGW=0
IS_DARWIN=0
UNAME=CrossCompile

# Default prefix for make install
ifeq ($(PREFIX),)
PREFIX = /usr/local
endif

# iOS cross-compile configuration. Works in conjunction with IS_CROSS_COMPILE.
#   See http://www.cryptopp.com/wiki/iOS_(Command_Line).
ifeq ($(IS_IOS),1)
  CXX = clang++

  CXXFLAGS = -DNDEBUG -g2 -Os -fPIC -pipe
  CXXFLAGS += -DCRYPTOPP_DISABLE_ASM  $(IOS_FLAGS)
  CXXFLAGS += -arch $(IOS_ARCH) -isysroot $(IOS_SYSROOT)
  CXXFLAGS += -stdlib=libc++

  AR = libtool
  ARFLAGS = -static -o
endif

# Android cross-compile configuration. Works in conjunction with IS_CROSS_COMPILE.
#   See http://www.cryptopp.com/wiki/Android_(Command_Line).
ifeq ($(IS_ANDROID),1)
  # CPP, CXX, AR, RANLIB, LD, etc are set in 'setenv-android.sh'
  CXXFLAGS = -DNDEBUG -g2 -Os -pipe -fPIC
  CXXFLAGS += -DCRYPTOPP_DISABLE_ASM $(ANDROID_FLAGS)
  CXXFLAGS += --sysroot=$(ANDROID_SYSROOT) -I$(ANDROID_STL_INC)
  LDLIBS += $(ANDROID_STL_LIB)
endif

# ARM embedded cross-compile configuration. Works in conjunction with IS_CROSS_COMPILE.
#   See http://www.cryptopp.com/wiki/ARM_Embedded_(Command_Line)
#   and http://www.cryptopp.com/wiki/ARM_Embedded_(Bare Metal).
ifeq ($(IS_ARM_EMBEDDED),1)
  # CPP, CXX, AR, RANLIB, LD, etc are set in 'setenv-embedded.sh'
  CXXFLAGS = -DNDEBUG -g2 -Os -pipe -fPIC
  CXXFLAGS += -DCRYPTOPP_DISABLE_ASM $(ARM_EMBEDDED_FLAGS)
  CXXFLAGS += --sysroot=$(ARM_EMBEDDED_SYSROOT)
endif

ifneq ($(CLANG_COMPILER),0)
CXXFLAGS += -Wno-tautological-compare -Wno-unused-value
endif

SRCS = $(wildcard *.cpp)
ifeq ($(SRCS),)				# workaround wildcard function bug in GNU Make 3.77
SRCS = $(shell echo *.cpp)
endif

OBJS = $(SRCS:.cpp=.o)
# test.o needs to be after bench.o for cygwin 1.1.4 (possible ld bug?)
TESTOBJS = bench.o bench2.o test.o validat1.o validat2.o validat3.o adhoc.o datatest.o regtest.o fipsalgt.o dlltest.o
LIBOBJS = $(filter-out $(TESTOBJS),$(OBJS))

DLLSRCS = algebra.cpp algparam.cpp asn.cpp basecode.cpp cbcmac.cpp channels.cpp cryptlib.cpp des.cpp dessp.cpp dh.cpp dll.cpp dsa.cpp ec2n.cpp eccrypto.cpp ecp.cpp eprecomp.cpp files.cpp filters.cpp fips140.cpp fipstest.cpp gf2n.cpp gfpcrypt.cpp hex.cpp hmac.cpp integer.cpp iterhash.cpp misc.cpp modes.cpp modexppc.cpp mqueue.cpp nbtheory.cpp oaep.cpp osrng.cpp pch.cpp pkcspad.cpp pubkey.cpp queue.cpp randpool.cpp rdtables.cpp rijndael.cpp rng.cpp rsa.cpp sha.cpp simple.cpp skipjack.cpp strciphr.cpp trdlocal.cpp
DLLOBJS = $(DLLSRCS:.cpp=.export.o)
LIBIMPORTOBJS = $(LIBOBJS:.o=.import.o)
TESTIMPORTOBJS = $(TESTOBJS:.o=.import.o)
DLLTESTOBJS = dlltest.dllonly.o

all: cryptest.exe
static: libcryptopp.a
dynamic: libcryptopp.so

test: cryptest.exe
	./cryptest.exe v

clean:
	-$(RM) cryptest.exe libcryptopp.a libcryptopp.so $(LIBOBJS) $(DLLOBJS) $(TESTOBJS)
	-$(RM) -r *.dSYM

install:
	$(MKDIR) -p $(PREFIX)/include/cryptopp $(PREFIX)/lib $(PREFIX)/bin
	-$(CP) *.h $(PREFIX)/include/cryptopp
	-$(CP) *.a $(PREFIX)/lib
	-$(CP) *.so $(PREFIX)/lib
	-$(CP) *.exe $(PREFIX)/bin

remove:
	-$(RM) -rf $(PREFIX)/include/cryptopp
	-$(RM) $(PREFIX)/lib/libcryptopp.a
	-$(RM) $(PREFIX)/lib/libcryptopp.so
	-$(RM) $(PREFIX)/bin/cryptest.exe

libcryptopp.a: $(LIBOBJS)
	$(AR) $(ARFLAGS) $@ $(LIBOBJS)
	$(RANLIB) $@

libcryptopp.so: $(LIBOBJS)
	$(CXX) $(CXXFLAGS) -shared -o $@ $(LIBOBJS) $(LDFLAGS) $(LDLIBS)

cryptest.exe: libcryptopp.a $(TESTOBJS)
	$(CXX) -o $@ $(CXXFLAGS) $(TESTOBJS) ./libcryptopp.a $(LDFLAGS) $(LDLIBS)

adhoc.cpp: adhoc.cpp.proto
ifeq ($(wildcard adhoc.cpp),)
	cp adhoc.cpp.proto adhoc.cpp
else
	touch adhoc.cpp
endif

%.o : %.cpp
	$(CXX) $(CXXFLAGS) -c $<
