Skip to content

Commit

Permalink
使用cargo管理一些C文件的编译,并且移动部分汇编到arch目录 (DragonOS-Community#447)
Browse files Browse the repository at this point in the history
* 使用cargo管理main.c的编译

* 使用build-scripts编译架构相关的c代码

* 删除elf.h
  • Loading branch information
fslongjin committed Nov 17, 2023
1 parent e4600f7 commit 46e234a
Show file tree
Hide file tree
Showing 36 changed files with 556 additions and 1,683 deletions.
6 changes: 2 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,9 @@
"mm-types.h": "c",
"vfs.h": "c",
"current.h": "c",
"proc-types.h": "c",
"traceback.h": "c",
"bitcount.h": "c",
"limits.h": "c",
"block.h": "c",
"blk_types.h": "c",
"mutex.h": "c",
"mount.h": "c",
"internal.h": "c",
Expand Down Expand Up @@ -173,7 +170,8 @@
"cmpxchg.h": "c",
"mman.h": "c",
"clocksource.h": "c",
"ata.h": "c"
"ata.h": "c",
"barrier": "c"
},
"C_Cpp.errorSquiggles": "enabled",
"esbonio.sphinx.confDir": "",
Expand Down
26 changes: 23 additions & 3 deletions build-scripts/kernel_build/src/cfiles/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use cc::Build;

use crate::utils::FileUtils;
use crate::{constant::ARCH_DIR_X86_64, utils::FileUtils};

use super::CFilesArch;

Expand All @@ -18,10 +18,26 @@ impl CFilesArch for X86_64CFilesArch {
}

fn setup_files(&self, _c: &mut Build, files: &mut Vec<PathBuf>) {
files.push(PathBuf::from("src/arch/x86_64/driver/hpet.c"));
files.push(arch_path("driver/hpet.c"));
// 获取`kernel/src/arch/x86_64/driver/apic`下的所有C文件
files.append(&mut FileUtils::list_all_files(
&PathBuf::from("src/arch/x86_64/driver/apic"),
&arch_path("driver/apic"),
Some("c"),
true,
));

files.append(&mut FileUtils::list_all_files(
&arch_path("init"),
Some("c"),
true,
));
files.append(&mut FileUtils::list_all_files(
&arch_path("asm"),
Some("c"),
true,
));
files.append(&mut FileUtils::list_all_files(
&arch_path("interrupt"),
Some("c"),
true,
));
Expand All @@ -36,3 +52,7 @@ impl CFilesArch for X86_64CFilesArch {
c.asm_flag("-m64");
}
}

fn arch_path(relative_path: &str) -> PathBuf {
PathBuf::from(format!("{}/{}", ARCH_DIR_X86_64, relative_path))
}
1 change: 1 addition & 0 deletions build-scripts/kernel_build/src/constant/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const ARCH_DIR_X86_64: &str = "src/arch/x86_64";
1 change: 1 addition & 0 deletions build-scripts/kernel_build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate cc;

mod bindgen;
mod cfiles;
mod constant;
mod kconfig;
mod utils;

Expand Down
13 changes: 4 additions & 9 deletions kernel/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,16 @@ export ASFLAGS := --64
LD_LIST := ""


kernel_subdirs := common driver debug arch exception smp syscall ktest libs time
kernel_subdirs := common driver debug exception smp syscall ktest libs time


main.o: main.c
# -fno-builtin: 不使用C语言内建函数
# The -m64 option sets int to 32bits and long and pointer to 64 bits and generates code for AMD’s x86-64 architecture.
$(CC) $(CFLAGS) -c main.c -o main.o

kernel_rust:
RUSTFLAGS="$(RUSTFLAGS_UNWIND)" cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json

all: kernel

@echo "Linking kernel..."
$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T link.lds --no-relax
$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a -T link.lds --no-relax
# 生成kallsyms
current_dir=$(pwd)

Expand All @@ -54,7 +49,7 @@ all: kernel
# 重新链接
@echo "Re-Linking kernel..."
@echo $(shell find . -name "*.o")
$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel main.o $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a ./debug/kallsyms.o -T link.lds --no-relax
$(LD) -b elf64-x86-64 -z muldefs $(LDFLAGS_UNWIND) -o kernel $(shell find . -name "*.o") ../target/x86_64-unknown-none/release/libdragonos_kernel.a ./debug/kallsyms.o -T link.lds --no-relax
@echo "Generating kernel ELF file..."
# 生成内核文件
ifeq ($(UNWIND_ENABLE), yes)
Expand All @@ -71,7 +66,7 @@ $(kernel_subdirs): ECHO

$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" kernel_root_path="$(shell pwd)"

kernel: main.o $(kernel_subdirs) kernel_rust
kernel: $(kernel_subdirs) kernel_rust



Expand Down
17 changes: 0 additions & 17 deletions kernel/src/arch/Makefile

This file was deleted.

23 changes: 0 additions & 23 deletions kernel/src/arch/x86_64/Makefile

This file was deleted.

22 changes: 0 additions & 22 deletions kernel/src/arch/x86_64/asm/Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/asm/head.S
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ ENTRY(_start64)
// 50-100M填0,共25个页表
mov $12800, %ecx
.fill_pt_64_2:
mov $0, 0(%eax)
movq $0, 0(%eax)
add $8, %eax
loop .fill_pt_64_2

Expand Down
Loading

0 comments on commit 46e234a

Please sign in to comment.