Skip to content

Commit 67fd861

Browse files
committed
feat: retire lazy_static and upgrade dependencies
Signed-off-by: h1994st <[email protected]>
1 parent b3c2251 commit 67fd861

16 files changed

+421
-402
lines changed

Cargo.toml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ repository = "https://github.com/h1994st/rllvm"
1616
version = "0.1.1"
1717

1818
[dependencies]
19-
clap = {version = "~4.4.11", features = ["derive"]}
20-
confy = "~0.5"
21-
lazy_static = "~1.4"
22-
log = "~0.4.20"
23-
object = {version = "~0.32", features = ["all"]}
24-
regex = "~1.10"
25-
serde = {version = "~1.0.193", features = ["derive"]}
26-
simple_logger = "~4.3.0"
27-
which = "~5.0.0"
19+
clap = {version = "~4.5.23", features = ["derive"]}
20+
confy = "~0.6.1"
21+
log = "~0.4.22"
22+
object = {version = "~0.36.5", features = ["all"]}
23+
regex = "~1.11.1"
24+
serde = {version = "~1.0.216", features = ["derive"]}
25+
simple_logger = "~5.0.0"
26+
which = "~7.0.0"
2827

2928
[target.'cfg(target_vendor = "apple")'.dependencies]
3029
glob = "~0.3.1"

src/arg_parser.rs

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
//! Command-line argument parser
22
3-
use std::path::PathBuf;
4-
5-
use lazy_static::lazy_static;
6-
use regex::Regex;
7-
83
use crate::{
9-
config::RLLVM_CONFIG,
10-
constants::{ARG_EXACT_MATCH_MAP, ARG_PATTERNS},
4+
config::rllvm_config,
5+
constants::{arg_exact_match_map, arg_patterns},
116
error::Error,
127
utils::*,
138
};
9+
use regex::Regex;
10+
use std::{path::PathBuf, sync::OnceLock};
1411

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

