Skip to content

Commit

Permalink
axstd: allow some apps to run with both std and axstd
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 committed Jul 2, 2023
1 parent 9c3ccfd commit f7c1dc3
Show file tree
Hide file tree
Showing 35 changed files with 255 additions and 105 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,37 @@ 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/sleep
run: cargo build -p arceos-sleep
- 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
4 changes: 2 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 <[email protected]>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libax = { path = "../../ulib/libax", 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 libax::display::{framebuffer_flush, framebuffer_info, DisplayInfo};
pub use std::os::arceos::display::{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 libax;
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 <[email protected]>"]
# 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", features = ["paging"], optional = true }
7 changes: 4 additions & 3 deletions apps/exception/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![no_main]
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

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

use std::arch::asm;
Expand All @@ -17,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
2 changes: 1 addition & 1 deletion apps/fs/shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ default = []
# 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 = ["alloc", "fs"] }
axstd = { path = "../../../ulib/axstd", features = ["alloc", "fs"], optional = true }
89 changes: 59 additions & 30 deletions apps/fs/shell/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
use std::fs::{self, File};
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 = 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 @@ -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 @@ -202,7 +231,7 @@ fn do_cd(mut args: &str) {
}
if !args.contains(char::is_whitespace) {
if let Err(e) = std::env::set_current_dir(args) {
print_err!("cd", args, e.as_str());
print_err!("cd", args, e);
}
} else {
print_err!("cd", "too many arguments");
Expand All @@ -211,7 +240,7 @@ fn do_cd(mut args: &str) {

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

fn do_uname(_args: &str) {
Expand Down Expand Up @@ -240,7 +269,7 @@ fn do_help(_args: &str) {

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

pub fn run_cmd(line: &[u8]) {
Expand Down
26 changes: 22 additions & 4 deletions apps/fs/shell/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
#![no_std]
#![no_main]
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

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

macro_rules! path_to_str {
($path:expr) => {{
#[cfg(not(feature = "axstd"))]
{
$path.to_str().unwrap() // Path/OsString -> &str
}
#[cfg(feature = "axstd")]
{
$path.as_str() // String -> &str
}
}};
}

mod cmd;

// #[cfg(feature = "use_ramfs")]
Expand All @@ -20,10 +34,14 @@ const SPACE: u8 = b' ';
const MAX_CMD_LEN: usize = 256;

fn print_prompt() {
print!("arceos:{}$ ", std::env::current_dir().unwrap());
print!(
"arceos:{}$ ",
path_to_str!(std::env::current_dir().unwrap())
);
std::io::stdout().flush().unwrap();
}

#[no_mangle]
#[cfg_attr(feature = "axstd", no_mangle)]
fn main() {
let mut stdin = std::io::stdin();
let mut stdout = std::io::stdout();
Expand Down
2 changes: 1 addition & 1 deletion apps/helloworld/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ authors = ["Yuekai Jia <[email protected]>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axstd = { path = "../../ulib/axstd" }
axstd = { path = "../../ulib/axstd", optional = true }
11 changes: 7 additions & 4 deletions apps/helloworld/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![no_std]
#![no_main]
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

#[no_mangle]
#[cfg(feature = "axstd")]
use axstd::println;

#[cfg_attr(feature = "axstd", no_mangle)]
fn main() {
axstd::println!("Hello, world!");
println!("Hello, world!");
}
2 changes: 1 addition & 1 deletion apps/memtest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ authors = ["Yuekai Jia <[email protected]>"]

[dependencies]
rand = { version = "0.8", default-features = false, features = ["small_rng"] }
axstd = { path = "../../ulib/axstd", features = ["alloc", "paging"] }
axstd = { path = "../../ulib/axstd", features = ["alloc", "paging"], optional = true }
7 changes: 4 additions & 3 deletions apps/memtest/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![no_std]
#![no_main]
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

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

use rand::{rngs::SmallRng, RngCore, SeedableRng};
Expand Down Expand Up @@ -37,7 +38,7 @@ fn test_btree_map(rng: &mut impl RngCore) {
println!("test_btree_map() OK!");
}

#[no_mangle]
#[cfg_attr(feature = "axstd", no_mangle)]
fn main() {
println!("Running memory tests...");

Expand Down
Loading

0 comments on commit f7c1dc3

Please sign in to comment.