|
| 1 | +// Copyright (C) 2019-2026 Provable Inc. |
| 2 | +// This file is part of the Leo library. |
| 3 | + |
| 4 | +// The Leo library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// The Leo library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//! Builds the symbol strings into `Symbol` values. |
| 18 | +
|
| 19 | +use std::{ |
| 20 | + env, |
| 21 | + fmt, |
| 22 | + fs, |
| 23 | + io::{BufWriter, Read, Write}, |
| 24 | + path::{Path, PathBuf}, |
| 25 | + process::ExitCode, |
| 26 | +}; |
| 27 | + |
| 28 | +const SYMBOLS_FILE: &str = "symbols.txt"; |
| 29 | +const SYMBOLS_GENERATED_FILE: &str = "symbols_generated.rs"; |
| 30 | +const PRE_DEFINED_HEADER: &str = "// @generated by build.rs. Do not edit.\nconst PRE_DEFINED: &[&str] = &[\n"; |
| 31 | +const SYM_HEADER: &str = "];\n\n#[allow(non_upper_case_globals)]\npub mod sym {\n"; |
| 32 | +const SYM_FOOTER: &str = "}\n"; |
| 33 | + |
| 34 | +struct SymbolLine<'a> { |
| 35 | + ident: &'a str, |
| 36 | + literal: Option<&'a str>, |
| 37 | +} |
| 38 | + |
| 39 | +type BuildResult<T> = Result<T, String>; |
| 40 | + |
| 41 | +fn main() -> ExitCode { |
| 42 | + match run() { |
| 43 | + Ok(()) => ExitCode::SUCCESS, |
| 44 | + Err(err) => { |
| 45 | + eprintln!("cargo:warning={err}"); |
| 46 | + ExitCode::FAILURE |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +fn run() -> BuildResult<()> { |
| 52 | + let manifest_dir = manifest_dir()?; |
| 53 | + let input_path = manifest_dir.join(SYMBOLS_FILE); |
| 54 | + println!("cargo:rerun-if-changed={}", input_path.display()); |
| 55 | + |
| 56 | + let input = read_symbols(&input_path)?; |
| 57 | + let symbols = parse_symbols(&input, &input_path)?; |
| 58 | + if symbols.is_empty() { |
| 59 | + return Err(format!("no symbols found in {}", input_path.display())); |
| 60 | + } |
| 61 | + |
| 62 | + let out_dir = out_dir()?; |
| 63 | + let out_path = out_dir.join(SYMBOLS_GENERATED_FILE); |
| 64 | + let output = fs::File::create(&out_path).map_err(|source| io_error("create", &out_path, source))?; |
| 65 | + let mut output = BufWriter::new(output); |
| 66 | + |
| 67 | + write_output(&mut output, PRE_DEFINED_HEADER, &out_path)?; |
| 68 | + emit_pre_defined(&symbols, &mut output, &out_path)?; |
| 69 | + |
| 70 | + write_output(&mut output, SYM_HEADER, &out_path)?; |
| 71 | + emit_sym_module(&symbols, &mut output, &out_path)?; |
| 72 | + |
| 73 | + write_output(&mut output, SYM_FOOTER, &out_path)?; |
| 74 | + output.flush().map_err(|source| io_error("write", &out_path, source))?; |
| 75 | + Ok(()) |
| 76 | +} |
| 77 | + |
| 78 | +fn manifest_dir() -> BuildResult<PathBuf> { |
| 79 | + env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).map_err(|source| missing_env_var("CARGO_MANIFEST_DIR", source)) |
| 80 | +} |
| 81 | + |
| 82 | +fn out_dir() -> BuildResult<PathBuf> { |
| 83 | + env::var("OUT_DIR").map(PathBuf::from).map_err(|source| missing_env_var("OUT_DIR", source)) |
| 84 | +} |
| 85 | + |
| 86 | +fn read_symbols(path: &Path) -> BuildResult<String> { |
| 87 | + let input_len = fs::metadata(path).map(|meta| meta.len() as usize).unwrap_or(0); |
| 88 | + let mut input = String::with_capacity(input_len); |
| 89 | + let mut file = fs::File::open(path).map_err(|source| io_error("open", path, source))?; |
| 90 | + file.read_to_string(&mut input).map_err(|source| io_error("read", path, source))?; |
| 91 | + Ok(input) |
| 92 | +} |
| 93 | + |
| 94 | +fn is_valid_ident(ident: &str) -> bool { |
| 95 | + let mut chars = ident.chars(); |
| 96 | + |
| 97 | + chars.next().is_some_and(|first| { |
| 98 | + (first == '_' || first.is_ascii_alphabetic()) && chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric()) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +fn parse_symbol_line<'a>(line: &'a str, path: &Path, line_no: usize) -> BuildResult<Option<SymbolLine<'a>>> { |
| 103 | + let trimmed = line.trim(); |
| 104 | + if trimmed.is_empty() || trimmed.starts_with("//") { |
| 105 | + return Ok(None); |
| 106 | + } |
| 107 | + |
| 108 | + let (ident, literal) = match trimmed.split_once(':') { |
| 109 | + Some((left, right)) => { |
| 110 | + let ident = left.trim(); |
| 111 | + let literal = right.trim(); |
| 112 | + if ident.is_empty() || literal.is_empty() { |
| 113 | + return Err(format!("invalid symbol entry at {}:{line_no}", path.display())); |
| 114 | + } |
| 115 | + if !(literal.starts_with('"') && literal.ends_with('"')) { |
| 116 | + return Err(format!("invalid symbol literal for '{ident}' at {}:{line_no}", path.display())); |
| 117 | + } |
| 118 | + (ident, Some(literal)) |
| 119 | + } |
| 120 | + None => (trimmed, None), |
| 121 | + }; |
| 122 | + |
| 123 | + if !is_valid_ident(ident) { |
| 124 | + return Err(format!("invalid symbol identifier '{ident}' at {}:{line_no}", path.display())); |
| 125 | + } |
| 126 | + |
| 127 | + Ok(Some(SymbolLine { ident, literal })) |
| 128 | +} |
| 129 | + |
| 130 | +fn parse_symbols<'a>(input: &'a str, path: &Path) -> BuildResult<Vec<SymbolLine<'a>>> { |
| 131 | + input |
| 132 | + .lines() |
| 133 | + .enumerate() |
| 134 | + .map(|(i, line)| parse_symbol_line(line, path, i + 1)) |
| 135 | + .filter_map(Result::transpose) |
| 136 | + .collect() |
| 137 | +} |
| 138 | + |
| 139 | +fn missing_env_var(name: &'static str, source: env::VarError) -> String { |
| 140 | + format!("missing environment variable {name}: {source}") |
| 141 | +} |
| 142 | + |
| 143 | +fn io_error(action: &'static str, path: &Path, source: std::io::Error) -> String { |
| 144 | + format!("failed to {action} {}: {source}", path.display()) |
| 145 | +} |
| 146 | + |
| 147 | +fn write_output(output: &mut impl Write, contents: &str, out_path: &Path) -> BuildResult<()> { |
| 148 | + output.write_all(contents.as_bytes()).map_err(|source| io_error("write", out_path, source)) |
| 149 | +} |
| 150 | + |
| 151 | +fn write_output_fmt(output: &mut impl Write, out_path: &Path, args: fmt::Arguments<'_>) -> BuildResult<()> { |
| 152 | + output.write_fmt(args).map_err(|source| io_error("write", out_path, source)) |
| 153 | +} |
| 154 | + |
| 155 | +fn emit_pre_defined(symbols: &[SymbolLine<'_>], output: &mut impl Write, out_path: &Path) -> BuildResult<()> { |
| 156 | + for symbol in symbols { |
| 157 | + match symbol.literal { |
| 158 | + Some(literal) => write_output_fmt(output, out_path, format_args!(" {literal},\n"))?, |
| 159 | + None => write_output_fmt(output, out_path, format_args!(" {:?},\n", symbol.ident))?, |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + Ok(()) |
| 164 | +} |
| 165 | + |
| 166 | +fn emit_sym_module(symbols: &[SymbolLine<'_>], output: &mut impl Write, out_path: &Path) -> BuildResult<()> { |
| 167 | + for (index, symbol) in symbols.iter().enumerate() { |
| 168 | + write_output_fmt( |
| 169 | + output, |
| 170 | + out_path, |
| 171 | + format_args!( |
| 172 | + " pub const {}: crate::symbol::Symbol = crate::symbol::Symbol::new({index});\n", |
| 173 | + symbol.ident |
| 174 | + ), |
| 175 | + )?; |
| 176 | + } |
| 177 | + |
| 178 | + Ok(()) |
| 179 | +} |
0 commit comments