Skip to content

Commit 36cf0a0

Browse files
authored
chore: remove build_const dependency (#161)
Just write to a string, since that's all it does
1 parent 784eda3 commit 36cf0a0

File tree

4 files changed

+69
-58
lines changed

4 files changed

+69
-58
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/svm-builds/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ svm = { workspace = true, default-features = false, features = [
1919
"rustls",
2020
] }
2121

22-
build_const = "0.2"
2322
hex.workspace = true
2423
semver = { workspace = true, features = ["serde"] }
2524
serde_json.workspace = true
2625

2726
[dependencies]
28-
build_const = "0.2"
2927
hex.workspace = true
3028
semver = { workspace = true, features = ["serde"] }
3129

crates/svm-builds/build.rs

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(dead_code)]
22

3-
use std::fs::File;
3+
use std::fs::{self, File};
4+
use std::path::PathBuf;
45

56
use semver::Version;
67
use svm::Releases;
@@ -47,25 +48,23 @@ fn version_const_name(version: &Version) -> String {
4748
}
4849

4950
/// Adds build info related constants
50-
fn add_build_info_constants(
51-
writer: &mut build_const::ConstValueWriter,
52-
releases: &Releases,
53-
platform: svm::Platform,
54-
) {
51+
fn add_build_info_constants(output: &mut String, releases: &Releases, platform: svm::Platform) {
5552
let mut version_idents = Vec::with_capacity(releases.builds.len());
5653
let mut checksum_match_arms = Vec::with_capacity(releases.builds.len());
5754

5855
for build in releases.builds.iter() {
59-
let version_name = version_const_name(&build.version);
60-
61-
writer.add_value_raw(
62-
&version_name,
63-
"semver::Version",
64-
&format!(
65-
"semver::Version::new({},{},{})",
66-
build.version.major, build.version.minor, build.version.patch
67-
),
56+
let version = Version::new(
57+
build.version.major,
58+
build.version.minor,
59+
build.version.patch,
6860
);
61+
let version_name = version_const_name(&version);
62+
63+
output.push_str(&format!(
64+
"/// Solidity compiler version `{version}`.\n\
65+
pub const {version_name}: semver::Version = semver::Version::new({}, {}, {});\n\n",
66+
version.major, version.minor, version.patch
67+
));
6968
version_idents.push(version_name);
7069

7170
let sha256 = hex::encode(&build.sha256);
@@ -74,24 +73,28 @@ fn add_build_info_constants(
7473
build.version.major, build.version.minor, build.version.patch
7574
);
7675

77-
writer.add_value(&checksum_name, "&str", sha256);
76+
output.push_str(&format!(
77+
"/// Checksum for Solidity compiler version `{version}`.\n\
78+
pub const {checksum_name}: &str = \"{sha256}\";\n\n",
79+
));
7880
checksum_match_arms.push(format!(
79-
"({},{},{}) => {}",
80-
build.version.major, build.version.minor, build.version.patch, checksum_name
81+
"({}, {}, {}) => {}",
82+
version.major, version.minor, version.patch, checksum_name
8183
));
8284
}
8385

8486
let raw_static_array = format!(
8587
r#"
8688
/// All available releases for {}
8789
pub static ALL_SOLC_VERSIONS : [semver::Version; {}] = [
88-
{} ];
89-
"#,
90+
{}
91+
];
92+
"#,
9093
platform,
9194
version_idents.len(),
92-
version_idents.join(",\n")
95+
version_idents.join(",\n ")
9396
);
94-
writer.add_raw(&raw_static_array);
97+
output.push_str(&raw_static_array);
9598

9699
let get_check_sum_fn = format!(
97100
r#"
@@ -101,18 +104,18 @@ pub fn get_checksum(version: &semver::Version) -> Option<Vec<u8>> {{
101104
{},
102105
_ => return None
103106
}};
104-
Some(hex::decode(checksum).expect("valid hex;"))
107+
Some(hex::decode(checksum).unwrap())
105108
}}
106-
"#,
107-
checksum_match_arms.join(",\n")
109+
"#,
110+
checksum_match_arms.join(",\n ")
108111
);
109112

