Skip to content

Commit

Permalink
ulib: make APIs in axstd more similar to rust std
Browse files Browse the repository at this point in the history
- now apps can be built with both std and ArceOS without modification
  • Loading branch information
equation314 committed Jul 24, 2023
1 parent 3013f67 commit 5956e65
Show file tree
Hide file tree
Showing 75 changed files with 1,101 additions and 719 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,41 @@ jobs:
run: make ARCH=${{ matrix.arch }} A=apps/c/udpserver
- name: Build c/iperf
run: make ARCH=${{ matrix.arch }} A=apps/c/iperf

build-apps-for-std:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
arch: [x86_64]
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ env.rust-toolchain }}
- name: Build helloworld
run: cargo build -p arceos-helloworld
- name: Build memtest
run: cargo build -p arceos-memtest
- name: Build exception
run: cargo build -p arceos-exception
- name: Build task/yield
run: cargo build -p arceos-yield
- name: Build task/parallel
run: cargo build -p arceos-parallel
- name: Build task/sleep
run: cargo build -p arceos-sleep
- name: Build task/priority
run: cargo build -p arceos-priority
- name: Build fs/shell
run: cargo build -p arceos-shell
- name: Build net/echoserver
run: cargo build -p arceos-echoserver
- name: Build net/httpclient
run: cargo build -p arceos-httpclient
- name: Build net/httpserver
run: cargo build -p arceos-httpserver
- name: Build net/udpserver
run: cargo build -p arceos-udpserver
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/display/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ authors = ["Shiping Yuan <robert_yuan@pku.edu.com>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axstd = { path = "../../ulib/axstd", features = ["display"] }
axstd = { path = "../../ulib/axstd", features = ["display"], optional = true }
embedded-graphics = "0.8"
2 changes: 1 addition & 1 deletion apps/display/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use embedded_graphics::pixelcolor::Rgb888;
use embedded_graphics::prelude::{RgbColor, Size};
use embedded_graphics::{draw_target::DrawTarget, prelude::OriginDimensions};

pub use axstd::display::{framebuffer_flush, framebuffer_info, DisplayInfo};
pub use axstd::os::arceos::axdisplay::{framebuffer_flush, framebuffer_info, DisplayInfo};

pub struct Display {
size: Size,
Expand Down
11 changes: 6 additions & 5 deletions apps/display/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![no_std]
#![no_main]
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

extern crate axstd;
mod display;
#[cfg(feature = "axstd")]
extern crate axstd as std;

mod display;
use display::*;

use embedded_graphics::{
Expand Down Expand Up @@ -77,7 +78,7 @@ fn test_gpu() -> i32 {
0
}

#[no_mangle]
#[cfg_attr(feature = "axstd", no_mangle)]
fn main() -> ! {
test_gpu();
loop {
Expand Down
2 changes: 1 addition & 1 deletion apps/exception/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ authors = ["Yuekai Jia <equation618@gmail.com>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axstd = { path = "../../ulib/axstd", features = ["paging"] }
axstd = { path = "../../ulib/axstd", optional = true }
2 changes: 0 additions & 2 deletions apps/exception/expect_debug_aarch64.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Found physcial memory regions:
boot stack (READ | WRITE | RESERVED)
.bss (READ | WRITE | RESERVED)
free memory (READ | WRITE | FREE)
Initialize global memory allocator...
Initialize kernel page table...
Initialize platform devices...
Primary CPU 0 init OK.
Running exception tests...
Expand Down
2 changes: 0 additions & 2 deletions apps/exception/expect_debug_riscv64.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Found physcial memory regions:
boot stack (READ | WRITE | RESERVED)
.bss (READ | WRITE | RESERVED)
free memory (READ | WRITE | FREE)
Initialize global memory allocator...
Initialize kernel page table...
Initialize platform devices...
Primary CPU 0 init OK.
Running exception tests...
Expand Down
3 changes: 1 addition & 2 deletions apps/exception/expect_debug_x86_64.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ Found physcial memory regions:
boot stack (READ | WRITE | RESERVED)
.bss (READ | WRITE | RESERVED)
free memory (READ | WRITE | FREE)
Initialize global memory allocator...
Initialize kernel page table...
Initialize platform devices...
Primary CPU 0 init OK.
Running exception tests...
#BP @ 0x[0-9a-f]\{16\}
Expand Down
13 changes: 8 additions & 5 deletions apps/exception/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#![no_std]
#![no_main]
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

use axstd::println;
use core::arch::asm;
#[cfg(feature = "axstd")]
extern crate axstd as std;

use std::arch::asm;
use std::println;

fn raise_break_exception() {
unsafe {
Expand All @@ -15,7 +18,7 @@ fn raise_break_exception() {
}
}

#[no_mangle]
#[cfg_attr(feature = "axstd", no_mangle)]
fn main() {
println!("Running exception tests...");
raise_break_exception();
Expand Down
5 changes: 2 additions & 3 deletions apps/fs/shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ authors = ["Yuekai Jia <equation618@gmail.com>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
use_ramfs = ["axfs/myfs", "dep:axfs_vfs", "dep:axfs_ramfs", "dep:crate_interface"]
use-ramfs = ["axstd/myfs", "dep:axfs_vfs", "dep:axfs_ramfs", "dep:crate_interface"]
default = []

[dependencies]
axfs = { path = "../../../modules/axfs", optional = true }
axfs_vfs = { path = "../../../crates/axfs_vfs", optional = true }
axfs_ramfs = { path = "../../../crates/axfs_ramfs", optional = true }
crate_interface = { path = "../../../crates/crate_interface", optional = true }
axstd = { path = "../../../ulib/axstd", features = ["fs"] }
axstd = { path = "../../../ulib/axstd", features = ["alloc", "fs"], optional = true }
102 changes: 66 additions & 36 deletions apps/fs/shell/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
use axstd::fs::{self, File};
use axstd::io::{self, prelude::*};
use axstd::{string::String, vec::Vec};
use std::fs::{self, File, FileType};
use std::io::{self, prelude::*};
use std::{string::String, vec::Vec};

#[cfg(all(not(feature = "axstd"), unix))]
use std::os::unix::fs::{FileTypeExt, PermissionsExt};

macro_rules! print_err {
($cmd: literal, $msg: literal) => {
($cmd: literal, $msg: expr) => {
println!("{}: {}", $cmd, $msg);
};
($cmd: literal, $err: ident) => {
use io::Error::*;
println!("{}: {}", $cmd, $err.as_str());
};
($cmd: literal, $arg: expr, $err: ident) => {
use io::Error::*;
println!("{}: {}: {}", $cmd, $arg, $err.as_str());
};
($cmd: literal, $arg: expr, $msg: expr) => {
println!("{}: {}: {}", $cmd, $arg, $msg);
($cmd: literal, $arg: expr, $err: expr) => {
println!("{}: {}: {}", $cmd, $arg, $err);
};
}

Expand All @@ -34,10 +29,47 @@ const CMD_TABLE: &[(&str, CmdHandler)] = &[
("uname", do_uname),
];

fn file_type_to_char(ty: FileType) -> char {
if ty.is_char_device() {
'c'
} else if ty.is_block_device() {
'b'
} else if ty.is_socket() {
's'
} else if ty.is_fifo() {
'p'
} else if ty.is_symlink() {
'l'
} else if ty.is_dir() {
'd'
} else if ty.is_file() {
'-'
} else {
'?'
}
}

#[rustfmt::skip]
const fn file_perm_to_rwx(mode: u32) -> [u8; 9] {
let mut perm = [b'-'; 9];
macro_rules! set {
($bit:literal, $rwx:literal) => {
if mode & (1 << $bit) != 0 {
perm[8 - $bit] = $rwx
}
};
}

set!(2, b'r'); set!(1, b'w'); set!(0, b'x');
set!(5, b'r'); set!(4, b'w'); set!(3, b'x');
set!(8, b'r'); set!(7, b'w'); set!(6, b'x');
perm
}

fn do_ls(args: &str) {
let current_dir = axstd::env::current_dir().unwrap();
let current_dir = std::env::current_dir().unwrap();
let args = if args.is_empty() {
current_dir.as_str()
path_to_str!(current_dir)
} else {
args
};
Expand All @@ -47,8 +79,8 @@ fn do_ls(args: &str) {
let metadata = fs::metadata(path)?;
let size = metadata.len();
let file_type = metadata.file_type();
let file_type_char = file_type.as_char();
let rwx = metadata.permissions().rwx_buf();
let file_type_char = file_type_to_char(file_type);
let rwx = file_perm_to_rwx(metadata.permissions().mode());
let rwx = unsafe { core::str::from_utf8_unchecked(&rwx) };
println!("{}{} {:>8} {}", file_type_char, rwx, size, entry);
Ok(())
Expand All @@ -70,9 +102,10 @@ fn do_ls(args: &str) {
entries.sort();

for entry in entries {
let path = String::from(name) + "/" + &entry;
if let Err(e) = show_entry_info(&path, &entry) {
print_err!("ls", path, e.as_str());
let entry = path_to_str!(entry);
let path = String::from(name) + "/" + entry;
if let Err(e) = show_entry_info(&path, entry) {
print_err!("ls", path, e);
}
}
Ok(())
Expand All @@ -83,7 +116,7 @@ fn do_ls(args: &str) {
println!();
}
if let Err(e) = list_one(name, name_count > 1) {
print_err!("ls", name, e.as_str());
print_err!("ls", name, e);
}
}
}
Expand All @@ -100,7 +133,7 @@ fn do_cat(args: &str) {
loop {
let n = file.read(&mut buf)?;
if n > 0 {
io::stdout().write(&buf[..n])?;
io::stdout().write_all(&buf[..n])?;
} else {
return Ok(());
}
Expand All @@ -109,7 +142,7 @@ fn do_cat(args: &str) {

for fname in args.split_whitespace() {
if let Err(e) = cat_one(fname) {
print_err!("cat", fname, e.as_str());
print_err!("cat", fname, e);
}
}
}
Expand Down Expand Up @@ -138,7 +171,7 @@ fn do_echo(args: &str) {
"\n",
];
if let Err(e) = echo_file(fname, &text_list) {
print_err!("echo", fname, e.as_str());
print_err!("echo", fname, e);
}
} else {
println!("{}", args)
Expand All @@ -157,11 +190,7 @@ fn do_mkdir(args: &str) {

for path in args.split_whitespace() {
if let Err(e) = mkdir_one(path) {
print_err!(
"mkdir",
format_args!("cannot create directory '{path}'"),
e.as_str()
);
print_err!("mkdir", format_args!("cannot create directory '{path}'"), e);
}
}
}
Expand Down Expand Up @@ -191,7 +220,7 @@ fn do_rm(args: &str) {
continue;
}
if let Err(e) = rm_one(path, rm_dir) {
print_err!("rm", format_args!("cannot remove '{path}'"), e.as_str());
print_err!("rm", format_args!("cannot remove '{path}'"), e);
}
}
}
Expand All @@ -201,17 +230,17 @@ fn do_cd(mut args: &str) {
args = "/";
}
if !args.contains(char::is_whitespace) {
if let Err(e) = axstd::env::set_current_dir(args) {
print_err!("cd", args, e.as_str());
if let Err(e) = std::env::set_current_dir(args) {
print_err!("cd", args, e);
}
} else {
print_err!("cd", "too many arguments");
}
}

fn do_pwd(_args: &str) {
let pwd = axstd::env::current_dir().unwrap();
println!("{}", pwd);
let pwd = std::env::current_dir().unwrap();
println!("{}", path_to_str!(pwd));
}

fn do_uname(_args: &str) {
Expand Down Expand Up @@ -239,7 +268,8 @@ fn do_help(_args: &str) {
}

fn do_exit(_args: &str) {
axstd::thread::exit(0);
println!("Bye~");
std::process::exit(0);
}

pub fn run_cmd(line: &[u8]) {
Expand Down
Loading

0 comments on commit 5956e65

Please sign in to comment.