From 0ec3f7f68b5259de0f6aae93842ac2352afce3de Mon Sep 17 00:00:00 2001 From: l0nelyc0w Date: Wed, 16 Nov 2022 15:11:02 -0800 Subject: [PATCH] Instructions for building on Windows, closes #15 --- CMakeLists.txt | 57 +++++++++++++++++++++++++------ README.md | 38 ++++++++++++++++++++- bin/build_libmonero_cpp_dll.sh | 10 ++++++ src/wallet/monero_wallet_full.cpp | 39 ++++++++++----------- 4 files changed, 114 insertions(+), 30 deletions(-) create mode 100644 bin/build_libmonero_cpp_dll.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 7620b08..ff79fc6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,10 +2,23 @@ list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/cmake") cmake_minimum_required(VERSION 3.4.1) +if (WIN32) +# "'sleep_for' is not a member of 'std::this_thread'" in gcc 4.7/4.8 +add_definitions( "-D_GLIBCXX_USE_NANOSLEEP=1" ) +add_definitions( "-DWIN32_LEAN_AND_MEAN" ) +endif() + +#target_compile_options(gmock-matchers_test PRIVATE "-Wa,-mbig-obj") + + #SET(CMAKE_C_COMPILER /path/to/c/compiler) #SET(CMAKE_CXX_COMPILER /path/to/cpp/compiler) -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++14 -F/Library/Frameworks -pthread") -#SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address") +if (WIN32) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj -O2 -fPIC -std=c++14 -F/Library/Frameworks -pthread -lcrypto -lcrypt32") +else() + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++14 -F/Library/Frameworks -pthread") +endif() +#SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}-fsanitize=address") #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++14 -F/Library/Frameworks -pthread -fsanitize=address") # TODO: way to enable sanitize? get runtime error with this on project(MoneroCppLibrary) @@ -102,12 +115,18 @@ find_package(OpenSSL REQUIRED) message(STATUS "Using OpenSSL include dir at ${OPENSSL_INCLUDE_DIR}") include_directories(${OPENSSL_INCLUDE_DIR}) + if(STATIC AND NOT IOS) if(UNIX) set(OPENSSL_LIBRARIES "${OPENSSL_LIBRARIES};${CMAKE_DL_LIBS};${CMAKE_THREAD_LIBS_INIT}") endif() endif() +if (WIN32) + list(APPEND OPENSSL_LIBRARIES ws2_32 crypt32) +endif() + + ############ # libsodium ############ @@ -173,14 +192,26 @@ set_target_properties(epee PROPERTIES IMPORTED_LOCATION # Unbound find_package(Unbound) -include_directories(${UNBOUND_INCLUDE_DIR}) -if ("${UNBOUND_LIBRARIES}" MATCHES "libunbound") - message(STATUS "Using libunbound: ${UNBOUND_LIBRARIES}") - add_library(unbound SHARED IMPORTED) - set_target_properties(unbound PROPERTIES IMPORTED_LOCATION - ${UNBOUND_LIBRARIES}) + +if(NOT UNBOUND_INCLUDE_DIR) + die("Could not find libunbound") +else() + message(STATUS "Found libunbound include (unbound.h) in ${UNBOUND_INCLUDE_DIR}") + if(UNBOUND_LIBRARIES) + message(STATUS "Found libunbound library") + include_directories(${UNBOUND_INCLUDE_DIR}) + if (WIN32) + add_library(unbound STATIC IMPORTED) + else() + add_library(unbound SHARED IMPORTED) + endif() + set_target_properties(unbound PROPERTIES IMPORTED_LOCATION ${UNBOUND_LIBRARIES}) + else() + die("Found libunbound includes, but could not find libunbound library. Please make sure you have installed libunbound or libunbound-dev or the equivalent") + endif() endif() + add_library(rpc_base STATIC IMPORTED) set_target_properties(rpc_base PROPERTIES IMPORTED_LOCATION ${MONERO_PROJECT_BUILD}/src/rpc/librpc_base.a) @@ -371,7 +402,6 @@ if (BUILD_SAMPLE) multisig version randomx - dl epee hardforks cncrypto @@ -385,7 +415,11 @@ if (BUILD_SAMPLE) ${SODIUM_LIBRARY} ${HIDAPI_LIBRARIES} ${EXTRA_LIBRARIES} + ) + if (NOT WIN32) + target_link_libraries(sample_code dl) + endif() endif() ######################## @@ -420,7 +454,6 @@ if (BUILD_SCRATCHPAD) multisig version randomx - dl epee hardforks cncrypto @@ -435,4 +468,8 @@ if (BUILD_SCRATCHPAD) ${HIDAPI_LIBRARIES} ${EXTRA_LIBRARIES} ) + if (NOT WIN32) + target_link_libraries(sample_code dl) + endif() + endif() diff --git a/README.md b/README.md index e2994b5..499a700 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,42 @@ For example, [monero-java](https://github.com/monero-ecosystem/monero-java) comp 5. Build monero-project to create .a libraries, e.g.: `make release-static -j8` 6. Link to this library's source files in your application or build as a shared library in ./build/: `cd $MONERO_CPP && ./bin/build_libmonero_cpp.sh` +## Instructions for building libmonero-cpp.dll on Windows + +1. Download and install [MSYS2](https://www.msys2.org/). +2. Press windows button and type ```MSYS2 MINGW64``` for 64 bit systems or ```MSYS2 MINGW32``` for 32 bit. +3. Update packages: + ```pacman -Syu``` +4. Install dependencies + For 64 bit + ``` + pacman -S mingw-w64-x86_64-toolchain make mingw-w64-x86_64-cmake mingw-w64-x86_64-boost mingw-w64-x86_64-openssl mingw-w64-x86_64-zeromq mingw-w64-x86_64-libsodium mingw-w64-x86_64-hidapi mingw-w64-x86_64-unbound mingw-w64-x86_64-protobuf git mingw-w64-x86_64-libusb + ``` + For 32 bit + ``` + pacman -S mingw-w64-i686-toolchain make mingw-w64-i686-cmake mingw-w64-i686-boost mingw-w64-i686-openssl mingw-w64-i686-zeromq mingw-w64-i686-libsodium mingw-w64-i686-hidapi mingw-w64-i686-unbound mingw-w64-i686-protobuf git mingw-w64-i686-libusb + ``` +5. Clone repo + ``` + git clone --recurse-submodules https://github.com/monero-ecosystem/monero-cpp.git + ``` +6. Update submodules + ``` + cd ./monero-cpp && ./bin/update_submodules.sh + ``` +7. ```export MONERO_CPP=path/to/monero-cpp``` +8. ```cd $MONERO_CPP/external/monero-project``` +9. Build monero-project for 64 bit + ``` + make release-static-win64 + ``` + or for 32 bit + ``` + make release-static-win32 + ``` +10. Build as a shared library in ./build/: +```cd $MONERO_CPP && ./bin/build_libmonero_cpp_dll.sh``` + ## Related projects * [monero-java](https://github.com/monero-ecosystem/monero-java) @@ -223,4 +259,4 @@ If this library brings you value, please consider donating.


