Skip to content

Commit

Permalink
Merge pull request #74 from xxuejie/cleanup-work
Browse files Browse the repository at this point in the history
chore: Assorted cleanup work
  • Loading branch information
xxuejie committed Jan 3, 2024
2 parents c0cb2e1 + f087111 commit 8495d6b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "dl-c-impl/ckb-c-stdlib"]
path = dl-c-impl/ckb-c-stdlib
path = c/ckb-c-stdlib
url = https://github.com/nervosnetwork/ckb-c-stdlib.git
19 changes: 5 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ckb-std"
version = "0.14.3"
version = "0.15.0"
authors = ["Nervos network"]
edition = "2021"
license = "MIT"
Expand All @@ -10,25 +10,19 @@ exclude = ["docs"]

[package.metadata.docs.rs]
# All features except simulator and rustc-dep-of-std.
features = ["allocator", "ckb-types", "calc-hash", "dlopen-c", "dummy-libc", "ckb2023"]
features = ["allocator", "ckb-types", "calc-hash", "dlopen-c", "ckb2023"]
rustdoc-args = ["--cfg", "docsrs"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["allocator", "ckb-types", "dlopen-c", "dummy-libc", "calc-hash"]
default = ["allocator", "ckb-types", "libc", "calc-hash"]
calc-hash = ["ckb-types/calc-hash"]
allocator = ["buddy-alloc"]
simulator = ["ckb-x64-simulator"]
dlopen-c = []
dlopen-c = ["libc"]
build-with-clang = []
dummy-libc = []
rustc-dep-of-std = [
"alloc",
"core",
"compiler_builtins/rustc-dep-of-std",
"buddy-alloc/rustc-dep-of-std",
]
libc = []
ckb2023 = []

[build-dependencies]
Expand All @@ -38,9 +32,6 @@ cc = "1.0"
ckb-types = { package = "ckb-gen-types", version = "0.112", default-features = false, optional = true }
buddy-alloc = { version = "0.5.0", optional = true }
ckb-x64-simulator = { version = "0.8", optional = true }
alloc = { version = "1.0.0", optional = true, package = "rustc-std-workspace-alloc" }
core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core" }
compiler_builtins = { version = "0.1.0", optional = true }

[workspace]
exclude = ["test"]
107 changes: 58 additions & 49 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,77 @@
use std::env;

fn main() {
println!("cargo:rerun-if-changed=c/dlopen.c");
println!("cargo:rerun-if-changed=c/libc.c");

let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();

// ckb-std only supports riscv64 target arch
// but we can still use cargo check under other archs
if target_arch == "riscv64" && cfg!(feature = "dlopen-c") {
let mut build = cc::Build::new();
build
.file("dl-c-impl/lib.c")
.static_flag(true)
.flag("-fno-builtin-printf")
.flag("-fno-builtin-memcmp")
.flag("-nostdinc")
.flag("-nostdlib")
.flag("-fvisibility=hidden")
.flag("-fdata-sections")
.flag("-ffunction-sections")
.include("dl-c-impl/ckb-c-stdlib")
.include("dl-c-impl/ckb-c-stdlib/libc")
.flag("-Wall")
.flag("-Werror")
.flag("-Wno-unused-parameter")
.flag("-Wno-nonnull")
.define("__SHARED_LIBRARY__", None);
.file("c/dlopen.c")
.define("CKB_DECLARATION_ONLY", None);
setup_compiler_flags(&mut build);
build.compile("dl-c-impl");
}

if cfg!(feature = "build-with-clang") {
let clang = match std::env::var_os("CLANG") {
Some(val) => val,
None => "clang-16".into(),
};
if target_arch == "riscv64" && cfg!(feature = "libc") {
let mut build = cc::Build::new();
build.file("c/libc.c").define("__SHARED_LIBRARY__", None);
setup_compiler_flags(&mut build);
build.compile("libc");
}
}

build.compiler(clang);
}
fn setup_compiler_flags(build: &mut cc::Build) {
build
.static_flag(true)
.flag("-fno-builtin-printf")
.flag("-fno-builtin-memcmp")
.flag("-nostdinc")
.flag("-nostdlib")
.flag("-fvisibility=hidden")
.flag("-fdata-sections")
.flag("-ffunction-sections")
.include("c/ckb-c-stdlib")
.include("c/ckb-c-stdlib/libc")
.flag("-Wall")
.flag("-Werror")
.flag("-Wno-unused-parameter")
.flag("-Wno-nonnull");

let compiler = build.get_compiler();
if compiler.is_like_clang() {
build
.no_default_flags(true)
.flag("--target=riscv64")
.flag("-march=rv64imc_zba_zbb_zbc_zbs");
let clang = match std::env::var_os("CLANG") {
Some(val) => val,
None => "clang-16".into(),
};

if env::var("DEBUG").map(|v| v != "false").unwrap_or(false) {
build.flag("-g").flag("-fno-omit-frame-pointer");
}
if cfg!(feature = "build-with-clang") {
build.compiler(clang);
}

let opt_level = env::var("OPT_LEVEL").expect("fetching OPT_LEVEL");
if opt_level == "z" {
build.flag("-Os");
} else {
build.flag(&format!("-O{}", opt_level));
}
} else if compiler.is_like_gnu() {
build
.flag("-nostartfiles")
.flag("-Wno-dangling-pointer")
.flag("-Wno-nonnull-compare");
}
let compiler = build.get_compiler();
if compiler.is_like_clang() {
build
.no_default_flags(true)
.flag("--target=riscv64")
.flag("-march=rv64imc_zba_zbb_zbc_zbs");

build.compile("dl-c-impl");
}
if env::var("DEBUG").map(|v| v != "false").unwrap_or(false) {
build.flag("-g").flag("-fno-omit-frame-pointer");
}

if target_arch == "riscv64" && target_os == "ckb" && cfg!(feature = "dummy-libc") {
println!("cargo:rustc-link-lib=dummylibc");
let opt_level = env::var("OPT_LEVEL").expect("fetching OPT_LEVEL");
if opt_level == "z" {
build.flag("-Os");
} else {
build.flag(&format!("-O{}", opt_level));
}
} else if compiler.is_like_gnu() {
build
.flag("-nostartfiles")
.flag("-Wno-dangling-pointer")
.flag("-Wno-nonnull-compare");
}
}
1 change: 1 addition & 0 deletions c/ckb-c-stdlib
Submodule ckb-c-stdlib added at bdc87e
File renamed without changes.
3 changes: 3 additions & 0 deletions c/libc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <stdlib.h>
#include <string.h>
#include <memory.h>
2 changes: 1 addition & 1 deletion contracts/ckb-std-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ckb-std = { path = "../../" }
ckb-std = { path = "../../", features = [ "dlopen-c" ] }
blake2b-ref = { version = "0.3", default-features = false }
1 change: 0 additions & 1 deletion dl-c-impl/ckb-c-stdlib
Submodule ckb-c-stdlib deleted from b51f9b

0 comments on commit 8495d6b

Please sign in to comment.