CXXFLAGS=$(shell pkg-config --cflags --libs libbitcoin) -ggdb
INCLUDES=
LIBS=$(shell pkg-config --libs libbitcoin)
LIBBITCOIN=$(shell pkg-config --libs libbitcoin)
BASE_MODULES= \
	network.o  \
	handshake.o  \
	exporter.o  \
	channel.o \
	serializer.o \
	logger.o \
	kernel.o \
	sha256.o \
	address.o \
	types.o \
	script.o \
	ripemd.o \
	block.o \
    elliptic_curve_key.o \
	transaction.o \
	error.o \
	threaded_service.o \
    validate.o \
	base58.o \
	big_number.o \
	clock.o \
	constants.o
MODULES=$(addprefix obj/, $(BASE_MODULES))
STORAGE_MODULES=$(addprefix obj/, $(STORAGE_BASE_MODULES))
#OBJECTS=$(addprefix obj/, $(notdir $(SOURCES:.cpp=.o)))
LIBBITCOIN=`pkg-config --libs libbitcoin`

#%.o: %.cpp
#	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $(INCLUDES) -c $(input) $(output)

#$(OBJECTS): $(SOURCES)
#	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

default: libbitcoin.so

lib/libbitcoin.so: $(MODULES) $(STORAGE_MODULES)
	$(CXX) -shared $(MODULES) $(STORAGE_MODULES) -o lib/libbitcoin.so

lib/libbitcoin.a: $(MODULES) $(STORAGE_MODULES)
	ar crf lib/libbitcoin.a $(MODULES) $(STORAGE_MODULES)

libbitcoin: lib/libbitcoin.so lib/libbitcoin.a

libbitcoin_no_storage: $(MODULES)
	ar crf lib/libbitcoin.a $(MODULES)
	$(CXX) -shared $(MODULES) -o lib/libbitcoin.so

obj/mktx.o: examples/subvertx/mktx.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/examples/subvertx/mktx: libbitcoin obj/mktx.o
	$(CXX) -o bin/examples/subvertx/mktx obj/mktx.o $(LIBBITCOIN) $(LIBS)

mktx: bin/examples/subvertx/mktx

obj/balance.o: examples/subvertx/balance.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/examples/subvertx/balance: libbitcoin obj/balance.o
	$(CXX) -o bin/examples/subvertx/balance obj/balance.o $(LIBBITCOIN) $(LIBS)

balance: bin/examples/subvertx/balance

obj/poller.o: examples/poller.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/examples/poller: libbitcoin obj/poller.o
	$(CXX) -o bin/examples/poller obj/poller.o $(LIBBITCOIN) $(LIBS)

obj/accounting.o: examples/accounting.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/examples/accounting: libbitcoin obj/accounting.o
	$(CXX) -o bin/examples/accounting obj/accounting.o $(LIBBITCOIN) $(LIBS)

accounting: bin/examples/accounting

obj/exporter-test.o: exporter-test.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/exporter-test: obj/exporter-test.o
	$(CXX) -o bin/tests/exporter-test obj/exporter-test.o $(LIBBITCOIN)

exporter-test: bin/tests/exporter-test

obj/irc-test.o: irc.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/irc: obj/irc-test.o
	$(CXX) -o bin/tests/irc obj/irc-test.o $(LIBBITCOIN)

irc: bin/tests/irc

obj/minikey.o: minikey.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/minikey: obj/minikey.o
	$(CXX) -o bin/tests/minikey obj/minikey.o $(LIBBITCOIN)

minikey: bin/tests/minikey

obj/scritp.o: scritp.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/scritp: obj/scritp.o
	$(CXX) -o bin/tests/scritp obj/scritp.o $(LIBBITCOIN)

scritp: bin/tests/scritp

obj/import-test.o: import-test.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/import-test: obj/import-test.o
	$(CXX) -o bin/tests/import-test obj/import-test.o $(LIBBITCOIN)

import-test: bin/tests/import-test

obj/base58-test.o: base58-test.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/base58-test: obj/base58-test.o
	$(CXX) -o bin/tests/base58-test obj/base58-test.o $(LIBBITCOIN)

base58-test: bin/tests/base58-test

