Skip to content

Commit f4e4de7

Browse files
Ryan ClantonSchrodingerZhu
authored andcommitted
use cc instead of cmake
1 parent 17454b0 commit f4e4de7

File tree

4 files changed

+99
-100
lines changed

4 files changed

+99
-100
lines changed

snmalloc-sys/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ build = "build.rs"
1313
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1414

1515
[build-dependencies]
16-
cmake = "0.1"
16+
cc = {version = "1.0.67"}
1717

1818
[dependencies.libc]
1919
version = "0.2"
@@ -30,3 +30,4 @@ cache-friendly = []
3030
android-lld = []
3131
android-shared-stl = []
3232
native-cpu = []
33+
local_dynamic_tls = []

snmalloc-sys/build.rs

Lines changed: 71 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,75 @@
1-
use cmake::Config;
2-
31
fn main() {
4-
let mut cfg = &mut Config::new("snmalloc");
5-
6-
let build_type = if cfg!(feature = "debug") {
7-
"Debug"
8-
} else {
9-
"Release"
10-
};
11-
12-
cfg = cfg.define("SNMALLOC_RUST_SUPPORT", "ON")
13-
.profile(build_type);
2+
let mut build = cc::Build::new();
3+
build.include("snmalloc/src");
4+
build.file("snmalloc/src/override/rust.cc".to_string());
5+
build.flag_if_supported("/O2");
6+
build.flag_if_supported("/W4");
7+
build.flag_if_supported("/WX");
8+
build.flag_if_supported("/wd4127");
9+
build.flag_if_supported("/wd4324");
10+
build.flag_if_supported("/wd4201");
11+
build.flag_if_supported("/Ob2");
12+
build.flag_if_supported("/DNDEBUG");
13+
build.flag_if_supported("/EHsc");
14+
build.flag_if_supported("/std:c++17");
15+
build.flag_if_supported("-O3");
16+
build.flag_if_supported("-Wc++17-extensions");
17+
build.flag_if_supported("-std=c++1z");
18+
build.flag_if_supported("-std=gnu++1z");
19+
build.flag_if_supported("-mcx16");
20+
build.flag_if_supported("-fno-exceptions");
21+
build.flag_if_supported("-fno-rtti");
22+
build.flag_if_supported("-g");
23+
build.flag_if_supported("-fomit-frame-pointer");
24+
build.cpp(true);
25+
build.debug(false);
1426

1527
let triple = std::env::var("TARGET").unwrap();
16-
if triple.contains("android") {
17-
if let Ok(ndk) = std::env::var("ANDROID_NDK") {
18-
cfg = cfg.define("CMAKE_TOOLCHAIN_FILE", format!("{}/build/cmake/android.toolchain.cmake", ndk));
19-
} else {
20-
eprintln!("please set ANDROID_NDK environment variable");
21-
std::process::abort();
22-
}
23-
24-
if let Ok(platform) = std::env::var("ANDROID_PLATFORM") {
25-
cfg = cfg.define("ANDROID_PLATFORM", platform);
26-
}
28+
let target_os = std::env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!");
29+
let target_env = std::env::var("CARGO_CFG_TARGET_ENV").expect("target_env not defined!");
30+
let target_family = std::env::var("CARGO_CFG_TARGET_FAMILY").expect("target family not set");
2731

32+
33+
if triple.contains("android") {
2834
if cfg!(feature = "android-lld") {
29-
cfg = cfg.define("ANDROID_LD", "lld");
35+
build.define("ANDROID_LD", "lld");
3036
}
3137

3238
if cfg!(feature = "android-shared-stl") {
33-
println!("cargo:rustc-link-lib=dylib=c++_shared");
34-
cfg = cfg.define("ANDROID_STL", "c++_shared");
39+
build.define("ANDROID_STL", "c++_shared");
3540
}
3641

3742
if triple.contains("aarch64") {
38-
cfg = cfg.define("ANDROID_ABI", "arm64-v8a");
43+
build.define("ANDROID_ABI", "arm64-v8a");
3944
} else if triple.contains("armv7") {
40-
cfg = cfg.define("ANDROID_ABI", "armeabi-v7a")
41-
.define("ANDROID_ARM_MODE", "arm");
45+
build.define("ANDROID_ABI", "armeabi-v7a");
46+
build.define("ANDROID_ARM_MODE", "arm");
4247
} else if triple.contains("x86_64") {
43-
cfg = cfg.define("ANDROID_ABI", "x86_64");
48+
build.define("ANDROID_ABI", "x86_64");
4449
} else if triple.contains("i686") {
45-
cfg = cfg.define("ANDROID_ABI", "x86_64");
50+
build.define("ANDROID_ABI", "x86_64");
4651
} else if triple.contains("neon") {
47-
cfg = cfg.define("ANDROID_ABI", "armeabi-v7a with NEON")
52+
build.define("ANDROID_ABI", "armeabi-v7a with NEON");
4853
} else if triple.contains("arm") {
49-
cfg = cfg.define("ANDROID_ABI", "armeabi-v7a");
54+
build.define("ANDROID_ABI", "armeabi-v7a");
5055
}
5156
}
52-
53-
if cfg!(all(windows, target_env = "msvc")) {
54-
cfg = cfg.define("CMAKE_CXX_FLAGS_RELEASE", "/O2 /Ob2 /DNDEBUG /EHsc");
55-
cfg = cfg.define("CMAKE_C_FLAGS_RELEASE", "/O2 /Ob2 /DNDEBUG /EHsc");
57+
58+
if target_os=="windows" && target_env == "gnu" {
59+
build.define("CMAKE_SH", "CMAKE_SH-NOTFOUND");
60+
if cfg!(feature = "local_dynamic_tls") {
61+
build.flag_if_supported("-ftls-model=local-dynamic");
62+
} else {
63+
build.flag_if_supported("-ftls-model=initial-exec");
64+
}
5665
}
57-
58-
if cfg!(all(windows, target_env = "gnu")) {
59-
cfg = cfg.define("CMAKE_SH", "CMAKE_SH-NOTFOUND");
66+
67+
if target_family == "unix" && target_os != "haiku" {
68+
if cfg!(feature = "local_dynamic_tls") {
69+
build.flag_if_supported("-ftls-model=local-dynamic");
70+
} else {
71+
build.flag_if_supported("-ftls-model=initial-exec");
72+
}
6073
}
6174

6275
let target = if cfg!(feature = "1mib") {
@@ -68,77 +81,51 @@ fn main() {
6881
};
6982

7083
if cfg!(feature = "native-cpu") {
71-
cfg = cfg.define("SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE", "ON")
84+
build.define("SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE", "ON");
85+
build.flag_if_supported("-march=native");
7286
}
7387

7488
if cfg!(feature = "stats") {
75-
cfg = cfg.define("USE_SNMALLOC_STATS", "ON")
89+
build.define("USE_SNMALLOC_STATS", "ON");
7690
}
7791

7892
if cfg!(feature = "qemu") {
79-
cfg = cfg.define("SNMALLOC_QEMU_WORKAROUND", "ON")
93+
build.define("SNMALLOC_QEMU_WORKAROUND", "ON");
8094
}
8195

82-
let mut dst = if cfg!(feature = "cache-friendly") {
83-
cfg.define("CACHE_FRIENDLY_OFFSET", "64").build_target(target).build()
84-
} else {
85-
cfg.build_target(target).build()
86-
};
96+
if cfg!(feature = "cache-friendly") {
97+
build.define("CACHE_FRIENDLY_OFFSET", "64");
98+
}
8799

88-
dst.push("./build");
100+
build.compile(target);
89101

90-
println!("cargo:rustc-link-lib={}", target);
102+
if cfg!(feature = "android-shared-stl") {
103+
println!("cargo:rustc-link-lib=dylib=c++_shared");
104+
}
91105

92-
if cfg!(all(windows, target_env = "msvc")) {
106+
if target_env == "msvc" {
93107
println!("cargo:rustc-link-lib=dylib=mincore");
94-
println!("cargo:rustc-link-search=native={}/{}", dst.display(), build_type);
95-
} else {
96-
println!("cargo:rustc-link-search=native={}", dst.display());
97108
}
98109

99-
if cfg!(all(windows, target_env = "gnu")) {
100-
let stdout = std::process::Command::new("gcc")
101-
.args(&["-print-search-dirs"])
102-
.output().unwrap_or_else(|_| {
103-
eprintln!("Cannot run gcc.exe");
104-
std::process::abort();
105-
})
106-
.stdout;
107-
108-
let outputs = String::from_utf8(stdout)
109-
.unwrap_or_else(|_| {
110-
eprintln!("gcc output contains non-utf8 characters");
111-
std::process::abort();
112-
});
113-
114-
outputs.lines()
115-
.filter(|line| line.starts_with("libraries: ="))
116-
.map(|line| line.split_at("libraries: =".len()).1)
117-
.flat_map(|line| line.split(";"))
118-
.for_each(|path| {
119-
println!("cargo:rustc-link-search=native={}", path);
120-
});
121-
110+
if target_os=="windows" && target_env == "gnu" {
122111
println!("cargo:rustc-link-lib=dylib=stdc++");
123112
println!("cargo:rustc-link-lib=dylib=atomic");
124-
println!("cargo:rustc-link-lib=dylib=winpthread");
125-
println!("cargo:rustc-link-lib=dylib=gcc_s");
126113
}
127114

128-
if cfg!(target_os = "macos") {
115+
if target_os == "macos" {
129116
println!("cargo:rustc-link-lib=dylib=c++");
130117
}
131118

132-
if cfg!(target_os = "openbsd") {
119+
if target_os == "openbsd" {
133120
println!("cargo:rustc-link-lib=dylib=c++");
134121
}
135122

136-
if cfg!(target_os = "freebsd") {
123+
if target_os == "freebsd" {
137124
println!("cargo:rustc-link-lib=dylib=c++");
138125
}
139126

140-
if cfg!(target_os = "linux") {
127+
if target_os == "linux" {
141128
println!("cargo:rustc-link-lib=dylib=stdc++");
142129
println!("cargo:rustc-link-lib=dylib=atomic");
143-
}
130+
};
144131
}

snmalloc-sys/snmalloc

snmalloc-sys/src/lib.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,43 @@ use core::ffi::c_void;
55
use libc::size_t;
66

77
extern "C" {
8-
/// Allocate the memory with the given alignment and size.
9-
/// On success, it returns a pointer pointing to the required memory address.
8+
/// Allocate the memory with the given alignment and size.
9+
/// On success, it returns a pointer pointing to the required memory address.
1010
/// On failure, it returns a null pointer.
1111
/// The client must assure the following things:
1212
/// - `alignment` is greater than zero
1313
/// - `alignment` is a power of 2
1414
/// The program may be forced to abort if the constrains are not full-filled.
1515
pub fn rust_alloc(alignment: size_t, size: size_t) -> *mut c_void;
1616

17-
/// De-allocate the memory at the given address with the given alignment and size.
17+
/// De-allocate the memory at the given address with the given alignment and size.
1818
/// The client must assure the following things:
1919
/// - the memory is acquired using the same allocator and the pointer points to the start position.
2020
/// - `alignment` and `size` is the same as allocation
2121
/// The program may be forced to abort if the constrains are not full-filled.
2222
pub fn rust_dealloc(ptr: *mut c_void, alignment: size_t, size: size_t) -> c_void;
2323

24-
/// Re-allocate the memory at the given address with the given alignment and size.
25-
/// On success, it returns a pointer pointing to the required memory address.
24+
/// Re-allocate the memory at the given address with the given alignment and size.
25+
/// On success, it returns a pointer pointing to the required memory address.
2626
/// The memory content within the `new_size` will remains the same as previous.
2727
/// On failure, it returns a null pointer. In this situation, the previous memory is not returned to the allocator.
2828
/// The client must assure the following things:
2929
/// - the memory is acquired using the same allocator and the pointer points to the start position
3030
/// - `alignment` and `old_size` is the same as allocation
3131
/// - `alignment` fulfills all the requirements as `rust_alloc`
3232
/// The program may be forced to abort if the constrains are not full-filled.
33-
pub fn rust_realloc(ptr: *mut c_void, alignment: size_t, old_size: size_t, new_size: size_t) -> *mut c_void;
34-
33+
pub fn rust_realloc(
34+
ptr: *mut c_void,
35+
alignment: size_t,
36+
old_size: size_t,
37+
new_size: size_t,
38+
) -> *mut c_void;
39+
3540
/// Allocate `count` items of `size` length each.
3641
/// Returns `null` if `count * size` overflows or on out-of-memory.
3742
/// All items are initialized to zero.
3843
pub fn sn_calloc(count: usize, size: usize) -> *mut c_void;
39-
44+
4045
/// Allocate `size` bytes.
4146
/// Returns pointer to the allocated memory or null if out of memory.
4247
/// Returns a unique pointer if called with `size` 0.
@@ -55,7 +60,7 @@ extern "C" {
5560
/// Free previously allocated memory.
5661
/// The pointer `p` must have been allocated before (or be null).
5762
pub fn sn_free(p: *mut c_void);
58-
63+
5964
/// Return the available bytes in a memory block.
6065
pub fn sn_malloc_usable_size(p: *const c_void) -> usize;
6166
}
@@ -67,28 +72,34 @@ mod tests {
6772
#[test]
6873
fn it_frees_memory_malloc() {
6974
let ptr = unsafe { rust_alloc(8, 8) } as *mut u8;
70-
unsafe {*ptr = 127; assert_eq!(*ptr, 127)};
75+
unsafe {
76+
*ptr = 127;
77+
assert_eq!(*ptr, 127)
78+
};
7179
unsafe { rust_dealloc(ptr as *mut c_void, 8, 8) };
7280
}
7381
#[test]
7482
fn it_frees_memory_sn_malloc() {
7583
let ptr = unsafe { sn_malloc(8) } as *mut u8;
7684
unsafe { sn_free(ptr as *mut c_void) };
7785
}
78-
86+
7987
#[test]
8088
fn it_frees_memory_sn_realloc() {
8189
let ptr = unsafe { sn_malloc(8) } as *mut u8;
8290
let ptr = unsafe { sn_realloc(ptr as *mut c_void, 8) } as *mut u8;
8391
unsafe { sn_free(ptr as *mut c_void) };
8492
}
85-
93+
8694
#[test]
8795
fn it_reallocs_correctly() {
8896
let mut ptr = unsafe { rust_alloc(8, 8) } as *mut u8;
89-
unsafe {*ptr = 127; assert_eq!(*ptr, 127)};
97+
unsafe {
98+
*ptr = 127;
99+
assert_eq!(*ptr, 127)
100+
};
90101
ptr = unsafe { rust_realloc(ptr as *mut c_void, 8, 8, 16) } as *mut u8;
91-
unsafe {assert_eq!(*ptr, 127)};
102+
unsafe { assert_eq!(*ptr, 127) };
92103
unsafe { rust_dealloc(ptr as *mut c_void, 8, 16) };
93104
}
94105

0 commit comments

Comments
 (0)