Skip to content

Commit 61a0e79

Browse files
committed
Refactor pecos-qulacs build.rs to address clippy warning
1 parent 0983986 commit 61a0e79

File tree

1 file changed

+86
-54
lines changed

1 file changed

+86
-54
lines changed

crates/pecos-qulacs/build.rs

Lines changed: 86 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,65 @@ use pecos_build_utils::{
33
qulacs_download_info,
44
};
55
use std::env;
6-
use std::path::PathBuf;
6+
use std::path::{Path, PathBuf};
77

88
fn main() {
9-
println!("cargo:rerun-if-changed=build.rs");
10-
println!("cargo:rerun-if-changed=src/bridge.rs");
11-
println!("cargo:rerun-if-changed=src/qulacs_wrapper.cpp");
12-
println!("cargo:rerun-if-changed=src/qulacs_wrapper.h");
9+
setup_rerun_conditions();
1310

1411
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
1512
let target = env::var("TARGET").unwrap_or_default();
1613
let is_windows = target.contains("windows");
1714

15+
// Download and extract dependencies
16+
let (qulacs_path, eigen_path, boost_path) = download_and_extract_dependencies(&out_dir);
17+
18+
// Build our wrapper with actual Qulacs
19+
let mut build = cxx_build::bridge("src/bridge.rs");
20+
21+
// Add our wrapper
22+
build.file("src/qulacs_wrapper.cpp");
23+
24+
// Add essential Qulacs source files
25+
let qulacs_src = qulacs_path.join("src");
26+
add_qulacs_source_files(&mut build, &qulacs_src);
27+
28+
// Configure includes and compiler flags
29+
configure_build(&mut build, &eigen_path, &boost_path, &qulacs_src, &out_dir, is_windows);
30+
31+
// Compile everything
32+
build.compile("qulacs_wrapper");
33+
34+
// Add Windows-specific boost exception stub if needed
35+
if is_windows {
36+
create_windows_boost_stub(&out_dir);
37+
}
38+
}
39+
40+
fn setup_rerun_conditions() {
41+
println!("cargo:rerun-if-changed=build.rs");
42+
println!("cargo:rerun-if-changed=src/bridge.rs");
43+
println!("cargo:rerun-if-changed=src/qulacs_wrapper.cpp");
44+
println!("cargo:rerun-if-changed=src/qulacs_wrapper.h");
45+
}
46+
47+
fn download_and_extract_dependencies(out_dir: &Path) -> (PathBuf, PathBuf, PathBuf) {
1848
// Download all dependencies
1949
let qulacs_data = download_cached(&qulacs_download_info()).expect("Failed to download Qulacs");
2050
let eigen_data = download_cached(&eigen_download_info()).expect("Failed to download Eigen");
2151
let boost_data = download_cached(&boost_download_info()).expect("Failed to download Boost");
2252

2353
// Extract archives
2454
let qulacs_path =
25-
extract_archive(&qulacs_data, &out_dir, Some("qulacs")).expect("Failed to extract Qulacs");
55+
extract_archive(&qulacs_data, out_dir, Some("qulacs")).expect("Failed to extract Qulacs");
2656
let eigen_path =
27-
extract_archive(&eigen_data, &out_dir, Some("eigen")).expect("Failed to extract Eigen");
57+
extract_archive(&eigen_data, out_dir, Some("eigen")).expect("Failed to extract Eigen");
2858
let boost_path =
29-
extract_archive(&boost_data, &out_dir, Some("boost")).expect("Failed to extract Boost");
59+
extract_archive(&boost_data, out_dir, Some("boost")).expect("Failed to extract Boost");
3060

31-
// Build our wrapper with actual Qulacs
32-
let mut build = cxx_build::bridge("src/bridge.rs");
61+
(qulacs_path, eigen_path, boost_path)
62+
}
3363