obj/chanstop.o: chanstop.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/chanstop: obj/chanstop.o
	$(CXX) -o bin/tests/chanstop obj/chanstop.o $(LIBBITCOIN)

chanstop: bin/tests/chanstop

obj/protst.o: protst.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/protst: obj/protst.o
	$(CXX) -o bin/tests/protst obj/protst.o $(LIBBITCOIN)

protst: bin/tests/protst

obj/sesh.o: sesh.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $< -O0

bin/tests/sesh: obj/sesh.o
	$(CXX) -o bin/tests/sesh obj/sesh.o $(LIBBITCOIN)

sesh: bin/tests/sesh

obj/crap_main.o: crap/main.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/crap: obj/crap_main.o
	$(CXX) -o bin/tests/crap obj/crap_main.o $(LIBBITCOIN)

crap: bin/tests/crap

obj/priv.o: priv.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/priv: obj/priv.o
	$(CXX) -o bin/tests/priv obj/priv.o $(LIBBITCOIN)

priv: bin/tests/priv

obj/addr.o: addr.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/addr: obj/addr.o
	$(CXX) -o bin/tests/addr obj/addr.o $(LIBBITCOIN)

addr: bin/tests/addr

obj/corot.o: corot.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/corot: obj/corot.o
	$(CXX) -o bin/tests/corot obj/corot.o $(LIBBITCOIN)

corot: bin/tests/corot

obj/logtst.o: logtst.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/logtst: obj/logtst.o
	$(CXX) -o bin/tests/logtst obj/logtst.o $(LIBBITCOIN)

logtst: bin/tests/logtst

obj/mempool.o: mempool.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/mempool: obj/mempool.o
	$(CXX) -o bin/tests/mempool obj/mempool.o $(LIBBITCOIN)

mempool: bin/tests/mempool

obj/benchmark-chain.o: benchmark-chain.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/benchmark-chain: obj/benchmark-chain.o
	$(CXX) -o bin/tests/benchmark-chain obj/benchmark-chain.o $(LIBBITCOIN)

benchmark-chain: bin/tests/benchmark-chain

obj/kyot.o: kyot.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/kyot: obj/kyot.o
	$(CXX) -o bin/tests/kyot obj/kyot.o $(LIBBITCOIN)

kyot: bin/tests/kyot

obj/getx.o: getx.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/getx: obj/getx.o
	$(CXX) -o bin/tests/getx obj/getx.o $(LIBBITCOIN)

getx: bin/tests/getx

obj/determ.o: determ.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/determ: obj/determ.o
	$(CXX) -o bin/tests/determ obj/determ.o $(LIBBITCOIN)

determ: bin/tests/determ

obj/pol.o: pol.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/pol: obj/pol.o
	$(CXX) -o bin/tests/pol obj/pol.o $(LIBBITCOIN)

pol: bin/tests/pol

obj/1vaynert.o: 1vaynert.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/1vaynert: obj/1vaynert.o
	$(CXX) -o bin/tests/1vaynert obj/1vaynert.o $(LIBBITCOIN)

1vaynert: bin/tests/1vaynert

obj/hosts-test.o: hosts-test.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/hosts-test: obj/hosts-test.o
	$(CXX) -o bin/tests/hosts-test obj/hosts-test.o $(LIBBITCOIN)

hosts-test: bin/tests/hosts-test

obj/serial-test.o: serial-test.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/serial-test: obj/serial-test.o
	$(CXX) -o bin/tests/serial-test obj/serial-test.o $(LIBBITCOIN)

serial-test: bin/tests/serial-test

obj/echo.o: echo.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/echo: obj/echo.o
	$(CXX) -o bin/tests/echo obj/echo.o $(LIBBITCOIN)

echo: bin/tests/echo

obj/big-number-test.o: big-number-test.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/big-number-test: obj/big-number-test.o
	$(CXX) -o bin/tests/big-number-test obj/big-number-test.o $(LIBBITCOIN)

big-number-test: bin/tests/big-number-test

obj/tx-hash.o: tx-hash.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/subscribe.o: subscribe.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/subscribe: obj/subscribe.o
	$(CXX) -o bin/tests/subscribe obj/subscribe.o $(LIBBITCOIN)

