use std::path::{Path, PathBuf};
use log::*;
#[derive(Debug, PartialEq)]
pub enum OptLevel {
None,
LTO,
}
impl Default for OptLevel {
fn default() -> Self {
OptLevel::None
}
}
#[derive(Debug, PartialEq)]
pub enum Output {
PTXAssembly,
IntermediateRepresentation,
Bitcode,
}
#[derive(Debug, PartialEq, Default)]
pub struct Session {
pub output: Option<PathBuf>,
pub include_rlibs: Vec<PathBuf>,
pub include_bitcode_modules: Vec<PathBuf>,
pub opt_level: OptLevel,
pub debug_info: bool,
pub emit: Vec<Output>,
pub ptx_archs: Vec<String>,
pub ptx_fallback_arch: String,
}
impl Session {
pub fn set_output(&mut self, path: &Path) {
let extension = path.extension().unwrap_or_default();
if extension != "ptx" {
warn!(
"The output extension is not '.ptx'. Consider changing from '.{}' to '.ptx'",
extension.to_str().unwrap()
);
}
self.output = Some(path.to_path_buf());
}
pub fn set_opt_level(&mut self, level: OptLevel) {
self.opt_level = level;
}
pub fn set_debug_info(&mut self, debug: bool) {
self.debug_info = debug;
}
pub fn link_bitcode(&mut self, path: &Path) {
if self.is_metadata_bitcode(path) {
info!("Ignoring metadata bitcode: {:?}", path)
} else {
self.include_bitcode_modules.push(path.to_path_buf());
}
}
pub fn link_rlib(&mut self, path: &Path) {
self.include_rlibs.push(path.to_path_buf());
}
pub fn add_output_type(&mut self, output: Output) {
self.emit.push(output);
}
pub fn add_output_arch(&mut self, arch: &str) {
self.ptx_archs.push(arch.into());
}
pub fn set_fallback_arch(&mut self, arch: &str) {
self.ptx_fallback_arch = arch.into();
}
fn is_metadata_bitcode(&self, path: &Path) -> bool {
path.to_str().unwrap().ends_with(".crate.metadata.o")
}
}