Skip to content

Commit

Permalink
c: use CMakeLists.txt
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 4, 2024
1 parent 49350e9 commit 3ae702e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 7 deletions.
38 changes: 38 additions & 0 deletions bindings/nostr-sdk-c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
publish = false

[lib]
crate-type = ["staticlib"]
crate-type = ["cdylib", "staticlib"]

[dependencies]
nostr-sdk = { workspace = true, features = ["all-nips"] }
Expand Down
30 changes: 30 additions & 0 deletions bindings/nostr-sdk-c/README.md
Original file line number Diff line number Diff line change
@@ -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(<path>/nostr/bindings/nostr-sdk-c nostr_sdk)
include_directories("<path>/nostr/bindings/nostr-sdk-c/include")
link_directories("<path>/nostr/target/release")
# Link library to a binary
target_link_libraries(<binary> PRIVATE nostr_sdk_interface)
```

### Build your project

```bash
mkdir build
cd build
cmake ..
make
```

## Supported NIPs

Look at <https://github.com/rust-nostr/nostr/tree/master/crates/nostr#supported-nips>
Expand Down
18 changes: 18 additions & 0 deletions bindings/nostr-sdk-c/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Empty file.
8 changes: 2 additions & 6 deletions bindings/nostr-sdk-c/justfile
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3ae702e

Please sign in to comment.