Skip to content

Commit

Permalink
[chore] Add axlibc for Starry
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure-stars committed Nov 4, 2024
1 parent e81ea37 commit d2f7956
Show file tree
Hide file tree
Showing 122 changed files with 11,692 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ arceos-fada.itb
linker_*
crates/
minicom_output.log
.errorviz*
tools/axlibc
.errorviz*
22 changes: 22 additions & 0 deletions tools/axlibc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

src/libctypes_gen.rs

include/ax_pthread_mutex.h

build_*

lwext4*
64 changes: 64 additions & 0 deletions tools/axlibc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[package]
name = "axlibc"
version = "0.1.0"
edition = "2021"
authors = [
"Yuekai Jia <[email protected]>",
"yanjuguang <[email protected]>",
"wudashuai <[email protected]>",
"yfblock <[email protected]>",
"scPointer <[email protected]>",
"Shiping Yuan <[email protected]>",
]
description = "ArceOS user program library for C apps"
license = "GPL-3.0-or-later OR Apache-2.0"
homepage = "https://github.com/rcore-os/arceos"
repository = "https://github.com/rcore-os/arceos/tree/main/ulib/axlibc"
documentation = "https://rcore-os.github.io/arceos/axlibc/index.html"

[lib]
crate-type = ["lib", "staticlib"]

[features]
default = []

irq = ["arceos_posix_api/irq", "arch_boot/irq"]

# Multicore
smp = ["arceos_posix_api/smp", "arch_boot/smp"]

# Floating point/SIMD
fp_simd = ["axfeat/fp_simd", "arch_boot/fp_simd"]

# Memory
alloc = ["arceos_posix_api/alloc"]
tls = ["alloc", "axfeat/tls"]

# Multi-task
multitask = ["arceos_posix_api/multitask"]

# File system
fs = ["arceos_posix_api/fs", "fd"]

# Networking
net = ["arceos_posix_api/net", "fd"]

# Schedule policy
sched_rr = ["arch_boot/preempt", "axfeat/sched_rr"]
sched_cfs = ["arch_boot/preempt", "axfeat/sched_cfs"]

# Libc features
fd = []
pipe = ["arceos_posix_api/pipe"]
select = ["arceos_posix_api/select"]
epoll = ["arceos_posix_api/epoll"]

[dependencies]
axfeat = { git = "https://github.com/Starry-OS/axfeat.git" }
arceos_posix_api = { git = "https://github.com/Starry-OS/arceos_posix_api.git" }
axio = { git = "https://github.com/Starry-OS/axio.git" }
axerrno = { git = "https://github.com/Starry-OS/axerrno.git" }
arch_boot = { git = "https://github.com/Starry-OS/arch_boot.git" }

[build-dependencies]
bindgen ={ version = "0.69" }
73 changes: 73 additions & 0 deletions tools/axlibc/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::env;
use std::path::PathBuf;
use std::process::Command;

fn main() {
fn gen_c_to_rust_bindings(in_file: &str, out_file: &str) {
println!("cargo:rerun-if-changed={in_file}");

let allow_types = ["tm", "jmp_buf"];
let mut builder = bindgen::Builder::default()
.header(in_file)
.clang_arg("-I./include")
.derive_default(true)
.size_t_is_usize(false)
.use_core();
for ty in allow_types {
builder = builder.allowlist_type(ty);
}

builder
.generate()
.expect("Unable to generate c->rust bindings")
.write_to_file(out_file)
.expect("Couldn't write bindings!");
}

gen_c_to_rust_bindings("ctypes.h", "src/libctypes_gen.rs");

let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
if arch == "aarch64" {
aarch64_vfp_compile();
}
}

