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

Windows support #20

Merged
merged 9 commits into from
Nov 28, 2024
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
36 changes: 36 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build & Test

on:
workflow_call:
inputs:
os:
required: true
type: string
additional-setup:
required: false
type: string

jobs:
run:
runs-on: ${{ inputs.os }}
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
submodules: recursive

- name: Rust toolchain
uses: dtolnay/rust-toolchain@stable

- name: Rust cache
uses: Swatinem/rust-cache@v2

- name: Additional Setup (if specified)
if: ${{ inputs.additional-setup != '' }}
run: ${{ inputs.additional-setup }}

- name: Run Tests
run: cargo test --release

- name: Build
run: cargo build --release
51 changes: 37 additions & 14 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
name: Rust

on:
push:
branches: [ "master", "ci" ]
pull_request:
branches: [ "master" ]
on: [pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
build:

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- run: cargo fmt --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Initialize Submodules
run: git submodule update --init --recursive
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- run: cargo clippy -- -D warnings

linux-build-test:
name: Build and Test (Linux)
uses: ./.github/workflows/build-and-test.yml
with:
os: ubuntu-latest

macos-build-test:
name: Build and Test (macOS)
uses: ./.github/workflows/build-and-test.yml
with:
os: macos-latest

windows-build-test:
name: Build and Test (Windows)
uses: ./.github/workflows/build-and-test.yml
with:
os: windows-latest
105 changes: 56 additions & 49 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use cc::Build;
use std::env;
use std::path::PathBuf;

fn secp256k1_build() {
fn secp256k1_build(base_config: &mut Build) {
// Actual build
let mut base_config = cc::Build::new();
//let mut base_config = cc::Build::new();
base_config
.include("nostrdb/deps/secp256k1/")
.include("nostrdb/deps/secp256k1/include")
.include("nostrdb/deps/secp256k1/src")
.flag_if_supported("-Wno-unused-function") // some ecmult stuff is defined but not used upstream
.flag_if_supported("-Wno-unused-parameter") // patching out printf causes this warning
//.define("SECP256K1_API", Some(""))
.define("SECP256K1_STATIC", "1")
.define("ENABLE_MODULE_ECDH", Some("1"))
.define("ENABLE_MODULE_SCHNORRSIG", Some("1"))
.define("ENABLE_MODULE_EXTRAKEYS", Some("1"));
Expand All @@ -34,13 +34,25 @@ fn secp256k1_build() {
base_config.flag("-O1");
}

if base_config.try_compile("libsecp256k1.a").is_err() {
// Some embedded platforms may not have, eg, string.h available, so if the build fails
// simply try again with the wasm sysroot (but without the wasm type sizes) in the hopes
// that it works.
base_config.include("wasm/wasm-sysroot");
base_config.compile("libsecp256k1.a");
}
//base_config.compile("libsecp256k1.a");
}

/// bolt11 deps with portability issues, exclude these on windows build
fn bolt11_deps() -> &'static [&'static str] {
&[
"nostrdb/ccan/ccan/likely/likely.c",
"nostrdb/ccan/ccan/list/list.c",
"nostrdb/ccan/ccan/mem/mem.c",
"nostrdb/ccan/ccan/str/debug.c",
"nostrdb/ccan/ccan/str/str.c",
"nostrdb/ccan/ccan/take/take.c",
"nostrdb/ccan/ccan/tal/str/str.c",
"nostrdb/ccan/ccan/tal/tal.c",
"nostrdb/ccan/ccan/utf8/utf8.c",
"nostrdb/src/bolt11/bolt11.c",
"nostrdb/src/bolt11/amount.c",
"nostrdb/src/bolt11/hash_u5.c",
]
}

fn main() {
Expand All @@ -50,30 +62,11 @@ fn main() {
build
.files([
"nostrdb/src/nostrdb.c",
"nostrdb/src/bolt11/bolt11.c",
"nostrdb/src/bolt11/amount.c",
"nostrdb/src/bolt11/bech32.c",
"nostrdb/src/bolt11/hash_u5.c",
"nostrdb/src/invoice.c",
"nostrdb/src/nostr_bech32.c",
"nostrdb/src/content_parser.c",
"nostrdb/ccan/ccan/crypto/sha256/sha256.c",
//"nostrdb/ccan/ccan/htable/htable.c",
//"nostrdb/ccan/ccan/htable/tools/density.c",
//"nostrdb/ccan/ccan/htable/tools/hsearchspeed.c",
//"nostrdb/ccan/ccan/htable/tools/speed.c",
//"nostrdb/ccan/ccan/htable/tools/stringspeed.c",
"nostrdb/ccan/ccan/likely/likely.c",
"nostrdb/ccan/ccan/list/list.c",
"nostrdb/ccan/ccan/mem/mem.c",
"nostrdb/ccan/ccan/str/debug.c",
"nostrdb/ccan/ccan/str/str.c",
"nostrdb/ccan/ccan/take/take.c",
//"nostrdb/ccan/ccan/tal/benchmark/samba-allocs.c",
//"nostrdb/ccan/ccan/tal/benchmark/speed.c",
"nostrdb/ccan/ccan/tal/str/str.c",
"nostrdb/ccan/ccan/tal/tal.c",
"nostrdb/ccan/ccan/utf8/utf8.c",
"nostrdb/src/bolt11/bech32.c",
"nostrdb/src/block.c",
"nostrdb/deps/flatcc/src/runtime/json_parser.c",
"nostrdb/deps/flatcc/src/runtime/verifier.c",
Expand All @@ -87,41 +80,49 @@ fn main() {
.include("nostrdb/deps/flatcc/include")
.include("nostrdb/deps/secp256k1/include")
.include("nostrdb/ccan")
.include("nostrdb/src")
// Add other include paths
//.flag("-Wall")
.flag("-Wno-sign-compare")
.flag("-Wno-misleading-indentation")
.flag("-Wno-unused-function")
.flag("-Wno-unused-parameter");
.include("nostrdb/src");
// Add other include paths
//.flag("-Wall")
//.flag("-Werror")
//.flag("-g")

// Link Security framework on macOS
if !cfg!(target_os = "windows") {
build.files(bolt11_deps());
build
.flag("-Wno-sign-compare")
.flag("-Wno-misleading-indentation")
.flag("-Wno-unused-function")
.flag("-Wno-unused-parameter");
} else {
// need this on windows
println!("cargo:rustc-link-lib=bcrypt");
}

if env::var("PROFILE").unwrap() == "debug" {
build.flag("-DDEBUG");
build.flag("-O1");
}

// Print out the path to the compiled library
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
println!("cargo:rustc-link-search=native={}", out_path.display());

secp256k1_build(&mut build);

build.compile("libnostrdb.a");

secp256k1_build();
println!("cargo:rustc-link-lib=static=nostrdb");

// Re-run the build script if any of the C files or headers change
for file in &["nostrdb/src/nostrdb.c", "nostrdb/src/nostrdb.h"] {
println!("cargo:rerun-if-changed={}", file);
}

println!("cargo:rustc-link-lib=secp256k1");

// Print out the path to the compiled library
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
println!("cargo:rustc-link-search=native={}", out_path.display());
println!("cargo:rustc-link-lib=static=nostrdb");

// Link Security framework on macOS
//if cfg!(target_os = "macos") {
//println!("cargo:rustc-link-lib=framework=Security");
//}
if cfg!(target_os = "macos") {
println!("cargo:rustc-link-lib=framework=Security");
}

//
// We only need bindgen when we update the bindings.
Expand All @@ -137,8 +138,14 @@ fn main() {
.generate()
.expect("Unable to generate bindings");

#[cfg(target_os = "windows")]
let filename = "src/bindings_win.rs";

#[cfg(not(target_os = "windows"))]
let filename = "src/bindings_posix.rs";

bindings
.write_to_file("src/bindings.rs")
.write_to_file(filename)
.expect("Couldn't write bindings!");
}
}
Loading