]> git.lizzy.rs Git - rust.git/commitdiff
trans: Add kind to writeArchive
authorAlex Crichton <alex@alexcrichton.com>
Thu, 16 Jul 2015 07:11:09 +0000 (00:11 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 17 Jul 2015 03:25:51 +0000 (20:25 -0700)
Updates our LLVM bindings to be able to write out multiple kinds of archives.
This commit also enables using LLVM instead of the system ar on all current
targets.

src/librustc_back/target/apple_base.rs
src/librustc_back/target/bitrig_base.rs
src/librustc_back/target/dragonfly_base.rs
src/librustc_back/target/freebsd_base.rs
src/librustc_back/target/netbsd_base.rs
src/librustc_back/target/openbsd_base.rs
src/librustc_llvm/archive_ro.rs
src/librustc_llvm/lib.rs
src/librustc_trans/back/archive.rs
src/rustllvm/ArchiveWrapper.cpp

index 795a2c18bc6e6322744cb9f2b6f1c80452c63640..f34ba40a8b2fe2888d61d84551ba05d056c5625a 100644 (file)
@@ -23,6 +23,7 @@ pub fn opts() -> TargetOptions {
         has_rpath: true,
         dll_prefix: "lib".to_string(),
         dll_suffix: ".dylib".to_string(),
+        archive_format: "bsd".to_string(),
         pre_link_args: Vec::new(),
         .. Default::default()
     }
index 6e5a48c0ea16c55f26ba500d1bb21accf3cdcf7d..9f6a1f1e530079f3b5239ce4edf9f3caf5a8bb27 100644 (file)
@@ -22,6 +22,7 @@ pub fn opts() -> TargetOptions {
         position_independent_executables: true,
         pre_link_args: vec!(
         ),
+        archive_format: "bsd".to_string(),
 
         .. Default::default()
     }
index a56621ff97ea541cba2cd0e500a2ce471d4ef5d1..51a371db72446a04745316d64706785c2af5c78e 100644 (file)
@@ -29,6 +29,7 @@ pub fn opts() -> TargetOptions {
             "-Wl,--as-needed".to_string(),
         ),
         position_independent_executables: true,
+        archive_format: "bsd".to_string(),
         .. Default::default()
     }
 }
index 3ec6307c72f86420481f59fd9badeac245f2ffbf..2c3d240dbf3d53851ad732980b532c4620d1fb29 100644 (file)
@@ -18,6 +18,7 @@ pub fn opts() -> TargetOptions {
         executables: true,
         morestack: true,
         has_rpath: true,
+        archive_format: "bsd".to_string(),
 
         .. Default::default()
     }
index 0f2ab32be2431e059a37eeec09adb539ba7bbcef..9b20bd927cb7a1d161df8a1b19c512d5d080a0f8 100644 (file)
@@ -27,6 +27,7 @@ pub fn opts() -> TargetOptions {
             "-Wl,--as-needed".to_string(),
         ),
         position_independent_executables: true,
+        archive_format: "bsd".to_string(),
         .. Default::default()
     }
 }
index 0f2ab32be2431e059a37eeec09adb539ba7bbcef..9b20bd927cb7a1d161df8a1b19c512d5d080a0f8 100644 (file)
@@ -27,6 +27,7 @@ pub fn opts() -> TargetOptions {
             "-Wl,--as-needed".to_string(),
         ),
         position_independent_executables: true,
+        archive_format: "bsd".to_string(),
         .. Default::default()
     }
 }
index 2c6022bc6149584488f988552e1759a06b40f2b3..85c0c721114f5837d6c112e9a47a74420c0087e3 100644 (file)
@@ -118,6 +118,9 @@ pub fn data(&self) -> &'a [u8] {
         unsafe {
             let mut data_len = 0;
             let data_ptr = ::LLVMRustArchiveChildData(self.ptr, &mut data_len);
+            if data_ptr.is_null() {
+                panic!("failed to read data from archive child");
+            }
             slice::from_raw_parts(data_ptr as *const u8, data_len as usize)
         }
     }
index 05f82b86ebbc9c17ec2d9725d35de8e083d95a39..051cc1c5bb2485544f07dd1e92ea5e3502d2b4de 100644 (file)
@@ -452,6 +452,15 @@ pub enum DiagnosticKind {
     DK_OptimizationFailure,
 }
 
+#[repr(C)]
+#[derive(Copy, Clone)]
+pub enum ArchiveKind {
+    K_GNU,
+    K_MIPS64,
+    K_BSD,
+    K_COFF,
+}
+
 // Opaque pointer types
 #[allow(missing_copy_implementations)]
 pub enum Module_opaque {}
@@ -2119,7 +2128,8 @@ pub fn LLVMSetInlineAsmDiagnosticHandler(C: ContextRef,
     pub fn LLVMRustWriteArchive(Dst: *const c_char,
                                 NumMembers: size_t,
                                 Members: *const RustArchiveMemberRef,
-                                WriteSymbtab: bool) -> c_int;
+                                WriteSymbtab: bool,
+                                Kind: ArchiveKind) -> c_int;
     pub fn LLVMRustArchiveMemberNew(Filename: *const c_char,
                                     Name: *const c_char,
                                     Child: ArchiveChildRef) -> RustArchiveMemberRef;