fn aarch64_vfp_compile() {
// 获取当前 crate 输出目录
let out_dir = env::var("OUT_DIR").unwrap();
// 指定汇编文件路径
let asm_file = PathBuf::from("src/vfp_setjmp.S");
let asm_out_file = PathBuf::from(&out_dir).join("vfp_setjmp.o");

// 编译汇编文件,增加 target-feature 选项
let status = Command::new("clang")
.args([
"-c",
asm_file.to_str().unwrap(),
"-o",
asm_out_file.to_str().unwrap(),
"-target",
"aarch64-unknown-none",
"-mfpu=neon",
])
.status()
.expect("failed to execute clang");
assert!(status.success(), "clang failed to compile assembly file");

// 打包对象文件为静态库
let lib_out_file = PathBuf::from(&out_dir).join("libvfp_setjmp.a");
let status = Command::new("ar")
.args([
"crus",
lib_out_file.to_str().unwrap(),
asm_out_file.to_str().unwrap(),
])
.status()
.expect("failed to execute ar");
assert!(status.success(), "ar failed to create static library");

// 指示 rustc 链接器链接汇编对象文件
println!("cargo:rerun-if-changed=src/vfp_setjmp.S");
println!("cargo:rustc-link-search={}", out_dir);
println!("cargo:rustc-link-lib=static=vfp_setjmp");
}
8 changes: 8 additions & 0 deletions tools/axlibc/c/assert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
#include <stdlib.h>

_Noreturn void __assert_fail(const char *expr, const char *file, int line, const char *func)
{
fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line);
abort();
}
17 changes: 17 additions & 0 deletions tools/axlibc/c/ctype.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int tolower(int c)
{
if (isupper(c))
return c | 32;
return c;
}

int toupper(int c)
{
if (islower(c))
return c & 0x5f;
return c;
}
98 changes: 98 additions & 0 deletions tools/axlibc/c/dirent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#ifdef AX_CONFIG_FS

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int closedir(DIR *dir)
{
int ret = close(dir->fd);
free(dir);
return ret;
}

DIR *fdopendir(int fd)
{
DIR *dir;
struct stat st;

if (fstat(fd, &st) < 0) {
return 0;
}
if (fcntl(fd, F_GETFL) & O_PATH) {
errno = EBADF;
return 0;
}
if (!S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return 0;
}
if (!(dir = calloc(1, sizeof(*dir)))) {
return 0;
}

fcntl(fd, F_SETFD, FD_CLOEXEC);
dir->fd = fd;
return dir;
}

int dirfd(DIR *d)
{
return d->fd;
}

// TODO
DIR *opendir(const char *__name)
{
unimplemented();
return NULL;
}

// TODO
struct dirent *readdir(DIR *__dirp)
{
unimplemented();
return NULL;
}

// TODO
int readdir_r(DIR *restrict dir, struct dirent *restrict buf, struct dirent **restrict result)
{
struct dirent *de;
int errno_save = errno;
int ret;

// LOCK(dir->lock);
errno = 0;
de = readdir(dir);
if ((ret = errno)) {
// UNLOCK(dir->lock);
return ret;
}
errno = errno_save;
if (de)
memcpy(buf, de, de->d_reclen);
else
buf = NULL;

// UNLOCK(dir->lock);
*result = buf;
return 0;
}

// TODO
void rewinddir(DIR *dir)
{
// LOCK(dir->lock);
lseek(dir->fd, 0, SEEK_SET);
dir->buf_pos = dir->buf_end = 0;
dir->tell = 0;
// UNLOCK(dir->lock);
}

#endif // AX_CONFIG_FS
39 changes: 39 additions & 0 deletions tools/axlibc/c/dlfcn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <dlfcn.h>
#include <pthread.h>
#include <stdio.h>

// TODO
int dladdr(const void *__address, Dl_info *__info)
{
unimplemented();
return 0;
}

// TODO
void *dlopen(const char *__file, int __mode)
{
unimplemented();
return NULL;
}

// TODO
char *dlerror()
{
unimplemented();
return NULL;
}

// TODO
void *dlsym(void *__restrict__ __handle, const char *__restrict__ __name)
{

unimplemented();
return NULL;
}

// TODO
int dlclose(void *p)
{
unimplemented();
return 0;
}
31 changes: 31 additions & 0 deletions tools/axlibc/c/env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

char *environ_[2] = {"dummy", NULL};
char **environ = (char **)environ_;

char *getenv(const char *name)
{
size_t l = strchrnul(name, '=') - name;
if (l && !name[l] && environ)
for (char **e = environ; *e; e++)
if (!strncmp(name, *e, l) && l[*e] == '=')
return *e + l + 1;
return 0;
}

// TODO
int setenv(const char *__name, const char *__value, int __replace)
{
unimplemented();
return 0;
}

// TODO
int unsetenv(const char *__name)
{
unimplemented();
return 0;
}
Loading

0 comments on commit d2f7956

Please sign in to comment.