Skip to content

Commit 491444a

Browse files
committed
feat: add clang sysroot
1 parent 28c7c5b commit 491444a

File tree

13 files changed

+247
-2
lines changed

13 files changed

+247
-2
lines changed

openthread-sys/build.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fn main() -> Result<()> {
1717
let host = env::var("HOST").unwrap();
1818
let target = env::var("TARGET").unwrap();
1919

20+
let use_gcc = env::var("CARGO_FEATURE_USE_GCC").is_ok();
2021
let force_esp_riscv_toolchain = env::var("CARGO_FEATURE_FORCE_ESP_RISCV_TOOLCHAIN").is_ok();
2122

2223
let pregen_bindings = env::var("CARGO_FEATURE_FORCE_GENERATE_BINDINGS").is_err();
@@ -33,16 +34,20 @@ fn main() -> Result<()> {
3334
// Nothing to do for ESP-IDF, `esp-idf-sys` will do everything for us
3435
None
3536
} else {
37+
// For clang, we can use our own cross-platform sysroot.
38+
// TODO: explicitly let cargo track changes to sysroot
39+
let clang_sysroot = (!use_gcc).then(|| crate_root_path.join("gen").join("sysroot"));
40+
3641
// Need to do on-the-fly build and bindings' generation
3742
let out = PathBuf::from(env::var_os("OUT_DIR").unwrap());
3843

3944
let builder = builder::OpenThreadBuilder::new(
40-
false,
45+
!use_gcc,
4146
crate_root_path.clone(),
4247
Some(target),
4348
Some(host),
4449
None,
45-
None,
50+
clang_sysroot,
4651
None,
4752
force_esp_riscv_toolchain,
4853
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// See: <https://en.cppreference.com/w/c/header/assert.html>
2+
#pragma once
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
// This is inspired by Clang's implementation of <assert.h>.
9+
#ifdef NDEBUG
10+
# define assert(...) ((void)0)
11+
#else
12+
// We just delegate to the `otPlatAssertFail` function, which will be provided
13+
// by our platform implementation.
14+
// TODO: Actually set OPENTHREAD_CONFIG_PLATFORM_ASSERT_MANAGEMENT to 1 so our
15+
// platform can handle this!
16+
_Noreturn void otPlatAssertFail(const char *aFilename, int aLineNumber);
17+
# define assert(...) ((__VA_ARGS__) ? ((void)0) : otPlatAssertFail(__FILE__, __LINE__))
18+
#endif
19+
20+
#ifdef __cplusplus
21+
} // extern "C"
22+
#endif
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// See: <https://en.cppreference.com/w/c/header/ctype.html>
2+
#pragma once
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
int iscntrl(int c);
9+
int isprint(int c);
10+
int isupper(int c);
11+
12+
#ifdef __cplusplus
13+
} // extern "C"
14+
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// See: <https://en.cppreference.com/w/c/header/errno.html>
2+
#pragma once
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
// HACK: By defining this we tell OpenThread's spinel.c that we don't provide
9+
// a `errno` macro. This makes our life slightly easier as long as we
10+
// don't actually use `errno` at runtime.
11+
#define SPINEL_PLATFORM_DOESNT_IMPLEMENT_ERRNO_VAR 1
12+
13+
#define EPERM 1
14+
#define ENOMEM 12
15+
#define EINVAL 22
16+
#define EPIPE 32
17+
#define ERANGE 34
18+
#define ENOBUFS 64
19+
#define EOVERFLOW 75
20+
#define EMSGSIZE 90
21+
#define EAFNOSUPPORT 97
22+
#define ENETDOWN 100
23+
#define ENETUNREACH 101
24+
#define ECONNABORTED 103
25+
#define ECONNRESET 104
26+
#define EISCONN 106
27+
#define ENOTCONN 107
28+
#define ETIMEDOUT 110
29+
#define ECONNREFUSED 111
30+
#define EHOSTDOWN 112
31+
#define EHOSTUNREACH 113
32+
33+
#ifdef __cplusplus
34+
} // extern "C"
35+
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// See: <https://en.cppreference.com/w/c/header/inttypes.html>
2+
#pragma once
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
// This file only needs to exist for mbedtls.
9+
10+
#ifdef __cplusplus
11+
} // extern "C"
12+
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// See: <https://en.cppreference.com/w/c/header/stdarg.html>
2+
#pragma once
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#define va_start(ap, param) __builtin_va_start(ap, param)
9+
#define va_end(ap) __builtin_va_end(ap)
10+
#define va_arg(ap, type) __builtin_va_arg(ap, type)
11+
12+
#define va_copy(dest, src) __builtin_va_copy(dest, src)
13+
14+
typedef __builtin_va_list va_list;
15+
16+
#ifdef __cplusplus
17+
} // extern "C"
18+
#endif
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// See: <https://en.cppreference.com/w/c/header/stddef.html>
2+
#pragma once
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
typedef __SIZE_TYPE__ size_t;
9+
10+
#ifdef __cplusplus
11+
#define NULL __null
12+
#else
13+
#define NULL ((void*)0)
14+
#endif
15+
16+
#define offsetof(t, d) __builtin_offsetof(t, d)
17+
18+
#ifdef __cplusplus
19+
} // extern "C"
20+
#endif
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// See: <https://en.cppreference.com/w/c/header/stdio.html>
2+
#pragma once
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <stdarg.h>
9+
#include <stddef.h>
10+
11+
// mbedtls uses `FILE` for one of its function declarations.
12+
// To ensure we're not actually using `FILE` at runtime we define it as an
13+
// opaque struct.
14+
typedef struct __forbidden_FILE FILE;
15+
16+
// The following two functions are defined by our `snprintf.c` file in `gen/support`.
17+
18+
int snprintf(char* s, size_t n, const char* format, ...);
19+
20+
int vsnprintf(char* s, size_t n, const char* format, va_list arg);
21+
22+
#ifdef __cplusplus
23+
} // extern "C"
24+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// See: <https://en.cppreference.com/w/c/header/stdlib.html>
2+
#pragma once
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <stdarg.h>
9+
#include <stddef.h>
10+
11+
// Called by both mbedtls and OpenThread in case of assertion failures.
12+
_Noreturn void exit(int status);
13+
14+
#ifdef __cplusplus
15+
} // extern "C"
16+
#endif
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// See: <https://en.cppreference.com/w/c/header/string.html>
2+
#pragma once
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
// Since <string.h> has to define `NULL` and `size_t`, let's re-export the
9+
// contents of <stddef.h> here.
10+
#include <stddef.h>
11+
12+
#define strcpy __builtin_strcpy
13+
#define strncpy __builtin_strncpy
14+
15+
#define strlen __builtin_strlen
16+
#define strcmp __builtin_strcmp
17+
#define strncmp __builtin_strncmp
18+
#define strchr __builtin_strchr
19+
#define strrchr __builtin_strrchr
20+
#define strstr __builtin_strstr
21+
22+
#define memcmp __builtin_memcmp
23+
24+
// We need memset to be a function rather than a macro because mbedtls
25+
// assigns it to a static variable. Clang doesn't allow built-in functions
26+
// to be called through a function pointer, so we have to wrap it in an inline
27+
// function.
28+
static inline void* memset(void* s, int c, size_t n) {
29+
return __builtin_memset(s, c, n);
30+
}
31+
32+
#define memcpy __builtin_memcpy
33+
#define memmove __builtin_memmove
34+
35+
#ifdef __cplusplus
36+
} // extern "C"
37+
#endif

0 commit comments

Comments
 (0)