1
1
فهرست منبع

Tweak build process

徐启航 1 سال پیش
والد
کامیت
5a6f17c098
5فایلهای تغییر یافته به همراه56 افزوده شده و 59 حذف شده
  1. 23 20
      .github/workflows/ci.yml
  2. 2 1
      README.md
  3. 2 1
      README.zh-cn.md
  4. 28 37
      xtask/src/dist.rs
  5. 1 0
      xtask/src/main.rs

+ 23 - 20
.github/workflows/ci.yml

@@ -2,33 +2,36 @@ name: CI
 
 on:
   push:
-    branches: [ "master" ]
+    branches: ["master"]
   pull_request:
-    branches: [ "master" ]
+    branches: ["master"]
 
 env:
   CARGO_TERM_COLOR: always
 
 jobs:
   build:
-
     runs-on: ubuntu-latest
 
     steps:
-    - uses: actions/checkout@v3
-    - name: Cache LLVM
-      id: cache-llvm
-      uses: actions/cache@v3
-      with:
-        path: ../llvm
-        key: llvm-14.0
-    - name: Install LLVM
-      uses: KyleMayes/install-llvm-action@v1
-      with:
-        version: "14.0"
-        directory: ../llvm
-        cached: ${{ steps.cache-llvm.output.cache-hit }}
-    - name: Create LLVM symlinks
-      run: ln -s ${{ env.LLVM_PATH }}/bin/llvm-ifs llvm-ifs && ln -s ${{ env.LLVM_PATH }}/bin/llvm-objdump llvm-objdump
-    - name: Build
-      run: cargo xtask dist --release img
+      - uses: actions/checkout@v3
+
+      - name: Install nasm
+        run: sudo apt install nasm
+
+      - name: Cache LLVM
+        id: cache-llvm
+        uses: actions/cache@v3
+        with:
+          path: ../llvm
+          key: llvm-14.0
+
+      - name: Install LLVM
+        uses: KyleMayes/install-llvm-action@v1
+        with:
+          version: "14.0"
+          directory: ../llvm
+          cached: ${{ steps.cache-llvm.output.cache-hit }}
+
+      - name: Build
+        run: cargo xtask dist --release img

+ 2 - 1
README.md

@@ -50,7 +50,8 @@ support aarch64 in the future.
    ```sh
    # Select the nightly channel for rust
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-   sudo apt install build-essential qemu-system-x86 llvm
+   sudo apt install build-essential qemu-system-x86 llvm-14 nasm
+   export LLVM_PATH="/usr/lib/llvm-14"
    ```
 
 2. Add the following target:

+ 2 - 1
README.zh-cn.md

@@ -45,7 +45,8 @@
    ```sh
    # 配置 Rust 时需要选择 nightly 通道
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-   sudo apt install build-essential qemu-system-x86 llvm
+   sudo apt install build-essential qemu-system-x86 llvm-14 nasm
+   export LLVM_PATH="/usr/lib/llvm-14"
    ```
 
 2. 添加下列目标:

+ 28 - 37
xtask/src/dist.rs

@@ -5,6 +5,7 @@ use std::{
     fs,
     path::{Path, PathBuf},
     process::Command,
+    sync::LazyLock,
 };
 
 use anyhow::Context;
@@ -14,6 +15,18 @@ use crate::{
     BOOTFS, DEBUG_DIR, H2O_BOOT, H2O_KERNEL, H2O_SYSCALL, H2O_TINIT, OC_BIN, OC_DRV, OC_LIB,
 };
 
