Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiling the core library on Linux(ubuntu) #45

Open
idykman opened this issue May 17, 2019 · 12 comments
Open

Compiling the core library on Linux(ubuntu) #45

idykman opened this issue May 17, 2019 · 12 comments

Comments

@idykman
Copy link

idykman commented May 17, 2019

trying to compile the core lib under ubuntu using https://github.com/breadwallet/breadwallet-core/blob/master/Java/Core/CMakeLists.txt and facing several problems:

  1. core/support/BRAssert.c:196:42: error: ‘PTHREAD_MUTEX_RECURSIVE’ undeclared
    Solved by CFLAG _XOPEN_SOURCE=600

  2. core/support/BRFileService.c:286:33: error: ‘DT_REG’ undeclared (first use in this function)
    Solved by CFLAG _GNU_SOURCE

  3. Required code change:
    core/support/BRAssert.c:138:5: error: too few arguments to function ‘pthread_setname_np’

core/src/main/cpp/core/support/BRInt.h:138:31: error: initializer element is not constant
#define uint256(s) ((UInt256) { .u8 = {
^
Can you help me with those issues? Thanks!

@michael-brd
Copy link
Contributor

Hi @idykman, how are you trying to build? Could you outline what your steps are here?

@idykman
Copy link
Author

idykman commented May 17, 2019

I'm building using provided CMakeList.txt using default gcc, cmake and make installation on Ubuntu 18.04. My only changes to CMakeList.txt so far are related to flags I've mentioned in OP.
Build process is very simple:
cd Java/Core
mkdir build
cmake .. && make

@idykman
Copy link
Author

idykman commented May 17, 2019

Just in case, below is the diff:

--- a/Java/Core/CMakeLists.txt
+++ b/Java/Core/CMakeLists.txt
@@ -6,7 +6,7 @@
 cmake_minimum_required(VERSION 3.4.1)
 
 # now build app's shared lib
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -DANDROID_STL=gnustl_static -DANDROID_TOOLCHAIN=clang")
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -D_GNU_SOURCE -D_XOPEN_SOURCE=600 -D__LINUX__ -DANDROID_STL=gnustl_static -DANDROID_TOOLCHAIN=clang")
 
 # -Wimplicit-function-declaration
 # -Wno-missing-prototypes -Werror=return-type -Wdocumentation -Wunreachable-code-aggressive -Wno-missing-braces
@@ -297,12 +297,12 @@ include_directories(src/main/cpp/core/secp256k1/)
 # you want to add. CMake verifies that the library exists before
 # completing its build.
 
-find_library( # Sets the name of the path variable.
-              log-lib
+#find_library( # Sets the name of the path variable.
+#              log-lib
 
               # Specifies the name of the NDK library that
               # you want CMake to locate.
-              log )
+              #log)
 
 # Specifies libraries CMake should link to your target library. You
 # can link multiple libraries, such as libraries you define in this
