From 35ec4dfb326b3ed5c31f0cf56edc750d9dae4e73 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Thu, 21 Jul 2016 21:04:39 +0000 Subject: [PATCH 1/5] Basic autotools library build framework --- .gitignore | 20 ++++++++++- Makefile.am | 54 ++++++++++++++++++++++++++++++ autogen.sh | 11 ++++++ configure.ac | 70 +++++++++++++++++++++++++++++++++++++++ libbreadwallet-core.pc.in | 12 +++++++ 5 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 Makefile.am create mode 100755 autogen.sh create mode 100644 configure.ac create mode 100644 libbreadwallet-core.pc.in diff --git a/.gitignore b/.gitignore index 77efe9de8..b868ccfb3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,21 @@ +*.kate-swp +.deps +Makefile +Makefile.in +aclocal.m4 +ar-lib +autom4te.cache +compile +config.* +configure +depcomp +install-sh +libbreadwallet-core.pc +libtool +ltmain.sh +missing +.libs + # Object files *.o *.ko @@ -35,4 +53,4 @@ *.xcworkspace *.xcodeproj -.idea/ \ No newline at end of file +.idea/ diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 000000000..6c2d76105 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,54 @@ +# Copyright 2014-2016 Luke Dashjr +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the standard MIT license. See COPYING for more details. + +lib_LTLIBRARIES = libbreadwallet-core.la +libbreadwallet_core_la_SOURCES = \ + BRAddress.c \ + BRBIP32Sequence.c \ + BRBIP38Key.c \ + BRBIP39Mnemonic.c \ + BRBase58.c \ + BRBloomFilter.c \ + BRCrypto.c \ + BRKey.c \ + BRMerkleBlock.c \ + BRPaymentProtocol.c \ + BRPeer.c \ + BRPeerManager.c \ + BRSet.c \ + BRTransaction.c \ + BRWallet.c + +libbreadwallet_core_la_CFLAGS = $(libsecp256k1_CFLAGS) $(PTHREAD_CFLAGS) +libbreadwallet_core_la_LDFLAGS = -version-info $(LIBBREADWALLET_CORE_SO_VERSION) -no-undefined +libbreadwallet_core_la_LIBADD = $(libsecp256k1_LIBS) $(PTHREAD_LIBS) + +libbreadwallet_core_includedir = $(includedir) +libbreadwallet_core_include_HEADERS = \ + BRAddress.h \ + BRArray.h \ + BRBIP32Sequence.h \ + BRBIP38Key.h \ + BRBIP39Mnemonic.h \ + BRBIP39WordsEn.h \ + BRBase58.h \ + BRBloomFilter.h \ + BRCrypto.h \ + BRInt.h \ + BRKey.h \ + BRList.h \ + BRMerkleBlock.h \ + BRPaymentProtocol.h \ + BRPeer.h \ + BRPeerManager.h \ + BRSet.h \ + BRTransaction.h \ + BRWallet.h + +pkgconfigdir = $(libdir)/pkgconfig +pkgconfig_DATA = libbreadwallet-core.pc + +dist_noinst_SCRIPTS = autogen.sh +dist_doc_DATA = LICENSE README.md diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 000000000..0e2f2d143 --- /dev/null +++ b/autogen.sh @@ -0,0 +1,11 @@ +#!/bin/sh -e +# Written by Luke Dashjr in 2012 +# This program is released under the terms of the Creative Commons "CC0 1.0 Universal" license and/or copyright waiver. + +if test -z "$srcdir"; then + srcdir=`dirname "$0"` + if test -z "$srcdir"; then + srcdir=. + fi +fi +autoreconf --force --install --verbose "$srcdir" diff --git a/configure.ac b/configure.ac new file mode 100644 index 000000000..26e2f1e63 --- /dev/null +++ b/configure.ac @@ -0,0 +1,70 @@ +dnl * Copyright 2012-2016 Luke Dashjr +dnl * +dnl * This program is free software; you can redistribute it and/or modify it +dnl * under the terms of the standard MIT license. See COPYING for more details. + +AC_INIT( + [libbreadwallet-core], + [0.1.0], + [https://github.com/breadwallet/breadwallet-core/issues], + [libbreadwallet-core]) +AC_CONFIG_AUX_DIR([.]) +AC_PREREQ([2.59]) +AM_INIT_AUTOMAKE([1.11 -Wall dist-xz foreign]) +m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) + +AC_PROG_CC_C99 +m4_ifdef([AM_PROG_AR], [AM_PROG_AR]) +LT_INIT([disable-static]) + +# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html +AC_SUBST([LIBBREADWALLET_CORE_SO_VERSION], [0:0:0]) + +AC_CONFIG_FILES([Makefile + libbreadwallet-core.pc:libbreadwallet-core.pc.in +]) + +m4_define([BFG_PTHREAD_FLAG_CHECK], + AC_MSG_CHECKING([for $1]) + save_CFLAGS="${CFLAGS}" + save_LIBS="${LIBS}" + found_pthread=false + for cflag in ' -pthread' ''; do + for lib in ' -lpthread' ' -lwinpthread' ''; do + CFLAGS="${save_CFLAGS}${cflag}" + LIBS="${save_LIBS}${lib}" + AC_LINK_IFELSE([ + AC_LANG_PROGRAM([ + #include + ], [ + void *f = $1; + ]) + ], [ + found_pthread=true + PTHREAD_FLAGS="${cflag}" + PTHREAD_LIBS="${lib}" + if test "x${cflag}${lib}" = "x"; then + AC_MSG_RESULT([yes]) + else + AC_MSG_RESULT([with${cflag}${lib}]) + fi + $2 + break 2 + ]) + done + done + CFLAGS="${save_CFLAGS}" + LIBS="${save_LIBS}" + if test "x${found_pthread}" = "xfalse"; then + AC_MSG_RESULT([no]) + fi + AC_SUBST([PTHREAD_FLAGS]) + AC_SUBST([PTHREAD_LIBS]) +) +BFG_PTHREAD_FLAG_CHECK([pthread_create]) + +AC_SEARCH_LIBS([log],[m]) + +PKG_CHECK_MODULES([libsecp256k1],[libsecp256k1]) + +AC_OUTPUT diff --git a/libbreadwallet-core.pc.in b/libbreadwallet-core.pc.in new file mode 100644 index 000000000..19428a2e4 --- /dev/null +++ b/libbreadwallet-core.pc.in @@ -0,0 +1,12 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: @PACKAGE_NAME@ +Description: Library for Bitcoin's base58 encoding. +Version: @PACKAGE_VERSION@ +Libs: -L${libdir} -lbase58 +Cflags: -I${includedir} +Requires.private: libsecp256k1 +Libs.private: @LIBS@ @PTHREAD_LIBS@ From 5108c2d582728a8be64af201d277fb52ede7f53a Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Thu, 21 Jul 2016 21:09:26 +0000 Subject: [PATCH 2/5] Bugfix: Missing includes and header fixes --- BRKey.c | 4 ++-- BRKey.h | 2 ++ BRWallet.c | 2 +- test.c | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/BRKey.c b/BRKey.c index 75eae308f..f03468f77 100644 --- a/BRKey.c +++ b/BRKey.c @@ -49,8 +49,8 @@ #pragma GCC diagnostic ignored "-Wunused-function" #pragma clang diagnostic ignored "-Wconditional-uninitialized" #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" -#include "secp256k1/src/basic-config.h" -#include "secp256k1/src/secp256k1.c" +#include +#include #pragma clang diagnostic pop #pragma GCC diagnostic pop diff --git a/BRKey.h b/BRKey.h index 4afa215c0..38fd3f52c 100644 --- a/BRKey.h +++ b/BRKey.h @@ -25,6 +25,8 @@ #ifndef BRKey_h #define BRKey_h +#include + #include "BRInt.h" #ifdef __cplusplus diff --git a/BRWallet.c b/BRWallet.c index 742871753..13ee47056 100644 --- a/BRWallet.c +++ b/BRWallet.c @@ -31,7 +31,7 @@ #include #include #include -#include +#include struct BRWalletStruct { uint64_t balance, totalSent, totalReceived, feePerKb, *balanceHist; diff --git a/test.c b/test.c index ec292c61f..4c5af9d67 100644 --- a/test.c +++ b/test.c @@ -31,7 +31,7 @@ #include "BRAddress.h" #include "BRBase58.h" #include "BRBIP39Mnemonic.h" -#include "BRBIP39WordsEn.H" +#include "BRBIP39WordsEn.h" #include "BRPeer.h" #include "BRPeerManager.h" #include "BRPaymentProtocol.h" From 3c74fe236f96aa5e8eca81e1b8b2c3403a1daf1b Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Thu, 21 Jul 2016 21:15:48 +0000 Subject: [PATCH 3/5] Bugfix: Convert set_u64{b,l}e to inline functions to ensure x is uint64_t time() is not on 32-bit platforms --- BRInt.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/BRInt.h b/BRInt.h index 3a0f40119..271144ee9 100644 --- a/BRInt.h +++ b/BRInt.h @@ -176,12 +176,18 @@ union _u256 { uint8_t u8[256/8]; }; #define set_u32le(r, x) (*(union _u32 *)(r) =\ (union _u32) { (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }) -#define set_u64be(r, x) (*(union _u64 *)(r) =\ - (union _u64) { ((x) >> 56) & 0xff, ((x) >> 48) & 0xff, ((x) >> 40) & 0xff, ((x) >> 32) & 0xff,\ - ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, ((x) >> 8) & 0xff, (x) & 0xff }) -#define set_u64le(r, x) (*(union _u64 *)(r) =\ - (union _u64) { (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, ((x) >> 24) & 0xff,\ - ((x) >> 32) & 0xff, ((x) >> 40) & 0xff, ((x) >> 48) & 0xff, ((x) >> 56) & 0xff }) +static inline void set_u64be(void * const r, const uint64_t x) { + *(union _u64 *)r = (union _u64) { + ((x) >> 56) & 0xff, ((x) >> 48) & 0xff, ((x) >> 40) & 0xff, ((x) >> 32) & 0xff, + ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, ((x) >> 8) & 0xff, (x) & 0xff + }; +} +static inline void set_u64le(void * const r, const uint64_t x) { + *(union _u64 *)r = (union _u64) { + (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, ((x) >> 24) & 0xff, + ((x) >> 32) & 0xff, ((x) >> 40) & 0xff, ((x) >> 48) & 0xff, ((x) >> 56) & 0xff + }; +} #define set_u128(r, x) (*(union _u128 *)(r) =\ (union _u128) { (x).u8[0], (x).u8[1], (x).u8[2], (x).u8[3], (x).u8[4], (x).u8[5], (x).u8[6], (x).u8[7],\ From 8b1a76ee40c1d808cd780dcb7b9724cd21ae5a13 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Thu, 21 Jul 2016 21:17:34 +0000 Subject: [PATCH 4/5] Install headers into a subdirectory --- Makefile.am | 2 +- libbreadwallet-core.pc.in | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile.am b/Makefile.am index 6c2d76105..13b26b282 100644 --- a/Makefile.am +++ b/Makefile.am @@ -25,7 +25,7 @@ libbreadwallet_core_la_CFLAGS = $(libsecp256k1_CFLAGS) $(PTHREAD_CFLAGS) libbreadwallet_core_la_LDFLAGS = -version-info $(LIBBREADWALLET_CORE_SO_VERSION) -no-undefined libbreadwallet_core_la_LIBADD = $(libsecp256k1_LIBS) $(PTHREAD_LIBS) -libbreadwallet_core_includedir = $(includedir) +libbreadwallet_core_includedir = $(includedir)/breadwallet-core libbreadwallet_core_include_HEADERS = \ BRAddress.h \ BRArray.h \ diff --git a/libbreadwallet-core.pc.in b/libbreadwallet-core.pc.in index 19428a2e4..b89fe4be8 100644 --- a/libbreadwallet-core.pc.in +++ b/libbreadwallet-core.pc.in @@ -7,6 +7,6 @@ Name: @PACKAGE_NAME@ Description: Library for Bitcoin's base58 encoding. Version: @PACKAGE_VERSION@ Libs: -L${libdir} -lbase58 -Cflags: -I${includedir} +Cflags: -I${includedir}/breadwallet-core Requires.private: libsecp256k1 Libs.private: @LIBS@ @PTHREAD_LIBS@ From 6db819bf51c03943049abbc56fa5b357c1409a80 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Thu, 21 Jul 2016 21:46:32 +0000 Subject: [PATCH 5/5] BRLocalAmount: Do multiplication before division to improve accuracy 32-bit x86 was getting 49999 instead of 50000 for tests --- BRWallet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BRWallet.c b/BRWallet.c index 13ee47056..2c8072e58 100644 --- a/BRWallet.c +++ b/BRWallet.c @@ -1159,7 +1159,7 @@ void BRWalletFree(BRWallet *wallet) // price is local currency units per bitcoin int64_t BRLocalAmount(int64_t amount, double price) { - int64_t localAmount = llabs(amount)*(price/SATOSHIS); + int64_t localAmount = llabs(amount)*price/SATOSHIS; // if amount is not 0, but is too small to be represented in local currency, return minimum non-zero localAmount if (localAmount == 0 && amount != 0) localAmount = 1;