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

Problem: missing support for CMake (fix #265) #461

Merged
merged 18 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/linux-build.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .github/workflows/mac-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
- main
paths-ignore:
- README.md
tags:
- "v*.*.*"
pull_request:
paths-ignore:
- README.md
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/win-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
- main
paths-ignore:
- README.md
tags:
- "v*.*.*"
pull_request:
paths-ignore:
- README.md
Expand Down
20 changes: 9 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,18 @@ mac_install:
brew install ktlint
brew install swiftformat


build_cpp:
. ./checkmac.sh && cargo build --package defi-wallet-core-cpp --release
cd $(cpp_example) && make build
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
endif
cd $(cpp_example) && make

cpp: build_cpp
# to fix link error on macos
. ./checkmac.sh && . ./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

. ./scripts/.env && cd $(cpp_example) && make run

proto:
cd proto-build && cargo run
Expand Down
1 change: 1 addition & 0 deletions checkmac.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
# to fix link error on macosx
# cd example/cpp-example
# otool -l libcxxbridge1.a > out
Expand Down
6 changes: 3 additions & 3 deletions docs/cpp/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions example/cpp-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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()

project(cppexample VERSION 1.0)

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} pthread ssl crypto dl)
else()
target_link_libraries(cppexamplestatic PUBLIC ${DEFI_WALLET_CORE_CPP_LIB})
endif()

# 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)
85 changes: 50 additions & 35 deletions example/cpp-example/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +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
Expand All @@ -8,64 +20,67 @@ ifeq ($(UNAME), Linux)
FLAGS=-lpthread -lssl -lcrypto -ldl
endif

all: build run
build: prepare static dynamic
all: clean build
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 $(sdk_dir)/lib/libdefi_wallet_core_cpp.dylib
endif

static:
g++ -o cppexamplestatic \
$(CXX) -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)

dynamic:
g++ -o cppexample \
main.cc \
chainmain.cc \
cronos.cc \
include/*.cc \
include/defi-wallet-core-cpp/src/*.cc \
lib/libcxxbridge1.a \
-ldefi_wallet_core_cpp \
-std=c++11 \
$(FLAGS) \
-L lib \

x86_64_static:
arch -x86_64 g++ -o cppexamplestatic \
dynamic: libdefi_wallet_core_cpp.dylib
ifeq ($(UNAME), Linux)
$(CXX) -o cppexample \
main.cc \
chainmain.cc \
cronos.cc \
lib/libdefi_wallet_core_cpp.a \
$(sdk_dir)/include/*.cc \
$(sdk_dir)/include/defi-wallet-core-cpp/src/*.cc \
$(sdk_dir)/lib/libcxxbridge1.a \
-std=c++11 \
-L$(sdk_dir)/lib -Wl,-rpath=$(sdk_dir)/lib -Wall \
-ldefi_wallet_core_cpp \
$(FLAGS)

x86_64_dynamic:
arch -x86_64 g++ -o cppexample \
endif
ifeq ($(UNAME), Darwin)
$(CXX) -o cppexample \
main.cc \
chainmain.cc \
cronos.cc \
include/*.cc \
include/defi-wallet-core-cpp/src/*.cc \
lib/libcxxbridge1.a \
-ldefi_wallet_core_cpp \
$(sdk_dir)/include/*.cc \
$(sdk_dir)/include/defi-wallet-core-cpp/src/*.cc \
$(sdk_dir)/lib/libcxxbridge1.a \
-std=c++11 \
$(FLAGS) \
-L lib \
$(sdk_dir)/lib/libdefi_wallet_core_cpp.dylib \
-rpath $(sdk_dir)/lib \
$(FLAGS)
endif

x86_64_build: prepare_x86_64 x86_64_static x86_64_dynamic
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
rm -rf build
rm -rf $(sdk_dir)/lib
rm -rf $(sdk_dir)/include
6 changes: 3 additions & 3 deletions example/cpp-example/chainmain.cc
Original file line number Diff line number Diff line change
@@ -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 <cassert>
#include <chrono>
#include <iostream>
Expand Down
8 changes: 4 additions & 4 deletions example/cpp-example/cronos.cc
Original file line number Diff line number Diff line change
@@ -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 <cassert>
#include <chrono>
#include <cstring>
Expand Down
13 changes: 9 additions & 4 deletions example/cpp-example/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand Down Expand Up @@ -76,21 +76,26 @@ 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`
# 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])

# 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`
Expand Down
30 changes: 15 additions & 15 deletions example/cpp-example/main.cc
Original file line number Diff line number Diff line change
@@ -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 <atomic>
#include <cassert>
#include <chrono>
Expand All @@ -13,17 +13,17 @@
#include <thread>

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;
}
31 changes: 31 additions & 0 deletions example/cpp-example/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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.2.0)

# 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})
17 changes: 7 additions & 10 deletions example/vs-example/vs-example/vs-example.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,13 @@
<ClCompile Include="cronos.cc" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="bindings.h" />
</ItemGroup>
<ItemGroup>
<Library Include="lib\defi_wallet_core_cpp.dll.lib" />
<Library Include="lib\libcxxbridge1.a" />
<ClCompile Include="include\nft.cc" />
<ClCompile Include="include\defi-wallet-core-cpp\src\lib.rs.cc" />
<ClCompile Include="include\defi-wallet-core-cpp\src\nft.rs.cc" />
<ClCompile Include="include\defi-wallet-core-cpp\src\contract.rs.cc" />
<ClCompile Include="include\defi-wallet-core-cpp\src\uint.rs.cc" />
<Library Include="sdk\lib\defi_wallet_core_cpp.dll.lib" />
<Library Include="sdk\lib\libcxxbridge1.a" />
<ClCompile Include="sdk\include\nft.cc" />
<ClCompile Include="sdk\include\defi-wallet-core-cpp\src\lib.rs.cc" />
<ClCompile Include="sdk\include\defi-wallet-core-cpp\src\nft.rs.cc" />
<ClCompile Include="sdk\include\defi-wallet-core-cpp\src\contract.rs.cc" />
<ClCompile Include="sdk\include\defi-wallet-core-cpp\src\uint.rs.cc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
Expand Down