Skip to content

Commit

Permalink
feat: retire lazy_static and upgrade dependencies
Browse files Browse the repository at this point in the history
Signed-off-by: h1994st <[email protected]>
  • Loading branch information
h1994st committed Dec 11, 2024
1 parent b3c2251 commit 67fd861
Show file tree
Hide file tree
Showing 16 changed files with 421 additions and 402 deletions.
17 changes: 8 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ repository = "https://github.com/h1994st/rllvm"
version = "0.1.1"

[dependencies]
clap = {version = "~4.4.11", features = ["derive"]}
confy = "~0.5"
lazy_static = "~1.4"
log = "~0.4.20"
object = {version = "~0.32", features = ["all"]}
regex = "~1.10"
serde = {version = "~1.0.193", features = ["derive"]}
simple_logger = "~4.3.0"
which = "~5.0.0"
clap = {version = "~4.5.23", features = ["derive"]}
confy = "~0.6.1"
log = "~0.4.22"
object = {version = "~0.36.5", features = ["all"]}
regex = "~1.11.1"
serde = {version = "~1.0.216", features = ["derive"]}
simple_logger = "~5.0.0"
which = "~7.0.0"

[target.'cfg(target_vendor = "apple")'.dependencies]
glob = "~0.3.1"
Expand Down
68 changes: 53 additions & 15 deletions src/arg_parser.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
//! Command-line argument parser
use std::path::PathBuf;

use lazy_static::lazy_static;
use regex::Regex;

use crate::{
config::RLLVM_CONFIG,
constants::{ARG_EXACT_MATCH_MAP, ARG_PATTERNS},
config::rllvm_config,
constants::{arg_exact_match_map, arg_patterns},
error::Error,
utils::*,
};
use regex::Regex;
use std::{path::PathBuf, sync::OnceLock};