110-
writer.add_raw(&get_check_sum_fn);
113+
output.push_str(&get_check_sum_fn);
111114
}
112115

113116
/// checks the current platform and adds it as constant
114-
fn add_platform_const(writer: &mut build_const::ConstValueWriter, platform: svm::Platform) {
115-
writer.add_raw(&format!(
117+
fn add_platform_const(output: &mut String, platform: svm::Platform) {
118+
output.push_str(&format!(
116119
r#"
117120
/// The `svm::Platform` all constants were built for
118121
pub const TARGET_PLATFORM: &str = "{platform}";
@@ -122,6 +125,18 @@ pub const TARGET_PLATFORM: &str = "{platform}";
122125

123126
fn generate() {
124127
let platform = get_platform();
128+
let out_dir =
129+
PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR environment variable not set"));
130+
let output_file = out_dir.join("generated.rs");
131+
132+
// Start with an empty string that we'll append to
133+
let mut output = String::new();
134+
135+
// Add standard header
136+
output.push_str("// Generated file, do not edit by hand\n\n");
137+
138+
// add the platform as constant
139+
add_platform_const(&mut output, platform);
125140

126141
let releases: Releases = if let Ok(file_path) = std::env::var(SVM_RELEASES_LIST_JSON) {
127142
let file = File::open(file_path).unwrap_or_else(|_| {
@@ -135,41 +150,46 @@ fn generate() {
135150
svm::blocking_all_releases(platform).expect("Failed to fetch releases")
136151
};
137152

138-
let mut writer = build_const::ConstWriter::for_build("builds")
139-
.unwrap()
140-
.finish_dependencies();
141-
142-
// add the platform as constant
143-
add_platform_const(&mut writer, platform);
144-
145153
// add all solc version info
146-
add_build_info_constants(&mut writer, &releases, platform);
154+
add_build_info_constants(&mut output, &releases, platform);
147155

148156
// add the whole release string
149157
let release_json = serde_json::to_string(&releases).unwrap();
150-
writer.add_raw(&format!(
151-
r#"
158+
output.push_str(&format!(
159+
r##"
152160
/// JSON release list
153-
pub static RELEASE_LIST_JSON : &str = {}"{}"{};"#,
154-
"r#", release_json, "#"
161+
pub static RELEASE_LIST_JSON : &str = r#"{release_json}"#;"##
155162
));
156163

157-
writer.finish();
164+
// Write the string to file
165+
fs::write(output_file, output).expect("failed to write output file");
166+
167+
// Tell Cargo that we need to rerun this if any of the relevant env vars change
168+
println!("cargo:rerun-if-env-changed={SVM_TARGET_PLATFORM}");
169+
println!("cargo:rerun-if-env-changed={SVM_RELEASES_LIST_JSON}");
158170
}
159171

160172
/// generates an empty `RELEASE_LIST_JSON` static
161173
fn generate_offline() {
162-
let mut writer = build_const::ConstWriter::for_build("builds")
163-
.unwrap()
164-
.finish_dependencies();
174+
let out_dir =
175+
PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR environment variable not set"));
176+
let output_file = out_dir.join("generated.rs");
177+
178+
// Start with an empty string
179+
let mut output = String::new();
180+
181+
// Add standard header
182+
output.push_str("// Generated file, do not edit by hand\n\n");
165183

166184
let release_json = serde_json::to_string(&Releases::default()).unwrap();
167-
writer.add_raw(&format!(
168-
r#"
185+
output.push_str(&format!(
186+
r##"
169187
/// JSON release list
170-
pub static RELEASE_LIST_JSON : &str = {}"{}"{};"#,
171-
"r#", release_json, "#"
188+
pub static RELEASE_LIST_JSON : &str = r#"{release_json}"#;"##
172189
));
190+
191+
// Write the string to file
192+
fs::write(output_file, output).expect("failed to write output file");
173193
}
174194

175195
fn main() {

crates/svm-builds/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
//! Contains all Solc builds for the platform it was compiled with.
22
3-
build_const::build_const!("builds");
3+
include!(concat!(env!("OUT_DIR"), "/generated.rs"));

0 commit comments

Comments
 (0)