@@ -313,4 +313,5 @@ target_link_libraries( # Specifies the target library.
 
                        # Links the target library to the log library
                        # included in the NDK.
-                       ${log-lib} )
\ No newline at end of file
+                       #${log-lib} 
+)
diff --git a/support/BRAssert.c b/support/BRAssert.c
index 49542d2..cc42232 100644
--- a/support/BRAssert.c
+++ b/support/BRAssert.c
@@ -132,7 +132,7 @@ typedef void* (*ThreadRoutine) (void*);         // pthread_create
 
 static void *
 BRAssertThread (BRAssertContext *context) {
-#if defined (__ANDROID__)
+#if defined (__ANDROID__) || defined (__LINUX__)
     pthread_setname_np (context->thread, ASSERT_THREAD_NAME);
 #else
     pthread_setname_np (ASSERT_THREAD_NAME);

@michael-brd
Copy link
Contributor

michael-brd commented May 17, 2019

We build for Android using Android Studio and for iOS via XCode (in the breadwallet-ios project).

So we don't officially build for Linux (per say) but I will take a look next week at seeing if I can get a build going.

@idykman
Copy link
Author

idykman commented May 17, 2019

@michael-brd Just FYI:
This error:

BRInt.h:138:31: error: initializer element is not constant

boil down to the fact that linux gcc is not happy about following initialization:

test.c:6:19: error: initializer element is not constant
static char xxx = "abc"[0];
                  ^

From the recent C11 draft (it did not change from C99):

An address constant is a null pointer, a pointer to an lvalue
designating an object of static storage duration, or a pointer to a
function designator; it shall be created explicitly using the unary &
operator or an integer constant cast to pointer type, or implicitly by
the use of an expression of array or function type. The
array-subscript [] and member-access . and -> operators, the address
& and indirection * unary operators, and pointer casts may be used in
the creation of an address constant, but the value of an object shall
not be accessed by use of these operators.

@michael-brd
Copy link
Contributor

Thanks @idykman. It sounds like you are able to build now, correct? I'll log a ticket to get this addressed.

@idykman
Copy link
Author

idykman commented May 21, 2019 via email

@michael-brd
Copy link
Contributor

I built on Ubuntu x64 18.04 by doing the following:

  • Installing cmake and clang-7
  • Making the changes included below
  • Running "cd Java/Core && cmake . -DCMAKE_C_COMPILER=clang-7 -DCMAKE_CXX_COMPILER=clang++-7"
  • Running "make"

This builds using clang (instead of GCC), which is what is used by the Android and iOS builds. In addition, it removes the JNI files (which aren't relevant if you are just trying to use the C functionality). Finally, it fixes some portability issues (i.e. preprocessor guards, includes, etc.).

Let me know if you have any issues.


diff --git a/Java/Core/CMakeLists.txt b/Java/Core/CMakeLists.txt
index 6a2c8ac1..44fdc8f9 100644
--- a/Java/Core/CMakeLists.txt
+++ b/Java/Core/CMakeLists.txt
@@ -6,7 +6,7 @@
 cmake_minimum_required(VERSION 3.4.1)
 
 # now build app's shared lib
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -DANDROID_STL=gnustl_static -DANDROID_TOOLCHAIN=clang")
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -D_GNU_SOURCE")
 
 # -Wimplicit-function-declaration
 # -Wno-missing-prototypes -Werror=return-type -Wdocumentation -Wunreachable-code-aggressive -Wno-missing-braces
@@ -82,38 +82,6 @@ target_sources (core
                 src/main/cpp/core/bcash/BRBCashParams.c
                 )
 
-# Core JNI
-target_sources (core
-                PUBLIC
-                src/main/cpp/jni/BRCoreJni.h
-                src/main/cpp/jni/BRCoreJni.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreJniReference.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreJniReference.h
-                src/main/cpp/jni/com_breadwallet_core_BRCoreAddress.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreAddress.h
-                src/main/cpp/jni/com_breadwallet_core_BRCoreChainParams.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreChainParams.h
-                src/main/cpp/jni/com_breadwallet_core_BRCorePeer.c
-                src/main/cpp/jni/com_breadwallet_core_BRCorePeer.h
-                src/main/cpp/jni/com_breadwallet_core_BRCoreMasterPubKey.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreMasterPubKey.h
-                src/main/cpp/jni/com_breadwallet_core_BRCoreKey.h
-                src/main/cpp/jni/com_breadwallet_core_BRCoreKey.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreMerkleBlock.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreMerkleBlock.h
-                src/main/cpp/jni/com_breadwallet_core_BRCorePeerManager.c
-                src/main/cpp/jni/com_breadwallet_core_BRCorePeerManager.h
-                src/main/cpp/jni/com_breadwallet_core_BRCoreTransaction.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreTransaction.h
-                src/main/cpp/jni/com_breadwallet_core_BRCoreTransactionInput.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreTransactionInput.h
-                src/main/cpp/jni/com_breadwallet_core_BRCoreTransactionOutput.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreTransactionOutput.h
-                src/main/cpp/jni/com_breadwallet_core_BRCorePaymentProtocol.c
-                src/main/cpp/jni/com_breadwallet_core_BRCorePaymentProtocol.h
-                src/main/cpp/jni/com_breadwallet_core_BRCoreWallet.c
-                src/main/cpp/jni/com_breadwallet_core_BRCoreWallet.h)
-
 # Core Ethereum
 target_sources (core
                 PUBLIC
@@ -262,21 +230,6 @@ target_sources (core
 #                src/main/cpp/core/ethereum/ewm/testEwm.c
                 )
 
-# Core Ethereum JNI
-target_sources (core
-                PUBLIC
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumAccount.h
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumAmount.h
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumBlock.h
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumEWM.c
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumEWM.h
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumNetwork.c
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumNetwork.h
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumToken.c
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumToken.h
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumTransaction.h
-                src/main/cpp/jni/ethereum/com_breadwallet_core_ethereum_BREthereumWallet.h)
-
 include_directories(src/main/cpp/core/)
 include_directories(src/main/cpp/core/support)
 include_directories(src/main/cpp/core/bitcoin)
@@ -290,20 +243,7 @@ include_directories(src/main/cpp/core/secp256k1/)
 # you want to add. CMake verifies that the library exists before
 # completing its build.
 
-find_library( # Sets the name of the path variable.
-              log-lib
-
-              # Specifies the name of the NDK library that
-              # you want CMake to locate.
-              log )
-
 # Specifies libraries CMake should link to your target library. You
 # can link multiple libraries, such as libraries you define in this
 # build script, prebuilt third-party libraries, or system libraries.
 
-target_link_libraries( # Specifies the target library.
-                       core
-
-                       # Links the target library to the log library
-                       # included in the NDK.
-                       ${log-lib} )
\ No newline at end of file
diff --git a/ethereum/event/BREvent.c b/ethereum/event/BREvent.c
index 774466c6..16a5be1e 100644
--- a/ethereum/event/BREvent.c
+++ b/ethereum/event/BREvent.c
@@ -174,7 +174,7 @@ typedef void* (*ThreadRoutine) (void*);
 static void *
 eventHandlerThread (BREventHandler handler) {
 
-#if defined (__ANDROID__)
+#if defined (__ANDROID__) || defined(__linux__)
     pthread_setname_np(handler->thread, handler->name);
 #else
     pthread_setname_np(handler->name);
diff --git a/ethereum/event/BREventAlarm.c b/ethereum/event/BREventAlarm.c
index f103c037..7eeca70f 100644
--- a/ethereum/event/BREventAlarm.c
+++ b/ethereum/event/BREventAlarm.c
@@ -260,7 +260,7 @@ typedef void* (*ThreadRoutine) (void*);
 static void *
 alarmClockThread (BREventAlarmClock clock) {
 
-#if defined (__ANDROID__)
+#if defined (__ANDROID__) || defined(__linux__)
     pthread_setname_np(clock->thread, "Core Ethereum Alarm Clock");
 #else
     pthread_setname_np("Core Ethereum Alarm Clock");
diff --git a/ethereum/les/BREthereumLES.c b/ethereum/les/BREthereumLES.c
index 14dede4d..d3b1337a 100644
--- a/ethereum/les/BREthereumLES.c
+++ b/ethereum/les/BREthereumLES.c
@@ -1122,7 +1122,7 @@ lesThreadBootstrapSeeds (BREthereumLES les) {
 
 static void *
 lesThread (BREthereumLES les) {
-#if defined (__ANDROID__)
+#if defined (__ANDROID__) || (__linux__)
     pthread_setname_np (les->thread, LES_THREAD_NAME);
 #else
     pthread_setname_np (LES_THREAD_NAME);
diff --git a/ethereum/util/BRUtilHex.h b/ethereum/util/BRUtilHex.h
index 46f4e21b..3a8c6f0f 100644
--- a/ethereum/util/BRUtilHex.h
+++ b/ethereum/util/BRUtilHex.h
@@ -12,6 +12,7 @@
 #define BR_Util_Hex_H
 
 #include <stdlib.h>
+#include <stdint.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/support/BRAssert.c b/support/BRAssert.c
index 49542d26..dd4e641f 100644
--- a/support/BRAssert.c
+++ b/support/BRAssert.c
@@ -132,7 +132,7 @@ typedef void* (*ThreadRoutine) (void*);         // pthread_create
 
 static void *
 BRAssertThread (BRAssertContext *context) {
-#if defined (__ANDROID__)
+#if defined (__ANDROID__) || defined (__linux__)
     pthread_setname_np (context->thread, ASSERT_THREAD_NAME);
 #else
     pthread_setname_np (ASSERT_THREAD_NAME);`

@idykman
Copy link
Author

idykman commented May 22, 2019

This works, thanks a lot for the solution! Are you planning to make .c and .h changes in this repo? It will help us with future integraions

@michael-brd
Copy link
Contributor

Yes, this repository undergoes regular feature development.

That said, we've create a ticket in our system to look at creating a supported Linux build. The idea being we keep that working as changes are made.

@piqjen-1xofFi-sikxyh
Copy link

Where was this build originally from

@piqjen-1xofFi-sikxyh
Copy link

Jennifer Warren's

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants