diff --git a/.cargo/config.toml b/.cargo/config.toml index 8b775af..4c223be 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,5 +1,6 @@ [alias] xtask = "run --package xtask --" +xtask-dbg = "run --package xtask --features dbg --" [build] rustflags = ["-Clink-arg=-fuse-ld=lld"] diff --git a/Cargo.lock b/Cargo.lock index eb3af56..ffdd586 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -799,6 +799,15 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +[[package]] +name = "litrs" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5" +dependencies = [ + "proc-macro2", +] + [[package]] name = "log" version = "0.4.21" @@ -1244,6 +1253,27 @@ dependencies = [ "serde", ] +[[package]] +name = "sh" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40748b229ba0f836ef0d162b4f479b7adc660494bb89c527dafaa7e98f9eb5a2" +dependencies = [ + "sh-macro", + "thiserror", +] + +[[package]] +name = "sh-macro" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16da17e38a2b1390a7ca0ec7251369bf0e06726ebb7df0a0236d3e90cbdf8384" +dependencies = [ + "litrs", + "proc-macro2", + "quote", +] + [[package]] name = "slab" version = "0.4.9" @@ -1751,5 +1781,6 @@ dependencies = [ "clap", "serde", "serde_json", + "sh", "xshell", ] diff --git a/Cargo.toml b/Cargo.toml index a22a792..234309a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ assets = [ "644", ], [ - "target/release/out/rsjudge.1", + "target/assets/rsjudge.1", "usr/share/man/man1/rsjudge.1", "644", ], @@ -47,28 +47,28 @@ assets = [ "644", ], [ - "target/release/out/rsjudge.bash", + "target/assets/rsjudge.bash", "usr/share/bash-completion/completions/rsjudge", "644", ], [ - "target/release/out/rsjudge.fish", + "target/assets/rsjudge.fish", "usr/share/fish/vendor_completions.d/rsjudge.fish", "644", ], [ - "target/release/out/_rsjudge", + "target/assets/_rsjudge", "usr/share/zsh/vendor-completions/_rsjudge", "644", ], ] copyright = "2023-2024 NJUPT-SAST" -changelog = "debian/changelog" +changelog = "packaging/deb/changelog" features = ["default"] conf-files = ["/etc/rsjudge/server.toml", "/etc/rsjudge/executors.toml"] extended-description = "An online judge sandbox server in Rust, inspired by go-judge, for SAST OJ." maintainer = "Xiaoqing Xuan " -maintainer-scripts = "debian/" +maintainer-scripts = "packaging/deb/" depends = "$auto, adduser, libc6, libgcc-s1, libstdc++6, libssl1.1, libzstd1, libzstd-dev" recommends = "gcc, g++, python3" suggests = "pypy3" @@ -108,7 +108,6 @@ rest = ["dep:rsjudge-rest"] default = ["grpc"] [build-dependencies] -anyhow = "1.0.81" clap = { version = "4.5.3", features = ["derive"] } clap_complete = "4.5.1" clap_mangen = "0.2.20" diff --git a/build.rs b/build.rs index eb260f8..fdade80 100644 --- a/build.rs +++ b/build.rs @@ -3,9 +3,9 @@ mod cli; use std::{ env::var_os, - fs::File, - io::{self, ErrorKind, Write}, - path::PathBuf, + fs::{create_dir_all, File}, + io::{self, Write}, + path::Path, }; use clap::CommandFactory; @@ -14,16 +14,21 @@ use clap_mangen::Man; use crate::cli::Args; -fn main() -> anyhow::Result<()> { - let out_dir = PathBuf::from(var_os("OUT_DIR").ok_or(io::Error::from(ErrorKind::NotFound))?); +fn main() -> io::Result<()> { + let asset_dir = Path::new( + &var_os("CARGO_MANIFEST_DIR").expect("Environment `CARGO_MANIFEST_DIR` not set by cargo."), + ) + .join("target/assets"); + + create_dir_all(&asset_dir)?; let mut cmd = Args::command(); for shell in [Shell::Bash, Shell::Fish, Shell::Zsh] { - generate_to(shell, &mut cmd, "rsjudge", &out_dir)?; + generate_to(shell, &mut cmd, "rsjudge", &asset_dir)?; } - let mut manpage = File::create(out_dir.join("rsjudge.1"))?; + let mut manpage = File::create(asset_dir.join("rsjudge.1"))?; Man::new(cmd).render(&mut manpage)?; manpage.flush()?; diff --git a/debian/changelog b/packaging/deb/changelog similarity index 100% rename from debian/changelog rename to packaging/deb/changelog diff --git a/debian/postinst b/packaging/deb/postinst similarity index 100% rename from debian/postinst rename to packaging/deb/postinst diff --git a/debian/postrm b/packaging/deb/postrm similarity index 100% rename from debian/postrm rename to packaging/deb/postrm diff --git a/debian/service b/packaging/deb/service similarity index 100% rename from debian/service rename to packaging/deb/service diff --git a/debian/tmpfile b/packaging/deb/tmpfile similarity index 100% rename from debian/tmpfile rename to packaging/deb/tmpfile diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index ac3bcf4..973fb1a 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -13,4 +13,8 @@ anyhow = "1.0.80" clap = { version = "4.5.1", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.114" +sh = "0.2.1" xshell = "0.2.5" + +[features] +dbg = [] diff --git a/xtask/src/dist/deb.rs b/xtask/src/dist/deb.rs deleted file mode 100644 index 3893009..0000000 --- a/xtask/src/dist/deb.rs +++ /dev/null @@ -1,21 +0,0 @@ -use xshell::Shell; - -#[cfg(unix)] -pub(crate) fn deb_package(sh: Shell) -> anyhow::Result<()> { - use xshell::cmd; - - use crate::dist::prepare_out_dir; - - prepare_out_dir(&sh)?; - - cmd!(sh, "cargo deb -v").run()?; - - Ok(()) -} - -#[cfg(not(unix))] -pub(crate) fn deb_package(sh: Shell) -> anyhow::Result<()> { - use anyhow::anyhow; - - Err(anyhow!("Not supported on non-unix platforms.")) -} diff --git a/xtask/src/dist/mod.rs b/xtask/src/dist/mod.rs deleted file mode 100644 index 27ff8b6..0000000 --- a/xtask/src/dist/mod.rs +++ /dev/null @@ -1,84 +0,0 @@ -use std::path::PathBuf; - -use anyhow::anyhow; -use serde::{Deserialize, Serialize}; -use serde_json::from_str; -use xshell::{cmd, Shell}; - -pub(crate) mod deb; -pub(crate) mod rpm; - -#[derive(Deserialize, Debug, Serialize)] -#[serde(tag = "reason", rename_all = "kebab-case")] -pub(crate) enum CargoCheckMessage { - CompilerArtifact, - BuildScriptExecuted { - package_id: String, - out_dir: PathBuf, - }, - BuildFinished, -} - -#[derive(Debug, Clone, Copy)] -pub(crate) enum Profile { - Release, - Debug, -} - -impl Profile { - fn flag(self) -> Option<&'static str> { - match self { - Profile::Release => Some("--release"), - Profile::Debug => None, - } - } -} - -/// Get the `OUT_DIR` directory path of a specified build profile. This will run `cargo build` under the hood. -// TODO: This function should be run only once during CI build process. -pub(crate) fn build_script_out_dir( - sh: &Shell, - profile: Profile, - build: bool, -) -> anyhow::Result { - let pkgid = cmd!(sh, "cargo pkgid").read()?; - let pkgid = pkgid - .split_once('#') - .unwrap_or_else(|| panic!("Unexpected pkgid: {:?}", pkgid)); - - let pkg_pattern = format!("({})", pkgid.0); - - let flag = profile.flag(); - if build { - cmd!(sh, "cargo build --locked {flag...} --message-format=json") - } else { - cmd!(sh, "cargo check --locked {flag...} --message-format=json") - } - .read()? - .lines() - .find_map(|line| { - let msg = from_str::(line).expect("Not a valid message"); - match msg { - CargoCheckMessage::BuildScriptExecuted { - package_id, - out_dir, - } if package_id.ends_with(&pkg_pattern) => Some(out_dir), - _ => None, - } - }) - .ok_or(anyhow!("No build script executed.")) -} - -#[cfg(unix)] -fn prepare_out_dir(sh: &Shell) -> Result<(), anyhow::Error> { - const OUT_DIR: &str = "target/release/out"; - - use std::{fs::remove_dir_all, os::unix::fs::symlink}; - - let build_script_out_dir = build_script_out_dir(sh, Profile::Release, true)?; - - let _ = remove_dir_all(OUT_DIR); - - symlink(build_script_out_dir, OUT_DIR)?; - Ok(()) -} diff --git a/xtask/src/dist/rpm.rs b/xtask/src/dist/rpm.rs deleted file mode 100644 index e69de29..0000000 diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 45af8ad..d82cf38 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,8 +1,6 @@ use std::{env::set_current_dir, path::Path}; -use anyhow::anyhow; use clap::{Parser, ValueEnum}; -use dist::{build_script_out_dir, deb::deb_package, Profile}; use xshell::{cmd, Shell}; #[derive(Debug, ValueEnum, Clone, Copy, PartialEq, Eq)] @@ -18,8 +16,6 @@ enum Package { #[clap(about, long_about)] /// Build related tasks. enum Command { - /// Generate Rust modules from Protobuf definitions. - Codegen, /// Package distribution-specific packages. Dist { /// Which package to build. @@ -29,11 +25,10 @@ enum Command { /// Build Docker image. Docker, /// Debug a command. + #[cfg(feature = "dbg")] Debug, } -mod dist; - fn main() -> anyhow::Result<()> { let command = Command::parse(); @@ -44,18 +39,13 @@ fn main() -> anyhow::Result<()> { let sh = Shell::new()?; { match command { - Command::Codegen => cmd!(sh, "echo Not implemented"), Command::Dist { package } => match package { - Package::Deb => return deb_package(sh), - Package::Rpm => Err(anyhow!("Not implemented"))?, + Package::Deb => cmd!(sh, "cargo deb -v"), + Package::Rpm => todo!("Not implemented"), }, Command::Docker => cmd!(sh, "docker build -t rsjudge ."), - Command::Debug => { - return Ok(println!( - "{:#?}", - build_script_out_dir(&sh, Profile::Debug, false)? - )) - } + #[cfg(feature = "dbg")] + Command::Debug => return Ok(()), } .run()? }