34-
// Add our wrapper
35-
build.file("src/qulacs_wrapper.cpp");
36-
37-
// Add essential Qulacs source files
38-
let qulacs_src = qulacs_path.join("src");
64+
fn add_qulacs_source_files(build: &mut cc::Build, qulacs_src: &Path) {
3965

4066
// Core cppsim files - only add files that exist
4167
let cppsim_files = vec![
@@ -55,17 +81,17 @@ fn main() {
5581
"observable.cpp",
5682
"gate_noisy_evolution.cpp",
5783
];
58-
84+
5985
for file in &cppsim_files {
6086
let path = qulacs_src.join("cppsim").join(file);
6187
if path.exists() {
6288
build.file(path);
6389
} else {
64-
eprintln!("Warning: Skipping missing file: cppsim/{}", file);
90+
eprintln!("Warning: Skipping missing file: cppsim/{file}");
6591
}
6692
}
6793

68-
// Core csim files - only add files that exist
94+
// Core csim files - only add files that exist
6995
let csim_files = vec![
7096
"memory_ops.cpp",
7197
"stat_ops.cpp",
@@ -97,24 +123,33 @@ fn main() {
97123
"stat_ops_dm.cpp",
98124
"constant.cpp",
99125
];
100-
126+
101127
for file in &csim_files {
102128
let path = qulacs_src.join("csim").join(file);
103129
if path.exists() {
104130
build.file(path);
105131
} else {
106-
eprintln!("Warning: Skipping missing file: csim/{}", file);
132+
eprintln!("Warning: Skipping missing file: csim/{file}");
107133
}
108134
}
135+
}
109136

137+
fn configure_build(
138+
build: &mut cc::Build,
139+
eigen_path: &Path,
140+
boost_path: &Path,
141+
qulacs_src: &Path,
142+
out_dir: &Path,
143+
is_windows: bool,
144+
) {
110145
// Include directories
111-
build.include(&eigen_path);
112-
build.include(&boost_path);
113-
build.include(&qulacs_src);
146+
build.include(eigen_path);
147+
build.include(boost_path);
148+
build.include(qulacs_src);
114149
build.include(qulacs_src.join("cppsim"));
115150
build.include(qulacs_src.join("csim"));
116151
build.include("src");
117-
build.include(&out_dir);
152+
build.include(out_dir);
118153

119154
// Set compiler flags
120155
if is_windows {
@@ -137,36 +172,33 @@ fn main() {
137172

138173
// Define preprocessor macros
139174
build.define("EIGEN_NO_DEBUG", None);
175+
}
140176

141-
// Compile everything
142-
build.compile("qulacs_wrapper");
143-
144-
// Add a stub implementation for boost::throw_exception for Windows
145-
if is_windows {
146-
println!("cargo:rustc-link-lib=static=qulacs_wrapper");
147-
// Create a simple boost exception handler stub
148-
std::fs::write(
149-
out_dir.join("boost_exception_stub.cpp"),
150-
r#"
151-
#include <exception>
152-
namespace boost {
153-
struct source_location {
154-
const char* file_name() const { return ""; }
155-
const char* function_name() const { return ""; }
156-
int line() const { return 0; }
157-
};
158-
void throw_exception(std::exception const& e, source_location const&) {
159-
throw e;
160-
}
177+
fn create_windows_boost_stub(out_dir: &Path) {
178+
println!("cargo:rustc-link-lib=static=qulacs_wrapper");
179+
// Create a simple boost exception handler stub
180+
std::fs::write(
181+
out_dir.join("boost_exception_stub.cpp"),
182+
r#"
183+
#include <exception>
184+
namespace boost {
185+
struct source_location {
186+
const char* file_name() const { return ""; }
187+
const char* function_name() const { return ""; }
188+
int line() const { return 0; }
189+
};
190+
void throw_exception(std::exception const& e, source_location const&) {
191+
throw e;
161192
}
162-
"#,
163-
).expect("Failed to write boost exception stub");
164-
165-
// Compile the stub
166-
cc::Build::new()
167-
.cpp(true)
168-
.file(out_dir.join("boost_exception_stub.cpp"))
169-
.std("c++14")
170-
.compile("boost_exception_stub");
171-
}
193+
}
194+
"#,
195+
)
196+
.expect("Failed to write boost exception stub");
197+
198+
// Compile the stub
199+
cc::Build::new()
200+
.cpp(true)
201+
.file(out_dir.join("boost_exception_stub.cpp"))
202+
.std("c++14")
203+
.compile("boost_exception_stub");
172204
}

0 commit comments

Comments
 (0)