Skip to content

Commit

Permalink
Add nginx document and optimize the code
Browse files Browse the repository at this point in the history
  • Loading branch information
BeichenY1 committed Apr 26, 2024
1 parent 9edffaa commit f579f71
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 73 deletions.
2 changes: 1 addition & 1 deletion 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ruxgo"
version = "0.9.5"
version = "0.9.6"
edition = "2021"
authors = ["Zewei Yang <[email protected]>"]
description = "A Cargo-like build tool for building C and C++ applications"
Expand Down
24 changes: 24 additions & 0 deletions apps/nginx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Build and Run [`nginx`](https://github.com/nginx/nginx) on RuxOS

Firstly, you need to copy `config_linux.toml` from `ruxgo/apps/nginx/ruxos` and place it in the `ruxos/apps/c/rux-nginx` at the same level as `nginx-1.24.0`.

Then, switch to `ruxos/apps/c/rux-nginx` directory. If `nginx-1.24.0` does not exist, execute the following prerequisite commands (if it does, it is not required):

```bash
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz && rm -f nginx-1.24.0.tar.gz
```

After that, you need to execute the following command:

```bash
./create_nginx_img.sh
```

Finally, execute the following commands to build and run it:

```bash
# Build and Run
ruxgo -b
ruxgo -r
```
108 changes: 52 additions & 56 deletions apps/nginx/ruxos/config_linux.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ compiler = "gcc"
[os]
name = "ruxos"
services = ["alloc","paging","irq","fp_simd","multitask","fs","blkfs",
"net","pipe","epoll","poll","rtc","select","virtio-9p"]
ulib = "ruxlibc"
"net","pipe","epoll","poll","rtc","select","virtio-9p","signal"]
ulib = "ruxmusl"

[os.platform]
name = "aarch64-qemu-virt"
smp = "4"
smp = "1"
mode = "release"
log = "info"

Expand All @@ -19,78 +19,80 @@ blk = "y"
net = "y"
v9p = "y"
v9p_path = "./html"
graphic = "n"
disk_img = "fat32.img"
args = "./nginx_app"
net_dev = "user"

### src to compile
[[targets]]
name = "libcore"
src = "nginx-1.24.0/src/core"
src = "./nginx-1.24.0/src/core"
src_exclude = ["ngx_regex.c","ngx_thread_pool.c"]
include_dir = ["./objs","nginx-1.24.0/src/core","nginx-1.24.0/src/event",
"nginx-1.24.0/src/event/modules","nginx-1.24.0/src/os/unix"]
type = "object"
cflags = "-Wno-format"
ldflags = "-r"
include_dir = ["./objs","./nginx-1.24.0/src/core","./nginx-1.24.0/src/event",
"./nginx-1.24.0/src/event/modules","./nginx-1.24.0/src/os/unix"]
type = "static"
cflags = ""
archive = "ar"
ldflags = "rcs"

[[targets]]
name = "libevent"
src = "nginx-1.24.0/src/event"
src = "./nginx-1.24.0/src/event"
src_exclude = ["/modules","ngx_event_acceptex.c","ngx_event_connectex.c",
"ngx_event_openssl_stapling.c","ngx_event_openssl.c"]
include_dir = ["./objs","nginx-1.24.0/src/core","nginx-1.24.0/src/event",
"nginx-1.24.0/src/event/modules","nginx-1.24.0/src/os/unix"]
type = "object"
cflags = "-Wno-format"
ldflags = "-r"
include_dir = ["./objs","./nginx-1.24.0/src/core","./nginx-1.24.0/src/event",
"./nginx-1.24.0/src/event/modules","./nginx-1.24.0/src/os/unix"]
type = "static"
cflags = ""
archive = "ar"
ldflags = "rcs"

