1#![doc = include_str!("../README.md")]
2#![deny(unused_must_use)]
3
4use std::path::PathBuf;
5
6mod dwarf;
7mod elf;
8mod fast_range_map;
9mod program_from_elf;
10mod reader_wrapper;
11mod riscv;
12mod utils;
13
14pub use crate::program_from_elf::{program_from_elf, Config, OptLevel, ProgramFromElfError};
15pub use polkavm_common::assembler::assemble;
16pub use polkavm_common::program::{ProgramBlob, ProgramParseError, ProgramParts};
17
18pub static TARGET_JSON_32_BIT: &str = include_str!("../riscv32emac-unknown-none-polkavm.json");
19pub static TARGET_JSON_64_BIT: &str = include_str!("../riscv64emac-unknown-none-polkavm.json");
20
21fn target_json_path_impl(json_name: &str, json_contents: &str) -> Result<PathBuf, String> {
22 let version = env!("CARGO_PKG_VERSION");
23 let cache_path = dirs::cache_dir()
24 .or_else(|| std::env::current_dir().ok())
25 .unwrap_or_else(|| PathBuf::from("./"))
26 .join(".polkavm-linker")
27 .join(version);
28
29 if let Err(error) = std::fs::create_dir_all(&cache_path) {
30 return Err(format!(
31 "failed to fetch path to the PolkaVM target JSON: failed to create {cache_path:?}: {error}"
32 ));
33 }
34
35 let json_path = cache_path.join(json_name);
36
37 if std::fs::read_to_string(&json_path)
39 .ok()
40 .map_or(true, |existing_contents| existing_contents != json_contents)
41 {
42 std::fs::write(&json_path, json_contents)
43 .map_err(|error| format!("failed to fetch path to the PolkaVM target JSON: failed to write to {json_path:?}: {error}"))?;
44 }
45
46 json_path
47 .canonicalize()
48 .map_err(|error| format!("failed to fetch path to the PolkaVM target JSON: failed to canonicalize path {json_path:?}: {error}"))
49}
50
51pub fn target_json_32_path() -> Result<PathBuf, String> {
52 target_json_path_impl("riscv32emac-unknown-none-polkavm.json", TARGET_JSON_32_BIT)
53}
54
55pub fn target_json_64_path() -> Result<PathBuf, String> {
56 target_json_path_impl("riscv64emac-unknown-none-polkavm.json", TARGET_JSON_64_BIT)
57}