Skip to content

Commit

Permalink
fix: Avoid clang's warning on Mac
Browse files Browse the repository at this point in the history
by explicitly passing `-target`

refs #281
  • Loading branch information
yhara committed Apr 25, 2021
1 parent 7add324 commit 631edf3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/code_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ pub fn run(
bc_path: &str,
opt_ll_path: Option<&str>,
generate_main: bool,
opt_target_triple: Option<&inkwell::targets::TargetTriple>,
) -> Result<(), Error> {
let context = inkwell::context::Context::create();
let module = context.create_module("main");
if let Some(triple) = opt_target_triple {
module.set_triple(triple);
}
let builder = context.create_builder();
let mut code_gen = CodeGen::new(&mir, &context, &module, &builder, &generate_main);
code_gen.gen_program(&mir.hir, &mir.imports)?;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod targets;
pub mod ast;
pub mod code_gen;
pub mod corelib;
Expand Down
13 changes: 9 additions & 4 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::*;
use crate::library;
use crate::targets;
use std::env;
use std::fs;
use std::io::{Read, Write};
Expand All @@ -24,7 +25,8 @@ pub fn compile<P: AsRef<Path>>(filepath: P) -> Result<(), Error> {
log::debug!("created mir");
let bc_path = path.clone() + ".bc";
let ll_path = path + ".ll";
crate::code_gen::run(&mir, &bc_path, Some(&ll_path), true)?;
let triple = targets::default_triple();
crate::code_gen::run(&mir, &bc_path, Some(&ll_path), true, Some(&triple))?;
log::debug!("created .bc");
Ok(())
}
Expand Down Expand Up @@ -52,7 +54,8 @@ pub fn build_corelib() -> Result<(), Error> {
let mir = crate::mir::build(hir, imports);
log::debug!("created mir");
let exports = library::LibraryExports::new(&mir);
crate::code_gen::run(&mir, "builtin/builtin.bc", Some("builtin/builtin.ll"), false)?;
let triple = targets::default_triple();
crate::code_gen::run(&mir, "builtin/builtin.bc", Some("builtin/builtin.ll"), false, Some(&triple))?;
log::debug!("created .bc");

let json = serde_json::to_string_pretty(&exports).unwrap();
Expand Down Expand Up @@ -96,6 +99,7 @@ fn run_<P: AsRef<Path>>(
sk_path: P,
capture_out: bool,
) -> Result<(String, String), Box<dyn std::error::Error>> {
let triple = targets::default_triple();
let s = sk_path.as_ref().to_str().expect("failed to unwrap sk_path");
//let ll_path = s.to_string() + ".ll";
//let opt_ll_path = s.to_string() + ".opt.ll";
Expand Down Expand Up @@ -131,7 +135,9 @@ fn run_<P: AsRef<Path>>(
add_args_from_env(&mut cmd, "CFLAGS");
add_args_from_env(&mut cmd, "LDFLAGS");
add_args_from_env(&mut cmd, "LDLIBS");
cmd.arg("-no-pie");
//cmd.arg("-no-pie");
cmd.arg("-target");
cmd.arg(triple.as_str().to_str().unwrap());
cmd.arg("-lm");
cmd.arg("-lgc");
cmd.arg("-o");
Expand All @@ -143,7 +149,6 @@ fn run_<P: AsRef<Path>>(
}

fs::remove_file(bc_path)?;
//fs::remove_file(asm_path).map_err(|e| runner_error("failed to remove .s", Box::new(e)))?;

let mut cmd = Command::new(format!("./{}", out_path));
if capture_out {
Expand Down
12 changes: 12 additions & 0 deletions src/targets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// Returns default `TargetTriple`
pub fn default_triple() -> inkwell::targets::TargetTriple {
if let Some(info) = mac_sys_info::get_mac_sys_info().ok() {
// #281: get_default_triple returns `darwin` but clang shows warning for it
let arch = info.cpu_info().architecture();
let ver = info.os_info().os_version();
let s = format!("{}-apple-macosx{}", arch, ver);
inkwell::targets::TargetTriple::create(&s)
} else {
inkwell::targets::TargetMachine::get_default_triple()
}
}

0 comments on commit 631edf3

Please sign in to comment.