11#![ allow( dead_code) ]
22
3- use std:: fs:: File ;
3+ use std:: fs:: { self , File } ;
4+ use std:: path:: PathBuf ;
45
56use semver:: Version ;
67use 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 {}
8789pub 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
118121pub const TARGET_PLATFORM: &str = "{platform}";
@@ -122,6 +125,18 @@ pub const TARGET_PLATFORM: &str = "{platform}";
122125
123126fn 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
161173fn 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
175195fn main ( ) {
0 commit comments