/// Compile mode
#[derive(Debug)]
Expand Down Expand Up @@ -93,10 +90,9 @@ impl CompilerArgsInfo {
self.input_files.push(flag.as_ref().to_string());

// Assembly files
lazy_static! {
static ref RE: Regex = Regex::new(r"\.(s|S)$").unwrap();
}
if RE.is_match(flag.as_ref()) {
static RE: OnceLock<Regex> = OnceLock::new();
let re = RE.get_or_init(|| Regex::new(r"\.(s|S)$").unwrap());
if re.is_match(flag.as_ref()) {
self.is_assembly = true;
}

Expand Down Expand Up @@ -313,7 +309,7 @@ impl CompilerArgsInfo {
let mut offset = 1;

// Try to match the flag exactly
if let Some(arg_info) = ARG_EXACT_MATCH_MAP.get(arg.as_str()) {
if let Some(arg_info) = arg_exact_match_map().get(arg.as_str()) {
// Consume more parameters
offset += self.consume_params(i, arg.to_string(), arg_info, &args);
} else if arg == "-Wl,--start-group" {
Expand All @@ -335,7 +331,7 @@ impl CompilerArgsInfo {
} else {
// Try to match a pattern
let mut matched = false;
for arg_pattern in ARG_PATTERNS.iter() {
for arg_pattern in arg_patterns().iter() {
let pattern = &arg_pattern.pattern;
let arg_info = &arg_pattern.arg_info;
if pattern.is_match(arg.as_str()) {
Expand Down Expand Up @@ -435,7 +431,7 @@ impl CompilerArgsInfo {

let conditions = [
(
RLLVM_CONFIG.is_configure_only(),
rllvm_config().is_configure_only(),
"we are in configure-only mode",
),
(
Expand Down Expand Up @@ -506,7 +502,7 @@ impl CompilerArgsInfo {
derive_object_and_bitcode_filepath(&src_filepath, self.is_compile_only)?;

// Update the bitcode filepath, if the bitcode store path is provided
if let Some(bitcode_store_path) = RLLVM_CONFIG.bitcode_store_path() {
if let Some(bitcode_store_path) = rllvm_config().bitcode_store_path() {
if bitcode_store_path.exists() {
// Obtain a new bitcode filename based on the hash of the source filepath
if bitcode_filepath.file_name().is_some() {
Expand Down Expand Up @@ -536,3 +532,45 @@ impl CompilerArgsInfo {
Ok(artifacts)
}
}

#[cfg(test)]
mod tests {
use super::CompilerArgsInfo;

fn test_parsing<F>(input: &str, check_func: F)
where
F: Fn(&CompilerArgsInfo) -> bool,
{
let mut args_info = CompilerArgsInfo::default();
let args: Vec<&str> = input.split_ascii_whitespace().collect();
let ret = args_info.parse_args(&args);
assert!(ret.is_ok());
assert!(check_func(ret.unwrap()));
}

fn test_parsing_lto_internal(input: &str) {
test_parsing(input, |args| args.is_lto());
}

#[test]
fn test_parsing_lto() {
let input = r#"-pthread -c -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -g -fdebug-prefix-map=/home/legend/makepkgs/python/src=/usr/src/debug -fno-semantic-interposition -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -g -fdebug-prefix-map=/home/legend/makepkgs/python/src=/usr/src/debug -fno-semantic-interposition -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -g -fdebug-prefix-map=/home/legend/makepkgs/python/src=/usr/src/debug -fno-semantic-interposition -flto -g -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -fprofile-instr-use=code.profclangd -I./Include/internal -I. -I./Include -D_FORTIFY_SOURCE=2 -D_FORTIFY_SOURCE=2 -fPIC -DPy_BUILD_CORE -DSOABI='"cpython-38-x86_64-linux-gnu"' -o Python/dynload_shlib.o ./Python/dynload_shlib.c"#;
test_parsing_lto_internal(input);

let input = r#"-pthread -c -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -g -fdebug-prefix-map=/home/legend/makepkgs/python/src=/usr/src/debug -fno-semantic-interposition -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -g -fdebug-prefix-map=/home/legend/makepkgs/python/src=/usr/src/debug -fno-semantic-interposition -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -g -fdebug-prefix-map=/home/legend/makepkgs/python/src=/usr/src/debug -fno-semantic-interposition -flto=thin -g -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -fprofile-instr-use=code.profclangd -I./Include/internal -I. -I./Include -D_FORTIFY_SOURCE=2 -D_FORTIFY_SOURCE=2 -fPIC -DPy_BUILD_CORE -DSOABI='"cpython-38-x86_64-linux-gnu"' -o Python/dynload_shlib.o ./Python/dynload_shlib.c"#;
test_parsing_lto_internal(input);
}

fn test_parsing_link_args_internal(input: &str, expected: usize) {
test_parsing(input, |args| args.link_args().len() == expected);
}

#[test]
fn test_parsing_link_args() {
let input = r#"-Wl,--fatal-warnings -Wl,--build-id=sha1 -fPIC -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,-z,defs -Wl,--as-needed -fuse-ld=lld -Wl,--icf=all -Wl,--color-diagnostics -flto=thin -Wl,--thinlto-jobs=8 -Wl,--thinlto-cache-dir=thinlto-cache -Wl,--thinlto-cache-policy,cache_size=10\%:cache_size_bytes=10g:cache_size_files=100000 -Wl,--lto-O0 -fwhole-program-vtables -Wl,--no-call-graph-profile-sort -m64 -Wl,-O2 -Wl,--gc-sections -Wl,--gdb-index -rdynamic -fsanitize=cfi-vcall -fsanitize=cfi-icall -pie -Wl,--disable-new-dtags -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o "./brotli" -Wl,--start-group @"./brotli.rsp" -Wl,--end-group -latomic -ldl -lpthread -lrt"#;
test_parsing_link_args_internal(input, 32);

let input = r#"1.c 2.c 3.c 4.c 5.c -Wl,--start-group 7.o 8.o 9.o -Wl,--end-group 10.c 11.c 12.c 13.c"#;
test_parsing_link_args_internal(input, 5);
}
}
4 changes: 2 additions & 2 deletions src/bin/rllvm_cc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::env;

use rllvm::{
compiler_wrapper::{llvm::ClangWrapper, CompilerKind, CompilerWrapper},
config::RLLVM_CONFIG,
config::rllvm_config,
error::Error,
};
use simple_logger::SimpleLogger;
Expand All @@ -13,7 +13,7 @@ pub fn rllvm_main(name: &str, compiler_kind: CompilerKind) -> Result<(), Error>
let args = &args[1..];

// Set log level
let log_level = RLLVM_CONFIG.log_level().to_level_filter();
let log_level = rllvm_config().log_level().to_level_filter();
SimpleLogger::new()
.with_level(log_level)
.init()
Expand Down
6 changes: 3 additions & 3 deletions src/compiler_wrapper/llvm/clang_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::path::{Path, PathBuf};

use crate::{
arg_parser::CompilerArgsInfo, compiler_wrapper::*, config::RLLVM_CONFIG, error::Error,
arg_parser::CompilerArgsInfo, compiler_wrapper::*, config::rllvm_config, error::Error,
};

#[derive(Debug)]
Expand All @@ -22,8 +22,8 @@ impl ClangWrapper {
pub fn new(name: &str, compiler_kind: CompilerKind) -> Self {
// Obtain the compiler path from the configuration
let compiler_path = match compiler_kind {
CompilerKind::Clang => RLLVM_CONFIG.clang_filepath(),
CompilerKind::ClangXX => RLLVM_CONFIG.clangxx_filepath(),
CompilerKind::Clang => rllvm_config().clang_filepath(),
CompilerKind::ClangXX => rllvm_config().clangxx_filepath(),
};

Self {
Expand Down
8 changes: 4 additions & 4 deletions src/compiler_wrapper/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use crate::{
arg_parser::{CompileMode, CompilerArgsInfo},
config::RLLVM_CONFIG,
config::rllvm_config,
error::Error,
utils::{embed_bitcode_filepath_to_object_file, execute_command_for_status},
};
Expand Down Expand Up @@ -53,7 +53,7 @@ pub trait CompilerWrapper {
// Linking
if args_info.is_lto() {
// Add LTO LDFLAGS
if let Some(lto_ldflags) = RLLVM_CONFIG.lto_ldflags() {
if let Some(lto_ldflags) = rllvm_config().lto_ldflags() {
args.extend(lto_ldflags.iter().cloned());
}
}
Expand Down Expand Up @@ -182,7 +182,7 @@ pub trait CompilerWrapper {
let mut args = vec![String::from(compiler_filepath.to_string_lossy())];
args.extend(self.args().compile_args().iter().cloned());
// Add bitcode generation flags
if let Some(bitcode_generation_flags) = RLLVM_CONFIG.bitcode_generation_flags() {
if let Some(bitcode_generation_flags) = rllvm_config().bitcode_generation_flags() {
args.extend(bitcode_generation_flags.iter().cloned());
}
args.extend_from_slice(&[
Expand Down Expand Up @@ -239,7 +239,7 @@ pub trait CompilerWrapper {
let mut args = vec![String::from(wrapped_compiler.to_string_lossy())];
if self.args().is_lto() {
// Add LTO LDFLAGS
if let Some(lto_ldflags) = RLLVM_CONFIG.lto_ldflags() {
if let Some(lto_ldflags) = rllvm_config().lto_ldflags() {
args.extend(lto_ldflags.iter().cloned());
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{
env, fs,
path::{Path, PathBuf},
sync::OnceLock,
};

use lazy_static::lazy_static;
use log::Level;
use serde::{Deserialize, Serialize};

Expand All @@ -14,8 +14,16 @@ use crate::{
utils::{execute_llvm_config, find_llvm_config},
};

lazy_static! {
pub static ref RLLVM_CONFIG: RLLVMConfig = RLLVMConfig::new();
#[cfg(not(test))]
pub fn rllvm_config() -> &'static RLLVMConfig {
static RLLVM_CONFIG: OnceLock<RLLVMConfig> = OnceLock::new();
RLLVM_CONFIG.get_or_init(|| RLLVMConfig::new())
}

#[cfg(test)]
pub fn rllvm_config() -> &'static RLLVMConfig {
static RLLVM_CONFIG: OnceLock<RLLVMConfig> = OnceLock::new();
RLLVM_CONFIG.get_or_init(|| RLLVMConfig::default())
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
Loading

0 comments on commit 67fd861

Please sign in to comment.