From ab87e1825a68ff43255adf3f50cc7ea9cbcea25e Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 7 May 2017 21:36:45 -0400 Subject: [PATCH 1/7] Fix Mac build issue error: use of undeclared identifier 'lseek64'; did you mean 'lseek'? --- src/utils/define.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/utils/define.h b/src/utils/define.h index 6814e133..4fa65322 100644 --- a/src/utils/define.h +++ b/src/utils/define.h @@ -8,6 +8,10 @@ #include #include +#ifdef MAC_OSX +#define lseek64 lseek +#endif + #ifndef WIN32 #include From 59ca7f742259f674ba620bef1c0a21621554c5ec Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 7 May 2017 21:53:36 -0400 Subject: [PATCH 2/7] Fix Mac build issue error: cast from pointer to smaller type 'unsigned int' loses information --- src/utils/utility.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/utils/utility.cpp b/src/utils/utility.cpp index be3f17e8..119832ca 100644 --- a/src/utils/utility.cpp +++ b/src/utils/utility.cpp @@ -150,7 +150,11 @@ void mc_MemoryDump(void *ptr, for(i=0;i Date: Sun, 14 May 2017 20:10:09 -0400 Subject: [PATCH 3/7] Fix Mac build issue sem_init() is deprecated, so use sem_open() instead. --- src/utils/systemdependent.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/utils/systemdependent.cpp b/src/utils/systemdependent.cpp index e7727e8a..a310d7d0 100644 --- a/src/utils/systemdependent.cpp +++ b/src/utils/systemdependent.cpp @@ -122,13 +122,32 @@ void* __US_SemCreate() lpsem=NULL; +#ifdef MAC_OSX + // Create a unique name for the named semaphore + static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + int namelen = 20; + char name[namelen + 1]; + for (int i = 0; i < namelen; ++i) { + name[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; + } + name[namelen] = 0; + + // Create named semaphore as unnamed semaphores are deprecated on OS X. + lpsem = sem_open(name, O_CREAT | O_EXCL, 0666, 1); + if (lpsem == SEM_FAILED) { + return NULL; + } +#else lpsem=new sem_t; if(sem_init(lpsem,0666,1)) { delete lpsem; return NULL; } - +#endif return (void*)lpsem; } @@ -150,12 +169,16 @@ void __US_SemPost(void* sem) void __US_SemDestroy(void* sem) { +#ifndef MAC_OSX sem_t *lpsem; +#endif if(sem) { sem_close((sem_t*)sem); +#ifndef MAC_OSX lpsem=(sem_t*)sem; delete lpsem; +#endif } } From bbafd912a18c976504fc045361a7b593b7383800 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 20 May 2017 13:26:55 -0400 Subject: [PATCH 4/7] Fix Mac build issue Datadir path now set to $HOME/.multichain to be the same as Unix --- src/utils/utilwrapper.cpp | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/utils/utilwrapper.cpp b/src/utils/utilwrapper.cpp index 747cb5f5..abae34c8 100644 --- a/src/utils/utilwrapper.cpp +++ b/src/utils/utilwrapper.cpp @@ -280,30 +280,22 @@ int64_t mc_Params::HasOption(const char* strArg) boost::filesystem::path mc_GetDefaultDataDir() { - // Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin - // Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin - // Mac: ~/Library/Application Support/Bitcoin - // Unix: ~/.bitcoin + // Windows < Vista: C:\Documents and Settings\Username\Application Data\MultiChain + // Windows >= Vista: C:\Users\Username\AppData\Roaming\MultiChain + // Mac and Unix: ~/.multichain #ifdef WIN32 // Windows return GetSpecialFolderPath(CSIDL_APPDATA) / "MultiChain"; #else + // Mac and Unix boost::filesystem::path pathRet; char* pszHome = getenv("HOME"); if (pszHome == NULL || strlen(pszHome) == 0) pathRet = boost::filesystem::path("/"); else pathRet = boost::filesystem::path(pszHome); -#ifdef MAC_OSX - // Mac - pathRet /= "Library/Application Support"; - TryCreateDirectory(pathRet); - return pathRet / "Bitcoin"; -#else - // Unix return pathRet / ".multichain"; #endif -#endif } int mc_GetDataDirArg(char *buf) @@ -930,4 +922,4 @@ int mc_FindIPv4ServerAddress(uint32_t *all_ips,int max_ips) return c; } - \ No newline at end of file + From 7e973319d707c32b02b3cffb47be1bf99e306aca Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 20 May 2017 13:27:40 -0400 Subject: [PATCH 5/7] Fix Mac build issue IP address auto-detection for Mac. --- src/utils/utilwrapper.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/utils/utilwrapper.cpp b/src/utils/utilwrapper.cpp index abae34c8..4afcdf34 100644 --- a/src/utils/utilwrapper.cpp +++ b/src/utils/utilwrapper.cpp @@ -863,7 +863,28 @@ int mc_FindIPv4ServerAddress(uint32_t *all_ips,int max_ips) result=0; c=0; -#ifndef WIN32 +#ifdef MAC_OSX + struct ifaddrs *ifaddr = NULL; + if (getifaddrs(&ifaddr) == -1) { + return c; + } + if (!ifaddr) { + return c; + } + int sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock > 0) { + for (struct ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr == 0) { + continue; + } + int family = ifa->ifa_addr->sa_family; + if (family != AF_INET) { + continue; + } + uint32_t a = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; + ptr=(unsigned char*)&a; + +#elif !defined WIN32 int sock; struct ifreq ifreqs[20]; @@ -919,6 +940,12 @@ int mc_FindIPv4ServerAddress(uint32_t *all_ips,int max_ips) } } +#ifdef MAC_OSX + if (sock > 0) { + close(sock); + } + freeifaddrs(ifaddr); +#endif return c; } From 5e7fe422a33627c011a9b9006dcd99f438b58808 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 21 May 2017 00:39:29 -0400 Subject: [PATCH 6/7] Add Mac build notes to README. --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/README.md b/README.md index 3769b158..7ed92336 100644 --- a/README.md +++ b/README.md @@ -76,3 +76,60 @@ Notes ----- * This will build `multichaind.exe`, `multichain-cli.exe` and `multichain-util.exe` in the `src` directory. + + +Mac Build Notes (on MacOS Sierra) +================ + +Install dependencies +-------------------- + + Install XCode and XCode command line tools + Install git from git-scm + Install brew (follow instructions on brew.sh) + brew install autoconf automake berkeley-db4 libtool boost openssl pkg-config rename + +Prepare for static linking +-------------------------- +Apple does not support statically linked binaries as [documented here](https://developer.apple.com/library/content/qa/qa1118/_index.html), however, it is convenient for end-users to launch a binary without having to first install brew, a third-party system designed for developers. + +To create a statically linked MultiChain which only depends on default MacOS dylibs, the following steps are taken: + +1. Hide the brew boost dylibs from the build system: + rename -e 's/.dylib/.dylib.hidden/' /usr/local/opt/boost/lib/*.dylib + +2. Hide the brew berekley-db dylibs from the build system: + rename -e 's/.dylib/.dylib.hidden/' /usr/local/opt/berkeley-db\@4/lib/*.dylib + +3. Hide the brew openssl dylibs from the build system: + rename -e 's/.dylib/.dylib.hidden/' /usr/local/opt/openssl/lib/*.dylib + +The default brew cookbook for berkeley-db and boost builds static libraries, but the default cookbook for openssl only builds dylibs. + +3. Tell brew to build openssl static libraries: + brew edit openssl + In 'def configure_args' change 'shared' to 'no-shared' + brew install openssl --force + +Compile MultiChain for Mac (64-bit) +-------------------------- + + export LDFLAGS=-L/usr/local/opt/openssl/lib + export CPPFLAGS=-I/usr/local/opt/openssl/include + ./configure --with-gui=no --with-libs=no --with-miniupnpc=no + make + +Clean up +-------- + + rename -e 's/.dylib.hidden/.dylib/' /usr/local/opt/berkeley-db\@4/lib/*.dylib.hidden + rename -e 's/.dylib.hidden/.dylib/' /usr/local/opt/boost/lib/*.dylib.hidden + rename -e 's/.dylib.hidden/.dylib/' /usr/local/opt/openssl/lib/*.dylib.hidden + brew edit openssl + In 'def configure_args' change 'no-shared' to 'shared' + +Notes +----- + +* This will build `multichaind`, `multichain-cli` and `multichain-util` in the `src` directory. + From c4283475a6d93c6727d29f23fa7d7162343878b5 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 22 May 2017 03:19:40 -0400 Subject: [PATCH 7/7] Fix Mac build issue Leveldb uses OSMemoryBarrier() which is deprecated in macOS 10.12 resulting in build warnings. Fixed by applying upstream patch to replace OSMemoryBarrier() with std::atomic_thread_fence(). --- src/leveldb/port/atomic_pointer.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/leveldb/port/atomic_pointer.h b/src/leveldb/port/atomic_pointer.h index 9bf091f7..f2204cdb 100644 --- a/src/leveldb/port/atomic_pointer.h +++ b/src/leveldb/port/atomic_pointer.h @@ -52,7 +52,9 @@ namespace port { // Mac OS #elif defined(OS_MACOSX) inline void MemoryBarrier() { - OSMemoryBarrier(); + // OSMemoryBarrier() deprecated in macOS 10.12. + // Apply patch from: https://github.com/google/leveldb/pull/422 + atomic_thread_fence(std::memory_order_seq_cst); } #define LEVELDB_HAVE_MEMORY_BARRIER