[[targets]]
name = "libevent_modules"
src = "nginx-1.24.0/src/event/modules"
src = "./nginx-1.24.0/src/event/modules"
src_exclude = [
"ngx_devpoll_module.c","ngx_eventport_module.c",
"ngx_iocp_module.c","ngx_kqueue_module.c",
"ngx_poll_module.c","ngx_win32_poll_module.c",
"ngx_win32_select_module.c"
]
include_dir = ["./objs","nginx-1.24.0/src/core","nginx-1.24.0/src/event",
"nginx-1.24.0/src/event/modules","nginx-1.24.0/src/os/unix"]
type = "object"
cflags = "-Wno-format"
ldflags = "-r"
include_dir = ["./objs","./nginx-1.24.0/src/core","./nginx-1.24.0/src/event",
"./nginx-1.24.0/src/event/modules","./nginx-1.24.0/src/os/unix"]
type = "static"
cflags = ""
archive = "ar"
ldflags = "rcs"

[[targets]]
name = "libos_unix"
src = "nginx-1.24.0/src/os/unix"
src = "./nginx-1.24.0/src/os/unix"
src_exclude = [
"ngx_darwin_init.c","ngx_darwin_sendfile_chain.c",
"ngx_file_aio_read.c","ngx_freebsd_init.c",
"ngx_freebsd_sendfile_chain.c","ngx_linux_aio_read.c",
"ngx_solaris_init.c","ngx_solaris_sendfilev_chain.c",
"ngx_thread_cond.c","ngx_thread_id.c","ngx_thread_mutex.c"
]
include_dir = ["./objs","nginx-1.24.0/src/core","nginx-1.24.0/src/event",
"nginx-1.24.0/src/event/modules","nginx-1.24.0/src/os/unix"]
type = "object"
cflags = "-Wno-format"
ldflags = "-r"
include_dir = ["./objs","./nginx-1.24.0/src/core","./nginx-1.24.0/src/event",
"./nginx-1.24.0/src/event/modules","./nginx-1.24.0/src/os/unix"]
type = "static"
cflags = ""
archive = "ar"
ldflags = "rcs"

[[targets]]
name = "libhttp"
src = "nginx-1.24.0/src/http"
src = "./nginx-1.24.0/src/http"
src_exclude = ["/modules","/v2","ngx_http_file_cache.c",
"ngx_http_huff_decode.c","ngx_http_huff_encode.c"]
include_dir = ["nginx-1.24.0/src/http","nginx-1.24.0/src/http/modules","nginx-1.24.0/src/core",
"nginx-1.24.0/src/event","nginx-1.24.0/src/event/modules","nginx-1.24.0/src/os/unix","./objs"]
type = "object"
cflags = "-Wno-format"
ldflags = "-r"
include_dir = ["./nginx-1.24.0/src/http","./nginx-1.24.0/src/http/modules","./nginx-1.24.0/src/core",
"./nginx-1.24.0/src/event","./nginx-1.24.0/src/event/modules","./nginx-1.24.0/src/os/unix","./objs"]
type = "static"
cflags = ""
archive = "ar"
ldflags = "rcs"

[[targets]]
name = "libhttp_modules"
src = "nginx-1.24.0/src/http/modules"
src = "./nginx-1.24.0/src/http/modules"
src_exclude = [
"/perl","ngx_http_addition_filter_module.c",
"ngx_http_auth_basic_module.c","ngx_http_auth_request_module.c",
Expand All @@ -104,30 +106,24 @@ src_exclude = [
"ngx_http_slice_filter_module.c","ngx_http_ssl_module.c",
"ngx_http_stub_status_module.c","ngx_http_xslt_filter_module.c"
]
include_dir = ["nginx-1.24.0/src/http","nginx-1.24.0/src/http/modules",
"nginx-1.24.0/src/core","nginx-1.24.0/src/event",
"nginx-1.24.0/src/event/modules","nginx-1.24.0/src/os/unix","./objs"]
type = "object"
cflags = "-Wno-format"
ldflags = "-r"

[[targets]]
name = "libobjs"
src = "./objs"
include_dir = ["./objs","nginx-1.24.0/src/core","nginx-1.24.0/src/event",
"nginx-1.24.0/src/event/modules","nginx-1.24.0/src/os/unix"]
type = "object"
cflags = "-Wno-format"
ldflags = "-r"
include_dir = ["./nginx-1.24.0/src/http","./nginx-1.24.0/src/http/modules",
"./nginx-1.24.0/src/core","./nginx-1.24.0/src/event",
"./nginx-1.24.0/src/event/modules","./nginx-1.24.0/src/os/unix","./objs"]
type = "static"
cflags = ""
archive = "ar"
ldflags = "rcs"

### linker
[[targets]]
name = "ruxos_nginx"
src = ""
include_dir = ""
src = "./objs"
src_exclude = ["./objs/nginx-1.24.0"]
include_dir = ["./objs","./nginx-1.24.0/src/core","./nginx-1.24.0/src/event",
"./nginx-1.24.0/src/event/modules","./nginx-1.24.0/src/os/unix"]
type = "exe"
cflags = "-Wno-format"
cflags = ""
linker = "rust-lld -flavor gnu"
ldflags = ""
deps = ["libobjs","libcore","libevent","libevent_modules",
deps = ["libcore","libevent","libevent_modules",
"libos_unix","libhttp","libhttp_modules"]
32 changes: 17 additions & 15 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ pub fn run(
let path = Path::new(&os_config.platform.qemu.disk_img);
if path.exists() {
log(
LogLevel::Log,
LogLevel::Info,
&format!(
"disk image \"{}\" already exists!",
os_config.platform.qemu.disk_img
Expand All @@ -624,7 +624,7 @@ pub fn run(
}
// create loaded app file systems if needed
if !build_config.app.is_empty() && &os_config.platform.qemu.v9p == "y" {
crate_app_fs(build_config, os_config);
create_app_fs(build_config, os_config);
}
// enable qemu gdb guest if needed
if &os_config.platform.qemu.debug == "y" {
Expand Down Expand Up @@ -1148,41 +1148,43 @@ int main(int argc, char **argv)
}

// Creates the app file system and related content
pub fn crate_app_fs(build_config: &BuildConfig, os_config: &OSConfig) {
pub fn create_app_fs(build_config: &BuildConfig, os_config: &OSConfig) {
// Copys the dynamic_lib if needed
let actual_lib_name = "libc.so";
let lib_name = format!("ld-musl-{}.so.1", os_config.platform.arch.as_str());
let src_path = Path::new(RUXMUSL_DIR).join(format!("install/lib/{}", actual_lib_name));
let dest_path = Path::new(&os_config.platform.qemu.v9p_path).join("lib");
if let Err(e) = fs::create_dir_all(&dest_path) {
let lib_src = Path::new(RUXMUSL_DIR).join("install/lib/libc.so");
let lib_dest = Path::new(&os_config.platform.qemu.v9p_path).join("lib");
if let Err(e) = fs::create_dir_all(&lib_dest) {
log(
LogLevel::Error,
&format!("Failed to create directories: {}", e),
);
std::process::exit(1);
}
if src_path.exists() {
if let Err(err) = fs::copy(&src_path, dest_path.join(lib_name)) {
if lib_src.exists() {
if let Err(err) = fs::copy(&lib_src, lib_dest.join(lib_name)) {
log(LogLevel::Error, &format!("Failed to copy file: {}", err));
std::process::exit(1);
}
} else {
log(LogLevel::Error, "The ruxmusl dynamic library does not exist");
log(
LogLevel::Error,
"The ruxmusl dynamic library does not exist",
);
std::process::exit(1);
}

// Copys the bin file
let app_dest_bin = Path::new(&os_config.platform.qemu.v9p_path).join("bin");
if let Err(e) = fs::create_dir_all(&app_dest_bin) {
let app_src = Path::new(&build_config.app);
let app_dest = Path::new(&os_config.platform.qemu.v9p_path).join("bin");
if let Err(e) = fs::create_dir_all(&app_dest) {
log(
LogLevel::Error,
&format!("Failed to create bin directory: {}", e),
);
std::process::exit(1);
}
let app_src = Path::new(&build_config.app);
if let Some(app_filename) = app_src.file_name() {
let app_dest = app_dest_bin.join(app_filename);
if let Err(err) = fs::copy(app_src, app_dest) {
if let Err(err) = fs::copy(app_src, app_dest.join(app_filename)) {
log(
LogLevel::Error,
&format!("Binary file does not exist or path is incorrect: {}", err),
Expand Down

0 comments on commit f579f71

Please sign in to comment.