Code cleanup
[novacoin.git] / src / makefile.unix
1 # Copyright (c) 2009-2010 Satoshi Nakamoto
2 # Distributed under the MIT/X11 software license, see the accompanying
3 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 USE_UPNP:=0
6 USE_LEVELDB:=0
7 USE_IPV6:=1
8
9 LINK:=$(CXX)
10 ARCH:=$(system lscpu | head -n 1 | awk '{print $2}')
11
12 DEFS=-DBOOST_SPIRIT_THREADSAFE -D__STDC_FORMAT_MACROS
13
14 DEFS += $(addprefix -I,$(CURDIR) $(CURDIR)/obj $(BOOST_INCLUDE_PATH) $(BDB_INCLUDE_PATH) $(OPENSSL_INCLUDE_PATH))
15 LIBS = $(addprefix -L,$(BOOST_LIB_PATH) $(BDB_LIB_PATH) $(OPENSSL_LIB_PATH))
16
17 LMODE = dynamic
18 LMODE2 = dynamic
19 ifdef STATIC
20         LMODE = static
21         ifeq (${STATIC}, all)
22                 LMODE2 = static
23         endif
24 endif
25
26 # for boost 1.37, add -mt to the boost libraries
27 LIBS += \
28  -Wl,-B$(LMODE) \
29    -l boost_system$(BOOST_LIB_SUFFIX) \
30    -l boost_filesystem$(BOOST_LIB_SUFFIX) \
31    -l boost_program_options$(BOOST_LIB_SUFFIX) \
32    -l boost_thread$(BOOST_LIB_SUFFIX) \
33    -l db_cxx$(BDB_LIB_SUFFIX) \
34    -l ssl \
35    -l crypto
36
37 ifndef USE_UPNP
38         override USE_UPNP = -
39 endif
40 ifneq (${USE_UPNP}, -)
41         LIBS += -l miniupnpc
42         DEFS += -DUSE_UPNP=$(USE_UPNP)
43 endif
44
45 ifneq (${USE_IPV6}, -)
46         DEFS += -DUSE_IPV6=$(USE_IPV6)
47 endif
48
49 LIBS+= \
50  -Wl,-B$(LMODE2) \
51    -l z \
52    -l dl \
53    -l pthread
54
55
56 # Hardening
57 # Make some classes of vulnerabilities unexploitable in case one is discovered.
58 #
59     # This is a workaround for Ubuntu bug #691722, the default -fstack-protector causes
60     # -fstack-protector-all to be ignored unless -fno-stack-protector is used first.
61     # see: https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/691722
62     HARDENING=-fno-stack-protector
63
64     # Stack Canaries
65     # Put numbers at the beginning of each stack frame and check that they are the same.
66     # If a stack buffer if overflowed, it writes over the canary number and then on return
67     # when that number is checked, it won't be the same and the program will exit with
68     # a "Stack smashing detected" error instead of being exploited.
69     HARDENING+=-fstack-protector-all -Wstack-protector
70
71     # Make some important things such as the global offset table read only as soon as
72     # the dynamic linker is finished building it. This will prevent overwriting of addresses
73     # which would later be jumped to.
74     LDHARDENING+=-Wl,-z,relro -Wl,-z,now
75
76     # Build position independent code to take advantage of Address Space Layout Randomization
77     # offered by some kernels.
78     # see doc/build-unix.txt for more information.
79     ifdef PIE
80         HARDENING+=-fPIE
81         LDHARDENING+=-pie
82     endif
83
84     # -D_FORTIFY_SOURCE=2 does some checking for potentially exploitable code patterns in
85     # the source such overflowing a statically defined buffer.
86     HARDENING+=-D_FORTIFY_SOURCE=2
87 #
88
89
90 DEBUGFLAGS=-g
91
92
93 ifeq (${ARCH}, i686)
94     EXT_OPTIONS=-msse2
95 endif
96
97
98 # CXXFLAGS can be specified on the make command line, so we use xCXXFLAGS that only
99 # adds some defaults in front. Unfortunately, CXXFLAGS=... $(CXXFLAGS) does not work.
100 xCXXFLAGS=-O2 $(EXT_OPTIONS) -pthread -Wall -Wextra -Wno-ignored-qualifiers -Wformat -Wformat-security -Wno-unused-parameter \
101     $(DEBUGFLAGS) $(DEFS) $(HARDENING) $(CXXFLAGS)
102
103 # LDFLAGS can be specified on the make command line, so we use xLDFLAGS that only
104 # adds some defaults in front. Unfortunately, LDFLAGS=... $(LDFLAGS) does not work.
105 xLDFLAGS=$(LDHARDENING) $(LDFLAGS)
106
107 OBJS= \
108     obj/alert.o \
109     obj/version.o \
110     obj/checkpoints.o \
111     obj/netbase.o \
112     obj/addrman.o \
113     obj/crypter.o \
114     obj/key.o \
115     obj/db.o \
116     obj/init.o \
117     obj/irc.o \
118     obj/keystore.o \
119     obj/miner.o \
120     obj/main.o \
121     obj/net.o \
122     obj/stun.o \
123     obj/protocol.o \
124     obj/bitcoinrpc.o \
125     obj/rpcdump.o \
126     obj/rpcnet.o \
127     obj/rpcmining.o \
128     obj/rpcwallet.o \
129     obj/rpcblockchain.o \
130     obj/rpcrawtransaction.o \
131     obj/script.o \
132     obj/sync.o \
133     obj/util.o \
134     obj/wallet.o \
135     obj/walletdb.o \
136     obj/noui.o \
137     obj/kernel.o \
138     obj/pbkdf2.o \
139     obj/scrypt.o
140
141 all: novacoind
142
143 #
144 # LevelDB support
145 #
146 ifeq (${USE_LEVELDB}, 1)
147 LIBS += $(CURDIR)/leveldb/libleveldb.a $(CURDIR)/leveldb/libmemenv.a
148 DEFS += $(addprefix -I,$(CURDIR)/leveldb/include) -DUSE_LEVELDB
149 DEFS += $(addprefix -I,$(CURDIR)/leveldb/helpers)
150 OBJS += obj/txdb-leveldb.o
151 leveldb/libleveldb.a:
152         @echo "Building LevelDB ..."; cd leveldb; make libleveldb.a libmemenv.a; cd ..;
153 obj/txdb-leveldb.o: leveldb/libleveldb.a
154 endif
155 ifneq (${USE_LEVELDB}, 1)
156 OBJS += obj/txdb-bdb.o
157 endif
158
159 ifeq (${USE_ASM}, 1)
160 OBJS += obj/scrypt-arm.o obj/scrypt-x86.o obj/scrypt-x86_64.o
161
162 obj/scrypt-x86.o: scrypt-x86.S
163         $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $<
164
165 obj/scrypt-x86_64.o: scrypt-x86_64.S
166         $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $<
167
168 obj/scrypt-arm.o: scrypt-arm.S
169         $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $<
170 endif
171 ifneq (${USE_ASM}, 1)
172 OBJS += obj/scrypt-generic.o
173
174 obj/scrypt-generic.o: scrypt-generic.c
175         $(CC) -c $(xCXXFLAGS) -MMD -o $@ $<
176 endif
177
178 # auto-generated dependencies:
179 -include obj/*.P
180
181 obj/build.h: FORCE
182         /bin/sh ../share/genbuild.sh obj/build.h
183 version.cpp: obj/build.h
184 DEFS += -DHAVE_BUILD_INFO
185
186
187 obj/%.o: %.cpp
188         $(CXX) -c $(xCXXFLAGS) -MMD -MF $(@:%.o=%.d) -o $@ $<
189         @cp $(@:%.o=%.d) $(@:%.o=%.P); \
190           sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
191               -e '/^$$/ d' -e 's/$$/ :/' < $(@:%.o=%.d) >> $(@:%.o=%.P); \
192           rm -f $(@:%.o=%.d)
193
194 novacoind: $(OBJS:obj/%=obj/%)
195         $(LINK) $(xCXXFLAGS) -o $@ $^ $(xLDFLAGS) $(LIBS)
196
197 clean:
198         -rm -f novacoind
199         -rm -f obj/*.o
200         -rm -f obj/*.P
201         -rm -f obj/build.h
202
203 FORCE: