From a76a22ba872acc1b4d53662e3552b28cfa04c785 Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Tue, 28 Jun 2022 11:35:35 +0800 Subject: [PATCH 01/18] Problem: missing support for CMake (fix #265) Solution: - Add cmake support - Move the libraries and bindings to sdk --- .github/workflows/linux-build.yml | 21 +++++++++++++++++ .github/workflows/mac-build.yml | 2 ++ .github/workflows/win-build.yml | 2 ++ example/cpp-example/CMakeLists.txt | 21 +++++++++++++++++ example/cpp-example/Makefile | 27 +++++++++++++--------- example/cpp-example/chainmain.cc | 6 ++--- example/cpp-example/cronos.cc | 8 +++---- example/cpp-example/helper.py | 6 +++-- example/cpp-example/main.cc | 30 ++++++++++++------------- example/cpp-example/sdk/CMakeLists.txt | 31 ++++++++++++++++++++++++++ 10 files changed, 119 insertions(+), 35 deletions(-) create mode 100644 .github/workflows/linux-build.yml create mode 100644 example/cpp-example/CMakeLists.txt create mode 100644 example/cpp-example/sdk/CMakeLists.txt diff --git a/.github/workflows/linux-build.yml b/.github/workflows/linux-build.yml new file mode 100644 index 00000000..d78a1bb1 --- /dev/null +++ b/.github/workflows/linux-build.yml @@ -0,0 +1,21 @@ +name: Linux Build CI +on: + push: + branches: + - main + paths-ignore: + - README.md + tags: + - "v*.*.*" + pull_request: + paths-ignore: + - README.md + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Compile Cpp + run: make build_cpp diff --git a/.github/workflows/mac-build.yml b/.github/workflows/mac-build.yml index c776c595..7aecbcb5 100644 --- a/.github/workflows/mac-build.yml +++ b/.github/workflows/mac-build.yml @@ -6,6 +6,8 @@ on: - main paths-ignore: - README.md + tags: + - "v*.*.*" pull_request: paths-ignore: - README.md diff --git a/.github/workflows/win-build.yml b/.github/workflows/win-build.yml index f8cde50d..23a71902 100644 --- a/.github/workflows/win-build.yml +++ b/.github/workflows/win-build.yml @@ -6,6 +6,8 @@ on: - main paths-ignore: - README.md + tags: + - "v*.*.*" pull_request: paths-ignore: - README.md diff --git a/example/cpp-example/CMakeLists.txt b/example/cpp-example/CMakeLists.txt new file mode 100644 index 00000000..c69fe1e9 --- /dev/null +++ b/example/cpp-example/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.10) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQURIED ON) +if (APPLE) + set(CMAKE_CXX_FLAGS "-framework Security -framework CoreFoundation") +endif() +if (UNIX AND NOT APPLE) + set(CMAKE_CXX_FLAGS "-lpthread -lssl -lcrypto -ldl") +endif() + +project(cppexample VERSION 1.0) + +add_subdirectory(sdk) + +# static cppexample +add_executable(cppexamplestatic main.cc chainmain.cc cronos.cc) +target_link_libraries(cppexamplestatic PUBLIC ${DEFI_WALLET_CORE_CPP_LIB}) + +# dynamic cppexample +add_executable(cppexample main.cc chainmain.cc cronos.cc) +target_link_libraries(cppexample PUBLIC ${RUST_LIB} ${DEFI_WALLET_CORE_CPP_DYLIB} defi_wallet_core_cpp) diff --git a/example/cpp-example/Makefile b/example/cpp-example/Makefile index 28994e0a..369a2f50 100644 --- a/example/cpp-example/Makefile +++ b/example/cpp-example/Makefile @@ -1,4 +1,5 @@ UNAME := $(shell uname) +sdk_dir = ./sdk ifeq ($(UNAME), Darwin) FLAGS=-framework Security -framework CoreFoundation @@ -9,7 +10,7 @@ ifeq ($(UNAME), Linux) endif all: build run -build: prepare static dynamic +build: prepare static dynamic cmake run: run_static run_dynamic prepare: @@ -23,7 +24,7 @@ static: main.cc \ chainmain.cc \ cronos.cc \ - lib/libdefi_wallet_core_cpp.a \ + $(sdk_dir)/lib/libdefi_wallet_core_cpp.a \ -std=c++11 \ $(FLAGS) @@ -32,20 +33,20 @@ dynamic: main.cc \ chainmain.cc \ cronos.cc \ - include/*.cc \ - include/defi-wallet-core-cpp/src/*.cc \ - lib/libcxxbridge1.a \ + $(sdk_dir)/include/*.cc \ + $(sdk_dir)/include/defi-wallet-core-cpp/src/*.cc \ + $(sdk_dir)/lib/libcxxbridge1.a \ -ldefi_wallet_core_cpp \ -std=c++11 \ $(FLAGS) \ - -L lib \ + -L $(sdk_dir)/lib \ x86_64_static: arch -x86_64 g++ -o cppexamplestatic \ main.cc \ chainmain.cc \ cronos.cc \ - lib/libdefi_wallet_core_cpp.a \ + $(sdk_dir)/lib/libdefi_wallet_core_cpp.a \ -std=c++11 \ $(FLAGS) @@ -54,16 +55,20 @@ x86_64_dynamic: main.cc \ chainmain.cc \ cronos.cc \ - include/*.cc \ - include/defi-wallet-core-cpp/src/*.cc \ - lib/libcxxbridge1.a \ + $(sdk_dir)/include/*.cc \ + $(sdk_dir)/include/defi-wallet-core-cpp/src/*.cc \ + $(sdk_dir)/lib/libcxxbridge1.a \ -ldefi_wallet_core_cpp \ -std=c++11 \ $(FLAGS) \ - -L lib \ + -L $(sdk_dir)/lib \ x86_64_build: prepare_x86_64 x86_64_static x86_64_dynamic +cmake: + mkdir -p build + cd build && cmake .. && make + run_static: ./cppexamplestatic diff --git a/example/cpp-example/chainmain.cc b/example/cpp-example/chainmain.cc index cb2ccb40..cf451459 100644 --- a/example/cpp-example/chainmain.cc +++ b/example/cpp-example/chainmain.cc @@ -1,7 +1,7 @@ -#include "include/defi-wallet-core-cpp/src/lib.rs.h" -#include "include/defi-wallet-core-cpp/src/nft.rs.h" -#include "include/rust/cxx.h" #include "chainmain.h" +#include "sdk/include/defi-wallet-core-cpp/src/lib.rs.h" +#include "sdk/include/defi-wallet-core-cpp/src/nft.rs.h" +#include "sdk/include/rust/cxx.h" #include #include #include diff --git a/example/cpp-example/cronos.cc b/example/cpp-example/cronos.cc index e63f1983..de927768 100644 --- a/example/cpp-example/cronos.cc +++ b/example/cpp-example/cronos.cc @@ -1,7 +1,7 @@ -#include "include/defi-wallet-core-cpp/src/contract.rs.h" -#include "include/defi-wallet-core-cpp/src/lib.rs.h" -#include "include/defi-wallet-core-cpp/src/uint.rs.h" -#include "include/rust/cxx.h" +#include "sdk/include/defi-wallet-core-cpp/src/contract.rs.h" +#include "sdk/include/defi-wallet-core-cpp/src/lib.rs.h" +#include "sdk/include/defi-wallet-core-cpp/src/uint.rs.h" +#include "sdk/include/rust/cxx.h" #include #include #include diff --git a/example/cpp-example/helper.py b/example/cpp-example/helper.py index 9aa10b23..91340009 100644 --- a/example/cpp-example/helper.py +++ b/example/cpp-example/helper.py @@ -20,8 +20,8 @@ CPP_EXAMPLE_PATH = Path(__file__).parent VS_EXAMPLE_PATH = Path(__file__).parent.parent / "vs-example/vs-example" -INCLUDE_PATH = "include" -LIB_PATH = "lib" +INCLUDE_PATH = "sdk/include" +LIB_PATH = "sdk/lib" INITIAL_INCLUDES = [ '#include "defi-wallet-core-cpp/src/lib.rs.h"', @@ -76,6 +76,7 @@ def has_include_string(s): # copy the bindings, need python 3.8+ shutil.copytree(OUT_DIR, output_path, dirs_exist_ok=True) + print("Copied", OUT_DIR, "to", output_path) # copy library files: `*.a`, `*.dylib`, and `*.dll.lib` (windows) to `output_path` @@ -91,6 +92,7 @@ def copy_lib_files(output_path): # copy the libraries, need python 3.8+ for f in files: shutil.copy(f, output_path) + print("Copied", f, "to", output_path) # copy `EXAMPLE_SOURCES` to `output_path` diff --git a/example/cpp-example/main.cc b/example/cpp-example/main.cc index 81f2aca2..3191b4e9 100644 --- a/example/cpp-example/main.cc +++ b/example/cpp-example/main.cc @@ -1,8 +1,8 @@ -#include "include/defi-wallet-core-cpp/src/lib.rs.h" -#include "include/defi-wallet-core-cpp/src/nft.rs.h" -#include "include/rust/cxx.h" #include "chainmain.h" #include "cronos.h" +#include "sdk/include/defi-wallet-core-cpp/src/lib.rs.h" +#include "sdk/include/defi-wallet-core-cpp/src/nft.rs.h" +#include "sdk/include/rust/cxx.h" #include #include #include @@ -13,17 +13,17 @@ #include int main(int argc, char *argv[]) { - try { - chainmain_process(); // chain-main - test_chainmain_nft(); // chainmain nft tests - test_login(); // decentralized login - cronos_process(); // cronos - test_cronos_testnet(); // cronos testnet - } catch (const rust::cxxbridge1::Error &e) { - // Use `Assertion failed`, the same as `assert` function - std::cout << "Assertion failed: " << e.what() << std::endl; - } + try { + chainmain_process(); // chain-main + test_chainmain_nft(); // chainmain nft tests + test_login(); // decentralized login + cronos_process(); // cronos + test_cronos_testnet(); // cronos testnet + } catch (const rust::cxxbridge1::Error &e) { + // Use `Assertion failed`, the same as `assert` function + std::cout << "Assertion failed: " << e.what() << std::endl; + } - test_interval(); - return 0; + test_interval(); + return 0; } diff --git a/example/cpp-example/sdk/CMakeLists.txt b/example/cpp-example/sdk/CMakeLists.txt new file mode 100644 index 00000000..2371b9e6 --- /dev/null +++ b/example/cpp-example/sdk/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 3.10) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQURIED ON) + +project(defi_wallet_core_cpp VERSION 0.0.1) + +# Find bindings source files +file(GLOB_RECURSE DEFI_WALLET_CORE_CPP_BINDINGS include/defi-wallet-core-cpp/src/*.cc) +file(GLOB DEFI_WALLET_CORE_CPP_SROUCES include/*.cc) + +# Find the rust types binding library +find_library(RUST_LIB libcxxbridge1.a REQUIRED PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib) + +# Find the prebuilt static and dynamic libraries +if (WIN32) + find_library(DEFI_WALLET_CORE_CPP_LIB defi_wallet_core_cpp.lib PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib) + find_library(DEFI_WALLET_CORE_CPP_DYLIB defi_wallet_core_cpp.dll.lib PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib) +endif() + +if (APPLE) + find_library(DEFI_WALLET_CORE_CPP_LIB libdefi_wallet_core_cpp.a PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib) + find_library(DEFI_WALLET_CORE_CPP_DYLIB libdefi_wallet_core_cpp.dylib PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib) +endif() + +if (UNIX AND NOT APPLE) + find_library(DEFI_WALLET_CORE_CPP_LIB libdefi_wallet_core_cpp.a PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib) + find_library(DEFI_WALLET_CORE_CPP_DYLIB libdefi_wallet_core_cpp.so PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib) +endif() + +# Add library defi_wallet_core_cpp built from bindings source files +add_library(defi_wallet_core_cpp ${DEFI_WALLET_CORE_CPP_BINDINGS} ${DEFI_WALLET_CORE_CPP_SROUCES}) From d5520275ae0266a94d3af0e944ff61b8d70c3871 Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Tue, 28 Jun 2022 11:41:01 +0800 Subject: [PATCH 02/18] Fix version --- example/cpp-example/sdk/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/cpp-example/sdk/CMakeLists.txt b/example/cpp-example/sdk/CMakeLists.txt index 2371b9e6..d31dc1a3 100644 --- a/example/cpp-example/sdk/CMakeLists.txt +++ b/example/cpp-example/sdk/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQURIED ON) -project(defi_wallet_core_cpp VERSION 0.0.1) +project(defi_wallet_core_cpp VERSION 0.2.0) # Find bindings source files file(GLOB_RECURSE DEFI_WALLET_CORE_CPP_BINDINGS include/defi-wallet-core-cpp/src/*.cc) From 907163171c6239a5e30baea5715a43ff2aab799a Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Tue, 28 Jun 2022 11:58:31 +0800 Subject: [PATCH 03/18] Copy *.so --- example/cpp-example/helper.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/example/cpp-example/helper.py b/example/cpp-example/helper.py index 91340009..ff46821f 100644 --- a/example/cpp-example/helper.py +++ b/example/cpp-example/helper.py @@ -79,13 +79,15 @@ def has_include_string(s): print("Copied", OUT_DIR, "to", output_path) -# copy library files: `*.a`, `*.dylib`, and `*.dll.lib` (windows) to `output_path` +# copy library files: `*.a`, `*.dylib`, `*.dll.lib` (windows), and `*.so` (linux), to +# `output_path` def copy_lib_files(output_path): os.makedirs(output_path, exist_ok=True) files = [] files.extend(collect_files("*.a", TARGET_DIR, recursive=False)) files.extend(collect_files("*.dylib", TARGET_DIR, recursive=False)) files.extend(collect_files("*.dll.lib", TARGET_DIR, recursive=False)) + files.extend(collect_files("*.so", TARGET_DIR, recursive=False)) # workaround: search libcxxbridge1.a and push the first one files.append(collect_files("libcxxbridge1.a", TARGET_DIR)[0]) From addf18ec6ab9dde238ac6a343a15c63331a69399 Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Tue, 28 Jun 2022 12:35:28 +0800 Subject: [PATCH 04/18] Update vcxproj to sdk --- .../vs-example/vs-example/vs-example.vcxproj | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/example/vs-example/vs-example/vs-example.vcxproj b/example/vs-example/vs-example/vs-example.vcxproj index 0e8ca413..c220e578 100644 --- a/example/vs-example/vs-example/vs-example.vcxproj +++ b/example/vs-example/vs-example/vs-example.vcxproj @@ -142,16 +142,13 @@ - - - - - - - - - - + + + + + + + From a5370a90d2fd407ef3facb4b0c79a0328857f814 Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Tue, 28 Jun 2022 12:53:58 +0800 Subject: [PATCH 05/18] Update Doxyfile to sdk --- docs/cpp/Doxyfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/cpp/Doxyfile b/docs/cpp/Doxyfile index e5e2e93f..a2d0ff2b 100644 --- a/docs/cpp/Doxyfile +++ b/docs/cpp/Doxyfile @@ -874,9 +874,9 @@ WARN_LOGFILE = # spaces. See also FILE_PATTERNS and EXTENSION_MAPPING # Note: If this tag is empty the current directory is searched. -INPUT = ../../example/cpp-example/include/rust \ - ../../example/cpp-example/include/defi-wallet-core-cpp/src \ - ../../example/cpp-example/include/ \ +INPUT = ../../example/cpp-example/sdk/include/rust \ + ../../example/cpp-example/sdk/include/defi-wallet-core-cpp/src \ + ../../example/cpp-example/sdk/include/ \ # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses From e3bdd9d08672298f85ee59d6813b977b9f3ec238 Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Tue, 28 Jun 2022 16:37:50 +0800 Subject: [PATCH 06/18] Add make clean --- example/cpp-example/Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/example/cpp-example/Makefile b/example/cpp-example/Makefile index 369a2f50..a195a1d0 100644 --- a/example/cpp-example/Makefile +++ b/example/cpp-example/Makefile @@ -74,3 +74,7 @@ run_static: run_dynamic: export LD_LIBRARY_PATH=$(PWD) && ./cppexample + +clean: + rm cppexample cppexamplestatic + rm -rf build From d958b7986a3904d84e1466881922d2b73550237d Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Tue, 28 Jun 2022 17:25:23 +0800 Subject: [PATCH 07/18] Fix linux can not build cmake issue --- Makefile | 8 ++++---- checkmac.sh | 1 + example/cpp-example/CMakeLists.txt | 9 +++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 22a63666..18992907 100644 --- a/Makefile +++ b/Makefile @@ -45,14 +45,14 @@ mac_install: build_cpp: - . ./checkmac.sh && cargo build --package defi-wallet-core-cpp --release +# to fix link error on macos + ./checkmac.sh && cargo build --package defi-wallet-core-cpp --release cd $(cpp_example) && make build cpp: build_cpp - # to fix link error on macos - . ./checkmac.sh && . ./scripts/.env && cd $(cpp_example) && make run + . ./scripts/.env && cd $(cpp_example) && make run -cppx86_64: +cppx86_64: . ./checkmac.sh && rustup target add x86_64-apple-darwin . ./checkmac.sh && cargo build --package defi-wallet-core-cpp --release --target x86_64-apple-darwin . ./checkmac.sh && cd $(cpp_example) && make x86_64_build diff --git a/checkmac.sh b/checkmac.sh index 07c1d475..0d1778e2 100755 --- a/checkmac.sh +++ b/checkmac.sh @@ -1,3 +1,4 @@ +#!/bin/bash # to fix link error on macosx # cd example/cpp-example # otool -l libcxxbridge1.a > out diff --git a/example/cpp-example/CMakeLists.txt b/example/cpp-example/CMakeLists.txt index c69fe1e9..b3273e28 100644 --- a/example/cpp-example/CMakeLists.txt +++ b/example/cpp-example/CMakeLists.txt @@ -4,9 +4,6 @@ set(CMAKE_CXX_STANDARD_REQURIED ON) if (APPLE) set(CMAKE_CXX_FLAGS "-framework Security -framework CoreFoundation") endif() -if (UNIX AND NOT APPLE) - set(CMAKE_CXX_FLAGS "-lpthread -lssl -lcrypto -ldl") -endif() project(cppexample VERSION 1.0) @@ -14,7 +11,11 @@ add_subdirectory(sdk) # static cppexample add_executable(cppexamplestatic main.cc chainmain.cc cronos.cc) -target_link_libraries(cppexamplestatic PUBLIC ${DEFI_WALLET_CORE_CPP_LIB}) +if (UNIX AND NOT APPLE) + target_link_libraries(cppexamplestatic PUBLIC ${DEFI_WALLET_CORE_CPP_LIB} ssl crypto) +else() + target_link_libraries(cppexamplestatic PUBLIC ${DEFI_WALLET_CORE_CPP_LIB}) +endif() # dynamic cppexample add_executable(cppexample main.cc chainmain.cc cronos.cc) From 045e403857699242670d330da14560241ad7f89d Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Tue, 28 Jun 2022 18:21:20 +0800 Subject: [PATCH 08/18] Copy dll and lib --- example/cpp-example/helper.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/example/cpp-example/helper.py b/example/cpp-example/helper.py index ff46821f..c7b70b12 100644 --- a/example/cpp-example/helper.py +++ b/example/cpp-example/helper.py @@ -79,14 +79,15 @@ def has_include_string(s): print("Copied", OUT_DIR, "to", output_path) -# copy library files: `*.a`, `*.dylib`, `*.dll.lib` (windows), and `*.so` (linux), to +# copy library files: `*.a`, `*.dylib`, `*.lib` (windows), `*.dll` (windows), `*.so` (linux) to # `output_path` def copy_lib_files(output_path): os.makedirs(output_path, exist_ok=True) files = [] files.extend(collect_files("*.a", TARGET_DIR, recursive=False)) files.extend(collect_files("*.dylib", TARGET_DIR, recursive=False)) - files.extend(collect_files("*.dll.lib", TARGET_DIR, recursive=False)) + files.extend(collect_files("*.lib", TARGET_DIR, recursive=False)) + files.extend(collect_files("*.dll", TARGET_DIR, recursive=False)) files.extend(collect_files("*.so", TARGET_DIR, recursive=False)) # workaround: search libcxxbridge1.a and push the first one files.append(collect_files("libcxxbridge1.a", TARGET_DIR)[0]) From c8570153b86ba54925f8bd58cef0f52ecd1da7c8 Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Tue, 28 Jun 2022 18:28:48 +0800 Subject: [PATCH 09/18] Fix lint --- example/cpp-example/helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/cpp-example/helper.py b/example/cpp-example/helper.py index c7b70b12..59b4b274 100644 --- a/example/cpp-example/helper.py +++ b/example/cpp-example/helper.py @@ -79,8 +79,8 @@ def has_include_string(s): print("Copied", OUT_DIR, "to", output_path) -# copy library files: `*.a`, `*.dylib`, `*.lib` (windows), `*.dll` (windows), `*.so` (linux) to -# `output_path` +# copy library files: `*.a`, `*.dylib`, `*.lib` (windows), `*.dll` (windows), `*.so` +# (linux) to `output_path` def copy_lib_files(output_path): os.makedirs(output_path, exist_ok=True) files = [] From 11ba167019ec9dbbca2a0458be453850bc37d59f Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Tue, 28 Jun 2022 18:37:45 +0800 Subject: [PATCH 10/18] Add back pthread and dl --- example/cpp-example/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/cpp-example/CMakeLists.txt b/example/cpp-example/CMakeLists.txt index b3273e28..fffe9082 100644 --- a/example/cpp-example/CMakeLists.txt +++ b/example/cpp-example/CMakeLists.txt @@ -12,7 +12,7 @@ add_subdirectory(sdk) # static cppexample add_executable(cppexamplestatic main.cc chainmain.cc cronos.cc) if (UNIX AND NOT APPLE) - target_link_libraries(cppexamplestatic PUBLIC ${DEFI_WALLET_CORE_CPP_LIB} ssl crypto) + target_link_libraries(cppexamplestatic PUBLIC ${DEFI_WALLET_CORE_CPP_LIB} pthread ssl crypto dl) else() target_link_libraries(cppexamplestatic PUBLIC ${DEFI_WALLET_CORE_CPP_LIB}) endif() From ec2a6e129435e11479279eacba04d0c7b85ad0f5 Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Wed, 29 Jun 2022 09:50:35 +0800 Subject: [PATCH 11/18] Use -f to delete --- example/cpp-example/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/cpp-example/Makefile b/example/cpp-example/Makefile index a195a1d0..7e9e52d8 100644 --- a/example/cpp-example/Makefile +++ b/example/cpp-example/Makefile @@ -76,5 +76,5 @@ run_dynamic: export LD_LIBRARY_PATH=$(PWD) && ./cppexample clean: - rm cppexample cppexamplestatic + rm -f cppexample cppexamplestatic rm -rf build From 8309bc99995a9ed077304d619d5287f15f2d65d1 Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Wed, 29 Jun 2022 19:10:20 +0800 Subject: [PATCH 12/18] Link to relative path --- example/cpp-example/Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/example/cpp-example/Makefile b/example/cpp-example/Makefile index 7e9e52d8..c307bab5 100644 --- a/example/cpp-example/Makefile +++ b/example/cpp-example/Makefile @@ -36,10 +36,10 @@ dynamic: $(sdk_dir)/include/*.cc \ $(sdk_dir)/include/defi-wallet-core-cpp/src/*.cc \ $(sdk_dir)/lib/libcxxbridge1.a \ - -ldefi_wallet_core_cpp \ -std=c++11 \ - $(FLAGS) \ - -L $(sdk_dir)/lib \ + -L$(sdk_dir)/lib -Wl,-rpath=$(sdk_dir)/lib -Wall \ + -ldefi_wallet_core_cpp \ + $(FLAGS) x86_64_static: arch -x86_64 g++ -o cppexamplestatic \ From 51998322074d4834252f62844ab40dfac429150c Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Thu, 30 Jun 2022 16:10:16 +0800 Subject: [PATCH 13/18] Integrate Makefile --- Makefile | 14 ++++------ example/cpp-example/Makefile | 54 +++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 18992907..b636e202 100644 --- a/Makefile +++ b/Makefile @@ -43,21 +43,19 @@ mac_install: brew install ktlint brew install swiftformat - build_cpp: -# to fix link error on macos +ifeq ($(shell uname -m), x86_64) ./checkmac.sh && cargo build --package defi-wallet-core-cpp --release +endif +ifeq ($(shell uname -m), amd64) + rustup target add x86_64-apple-darwin + ./checkmac.sh && cargo build --package defi-wallet-core-cpp --release --target x86_64-apple-darwin +else cd $(cpp_example) && make build cpp: build_cpp . ./scripts/.env && cd $(cpp_example) && make run -cppx86_64: - . ./checkmac.sh && rustup target add x86_64-apple-darwin - . ./checkmac.sh && cargo build --package defi-wallet-core-cpp --release --target x86_64-apple-darwin - . ./checkmac.sh && cd $(cpp_example) && make x86_64_build - - proto: cd proto-build && cargo run diff --git a/example/cpp-example/Makefile b/example/cpp-example/Makefile index c307bab5..d86cbfa8 100644 --- a/example/cpp-example/Makefile +++ b/example/cpp-example/Makefile @@ -1,5 +1,16 @@ UNAME := $(shell uname) sdk_dir = ./sdk +ifeq ($(shell uname -m), x86_64) + CXX = g++ + TARGET_DIR = ../target/release +endif + +ifeq ($(shell uname -m), amd64) + CXX = arch -x86_64 g++ + TARGET_DIR = ../target/x86_64-apple-darwin/release +else + +endif ifeq ($(UNAME), Darwin) FLAGS=-framework Security -framework CoreFoundation @@ -14,13 +25,15 @@ build: prepare static dynamic cmake run: run_static run_dynamic prepare: - python3 helper.py --target_dir ../../target/release + python3 helper.py --target_dir $(TARGET_DIR) -prepare_x86_64: - python3 helper.py --target_dir ../../target/x86_64-apple-darwin/release +libdefi_wallet_core_cpp.dylib: +ifeq ($(UNAME), Darwin) + install_name_tool -id @rpath/libdefi_wallet_core_cpp.dylib.dylib $(sdk_dir)/lib/libdefi_wallet_core_cpp.dylib.dylib +endif static: - g++ -o cppexamplestatic \ + $(CXX) -o cppexamplestatic \ main.cc \ chainmain.cc \ cronos.cc \ @@ -28,8 +41,9 @@ static: -std=c++11 \ $(FLAGS) -dynamic: - g++ -o cppexample \ +dynamic: libdefi_wallet_core_cpp.dylib +ifeq ($(UNAME), Linux) + $(CXX) -o cppexample \ main.cc \ chainmain.cc \ cronos.cc \ @@ -40,40 +54,30 @@ dynamic: -L$(sdk_dir)/lib -Wl,-rpath=$(sdk_dir)/lib -Wall \ -ldefi_wallet_core_cpp \ $(FLAGS) - -x86_64_static: - arch -x86_64 g++ -o cppexamplestatic \ - main.cc \ - chainmain.cc \ - cronos.cc \ - $(sdk_dir)/lib/libdefi_wallet_core_cpp.a \ - -std=c++11 \ - $(FLAGS) - -x86_64_dynamic: - arch -x86_64 g++ -o cppexample \ +endif +ifeq ($(UNAME), Darwin) + $(CXX) -o cppexample \ main.cc \ chainmain.cc \ cronos.cc \ $(sdk_dir)/include/*.cc \ $(sdk_dir)/include/defi-wallet-core-cpp/src/*.cc \ $(sdk_dir)/lib/libcxxbridge1.a \ - -ldefi_wallet_core_cpp \ -std=c++11 \ - $(FLAGS) \ - -L $(sdk_dir)/lib \ - -x86_64_build: prepare_x86_64 x86_64_static x86_64_dynamic + $(sdk_dir)/lib/libplay_cpp_sdk.dylib \ + -rpath $(sdk_dir)/lib \ + $(FLAGS) +endif cmake: mkdir -p build cd build && cmake .. && make run_static: - ./cppexamplestatic + ./cppexamplestatic && ./build/cppexamplestatic run_dynamic: - export LD_LIBRARY_PATH=$(PWD) && ./cppexample + export LD_LIBRARY_PATH=$(PWD) && ./cppexample && ./build/cppexample clean: rm -f cppexample cppexamplestatic From 222e6efd39304a560407b9d111b12c2f95319e9f Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Thu, 30 Jun 2022 16:13:58 +0800 Subject: [PATCH 14/18] Fix wrong endif --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b636e202..0b252c31 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ endif ifeq ($(shell uname -m), amd64) rustup target add x86_64-apple-darwin ./checkmac.sh && cargo build --package defi-wallet-core-cpp --release --target x86_64-apple-darwin -else +endif cd $(cpp_example) && make build cpp: build_cpp From e6768fee63ed928d76ce6f16aab779a3d61272e1 Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Thu, 30 Jun 2022 16:47:45 +0800 Subject: [PATCH 15/18] Fix the wrong TARGET_DIR --- example/cpp-example/Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/example/cpp-example/Makefile b/example/cpp-example/Makefile index d86cbfa8..be89a23a 100644 --- a/example/cpp-example/Makefile +++ b/example/cpp-example/Makefile @@ -2,12 +2,12 @@ UNAME := $(shell uname) sdk_dir = ./sdk ifeq ($(shell uname -m), x86_64) CXX = g++ - TARGET_DIR = ../target/release + TARGET_DIR = ../../target/release endif ifeq ($(shell uname -m), amd64) CXX = arch -x86_64 g++ - TARGET_DIR = ../target/x86_64-apple-darwin/release + TARGET_DIR = ../../target/x86_64-apple-darwin/release else endif @@ -29,7 +29,7 @@ prepare: libdefi_wallet_core_cpp.dylib: ifeq ($(UNAME), Darwin) - install_name_tool -id @rpath/libdefi_wallet_core_cpp.dylib.dylib $(sdk_dir)/lib/libdefi_wallet_core_cpp.dylib.dylib + install_name_tool -id @rpath/libdefi_wallet_core_cpp.dylib.dylib $(sdk_dir)/lib/libdefi_wallet_core_cpp.dylib endif static: @@ -64,7 +64,7 @@ ifeq ($(UNAME), Darwin) $(sdk_dir)/include/defi-wallet-core-cpp/src/*.cc \ $(sdk_dir)/lib/libcxxbridge1.a \ -std=c++11 \ - $(sdk_dir)/lib/libplay_cpp_sdk.dylib \ + $(sdk_dir)/lib/libdefi_wallet_core_cpp.dylib \ -rpath $(sdk_dir)/lib \ $(FLAGS) endif From 495db7a2064a43e6d94bca37f3ccecaf6b13939c Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Thu, 30 Jun 2022 17:13:50 +0800 Subject: [PATCH 16/18] Fix the wrong rpath --- example/cpp-example/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/cpp-example/Makefile b/example/cpp-example/Makefile index be89a23a..6a6336e5 100644 --- a/example/cpp-example/Makefile +++ b/example/cpp-example/Makefile @@ -29,7 +29,7 @@ prepare: libdefi_wallet_core_cpp.dylib: ifeq ($(UNAME), Darwin) - install_name_tool -id @rpath/libdefi_wallet_core_cpp.dylib.dylib $(sdk_dir)/lib/libdefi_wallet_core_cpp.dylib + install_name_tool -id @rpath/libdefi_wallet_core_cpp.dylib $(sdk_dir)/lib/libdefi_wallet_core_cpp.dylib endif static: From 1e047c77f039268466ed75d0c910da77b884da0a Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Thu, 30 Jun 2022 17:34:54 +0800 Subject: [PATCH 17/18] Simplify only one make for example --- Makefile | 2 +- example/cpp-example/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 0b252c31..dcfd3b4d 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ ifeq ($(shell uname -m), amd64) rustup target add x86_64-apple-darwin ./checkmac.sh && cargo build --package defi-wallet-core-cpp --release --target x86_64-apple-darwin endif - cd $(cpp_example) && make build + cd $(cpp_example) && make cpp: build_cpp . ./scripts/.env && cd $(cpp_example) && make run diff --git a/example/cpp-example/Makefile b/example/cpp-example/Makefile index 6a6336e5..ad3fcc65 100644 --- a/example/cpp-example/Makefile +++ b/example/cpp-example/Makefile @@ -20,7 +20,7 @@ ifeq ($(UNAME), Linux) FLAGS=-lpthread -lssl -lcrypto -ldl endif -all: build run +all: clean build build: prepare static dynamic cmake run: run_static run_dynamic From 444b1280cbe596902807c01e440e90606432eadc Mon Sep 17 00:00:00 2001 From: Damon Chen Date: Thu, 30 Jun 2022 17:38:19 +0800 Subject: [PATCH 18/18] Use sdk_dir --- example/cpp-example/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/example/cpp-example/Makefile b/example/cpp-example/Makefile index ad3fcc65..50de0ba7 100644 --- a/example/cpp-example/Makefile +++ b/example/cpp-example/Makefile @@ -82,3 +82,5 @@ run_dynamic: clean: rm -f cppexample cppexamplestatic rm -rf build + rm -rf $(sdk_dir)/lib + rm -rf $(sdk_dir)/include