From eeef8574498632db7f7351377ea06edaf0349c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Fri, 10 Sep 2021 01:32:55 +0200 Subject: [PATCH 1/4] Crude proof of concept --- .../rustc_codegen_llvm/src/back/archive.rs | 68 ++++++++----------- src/test/run-make/raw-dylib-c/Makefile | 6 +- 2 files changed, 31 insertions(+), 43 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index 4e86946219fb1..f877f422f9c32 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -8,7 +8,7 @@ use std::ptr; use std::str; use crate::llvm::archive_ro::{ArchiveRO, Child}; -use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport}; +use crate::llvm::{self, ArchiveKind, LLVMMachineType}; use rustc_codegen_ssa::back::archive::ArchiveBuilder; use rustc_data_structures::temp_dir::MaybeTempDir; use rustc_middle::middle::cstore::{DllCallingConvention, DllImport}; @@ -54,6 +54,7 @@ fn archive_config<'a>(sess: &'a Session, output: &Path, input: Option<&Path>) -> ArchiveConfig { sess, dst: output.to_path_buf(), src: input.map(|p| p.to_path_buf()) } } +#[allow(dead_code)] /// Map machine type strings to values of LLVM's MachineTypes enum. fn llvm_machine_type(cpu: &str) -> LLVMMachineType { match cpu { @@ -152,60 +153,47 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> { dll_imports: &[DllImport], tmpdir: &MaybeTempDir, ) { - let output_path = { - let mut output_path: PathBuf = tmpdir.as_ref().to_path_buf(); - output_path.push(format!("{}_imports", lib_name)); - output_path.with_extension("lib") - }; - - // we've checked for \0 characters in the library name already - let dll_name_z = CString::new(lib_name).unwrap(); - // All import names are Rust identifiers and therefore cannot contain \0 characters. - // FIXME: when support for #[link_name] implemented, ensure that import.name values don't - // have any \0 characters - let import_name_vector: Vec = dll_imports + let mut def_file_path = tmpdir.as_ref().to_path_buf(); + def_file_path.push(format!("{}_imports", lib_name)); + def_file_path.with_extension("def"); + let mut output_path: PathBuf = tmpdir.as_ref().to_path_buf(); + output_path.push(format!("{}_imports", lib_name)); + output_path.with_extension("dll.a"); + + let import_name_vector: Vec = dll_imports .iter() .map(|import: &DllImport| { if self.config.sess.target.arch == "x86" { - LlvmArchiveBuilder::i686_decorated_name(import) + LlvmArchiveBuilder::i686_decorated_name(import).into_string().unwrap() } else { - CString::new(import.name.to_string()).unwrap() + import.name.to_string() } }) .collect(); - let output_path_z = rustc_fs_util::path_to_c_string(&output_path); + let def_file_content = format!("EXPORTS\n{}", &import_name_vector.join("\n")); + std::fs::write(&def_file_path, &def_file_content).unwrap(); + std::fs::copy(&def_file_path, "d:/imports.def").unwrap(); - tracing::trace!("invoking LLVMRustWriteImportLibrary"); - tracing::trace!(" dll_name {:#?}", dll_name_z); - tracing::trace!(" output_path {}", output_path.display()); - tracing::trace!( - " import names: {}", - dll_imports.iter().map(|import| import.name.to_string()).collect::>().join(", "), - ); - - let ffi_exports: Vec = import_name_vector - .iter() - .map(|name_z| LLVMRustCOFFShortExport::from_name(name_z.as_ptr())) - .collect(); - let result = unsafe { - crate::llvm::LLVMRustWriteImportLibrary( - dll_name_z.as_ptr(), - output_path_z.as_ptr(), - ffi_exports.as_ptr(), - ffi_exports.len(), - llvm_machine_type(&self.config.sess.target.arch) as u16, - !self.config.sess.target.is_like_msvc, - ) - }; + let result = std::process::Command::new("dlltool") + .args([ + "-d", + def_file_path.to_str().unwrap(), + "-D", + lib_name, + "-l", + output_path.to_str().unwrap(), + ]) + .status(); - if result == crate::llvm::LLVMRustResult::Failure { + if let Err(e) = result { self.config.sess.fatal(&format!( "Error creating import library for {}: {}", lib_name, - llvm::last_error().unwrap_or("unknown LLVM error".to_string()) + e.to_string() )); } + std::fs::copy(&output_path, "d:/imports.dll.a").unwrap(); self.add_archive(&output_path, |_| false).unwrap_or_else(|e| { self.config.sess.fatal(&format!( diff --git a/src/test/run-make/raw-dylib-c/Makefile b/src/test/run-make/raw-dylib-c/Makefile index 26ab4d34764d1..f53a4b3493f4f 100644 --- a/src/test/run-make/raw-dylib-c/Makefile +++ b/src/test/run-make/raw-dylib-c/Makefile @@ -1,14 +1,14 @@ # Test the behavior of #[link(.., kind = "raw-dylib")] on windows-msvc -# only-windows-msvc +# only-windows-gnu -include ../../run-make-fulldeps/tools.mk all: $(call COMPILE_OBJ,"$(TMPDIR)"/extern_1.obj,extern_1.c) $(call COMPILE_OBJ,"$(TMPDIR)"/extern_2.obj,extern_2.c) - $(CC) "$(TMPDIR)"/extern_1.obj -link -dll -out:"$(TMPDIR)"/extern_1.dll - $(CC) "$(TMPDIR)"/extern_2.obj -link -dll -out:"$(TMPDIR)"/extern_2.dll + $(CC) "$(TMPDIR)"/extern_1.obj -shared -o "$(TMPDIR)"/extern_1.dll + $(CC) "$(TMPDIR)"/extern_2.obj -shared -o "$(TMPDIR)"/extern_2.dll $(RUSTC) --crate-type lib --crate-name raw_dylib_test lib.rs $(RUSTC) --crate-type bin driver.rs -L "$(TMPDIR)" "$(TMPDIR)"/driver > "$(TMPDIR)"/output.txt From e5f5895d3515233d4f3e0f67340d25ea7250679f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Sat, 11 Sep 2021 01:58:43 +0200 Subject: [PATCH 2/4] Almost fix i686 --- compiler/rustc_codegen_llvm/src/back/archive.rs | 12 ++++++++---- .../raw-dylib-alt-calling-convention/Makefile | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index f877f422f9c32..49d7ad91e9d87 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -164,7 +164,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> { .iter() .map(|import: &DllImport| { if self.config.sess.target.arch == "x86" { - LlvmArchiveBuilder::i686_decorated_name(import).into_string().unwrap() + LlvmArchiveBuilder::i686_decorated_name(import, true).into_string().unwrap() } else { import.name.to_string() } @@ -320,13 +320,17 @@ impl<'a> LlvmArchiveBuilder<'a> { } } - fn i686_decorated_name(import: &DllImport) -> CString { + fn i686_decorated_name(import: &DllImport, mingw: bool) -> CString { let name = import.name; + // MinGW doesn't want `_` prefix for `.def` file + let prefix = if mingw { "" } else { "_" }; // We verified during construction that `name` does not contain any NULL characters, so the // conversion to CString is guaranteed to succeed. CString::new(match import.calling_convention { - DllCallingConvention::C => format!("_{}", name), - DllCallingConvention::Stdcall(arg_list_size) => format!("_{}@{}", name, arg_list_size), + DllCallingConvention::C => format!("{}{}", prefix, name), + DllCallingConvention::Stdcall(arg_list_size) => { + format!("{}{}@{}", prefix, name, arg_list_size) + } DllCallingConvention::Fastcall(arg_list_size) => format!("@{}@{}", name, arg_list_size), DllCallingConvention::Vectorcall(arg_list_size) => { format!("{}@@{}", name, arg_list_size) diff --git a/src/test/run-make/raw-dylib-alt-calling-convention/Makefile b/src/test/run-make/raw-dylib-alt-calling-convention/Makefile index 0f874333fa09c..bcf28b06d134c 100644 --- a/src/test/run-make/raw-dylib-alt-calling-convention/Makefile +++ b/src/test/run-make/raw-dylib-alt-calling-convention/Makefile @@ -1,12 +1,12 @@ # Test the behavior of #[link(.., kind = "raw-dylib")] with alternative calling conventions. -# only-i686-pc-windows-msvc +# only-i686-pc-windows-gnu -include ../../run-make-fulldeps/tools.mk all: $(call COMPILE_OBJ,"$(TMPDIR)"/extern.obj,extern.c) - $(CC) "$(TMPDIR)"/extern.obj -link -dll -out:"$(TMPDIR)"/extern.dll + $(CC) "$(TMPDIR)"/extern.obj -shared -o "$(TMPDIR)"/extern.dll $(RUSTC) --crate-type lib --crate-name raw_dylib_alt_calling_convention_test lib.rs $(RUSTC) --crate-type bin driver.rs -L "$(TMPDIR)" "$(TMPDIR)"/driver > "$(TMPDIR)"/output.txt From d12b0e7d5763031bd6faf1b07bce518c6e6b8f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Sat, 11 Sep 2021 18:55:31 +0200 Subject: [PATCH 3/4] Make dlltool version coexist with LLVM one --- .../rustc_codegen_llvm/src/back/archive.rs | 155 ++++++++++++++---- 1 file changed, 119 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index 49d7ad91e9d87..1c3358005fa18 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -1,6 +1,7 @@ //! A helper class for dealing with static archives -use std::ffi::{CStr, CString}; +use std::env; +use std::ffi::{CStr, CString, OsString}; use std::io; use std::mem; use std::path::{Path, PathBuf}; @@ -8,7 +9,7 @@ use std::ptr; use std::str; use crate::llvm::archive_ro::{ArchiveRO, Child}; -use crate::llvm::{self, ArchiveKind, LLVMMachineType}; +use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport}; use rustc_codegen_ssa::back::archive::ArchiveBuilder; use rustc_data_structures::temp_dir::MaybeTempDir; use rustc_middle::middle::cstore::{DllCallingConvention, DllImport}; @@ -54,7 +55,6 @@ fn archive_config<'a>(sess: &'a Session, output: &Path, input: Option<&Path>) -> ArchiveConfig { sess, dst: output.to_path_buf(), src: input.map(|p| p.to_path_buf()) } } -#[allow(dead_code)] /// Map machine type strings to values of LLVM's MachineTypes enum. fn llvm_machine_type(cpu: &str) -> LLVMMachineType { match cpu { @@ -153,47 +153,107 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> { dll_imports: &[DllImport], tmpdir: &MaybeTempDir, ) { - let mut def_file_path = tmpdir.as_ref().to_path_buf(); - def_file_path.push(format!("{}_imports", lib_name)); - def_file_path.with_extension("def"); - let mut output_path: PathBuf = tmpdir.as_ref().to_path_buf(); - output_path.push(format!("{}_imports", lib_name)); - output_path.with_extension("dll.a"); - - let import_name_vector: Vec = dll_imports + let output_path = { + let mut output_path: PathBuf = tmpdir.as_ref().to_path_buf(); + output_path.push(format!("{}_imports", lib_name)); + output_path.with_extension("lib") + }; + + // BFD doesn't work properly with short imports + let mingw_gnu_toolchain = self.config.sess.target.llvm_target.ends_with("pc-windows-gnu"); + + // All import names are Rust identifiers and therefore cannot contain \0 characters. + // FIXME: when support for #[link_name] implemented, ensure that import.name values don't + // have any \0 characters + let import_name_vector: Vec = dll_imports .iter() .map(|import: &DllImport| { if self.config.sess.target.arch == "x86" { - LlvmArchiveBuilder::i686_decorated_name(import, true).into_string().unwrap() + LlvmArchiveBuilder::i686_decorated_name(import, mingw_gnu_toolchain) } else { - import.name.to_string() + CString::new(import.name.to_string()).unwrap() } }) .collect(); - let def_file_content = format!("EXPORTS\n{}", &import_name_vector.join("\n")); - std::fs::write(&def_file_path, &def_file_content).unwrap(); - std::fs::copy(&def_file_path, "d:/imports.def").unwrap(); - - let result = std::process::Command::new("dlltool") - .args([ - "-d", - def_file_path.to_str().unwrap(), - "-D", - lib_name, - "-l", - output_path.to_str().unwrap(), - ]) - .status(); - - if let Err(e) = result { - self.config.sess.fatal(&format!( - "Error creating import library for {}: {}", - lib_name, - e.to_string() - )); - } - std::fs::copy(&output_path, "d:/imports.dll.a").unwrap(); + if mingw_gnu_toolchain { + let mut def_file_path = tmpdir.as_ref().to_path_buf(); + def_file_path.push(format!("{}_imports", lib_name)); + def_file_path.with_extension("def"); + + let def_file_content = format!( + "EXPORTS\n{}", + import_name_vector + .iter() + .map(|cstring| cstring.to_str().unwrap()) + .intersperse("\n") + .collect::() + ); + std::fs::write(&def_file_path, def_file_content).unwrap(); + + let dlltool = find_dlltool(self.config.sess); + let result = std::process::Command::new(dlltool) + .args([ + "-d", + def_file_path.to_str().unwrap(), + "-D", + lib_name, + "-l", + output_path.to_str().unwrap(), + ]) + .output(); + + match result { + Err(e) => { + self.config.sess.fatal(&format!("Error calling dlltool: {}", e.to_string())) + } + Ok(output) if !output.status.success() => self.config.sess.fatal(&format!( + "Dlltool could not create import library: {}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + )), + _ => {} + } + } else { + // we've checked for \0 characters in the library name already + let dll_name_z = CString::new(lib_name).unwrap(); + let output_path_z = rustc_fs_util::path_to_c_string(&output_path); + + tracing::trace!("invoking LLVMRustWriteImportLibrary"); + tracing::trace!(" dll_name {:#?}", dll_name_z); + tracing::trace!(" output_path {}", output_path.display()); + tracing::trace!( + " import names: {}", + dll_imports + .iter() + .map(|import| import.name.to_string()) + .collect::>() + .join(", "), + ); + + let ffi_exports: Vec = import_name_vector + .iter() + .map(|name_z| LLVMRustCOFFShortExport::from_name(name_z.as_ptr())) + .collect(); + let result = unsafe { + crate::llvm::LLVMRustWriteImportLibrary( + dll_name_z.as_ptr(), + output_path_z.as_ptr(), + ffi_exports.as_ptr(), + ffi_exports.len(), + llvm_machine_type(&self.config.sess.target.arch) as u16, + !self.config.sess.target.is_like_msvc, + ) + }; + + if result == crate::llvm::LLVMRustResult::Failure { + self.config.sess.fatal(&format!( + "Error creating import library for {}: {}", + lib_name, + llvm::last_error().unwrap_or("unknown LLVM error".to_string()) + )); + } + }; self.add_archive(&output_path, |_| false).unwrap_or_else(|e| { self.config.sess.fatal(&format!( @@ -343,3 +403,26 @@ impl<'a> LlvmArchiveBuilder<'a> { fn string_to_io_error(s: String) -> io::Error { io::Error::new(io::ErrorKind::Other, format!("bad archive: {}", s)) } + +fn find_dlltool(sess: &Session) -> OsString { + // When cross-compiling first try binary prefixed with target triple + if sess.host.llvm_target != sess.target.llvm_target { + let prefixed_dlltool = if sess.target.arch == "x86" { + "i686-w64-mingw32-dlltool" + } else { + "x86_64-w64-mingw32-dlltool" + }; + let prefixed_dlltool = if cfg!(windows) { + [prefixed_dlltool, "exe"].concat() + } else { + prefixed_dlltool.to_string() + }; + for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) { + let full_path = dir.join(&prefixed_dlltool); + if full_path.is_file() { + return full_path.into_os_string(); + } + } + } + OsString::from("dlltool") +} From 83c2feaf066447912d129ae46206ec049430e2f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Sat, 11 Sep 2021 19:00:20 +0200 Subject: [PATCH 4/4] Build MinGW on try --- .github/workflows/ci.yml | 12 ++++++++++++ src/ci/github-actions/ci.yml | 16 +++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff4fa1527e93a..2f960c0710051 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -537,6 +537,18 @@ jobs: - name: dist-x86_64-linux os: ubuntu-latest-xl env: {} + - name: dist-i686-mingw + env: + RUST_CONFIGURE_ARGS: "--build=i686-pc-windows-gnu" + SCRIPT: python x.py dist + CUSTOM_MINGW: 1 + os: windows-latest-xl + - name: dist-x86_64-mingw + env: + SCRIPT: python x.py dist + RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-gnu" + CUSTOM_MINGW: 1 + os: windows-latest-xl timeout-minutes: 600 runs-on: "${{ matrix.os }}" steps: diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml index 6417f5a984ad5..a20bba1146f05 100644 --- a/src/ci/github-actions/ci.yml +++ b/src/ci/github-actions/ci.yml @@ -14,7 +14,6 @@ # step CI will fail. --- - ############################### # YAML Anchors Definition # ############################### @@ -30,7 +29,6 @@ # The expand-yaml-anchors tool will automatically remove this block from the # output YAML file. x--expand-yaml-anchors--remove: - - &shared-ci-variables CI_JOB_NAME: ${{ matrix.name }} @@ -77,7 +75,7 @@ x--expand-yaml-anchors--remove: <<: *base-job - &job-macos-xl - os: macos-latest # We don't have an XL builder for this + os: macos-latest # We don't have an XL builder for this <<: *base-job - &job-windows-xl @@ -669,6 +667,18 @@ jobs: matrix: include: - *dist-x86_64-linux + - name: dist-i686-mingw + env: + RUST_CONFIGURE_ARGS: --build=i686-pc-windows-gnu + SCRIPT: python x.py dist + CUSTOM_MINGW: 1 + <<: *job-windows-xl + - name: dist-x86_64-mingw + env: + SCRIPT: python x.py dist + RUST_CONFIGURE_ARGS: --build=x86_64-pc-windows-gnu + CUSTOM_MINGW: 1 + <<: *job-windows-xl master: name: master