Skip to content

Commit

Permalink
feat: generate rust-c bindings automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
shiqimei committed Jun 13, 2023
1 parent e88b3af commit d9cf56e
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/cmake-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
- name: Install Ninja
run: sudo apt-get install -y ninja-build

- name: Install cbindgen
run: cargo install --force cbindgen

- name: Configure CMake
run: cmake -G Ninja -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=Debug

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/cmake-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
- name: Install Ninja
run: sudo apt-get install -y ninja-build

- name: Install cbindgen
run: cargo install --force cbindgen

- name: Configure CMake
run: cmake -G Ninja -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=Release

Expand Down
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"rust-analyzer.linkedProjects": ["src/crates/Cargo.toml"]
"rust-analyzer.linkedProjects": [
"src/crates/Cargo.toml"
],
"files.associations": {
"quickjs-rs.h": "c",
"js-array.h": "c",
"types.h": "c",
"list.h": "c",
"js-function.h": "c"
}
}
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
# include c headers
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/vendor/mimalloc/include)
include_directories(${PROJECT_SOURCE_DIR}/src/crates/include)
add_subdirectory(src)
add_subdirectory(vendor/mimalloc)

Expand Down
3 changes: 3 additions & 0 deletions include/quickjs/quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,9 @@ int JS_SetModuleExportList(JSContext* ctx, JSModuleDef* m, const JSCFunctionList
} /* extern "C" { */
#endif

/* quickjs_rs c bindings */
#include "quickjs-rs.h"

/* Should use in `jsc` repl only */
#ifdef CONFIG_DEBUG_ON_RELEASE
void __internal_debug_log(const char* fmt, ...);
Expand Down
10 changes: 8 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ add_library(quickjs
core/misc/debug.c
)

# link quickjs rust library
# create a custom target for including headers
add_custom_target(include_rust_c_bindings
COMMAND echo "include headers: ${PROJECT_SOURCE_DIR}/src/crates/target/include"
COMMAND include_directories(${PROJECT_SOURCE_DIR}/src/crates/target/include)
)

# build rust libraries
corrosion_import_crate(MANIFEST_PATH ${CMAKE_SOURCE_DIR}/src/crates/Cargo.toml)
target_link_libraries(quickjs quickjs_rust)
target_link_libraries(quickjs quickjs_rs)

set(QUICKJS_VERSION STRING "0.1.0")

Expand Down
3 changes: 0 additions & 3 deletions src/core/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2612,9 +2612,6 @@ void JS_SetRuntimeInfo(JSRuntime* rt, const char* s) {
rt->rt_info = s;
}

// TODO remove it from `core`
void print_gc_objects(struct list_head *head);

void JS_FreeRuntime(JSRuntime* rt) {
struct list_head *el, *el1;
int i;
Expand Down
3 changes: 2 additions & 1 deletion src/crates/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target/
target/
include/
2 changes: 1 addition & 1 deletion src/crates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ resolver = "2"
members = [
"quickjs",
"quickjs_gc"
]
]
17 changes: 16 additions & 1 deletion src/crates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@

## Prerequisites

1. `rust >= 1.70.0`, make sure using `rustup` to update your rust toolchain.
### `rust nightly`

We rely on features that are only available in `rust nightly`. To install `rust nightly`:

```bash
rustup default nightly
rustup update
```

### `cbindgen`

We use `cbindgen` to generate the C header file for the Rust library. To install `cbindgen`:

```bash
cargo install --force cbindgen
```
10 changes: 10 additions & 0 deletions src/crates/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Whether cbindgen's default C/C++ standard imports should be suppressed. These
# imports are included by default because our generated headers tend to require
# them (e.g. for uint32_t). Currently, the generated imports are:
#
# * for C: <stdarg.h>, <stdbool.h>, <stdint.h>, <stdlib.h>, <uchar.h>
#
# * for C++: <cstdarg>, <cstdint>, <cstdlib>, <new>, <cassert> (depending on config)
#
# default: false
no_includes = true
3 changes: 2 additions & 1 deletion src/crates/quickjs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
name = "quickjs"
version = "0.1.0"
edition = "2021"
build = "build.rs"

[lib]
name = "quickjs_rust"
name = "quickjs_rs"
crate-type = ["staticlib"]

[dependencies]
Expand Down
28 changes: 28 additions & 0 deletions src/crates/quickjs/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::env;
use std::path::PathBuf;
use std::process::Command;

fn main() {
env::set_var("RUST_BACKTRACE", "1");
let current_dir = env::current_dir().unwrap();
let working_dir = current_dir.parent().unwrap();

let home_dir = env::var("HOME").expect("HOME environment variable not found.");
let cbindgen_bin_path = PathBuf::from(&home_dir).join(".cargo/bin/cbindgen");

let output = Command::new(cbindgen_bin_path)
.current_dir(&working_dir)
.arg("--config")
.arg("cbindgen.toml")
.arg("--crate")
.arg("quickjs_gc") // quickjs_gc
.arg("--lang")
.arg("c")
.arg("--output")
.arg("include/quickjs-rs.h")
.output()
.expect("FAiled");

println!("{}", String::from_utf8_lossy(&output.stdout));
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
}
2 changes: 1 addition & 1 deletion src/crates/quickjs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub use quickjs_gc::{print_gc_objects};
pub use quickjs_gc::print_gc_objects;

0 comments on commit d9cf56e

Please sign in to comment.