9592
// Assembly files
96-
lazy_static! {
97-
static ref RE: Regex = Regex::new(r"\.(s|S)$").unwrap();
98-
}
99-
if RE.is_match(flag.as_ref()) {
93+
static RE: OnceLock<Regex> = OnceLock::new();
94+
let re = RE.get_or_init(|| Regex::new(r"\.(s|S)$").unwrap());
95+
if re.is_match(flag.as_ref()) {
10096
self.is_assembly = true;
10197
}
10298

@@ -313,7 +309,7 @@ impl CompilerArgsInfo {
313309
let mut offset = 1;
314310

315311
// Try to match the flag exactly
316-
if let Some(arg_info) = ARG_EXACT_MATCH_MAP.get(arg.as_str()) {
312+
if let Some(arg_info) = arg_exact_match_map().get(arg.as_str()) {
317313
// Consume more parameters
318314
offset += self.consume_params(i, arg.to_string(), arg_info, &args);
319315
} else if arg == "-Wl,--start-group" {
@@ -335,7 +331,7 @@ impl CompilerArgsInfo {
335331
} else {
336332
// Try to match a pattern
337333
let mut matched = false;
338-
for arg_pattern in ARG_PATTERNS.iter() {
334+
for arg_pattern in arg_patterns().iter() {
339335
let pattern = &arg_pattern.pattern;
340336
let arg_info = &arg_pattern.arg_info;
341337
if pattern.is_match(arg.as_str()) {
@@ -435,7 +431,7 @@ impl CompilerArgsInfo {
435431

436432
let conditions = [
437433
(
438-
RLLVM_CONFIG.is_configure_only(),
434+
rllvm_config().is_configure_only(),
439435
"we are in configure-only mode",
440436
),
441437
(
@@ -506,7 +502,7 @@ impl CompilerArgsInfo {
506502
derive_object_and_bitcode_filepath(&src_filepath, self.is_compile_only)?;
507503

508504
// Update the bitcode filepath, if the bitcode store path is provided
509-
if let Some(bitcode_store_path) = RLLVM_CONFIG.bitcode_store_path() {
505+
if let Some(bitcode_store_path) = rllvm_config().bitcode_store_path() {
510506
if bitcode_store_path.exists() {
511507
// Obtain a new bitcode filename based on the hash of the source filepath
512508
if bitcode_filepath.file_name().is_some() {
@@ -536,3 +532,45 @@ impl CompilerArgsInfo {
536532
Ok(artifacts)
537533
}
538534
}
535+
536+
#[cfg(test)]
537+
mod tests {
538+
use super::CompilerArgsInfo;
539+
540+
fn test_parsing<F>(input: &str, check_func: F)
541+
where
542+
F: Fn(&CompilerArgsInfo) -> bool,
543+
{
544+
let mut args_info = CompilerArgsInfo::default();
545+
let args: Vec<&str> = input.split_ascii_whitespace().collect();
546+
let ret = args_info.parse_args(&args);
547+
assert!(ret.is_ok());
548+
assert!(check_func(ret.unwrap()));
549+
}
550+
551+
fn test_parsing_lto_internal(input: &str) {
552+
test_parsing(input, |args| args.is_lto());
553+
}
554+
555+
#[test]
556+
fn test_parsing_lto() {
557+
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"#;
558+
test_parsing_lto_internal(input);
559+
560+
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"#;
561+
test_parsing_lto_internal(input);
562+
}
563+
564+
fn test_parsing_link_args_internal(input: &str, expected: usize) {
565+
test_parsing(input, |args| args.link_args().len() == expected);
566+
}
567+
568+
#[test]
569+
fn test_parsing_link_args() {
570+
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"#;
571+
test_parsing_link_args_internal(input, 32);
572+
573+
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"#;
574+
test_parsing_link_args_internal(input, 5);
575+
}
576+
}

src/bin/rllvm_cc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22

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

1515
// Set log level
16-
let log_level = RLLVM_CONFIG.log_level().to_level_filter();
16+
let log_level = rllvm_config().log_level().to_level_filter();
1717
SimpleLogger::new()
1818
.with_level(log_level)
1919
.init()

src/compiler_wrapper/llvm/clang_wrapper.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::path::{Path, PathBuf};
44

55
use crate::{
6-
arg_parser::CompilerArgsInfo, compiler_wrapper::*, config::RLLVM_CONFIG, error::Error,
6+
arg_parser::CompilerArgsInfo, compiler_wrapper::*, config::rllvm_config, error::Error,
77
};
88

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

2929
Self {

src/compiler_wrapper/wrapper.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88

99
use crate::{
1010
arg_parser::{CompileMode, CompilerArgsInfo},
11-
config::RLLVM_CONFIG,
11+
config::rllvm_config,
1212
error::Error,
1313
utils::{embed_bitcode_filepath_to_object_file, execute_command_for_status},
1414
};
@@ -53,7 +53,7 @@ pub trait CompilerWrapper {
5353
// Linking
5454
if args_info.is_lto() {
5555
// Add LTO LDFLAGS
56-
if let Some(lto_ldflags) = RLLVM_CONFIG.lto_ldflags() {
56+
if let Some(lto_ldflags) = rllvm_config().lto_ldflags() {
5757
args.extend(lto_ldflags.iter().cloned());
5858
}
5959
}
@@ -182,7 +182,7 @@ pub trait CompilerWrapper {
182182
let mut args = vec![String::from(compiler_filepath.to_string_lossy())];
183183
args.extend(self.args().compile_args().iter().cloned());
184184
// Add bitcode generation flags
185-
if let Some(bitcode_generation_flags) = RLLVM_CONFIG.bitcode_generation_flags() {
185+
if let Some(bitcode_generation_flags) = rllvm_config().bitcode_generation_flags() {
186186
args.extend(bitcode_generation_flags.iter().cloned());
187187
}
188188
args.extend_from_slice(&[
@@ -239,7 +239,7 @@ pub trait CompilerWrapper {
239239
let mut args = vec![String::from(wrapped_compiler.to_string_lossy())];
240240
if self.args().is_lto() {
241241
// Add LTO LDFLAGS
242-
if let Some(lto_ldflags) = RLLVM_CONFIG.lto_ldflags() {
242+
if let Some(lto_ldflags) = rllvm_config().lto_ldflags() {
243243
args.extend(lto_ldflags.iter().cloned());
244244
}
245245
}

src/config.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::{
22
env, fs,
33
path::{Path, PathBuf},
4+
sync::OnceLock,
45
};
56

6-
use lazy_static::lazy_static;
77
use log::Level;
88
use serde::{Deserialize, Serialize};
99

@@ -14,8 +14,16 @@ use crate::{
1414
utils::{execute_llvm_config, find_llvm_config},
1515
};
1616

17-
lazy_static! {
18-
pub static ref RLLVM_CONFIG: RLLVMConfig = RLLVMConfig::new();
17+
#[cfg(not(test))]
18+
pub fn rllvm_config() -> &'static RLLVMConfig {
19+
static RLLVM_CONFIG: OnceLock<RLLVMConfig> = OnceLock::new();
20+
RLLVM_CONFIG.get_or_init(|| RLLVMConfig::new())
21+
}
22+
23+
#[cfg(test)]
24+
pub fn rllvm_config() -> &'static RLLVMConfig {
25+
static RLLVM_CONFIG: OnceLock<RLLVMConfig> = OnceLock::new();
26+
RLLVM_CONFIG.get_or_init(|| RLLVMConfig::default())
1927
}
2028

2129
#[derive(Serialize, Deserialize, Debug)]

0 commit comments

Comments
 (0)