subscribe: bin/tests/subscribe

obj/bdb-test.o: bdb-test.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/bdb-test: obj/bdb-test.o
	$(CXX) -o bin/tests/bdb-test obj/bdb-test.o $(LIBBITCOIN)

bdb-test: bin/tests/bdb-test

obj/bdb-last.o: bdb-last.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/bdb-last: obj/bdb-last.o
	$(CXX) -o bin/tests/bdb-last obj/bdb-last.o $(LIBBITCOIN)

bdb-last: bin/tests/bdb-last

obj/bdb-poller.o: bdb-poller.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/bdb-poller: obj/bdb-poller.o
	$(CXX) -o bin/tests/bdb-poller obj/bdb-poller.o $(LIBBITCOIN)

bdb-poller: bin/tests/bdb-poller

bin/tests/tx-hash: obj/tx-hash.o
	$(CXX) -o bin/tests/tx-hash obj/tx-hash.o $(LIBBITCOIN)

tx-hash: bin/tests/tx-hash

bin/tests/nettest: obj/nettest.o
	$(CXX) -o bin/tests/nettest obj/nettest.o $(LIBBITCOIN)

net: bin/tests/nettest

bin/tests/blockchain: libbitcoin.so obj/blockchain.o
	$(CXX) -o bin/tests/blockchain obj/blockchain.o $(LIBBITCOIN)

obj/script-test.o: script-test.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/script-test: obj/script-test.o
	$(CXX) -o bin/tests/script-test obj/script-test.o $(LIBBITCOIN)

script-test: bin/tests/script-test

obj/clock-test.o: clock-test.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/clock-test: obj/clock-test.o
	$(CXX) -o bin/tests/clock-test obj/clock-test.o $(LIBBITCOIN)

clock-test: bin/tests/clock-test

obj/merkle.o: merkle.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/merkle: obj/merkle.o
	$(CXX) -o bin/tests/merkle obj/merkle.o $(LIBBITCOIN)

merkle: bin/tests/merkle


block: src/block.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/ripemd.o: src/util/ripemd.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

sha256: src/util/sha256.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/logger.o: src/util/logger.cpp include/bitcoin/util/logger.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

transaction: src/transaction.cpp include/bitcoin/transaction.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

script: src/script.cpp include/bitcoin/script.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/block.o: src/block.cpp include/bitcoin/block.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/handshake.o: src/network/handshake.cpp include/bitcoin/network/network.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/network.o: src/network/network.cpp include/bitcoin/network/network.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/exporter.o: src/exporter.cpp include/bitcoin/exporter.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/channel.o: src/network/channel.cpp src/network/channel.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/benchmark.o: src/util/benchmark.cpp include/bitcoin/util/benchmark.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/sha256.o: src/util/sha256.cpp include/bitcoin/util/sha256.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/kernel.o: src/kernel.cpp include/bitcoin/kernel.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/error.o: src/error.cpp include/bitcoin/error.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/serializer.o: src/util/serializer.cpp include/bitcoin/util/serializer.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

libbitcoin.a: obj/serializer.o obj/error.o

obj/nettest.o: net.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/bdb_storage.o: src/storage/bdb/bdb_storage.cpp include/bitcoin/storage/bdb_storage.hpp 
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/postgresql_storage.o: src/storage/postgresql/postgresql_storage.cpp include/bitcoin/storage/postgresql_storage.hpp 
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/elliptic_curve_key.o: src/util/elliptic_curve_key.cpp include/bitcoin/util/elliptic_curve_key.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/validate.o: src/validate.cpp include/bitcoin/validate.hpp 
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<


obj/threaded_service.o: src/util/threaded_service.cpp include/bitcoin/util/threaded_service.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/address.o: src/address.cpp include/bitcoin/address.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/types.o: src/types.cpp include/bitcoin/types.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/gengen.o: gengen.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/gengen: obj/gengen.o obj/logger.o obj/sha256.o  obj/types.o obj/serializer.o obj/types.o
	$(CXX) -o bin/tests/gengen obj/gengen.o obj/logger.o obj/sha256.o  obj/types.o obj/serializer.o $(LIBS)

gengen: bin/tests/gengen

