From 3ae702ebd472702b90ea468a11fd5ae62a131e85 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Mon, 4 Nov 2024 12:09:02 +0100 Subject: [PATCH] c: use `CMakeLists.txt` Signed-off-by: Yuki Kishimoto --- bindings/nostr-sdk-c/CMakeLists.txt | 38 ++++++++++++++++++++ bindings/nostr-sdk-c/Cargo.toml | 2 +- bindings/nostr-sdk-c/README.md | 30 ++++++++++++++++ bindings/nostr-sdk-c/examples/CMakeLists.txt | 18 ++++++++++ bindings/nostr-sdk-c/include/.gitkeep | 0 bindings/nostr-sdk-c/justfile | 8 ++--- 6 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 bindings/nostr-sdk-c/CMakeLists.txt create mode 100644 bindings/nostr-sdk-c/examples/CMakeLists.txt delete mode 100644 bindings/nostr-sdk-c/include/.gitkeep diff --git a/bindings/nostr-sdk-c/CMakeLists.txt b/bindings/nostr-sdk-c/CMakeLists.txt new file mode 100644 index 000000000..07cab87b2 --- /dev/null +++ b/bindings/nostr-sdk-c/CMakeLists.txt @@ -0,0 +1,38 @@ +# Minimum CMake version required +cmake_minimum_required(VERSION 3.10) + +# Project Name +project(NostrSDK) + +# Add your Rust library as a CMake target +add_custom_target(nostr_sdk ALL + COMMAND cargo build --release + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +# Determine the library file extension based on the platform +if(WIN32) + set(LIBRARY_FILE "nostr_sdk_c.dll") +elseif(APPLE) + set(LIBRARY_FILE "libnostr_sdk_c.dylib") +else() + set(LIBRARY_FILE "libnostr_sdk_c.so") +endif() + +# Path to the generated header and library +set(HEADER_PATH "${CMAKE_CURRENT_SOURCE_DIR}/include") +set(LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../target/release") + +# Export the library's include directory +include_directories(${HEADER_PATH}) +link_directories(${LIB_PATH}) + +add_library(nostr_sdk_interface INTERFACE) +target_include_directories(nostr_sdk_interface INTERFACE ${HEADER_PATH}) +target_link_libraries(nostr_sdk_interface INTERFACE ${LIB_PATH}/${LIBRARY_FILE}) + +# Install header files +install(FILES ${HEADER_PATH}/nostr_sdk.h DESTINATION include) + +# Install the shared library +install(FILES ${LIB_PATH}/${LIBRARY_FILE} DESTINATION lib) diff --git a/bindings/nostr-sdk-c/Cargo.toml b/bindings/nostr-sdk-c/Cargo.toml index ace244eb2..456e0a998 100644 --- a/bindings/nostr-sdk-c/Cargo.toml +++ b/bindings/nostr-sdk-c/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" publish = false [lib] -crate-type = ["staticlib"] +crate-type = ["cdylib", "staticlib"] [dependencies] nostr-sdk = { workspace = true, features = ["all-nips"] } diff --git a/bindings/nostr-sdk-c/README.md b/bindings/nostr-sdk-c/README.md index 5733aa151..2251bad9c 100644 --- a/bindings/nostr-sdk-c/README.md +++ b/bindings/nostr-sdk-c/README.md @@ -1,5 +1,35 @@ # C/C++ bindings for Nostr SDK +## Integration + +### Clone the repository + +```bash +git clone https://github.com/rust-nostr/nostr.git +cd nostr +``` + +### Configure your CMake + +```cmake +# Add the external library project +add_subdirectory(/nostr/bindings/nostr-sdk-c nostr_sdk) +include_directories("/nostr/bindings/nostr-sdk-c/include") +link_directories("/nostr/target/release") + +# Link library to a binary +target_link_libraries( PRIVATE nostr_sdk_interface) +``` + +### Build your project + +```bash +mkdir build +cd build +cmake .. +make +``` + ## Supported NIPs Look at diff --git a/bindings/nostr-sdk-c/examples/CMakeLists.txt b/bindings/nostr-sdk-c/examples/CMakeLists.txt new file mode 100644 index 000000000..0c88b75db --- /dev/null +++ b/bindings/nostr-sdk-c/examples/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.10) + +# Project name +project(Examples) + +# Add the library +add_subdirectory(../ nostr_sdk) +include_directories("../include") +link_directories("../../../target/release") + +# Define the C binaries +add_executable(keys keys.c) +add_dependencies(keys nostr_sdk) +target_link_libraries(keys PRIVATE nostr_sdk_interface) + +add_executable(client client.c) +add_dependencies(client nostr_sdk) +target_link_libraries(client PRIVATE nostr_sdk_interface) diff --git a/bindings/nostr-sdk-c/include/.gitkeep b/bindings/nostr-sdk-c/include/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/bindings/nostr-sdk-c/justfile b/bindings/nostr-sdk-c/justfile index cd6df6925..9e51ce4d2 100755 --- a/bindings/nostr-sdk-c/justfile +++ b/bindings/nostr-sdk-c/justfile @@ -1,10 +1,6 @@ #!/usr/bin/env just --justfile -build: - cargo build --release +examples: + cd examples && mkdir -p build && cd build && cmake .. && make -examples: build - mkdir -p bin/ - gcc examples/keys.c -o bin/keys -I include/ -L ../../target/release/ -lnostr_sdk_c -lpthread -ldl -lm -lrt - gcc examples/client.c -o bin/client -I include/ -L ../../target/release/ -lnostr_sdk_c -lpthread -ldl -lm -lrt