index cc3d1d842fad0c9cd516a5fabd4a7147d0673b0f..02f4bc83b7524a0a040d66021e528323993f0f28 100644 (file)
@@ -22,7 +22,7 @@
 
 use libc;
 use llvm::archive_ro::{ArchiveRO, Child};
-use llvm;
+use llvm::{self, ArchiveKind};
 use rustc::metadata::loader::METADATA_FILENAME;
 use rustc::session::Session;
 use rustc_back::tempdir::TempDir;
@@ -208,28 +208,34 @@ pub fn update_symbols(&mut self) {
     /// Combine the provided files, rlibs, and native libraries into a single
     /// `Archive`.
     pub fn build(&mut self) {
-        let res = if self.using_llvm() {
-            self.build_with_llvm()
-        } else {
-            self.build_with_ar_cmd()
+        let res = match self.llvm_archive_kind() {
+            Some(kind) => self.build_with_llvm(kind),
+            None => self.build_with_ar_cmd(),
         };
         if let Err(e) = res {
             self.config.sess.fatal(&format!("failed to build archive: {}", e));
         }
     }
 
-    pub fn using_llvm(&self) -> bool {
+    pub fn llvm_archive_kind(&self) -> Option<ArchiveKind> {
         if unsafe { llvm::LLVMVersionMinor() < 7 } {
-            return false
+            return None
         }
 
         // Currently LLVM only supports writing archives in the 'gnu' format.
         match &self.config.sess.target.target.options.archive_format[..] {
-            "gnu" => true,
-            _ => false,
+            "gnu" => Some(ArchiveKind::K_GNU),
+            "mips64" => Some(ArchiveKind::K_MIPS64),
+            "bsd" => Some(ArchiveKind::K_BSD),
+            "coff" => Some(ArchiveKind::K_COFF),
+            _ => None,
         }
     }
 
+    pub fn using_llvm(&self) -> bool {
+        self.llvm_archive_kind().is_some()
+    }
+
     fn build_with_ar_cmd(&mut self) -> io::Result<()> {
         let removals = mem::replace(&mut self.removals, Vec::new());
         let additions = mem::replace(&mut self.additions, Vec::new());
@@ -425,7 +431,7 @@ fn prepare_ar_action(&self, cmd: &mut Command, dst: &Path, action: Action) {
         }
     }
 
-    fn build_with_llvm(&mut self) -> io::Result<()> {
+    fn build_with_llvm(&mut self, kind: ArchiveKind) -> io::Result<()> {
         let mut archives = Vec::new();
         let mut strings = Vec::new();
         let mut members = Vec::new();
@@ -482,7 +488,8 @@ fn build_with_llvm(&mut self) -> io::Result<()> {
             let r = llvm::LLVMRustWriteArchive(dst.as_ptr(),
                                                members.len() as libc::size_t,
                                                members.as_ptr(),
-                                               self.should_update_symbols);
+                                               self.should_update_symbols,
+                                               kind);
             let ret = if r != 0 {
                 let err = llvm::LLVMRustGetLastError();
                 let msg = if err.is_null() {
index 2e94c1969354a28cd51093cec27fa5c2c35c8d59..86225874df77d39b6e89e702b821bd092b9ae47b 100644 (file)
@@ -120,7 +120,17 @@ LLVMRustArchiveChildName(const Archive::Child *child, size_t *size) {
 
 extern "C" const char*
 LLVMRustArchiveChildData(Archive::Child *child, size_t *size) {
-    StringRef buf = child->getBuffer();
+    StringRef buf;
+#if LLVM_VERSION_MINOR >= 7
+    ErrorOr<StringRef> buf_or_err = child->getBuffer();
+    if (buf_or_err.getError()) {
+      LLVMRustSetLastError(buf_or_err.getError().message().c_str());
+      return NULL;
+    }
+    buf = buf_or_err.get();
+#else
+    buf = child->getBuffer();
+#endif
     *size = buf.size();
     return buf.data();
 }
@@ -144,7 +154,8 @@ extern "C" int
 LLVMRustWriteArchive(char *Dst,
                      size_t NumMembers,
                      const LLVMRustArchiveMember **NewMembers,
-                     bool WriteSymbtab) {
+                     bool WriteSymbtab,
+                     Archive::Kind Kind) {
 #if LLVM_VERSION_MINOR >= 7
   std::vector<NewArchiveIterator> Members;
 
@@ -157,7 +168,7 @@ LLVMRustWriteArchive(char *Dst,
       Members.push_back(NewArchiveIterator(Member->child, Member->name));
     }
   }
-  auto pair = writeArchive(Dst, Members, WriteSymbtab);
+  auto pair = writeArchive(Dst, Members, WriteSymbtab, Kind, false);
   if (!pair.second)
     return 0;
   LLVMRustSetLastError(pair.second.message().c_str());