obj/script.o: src/script.cpp include/bitcoin/script.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/postbind.o: postbind.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/postbind: obj/postbind.o
	$(CXX) -o bin/tests/postbind obj/postbind.o $(LIBS)

postbind: bin/tests/postbind

obj/psql.o: psql.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/psql: obj/postgresql_storage.o obj/psql.o obj/logger.o obj/script.o obj/ripemd.o obj/block.o obj/serializer.o obj/sha256.o obj/types.o obj/transaction.o obj/error.o obj/elliptic_curve_key.o obj/threaded_service.o
	$(CXX) -o bin/tests/psql obj/psql.o obj/postgresql_storage.o obj/logger.o obj/script.o obj/ripemd.o obj/block.o obj/serializer.o obj/sha256.o obj/types.o obj/transaction.o obj/error.o obj/elliptic_curve_key.o obj/threaded_service.o $(LIBS)

psql: bin/tests/psql

obj/types-test.o: types-test.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/types-test: obj/types.o obj/types-test.o
	$(CXX) -o bin/tests/types-test obj/types.o obj/types-test.o $(LIBS)

types-test: bin/tests/types-test

obj/transaction.o: src/transaction.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/block-hash.o: block-hash.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/block-hash: obj/block-hash.o
	$(CXX) -o bin/tests/block-hash obj/block-hash.o $(LIBBITCOIN) $(LIBS)

block-hash: bin/tests/block-hash

obj/raw_tx7208e5.o: bip16/raw_tx7208e5.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/raw_tx7208e5: obj/raw_tx7208e5.o
	$(CXX) -o bin/tests/raw_tx7208e5 obj/raw_tx7208e5.o $(LIBBITCOIN) -lssl -lcrypto

raw_tx7208e5: bin/tests/raw_tx7208e5

obj/checksig.o: checksig.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/checksig: obj/checksig.o
	$(CXX) -o bin/tests/checksig obj/checksig.o $(LIBBITCOIN) -lssl -lcrypto

checksig: bin/tests/checksig

obj/feeder.o: feeder.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/feeder: obj/feeder.o
	$(CXX) -o bin/tests/feeder obj/feeder.o $(LIBBITCOIN) -lssl -lcrypto

feeder: bin/tests/feeder

obj/hmac512.o: hmac512.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/hmac512: obj/hmac512.o
	$(CXX) -o bin/tests/hmac512 obj/hmac512.o $(LIBBITCOIN) -lssl -lcrypto

hmac512: bin/tests/hmac512

obj/ec-key.o: ec-key.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/ec-key: obj/ec-key.o
	$(CXX) -o bin/tests/ec-key obj/ec-key.o $(LIBBITCOIN)

ec-key: bin/tests/ec-key

obj/clock.o: src/util/clock.cpp include/bitcoin/util/clock.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/constants.o: src/constants.cpp include/bitcoin/constants.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/validate-block.o: validate-block.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

bin/tests/validate-block: obj/validate-block.o obj/postgresql_storage.o obj/logger.o obj/serializer.o obj/elliptic_curve_key.o obj/sha256.o obj/ripemd.o obj/types.o obj/block.o obj/error.o obj/validate.o obj/exporter.o obj/constants.o obj/big_number.o obj/clock.o
	$(CXX) -o bin/tests/validate-block obj/validate-block.o obj/postgresql_storage.o obj/transaction.o obj/script.o obj/logger.o obj/serializer.o obj/elliptic_curve_key.o obj/sha256.o obj/ripemd.o obj/types.o obj/block.o obj/error.o obj/validate.o obj/threaded_service.o obj/exporter.o obj/constants.o obj/big_number.o obj/clock.o $(LIBS)

validate-block: bin/tests/validate-block

obj/base58.o: src/util/base58.cpp include/bitcoin/util/base58.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/big_number.o: src/util/big_number.cpp include/bitcoin/util/big_number.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<


poller: bin/examples/poller

obj/postgresql_blockchain.o: src/storage/postgresql/postgresql_blockchain.cpp src/storage/postgresql/postgresql_blockchain.hpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

obj/blockchain.o: blockchain.cpp
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

blockchain: bin/tests/blockchain

