Skip to content

Commit 1615bdf

Browse files
committed
Resolve rust-lang#149
1 parent 0a864eb commit 1615bdf

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

src/module.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::data_layout::DataLayout;
3535
use crate::execution_engine::ExecutionEngine;
3636
use crate::memory_buffer::MemoryBuffer;
3737
use crate::support::LLVMString;
38-
use crate::targets::{Target, InitializationConfig};
38+
use crate::targets::{InitializationConfig, Target, TargetTriple};
3939
use crate::types::{AsTypeRef, BasicType, FunctionType, BasicTypeEnum};
4040
use crate::values::{AsValueRef, FunctionValue, GlobalValue, MetadataValue};
4141
#[llvm_versions(7.0..=latest)]
@@ -341,9 +341,9 @@ impl<'ctx> Module<'ctx> {
341341
///
342342
/// assert_eq!(module.get_target().unwrap(), target);
343343
/// ```
344-
pub fn set_target(&self, target: &Target) {
344+
pub fn set_target(&self, target_triple: &TargetTriple) {
345345
unsafe {
346-
LLVMSetTarget(self.module.get(), target.get_name().as_ptr())
346+
LLVMSetTarget(self.module.get(), target_triple.as_ptr())
347347
}
348348
}
349349

@@ -367,13 +367,13 @@ impl<'ctx> Module<'ctx> {
367367
///
368368
/// assert_eq!(module.get_target().unwrap(), target);
369369
/// ```
370-
pub fn get_target(&self) -> Option<Target> {
370+
pub fn get_target(&self) -> TargetTriple {
371371
// REVIEW: This isn't an owned LLVMString, is it? If so, need to deallocate.
372372
let target_str = unsafe {
373373
LLVMGetTarget(self.module.get())
374374
};
375375

376-
Target::from_name_raw(target_str)
376+
TargetTriple::new_borrowed(target_str)
377377
}
378378

379379
/// Creates an `ExecutionEngine` from this `Module`.

src/targets.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ use crate::data_layout::DataLayout;
2828
use crate::memory_buffer::MemoryBuffer;
2929
use crate::module::Module;
3030
use crate::passes::PassManager;
31-
use crate::support::LLVMString;
31+
use crate::support::{LLVMString, LLVMStringOrRaw};
3232
use crate::types::{AnyType, AsTypeRef, IntType, StructType};
3333
use crate::values::{AsValueRef, GlobalValue};
3434
use crate::{AddressSpace, OptimizationLevel};
3535

3636
use std::default::Default;
3737
use std::ffi::{CStr, CString};
38+
use std::fmt;
3839
use std::mem::MaybeUninit;
3940
use std::path::Path;
4041
use std::ptr;
@@ -96,6 +97,64 @@ impl Default for InitializationConfig {
9697
}
9798
}
9899

100+
#[derive(Eq)]
101+
pub struct TargetTriple {
102+
pub(crate) target_triple: LLVMStringOrRaw,
103+
}
104+
105+
impl TargetTriple {
106+
pub(crate) fn new_owned(target_triple: *const ::libc::c_char) -> TargetTriple {
107+
debug_assert!(!target_triple.is_null());
108+
109+
TargetTriple {
110+
target_triple: LLVMStringOrRaw::Owned(LLVMString::new(target_triple)),
111+
}
112+
}
113+
114+
pub(crate) fn new_borrowed(target_triple: *const ::libc::c_char) -> TargetTriple {
115+
debug_assert!(!target_triple.is_null());
116+
117+
TargetTriple {
118+
target_triple: LLVMStringOrRaw::Borrowed(target_triple),
119+
}
120+
}
121+
122+
pub fn create(target_triple: &str) -> TargetTriple {
123+
let mut target_triple_copy = target_triple.to_owned();
124+
target_triple_copy.push('\0');
125+
126+
TargetTriple {
127+
target_triple: LLVMStringOrRaw::Owned(LLVMString::create(&target_triple_copy))
128+
}
129+
}
130+
131+
pub fn as_str(&self) -> &CStr {
132+
self.target_triple.as_str()
133+
}
134+
135+
pub fn as_ptr(&self) -> *const ::libc::c_char {
136+
match self.target_triple {
137+
LLVMStringOrRaw::Owned(ref llvm_string) => llvm_string.ptr,
138+
LLVMStringOrRaw::Borrowed(ptr) => ptr,
139+
}
140+
}
141+
}
142+
143+
impl PartialEq for TargetTriple {
144+
fn eq(&self, other: &TargetTriple) -> bool {
145+
self.as_str() == other.as_str()
146+
}
147+
}
148+
149+
impl fmt::Debug for TargetTriple {
150+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
151+
f.debug_struct("DataLayout")
152+
.field("address", &self.as_ptr())
153+
.field("repr", &self.as_str())
154+
.finish()
155+
}
156+
}
157+
99158
static TARGET_LOCK: Lazy<RwLock<()>> = Lazy::new(|| RwLock::new(()));
100159

101160
// NOTE: Versions verified as target-complete: 3.6, 3.7, 3.8, 3.9, 4.0

tests/all/test_module.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use self::inkwell::OptimizationLevel;
44
use self::inkwell::context::Context;
55
use self::inkwell::memory_buffer::MemoryBuffer;
66
use self::inkwell::module::Module;
7-
use self::inkwell::targets::Target;
7+
use self::inkwell::targets::TargetTriple;
88

99
use std::env::temp_dir;
1010
use std::ffi::CString;
@@ -316,27 +316,25 @@ fn test_print_to_file() {
316316

317317
#[test]
318318
fn test_get_set_target() {
319-
Target::initialize_x86(&Default::default());
320-
321319
let context = Context::create();
322320
let module = context.create_module("mod");
323-
let target = Target::from_name("x86-64").unwrap();
321+
let triple = TargetTriple::create("x86_64-pc-linux-gnu");
324322

325323
#[cfg(not(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8")))]
326324
assert_eq!(*module.get_name(), *CString::new("mod").unwrap());
327-
assert!(module.get_target().is_none());
325+
assert_eq!(module.get_target(), TargetTriple::create(""));
328326

329327
#[cfg(not(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8", feature = "llvm3-9",
330328
feature = "llvm4-0", feature = "llvm5-0", feature = "llvm6-0")))]
331329
assert_eq!(*module.get_source_file_name(), *CString::new("mod").unwrap());
332330

333331
#[cfg(not(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8")))]
334332
module.set_name("mod2");
335-
module.set_target(&target);
333+
module.set_target(&triple);
336334

337335
#[cfg(not(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8")))]
338336
assert_eq!(*module.get_name(), *CString::new("mod2").unwrap());
339-
assert_eq!(module.get_target().unwrap(), target);
337+
assert_eq!(module.get_target(), triple);
340338

341339
#[cfg(not(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8", feature = "llvm3-9",
342340
feature = "llvm4-0", feature = "llvm5-0", feature = "llvm6-0")))]

0 commit comments

Comments
 (0)