diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..e61d7736 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,35 @@ +name: lint + +on: + push: + branches: + - main + - ci + + pull_request: + branches: + - main + +env: + CARGO_TERM_COLOR: always + +jobs: + lint: + runs-on: ubuntu-20.04 + + steps: + - uses: actions/checkout@v2 + + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: nightly + components: rustfmt, clippy, miri, rust-src + override: true + + - name: Check formatting + run: | + cargo fmt --all -- --check + - name: Run clippy + run: | + cargo clippy --workspace -- --deny warnings diff --git a/Cargo.lock b/Cargo.lock index 95232cd0..8c2b3df0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,15 +26,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - [[package]] name = "anyhow" version = "1.0.57" @@ -113,7 +104,6 @@ dependencies = [ "llvm-sys", "log", "simplelog", - "structopt", "thiserror", "which", ] @@ -163,17 +153,41 @@ dependencies = [ [[package]] name = "clap" -version = "2.34.0" +version = "3.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750" dependencies = [ - "ansi_term", "atty", "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", "strsim", + "termcolor", "textwrap", - "unicode-width", - "vec_map", +] + +[[package]] +name = "clap_derive" +version = "3.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2 1.0.38", + "quote 1.0.18", + "syn 1.0.94", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", ] [[package]] @@ -288,14 +302,17 @@ version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "heck" -version = "0.3.3" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" [[package]] name = "hermit-abi" @@ -306,6 +323,16 @@ dependencies = [ "libc", ] +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "itoa" version = "1.0.2" @@ -408,6 +435,18 @@ dependencies = [ "memchr", ] +[[package]] +name = "once_cell" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" + +[[package]] +name = "os_str_bytes" +version = "6.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -595,33 +634,9 @@ dependencies = [ [[package]] name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - -[[package]] -name = "structopt" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10" -dependencies = [ - "clap", - "lazy_static", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.4.18" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2 1.0.38", - "quote 1.0.18", - "syn 1.0.94", -] +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" @@ -667,6 +682,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "tester" version = "0.7.0" @@ -680,12 +704,9 @@ dependencies = [ [[package]] name = "textwrap" -version = "0.11.0" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] +checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16" [[package]] name = "thiserror" @@ -717,12 +738,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "unicode-segmentation" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" - [[package]] name = "unicode-width" version = "0.1.9" @@ -741,12 +756,6 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04" -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - [[package]] name = "version_check" version = "0.9.4" @@ -786,6 +795,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index d9e4bf71..0b0c3e54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,8 +12,7 @@ edition = "2021" [dependencies] # cli deps -structopt = { version = "0.3", optional = true } -clap = { version = "2", optional = true } +clap = { version = "3.2", optional = true, features = ["derive"] } simplelog = {version = "0.7.6", optional = true} # lib deps @@ -38,7 +37,7 @@ llvm-proxy = [ "llvm-sys/disable-alltargets-init" ] lib = ["thiserror", "ar", "log"] -cli = ["structopt", "clap", "simplelog"] +cli = ["clap", "simplelog"] system-llvm = ["cli", "lib", "llvm-sys"] rust-llvm = ["cli", "lib", "llvm-proxy"] default = ["rust-llvm"] diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 00000000..4909344d --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,3 @@ +ignore = [ + "third-party", +] diff --git a/src/bin/bpf-linker.rs b/src/bin/bpf-linker.rs index eec02861..23e9f527 100644 --- a/src/bin/bpf-linker.rs +++ b/src/bin/bpf-linker.rs @@ -3,11 +3,11 @@ #[cfg(feature = "llvm-proxy")] extern crate aya_rustc_llvm_proxy; +use clap::Parser; use log::*; use simplelog::{Config, LevelFilter, SimpleLogger, TermLogger, TerminalMode, WriteLogger}; use std::{collections::HashSet, env, fs::File, str::FromStr}; use std::{fs, path::PathBuf}; -use structopt::StructOpt; use thiserror::Error; use bpf_linker::{Cpu, Linker, LinkerOptions, OptLevel, OutputType}; @@ -57,74 +57,74 @@ impl FromStr for CliOutputType { })) } } -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] struct CommandLine { /// LLVM target triple. When not provided, the target is inferred from the inputs - #[structopt(long)] + #[clap(long)] target: Option, /// Target BPF processor. Can be one of `generic`, `probe`, `v1`, `v2`, `v3` - #[structopt(long, default_value = "generic")] + #[clap(long, default_value = "generic")] cpu: Cpu, /// Enable or disable CPU features. The available features are: alu32, dummy, dwarfris. Use /// +feature to enable a feature, or -feature to disable it. For example /// --cpu-features=+alu32,-dwarfris - #[structopt(long, value_name = "features", default_value = "")] + #[clap(long, value_name = "features", default_value = "")] cpu_features: String, /// Write output to - #[structopt(short, long)] + #[clap(short, long)] output: PathBuf, /// Output type. Can be one of `llvm-bc`, `asm`, `llvm-ir`, `obj` - #[structopt(long, default_value = "obj")] + #[clap(long, default_value = "obj")] emit: CliOutputType, /// Add a directory to the library search path - #[structopt(short = "L", number_of_values = 1)] + #[clap(short = 'L', number_of_values = 1)] libs: Vec, /// Optimization level. 0-3, s, or z - #[structopt(short = "O", default_value = "2", multiple = true)] + #[clap(short = 'O', default_value = "2", multiple = true)] optimize: Vec, /// Export the symbols specified in the file `path`. The symbols must be separated by new lines - #[structopt(long, value_name = "path")] + #[clap(long, value_name = "path")] export_symbols: Option, /// Output logs to the given `path` - #[structopt(long, value_name = "path")] + #[clap(long, value_name = "path")] log_file: Option, /// Set the log level. Can be one of `off`, `info`, `warn`, `debug`, `trace`. - #[structopt(long, value_name = "level")] + #[clap(long, value_name = "level")] log_level: Option, /// Try hard to unroll loops. Useful when targeting kernels that don't support loops - #[structopt(long)] + #[clap(long)] unroll_loops: bool, /// Ignore `noinline`/`#[inline(never)]`. Useful when targeting kernels that don't support function calls - #[structopt(long)] + #[clap(long)] ignore_inline_never: bool, /// Dump the final IR module to the given `path` before generating the code - #[structopt(long, value_name = "path")] + #[clap(long, value_name = "path")] dump_module: Option, /// Extra command line arguments to pass to LLVM - #[structopt(long, value_name = "args", use_delimiter = true, multiple = true)] + #[clap(long, value_name = "args", use_delimiter = true, multiple = true)] llvm_args: Vec, /// Disable passing --bpf-expand-memcpy-in-order to LLVM. - #[structopt(long)] + #[clap(long)] disable_expand_memcpy_in_order: bool, /// Disble exporting memcpy, memmove, memset, memcmp and bcmp. Exporting /// those is commonly needed when LLVM does not manage to expand memory /// intrinsics to a sequence of loads and stores. - #[structopt(long)] + #[clap(long)] disable_memory_builtins: bool, /// Input files. Can be object files or static libraries @@ -132,25 +132,31 @@ struct CommandLine { // The options below are for wasm-ld compatibility /// Comma separated list of symbols to export. See also `--export-symbols` - #[structopt(long, value_name = "symbols", use_delimiter = true, multiple = true)] + #[clap(long, value_name = "symbols", use_delimiter = true, multiple = true)] export: Vec, - #[structopt(short = "l", use_delimiter = true, multiple = true, hidden = true)] - lib: Option, - #[structopt(long, hidden = true)] - debug: bool, - #[structopt(long, hidden = true)] - rsp_quoting: Option, - #[structopt(long, hidden = true)] - flavor: Option, - #[structopt(long, hidden = true)] - no_entry: bool, - #[structopt(long, hidden = true)] - gc_sections: bool, - #[structopt(long, hidden = true)] - strip_debug: bool, - #[structopt(long, hidden = true)] - strip_all: bool, + #[clap( + short = 'l', + long = "lib", + use_delimiter = true, + multiple = true, + hidden = true + )] + _lib: Option, + #[clap(long = "debug", hidden = true)] + _debug: bool, + #[clap(long = "rsp-quoting", hidden = true)] + _rsp_quoting: Option, + #[clap(long = "flavor", hidden = true)] + _flavor: Option, + #[clap(long = "no-entry", hidden = true)] + _no_entry: bool, + #[clap(long = "gc-sections", hidden = true)] + _gc_sections: bool, + #[clap(long = "strip-debug", hidden = true)] + _strip_debug: bool, + #[clap(long = "strip-all", hidden = true)] + _strip_all: bool, } fn main() { @@ -255,5 +261,5 @@ fn main() { } fn error(desc: &str, kind: clap::ErrorKind) -> ! { - clap::Error::with_description(desc, kind).exit(); + clap::Error::with_description(desc.to_string(), kind).exit(); } diff --git a/src/linker.rs b/src/linker.rs index ca8df3b1..efaccb6f 100644 --- a/src/linker.rs +++ b/src/linker.rs @@ -437,7 +437,7 @@ impl Linker { fn write_ir(&mut self, output: &CStr) -> Result<(), LinkerError> { info!("writing IR to {:?}", output); - unsafe { llvm::write_ir(self.module, &output) }.map_err(LinkerError::WriteIRError) + unsafe { llvm::write_ir(self.module, output) }.map_err(LinkerError::WriteIRError) } fn emit(&mut self, output: &CStr, output_type: LLVMCodeGenFileType) -> Result<(), LinkerError> { diff --git a/src/llvm/mod.rs b/src/llvm/mod.rs index a396fe2e..79d48bf6 100644 --- a/src/llvm/mod.rs +++ b/src/llvm/mod.rs @@ -35,7 +35,7 @@ pub unsafe fn init>(args: &[T], overview: &str) { LLVMInitializeBPFAsmPrinter(); LLVMInitializeBPFDisassembler(); - parse_command_line_options(&args, overview); + parse_command_line_options(args, overview); } unsafe fn parse_command_line_options>(args: &[T], overview: &str) { @@ -138,7 +138,7 @@ pub unsafe fn target_from_triple(triple: &CStr) -> Result pub unsafe fn target_from_module(module: LLVMModuleRef) -> Result { let triple = LLVMGetTarget(module); - target_from_triple(&CStr::from_ptr(triple)) + target_from_triple(CStr::from_ptr(triple)) } pub unsafe fn create_target_machine(