-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.rs
More file actions
95 lines (81 loc) · 2.98 KB
/
Copy pathbuild.rs
File metadata and controls
95 lines (81 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::env;
use std::error::Error;
use std::process::Command;
fn get_git_describe() -> Result<String, Box<dyn Error>> {
let output = Command::new("git")
.args(["describe", "--tags"])
.output()
.expect("Failed to execute git describe");
if !output.status.success() {
return Err(format!("git describe failed with status: {}", output.status).into());
}
let git_describe = match String::from_utf8(output.stdout) {
Ok(str) => str.trim().to_string(),
Err(e) => return Err(format!("Invalid UTF-8 output: {e:?}").into()),
};
Ok(git_describe)
}
#[cfg(feature = "_transcoders_deps")]
fn compile_with_rasn() -> Result<(), Box<dyn Error>> {
use rasn_compiler::prelude::*;
use std::env;
use std::path::PathBuf;
let asn_files = [
"data/asn1/X509-ML-DSA-2025.asn",
"data/asn1/X509-Composite-ML-DSA-2025.asn",
"data/asn1/X509-SLH-DSA-Module-2024.asn",
];
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_file = out_path.join("rasn-generated.rs");
for path in &asn_files {
println!("cargo:rerun-if-changed={path}");
}
eprintln!("rasn-compiler output to be written at {out_file:?}");
// Initialize the compiler with the rust/rasn backend.
match Compiler::<RasnBackend, _>::new()
// add a single ASN1 source file
//.add_asn_by_path(PathBuf::from("data/asn1/X509-ML-DSA-2025.asn"))
// add several ASN1 source files
.add_asn_sources_by_path(asn_files.iter())
// set an output path for the generated rust code
.set_output_path(out_file)
// you may also compile literal ASN1 snippets
//.add_asn_literal(
// format!(
// "TestModule DEFINITIONS AUTOMATIC TAGS::= BEGIN {} END",
// "My-test-integer ::= INTEGER (1..128)"
// )
//)
.compile()
{
Ok(warnings) => {
/* handle compilation warnings */
for w in warnings {
println!("cargo:warning=rasn-compiler issued {w:?}");
}
Ok(())
}
Err(error) => {
/* handle unrecoverable compilation error */
panic!("rasn-compiler failed with: {error:?}")
}
}
}
fn main() {
if env::var("PROFILE").unwrap_or_default() != "debug" {
panic!(
"This project is NOT production-ready. Thus, \
the only compilation profile available is `debug`."
);
}
// Always rerun if the variable FORCE_REBUILD changes
println!("cargo:rerun-if-env-changed=FORCE_REBUILD");
let git_describe = get_git_describe().unwrap_or_else(|e| {
println!("cargo:warning=Failed to get git describe");
eprintln!("Error was {e:?}");
"FAILED_TO_GATHER_GIT_DESCRIBE".to_string()
});
#[cfg(feature = "_transcoders_deps")]
compile_with_rasn().expect("rasn-compiler failed");
println!("cargo:rustc-env=CARGO_GIT_DESCRIBE={}", git_describe);
}