46FR1GKVqFNQnDiFkH7AuzbUBrGQwz2VdaXTDD4jcjRE8YkkoTYTmZ2Vohsz9gLSqkj5EM6ai9Q7sBoX4FPPYJdGKQQXPVz -

\ No newline at end of file +

diff --git a/bin/build_libmonero_cpp_dll.sh b/bin/build_libmonero_cpp_dll.sh new file mode 100644 index 0000000..2d66571 --- /dev/null +++ b/bin/build_libmonero_cpp_dll.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +HOST_NCORES=$(nproc 2>/dev/null || shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 1) + +# build libmonero-cpp shared library +mkdir -p build && +cd build && +cmake .. && +cmake --build . && +make . diff --git a/src/wallet/monero_wallet_full.cpp b/src/wallet/monero_wallet_full.cpp index 95262c7..e09f29b 100644 --- a/src/wallet/monero_wallet_full.cpp +++ b/src/wallet/monero_wallet_full.cpp @@ -44,7 +44,7 @@ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * strict LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers @@ -52,6 +52,12 @@ #include "monero_wallet_full.h" +#ifdef WIN32_LEAN_AND_MEAN +#include +#include +#include +#endif + #include "utils/monero_utils.h" #include #include @@ -62,11 +68,6 @@ #include "serialization/string.h" #include "common/threadpool.h" -#ifdef WIN32 -#include -#include -#endif - using namespace tools; /** @@ -77,7 +78,7 @@ namespace monero { // ------------------------- INITIALIZE CONSTANTS --------------------------- static const int DEFAULT_CONNECTION_TIMEOUT_MILLIS = 1000 * 30; // default connection timeout 30 sec - static const bool STRICT = false; // relies exclusively on blockchain data if true, includes local wallet data if false TODO: good use case to expose externally? + static const bool strict = false; // relies exclusively on blockchain data if true, includes local wallet data if false TODO: good use case to expose externally? // ----------------------- INTERNAL PRIVATE HELPERS ----------------------- @@ -1589,29 +1590,29 @@ namespace monero { // isMultisigImportNeeded uint64_t monero_wallet_full::get_balance() const { - return m_w2->balance_all(STRICT); + return m_w2->balance_all(strict); } uint64_t monero_wallet_full::get_balance(uint32_t account_idx) const { - return m_w2->balance(account_idx, STRICT); + return m_w2->balance(account_idx, strict); } uint64_t monero_wallet_full::get_balance(uint32_t account_idx, uint32_t subaddress_idx) const { - std::map balance_per_subaddress = m_w2->balance_per_subaddress(account_idx, STRICT); + std::map balance_per_subaddress = m_w2->balance_per_subaddress(account_idx, strict); auto iter = balance_per_subaddress.find(subaddress_idx); return iter == balance_per_subaddress.end() ? 0 : iter->second; } uint64_t monero_wallet_full::get_unlocked_balance() const { - return m_w2->unlocked_balance_all(STRICT); + return m_w2->unlocked_balance_all(strict); } uint64_t monero_wallet_full::get_unlocked_balance(uint32_t account_idx) const { - return m_w2->unlocked_balance(account_idx, STRICT); + return m_w2->unlocked_balance(account_idx, strict); } uint64_t monero_wallet_full::get_unlocked_balance(uint32_t account_idx, uint32_t subaddress_idx) const { - std::map>> unlocked_balance_per_subaddress = m_w2->unlocked_balance_per_subaddress(account_idx, STRICT); + std::map>> unlocked_balance_per_subaddress = m_w2->unlocked_balance_per_subaddress(account_idx, strict); auto iter = unlocked_balance_per_subaddress.find(subaddress_idx); return iter == unlocked_balance_per_subaddress.end() ? 0 : iter->second.first; } @@ -1629,8 +1630,8 @@ namespace monero { monero_account account; account.m_index = account_idx; account.m_primary_address = get_address(account_idx, 0); - account.m_balance = m_w2->balance(account_idx, STRICT); - account.m_unlocked_balance = m_w2->unlocked_balance(account_idx, STRICT); + account.m_balance = m_w2->balance(account_idx, strict); + account.m_unlocked_balance = m_w2->unlocked_balance(account_idx, strict); if (include_subaddresses) account.m_subaddresses = get_subaddresses_aux(account_idx, std::vector(), transfers); accounts.push_back(account); } @@ -1649,8 +1650,8 @@ namespace monero { monero_account account; account.m_index = account_idx; account.m_primary_address = get_address(account_idx, 0); - account.m_balance = m_w2->balance(account_idx, STRICT); - account.m_unlocked_balance = m_w2->unlocked_balance(account_idx, STRICT); + account.m_balance = m_w2->balance(account_idx, strict); + account.m_unlocked_balance = m_w2->unlocked_balance(account_idx, strict); if (include_subaddresses) account.m_subaddresses = get_subaddresses_aux(account_idx, std::vector(), transfers); return account; } @@ -3696,8 +3697,8 @@ namespace monero { std::vector subaddresses; // get balances per subaddress as maps - std::map balance_per_subaddress = m_w2->balance_per_subaddress(account_idx, STRICT); - std::map>> unlocked_balance_per_subaddress = m_w2->unlocked_balance_per_subaddress(account_idx, STRICT); + std::map balance_per_subaddress = m_w2->balance_per_subaddress(account_idx, strict); + std::map>> unlocked_balance_per_subaddress = m_w2->unlocked_balance_per_subaddress(account_idx, strict); // get all indices if no indices given std::vector subaddress_indices_req;