+static CARGO: LazyLock<OsString> =
+    LazyLock::new(|| env::var_os("CARGO").unwrap_or_else(|| "cargo".into()));
+
+static LLVM: LazyLock<OsString> =
+    LazyLock::new(|| env::var_os("LLVM_PATH").unwrap_or_else(|| "/usr/lib/llvm-14".into()));
+
+static LLVM_OBJCOPY: LazyLock<PathBuf> =
+    LazyLock::new(|| Path::new(&*LLVM).join("bin/llvm-objcopy"));
+static LLVM_OBJDUMP: LazyLock<PathBuf> =
+    LazyLock::new(|| Path::new(&*LLVM).join("bin/llvm-objdump"));
+static LLVM_IFS: LazyLock<PathBuf> = LazyLock::new(|| Path::new(&*LLVM).join("bin/llvm-ifs"));
+
 #[derive(Debug, StructOpt)]
 pub enum Type {
     Img,
@@ -37,7 +50,6 @@ impl Dist {
     }
 
     pub fn build(self) -> Result<(), anyhow::Error> {
-        let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
         let src_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
         let target_root = env::var("CARGO_TARGET_DIR")
             .unwrap_or_else(|_| src_root.join("target").to_string_lossy().to_string());
@@ -56,7 +68,6 @@ impl Dist {
 
         // Build h2o_boot
         self.build_impl(
-            &cargo,
             "h2o_boot.efi",
             "BootX64.efi",
             src_root.join(H2O_BOOT),
@@ -66,12 +77,11 @@ impl Dist {
         .context("failed to build h2o_boot")?;
 
         // Build the VDSO
-        self.build_vdso(src_root, &cargo, &target_root)
+        self.build_vdso(src_root, &target_root)
             .context("failed to build VDSO")?;
 
         // Build h2o_kernel
         self.build_impl(
-            &cargo,
             "h2o",
             "KERNEL",
             src_root.join(H2O_KERNEL),
@@ -82,7 +92,6 @@ impl Dist {
 
         // Build h2o_tinit
         self.build_impl(
-            &cargo,
             "tinit",
             "TINIT",
             src_root.join(H2O_TINIT),
@@ -91,9 +100,9 @@ impl Dist {
         )
         .context("failed to build h2o_tinit")?;
 
-        self.build_lib(&cargo, src_root, &target_root)
+        self.build_lib(src_root, &target_root)
             .context("failed to build libraries")?;
-        self.build_bin(&cargo, src_root, &target_root)
+        self.build_bin(src_root, &target_root)
             .context("failed to build binaries or drivers")?;
 
         crate::gen::gen_bootfs(Path::new(BOOTFS).join("../BOOT.fs"))
@@ -113,19 +122,14 @@ impl Dist {
         Ok(())
     }
 
-    fn build_vdso(
-        &self,
-        src_root: &Path,
-        cargo: &String,
-        target_root: &String,
-    ) -> Result<(), anyhow::Error> {
+    fn build_vdso(&self, src_root: &Path, target_root: &String) -> Result<(), anyhow::Error> {
         let target_triple = src_root.join(".cargo/x86_64-pc-oceanic.json");
         let cd = src_root.join(H2O_SYSCALL);
         let ldscript = cd.join("syscall.ld");
 
         println!("Building VDSO");
 
-        let mut cmd = Command::new(cargo);
+        let mut cmd = Command::new(&*CARGO);
 
         let cmd = cmd.current_dir(&cd).arg("rustc").args([
             "--crate-type=cdylib",
@@ -154,7 +158,7 @@ impl Dist {
 
         fs::copy(bin_dir.join("libsv_call.so"), &path)?;
 
-        Command::new("llvm-ifs")
+        Command::new(&*LLVM_IFS)
             .arg("--input-format=ELF")
             .arg(format!(
                 "--output-elf={}/sysroot/usr/lib/libh2o.so",
@@ -164,7 +168,7 @@ impl Dist {
             .status()?
             .exit_ok()?;
 
-        let out = Command::new("llvm-objdump")
+        let out = Command::new(&*LLVM_OBJDUMP)
             .arg("--syms")
             .arg(&path)
             .output()?
@@ -187,18 +191,12 @@ impl Dist {
         Ok(())
     }
 
-    fn build_lib(
-        &self,
-        cargo: &str,
-        src_root: impl AsRef<Path>,
-        target_root: &str,
-    ) -> anyhow::Result<()> {
+    fn build_lib(&self, src_root: impl AsRef<Path>, target_root: &str) -> anyhow::Result<()> {
         let src_root = src_root.as_ref().join(OC_LIB);
         let bin_dir = Path::new(target_root).join("x86_64-pc-oceanic");
         let dst_root = Path::new(target_root).join("bootfs/lib");
 
         self.build_impl(
-            cargo,
             "libldso.so",
             "ld-oceanic.so",
             src_root.join("libc/ldso"),
@@ -206,7 +204,7 @@ impl Dist {
             &dst_root,
         )?;
 
-        Command::new("llvm-ifs")
+        Command::new(&*LLVM_IFS)
             .arg("--input-format=ELF")
             .arg(format!(
                 "--output-elf={}",
@@ -219,7 +217,6 @@ impl Dist {
             .exit_ok()?;
 
         self.build_impl(
-            cargo,
             "libco2.so",
             "libco2.so",
             src_root.join("libc"),
@@ -230,12 +227,7 @@ impl Dist {
         Ok(())
     }
 
-    fn build_bin(
-        &self,
-        cargo: &str,
-        src_root: impl AsRef<Path>,
-        target_root: &str,
-    ) -> anyhow::Result<()> {
+    fn build_bin(&self, src_root: impl AsRef<Path>, target_root: &str) -> anyhow::Result<()> {
         let bin_dir = Path::new(target_root).join("x86_64-pc-oceanic");
         let dep_root = Path::new(target_root).join("bootfs/lib");
 
@@ -259,7 +251,7 @@ impl Dist {
                     } else {
                         name
                     };
-                    self.build_impl(cargo, &dst_name, &dst_name, ent.path(), &bin_dir, &dst_root)?;
+                    self.build_impl(&dst_name, &dst_name, ent.path(), &bin_dir, &dst_root)?;
                     for dep in fs::read_dir(bin_dir.join(self.profile()).join("deps"))?.flatten() {
                         let name = dep.file_name();
                         match name.to_str() {
@@ -293,7 +285,6 @@ impl Dist {
 
     fn build_impl(
         &self,
-        cargo: &str,
         bin_name: impl AsRef<Path>,
         dst_name: impl AsRef<Path>,
         src_dir: impl AsRef<Path>,
@@ -302,7 +293,7 @@ impl Dist {
     ) -> anyhow::Result<()> {
         println!("Building {:?}", dst_name.as_ref());
 
-        let mut cmd = Command::new(cargo);
+        let mut cmd = Command::new(&*CARGO);
         let cmd = cmd.current_dir(src_dir).arg("build");
         if self.release {
             cmd.arg("--release");
@@ -324,12 +315,12 @@ impl Dist {
         {
             let mut sym_name = OsString::from(target_name.as_ref().as_os_str());
             sym_name.push(".sym");
-            Command::new("llvm-objcopy")
+            Command::new(&*LLVM_OBJCOPY)
                 .arg("--only-keep-debug")
                 .arg(&target_path)
                 .arg(dbg_dir.as_ref().join(sym_name))
                 .status()?;
-            Command::new("llvm-objcopy")
+            Command::new(&*LLVM_OBJCOPY)
                 .arg("--strip-debug")
                 .arg(&target_path)
                 .status()?;
@@ -351,7 +342,7 @@ impl Dist {
             txt_name.push(".txt");
             fs::write(
                 dbg_dir.as_ref().join(txt_name),
-                Command::new("llvm-objdump")
+                Command::new(&*LLVM_OBJDUMP)
                     .arg("-x")
                     .arg(&target_path)
                     .output()?

+ 1 - 0
xtask/src/main.rs

@@ -1,4 +1,5 @@
 #![feature(exit_status_error)]
+#![feature(once_cell)]
 
 use structopt::StructOpt;