Skip to content

Commit ce27752

Browse files
committed
auto merge of #8488 : klutzy/rust/mingw-w64, r=brson
This patchset enables rustc to cross-build mingw-w64 outputs. Tested on mingw + mingw-w64 (mingw-builds, win64/seh/win32-threads/gcc-4.8.1). I also patched llvm to support Win64 stack unwinding. klutzy/llvm@ebe22bd I cross-built test/run-pass/smallest-hello-world.rs and confirmed it works. However, I also found something went wrong if I don't have custom `#[start]` routine.
2 parents 36b5115 + 442f4a5 commit ce27752

File tree

14 files changed

+497
-40
lines changed

14 files changed

+497
-40
lines changed

src/librustc/middle/trans/base.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2370,7 +2370,12 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
23702370
decl_cdecl_fn(ccx.llmod, "amain", llfty)
23712371
} else {
23722372
let main_name = match ccx.sess.targ_cfg.os {
2373-
session::os_win32 => ~"WinMain@16",
2373+
session::os_win32 => {
2374+
match ccx.sess.targ_cfg.arch {
2375+
X86 => ~"WinMain@16",
2376+
_ => ~"WinMain",
2377+
}
2378+
},
23742379
_ => ~"main",
23752380
};
23762381
decl_cdecl_fn(ccx.llmod, main_name, llfty)

src/libstd/io.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use container::Container;
5353
use int;
5454
use iterator::Iterator;
5555
use libc::consts::os::posix88::*;
56-
use libc::{c_int, c_long, c_void, size_t, ssize_t};
56+
use libc::{c_int, c_void, size_t};
5757
use libc;
5858
use num;
5959
use ops::Drop;
@@ -970,7 +970,7 @@ impl Reader for *libc::FILE {
970970

971971
unsafe {
972972
assert!(libc::fseek(*self,
973-
offset as c_long,
973+
offset as libc::c_long,
974974
convert_whence(whence)) == 0 as c_int);
975975
}
976976
}
@@ -1199,7 +1199,7 @@ impl Writer for *libc::FILE {
11991199

12001200
unsafe {
12011201
assert!(libc::fseek(*self,
1202-
offset as c_long,
1202+
offset as libc::c_long,
12031203
convert_whence(whence)) == 0 as c_int);
12041204
}
12051205
}
@@ -1240,13 +1240,23 @@ impl Writer for fd_t {
12401240
fn write(&self, v: &[u8]) {
12411241
#[fixed_stack_segment]; #[inline(never)];
12421242

1243+
#[cfg(windows)]
1244+
type IoSize = libc::c_uint;
1245+
#[cfg(windows)]
1246+
type IoRet = c_int;
1247+
1248+
#[cfg(unix)]
1249+
type IoSize = size_t;
1250+
#[cfg(unix)]
1251+
type IoRet = libc::ssize_t;
1252+
12431253
unsafe {
12441254
let mut count = 0u;
12451255
do v.as_imm_buf |vbuf, len| {
12461256
while count < len {
12471257
let vb = ptr::offset(vbuf, count as int) as *c_void;
1248-
let nout = libc::write(*self, vb, len as size_t);
1249-
if nout < 0 as ssize_t {
1258+
let nout = libc::write(*self, vb, len as IoSize);
1259+
if nout < 0 as IoRet {
12501260
error!("error writing buffer");
12511261
error!("%s", os::last_os_error());
12521262
fail!();

src/libstd/libc.rs

+272
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,172 @@ pub mod types {
764764
pub type LPMEMORY_BASIC_INFORMATION = *mut MEMORY_BASIC_INFORMATION;
765765
}
766766
}
767+
768+
#[cfg(target_arch = "x86_64")]
769+
pub mod arch {
770+
pub mod c95 {
771+
pub type c_char = i8;
772+
pub type c_schar = i8;
773+
pub type c_uchar = u8;
774+
pub type c_short = i16;
775+
pub type c_ushort = u16;
776+
pub type c_int = i32;
777+
pub type c_uint = u32;
778+
pub type c_long = i32;
779+
pub type c_ulong = u32;
780+
pub type c_float = f32;
781+
pub type c_double = f64;
782+
pub type size_t = u64;
783+
pub type ptrdiff_t = i64;
784+
pub type clock_t = i32;
785+
pub type time_t = i64;
786+
pub type wchar_t = u16;
787+
}
788+
pub mod c99 {
789+
pub type c_longlong = i64;
790+
pub type c_ulonglong = u64;
791+
pub type intptr_t = int;
792+
pub type uintptr_t = uint;
793+
}
794+
pub mod posix88 {
795+
pub type off_t = i32; // XXX unless _FILE_OFFSET_BITS == 64
796+
pub type dev_t = u32;
797+
pub type ino_t = i16;
798+
pub type pid_t = i64;
799+
pub type useconds_t = u32;
800+
pub type mode_t = u16;
801+
pub type ssize_t = i64;
802+
}
803+
pub mod posix01 {
804+
}
805+
pub mod posix08 {
806+
}
807+
pub mod bsd44 {
808+
}
809+
pub mod extra {
810+
use ptr;
811+
use libc::types::common::c95::c_void;
812+
use libc::types::os::arch::c95::{c_char, c_int, c_uint, size_t};
813+
use libc::types::os::arch::c95::{c_ulong};
814+
use libc::types::os::arch::c95::{wchar_t};
815+
use libc::types::os::arch::c99::{c_ulonglong};
816+
817+
pub type BOOL = c_int;
818+
pub type BYTE = u8;
819+
pub type CCHAR = c_char;
820+
pub type CHAR = c_char;
821+
822+
pub type DWORD = c_ulong;
823+
pub type DWORDLONG = c_ulonglong;
824+
825+
pub type HANDLE = LPVOID;
826+
pub type HMODULE = c_uint;
827+
828+
pub type LONG_PTR = i64; // changed
829+
830+
pub type LPCWSTR = *WCHAR;
831+
pub type LPCSTR = *CHAR;
832+
pub type LPCTSTR = *CHAR;
833+
pub type LPTCH = *CHAR;
834+
835+
pub type LPWSTR = *mut WCHAR;
836+
pub type LPSTR = *mut CHAR;
837+
pub type LPTSTR = *mut CHAR;
838+
839+
// Not really, but opaque to us.
840+
pub type LPSECURITY_ATTRIBUTES = LPVOID;
841+
842+
pub type LPVOID = *mut c_void;
843+
pub type LPCVOID = *c_void;
844+
pub type LPBYTE = *mut BYTE;
845+
pub type LPWORD = *mut WORD;
846+
pub type LPDWORD = *mut DWORD;
847+
pub type LPHANDLE = *mut HANDLE;
848+
849+
pub type LRESULT = LONG_PTR;
850+
pub type PBOOL = *mut BOOL;
851+
pub type WCHAR = wchar_t;
852+
pub type WORD = u16;
853+
pub type SIZE_T = size_t;
854+
855+
pub type time64_t = i64;
856+
pub type int64 = i64;
857+
858+
pub struct STARTUPINFO {
859+
cb: DWORD,
860+
lpReserved: LPTSTR,
861+
lpDesktop: LPTSTR,
862+
lpTitle: LPTSTR,
863+
dwX: DWORD,
864+
dwY: DWORD,
865+
dwXSize: DWORD,
866+
dwYSize: DWORD,
867+
dwXCountChars: DWORD,
868+
dwYCountCharts: DWORD,
869+
dwFillAttribute: DWORD,
870+
dwFlags: DWORD,
871+
wShowWindow: WORD,
872+
cbReserved2: WORD,
873+
lpReserved2: LPBYTE,
874+
hStdInput: HANDLE,
875+
hStdOutput: HANDLE,
876+
hStdError: HANDLE
877+
}
878+
pub type LPSTARTUPINFO = *mut STARTUPINFO;
879+
880+
pub struct PROCESS_INFORMATION {
881+
hProcess: HANDLE,
882+
hThread: HANDLE,
883+
dwProcessId: DWORD,
884+
dwThreadId: DWORD
885+
}
886+
pub type LPPROCESS_INFORMATION = *mut PROCESS_INFORMATION;
887+
888+
pub struct SYSTEM_INFO {
889+
wProcessorArchitecture: WORD,
890+
wReserved: WORD,
891+
dwPageSize: DWORD,
892+
lpMinimumApplicationAddress: LPVOID,
893+
lpMaximumApplicationAddress: LPVOID,
894+
dwActiveProcessorMask: DWORD,
895+
dwNumberOfProcessors: DWORD,
896+
dwProcessorType: DWORD,
897+
dwAllocationGranularity: DWORD,
898+
wProcessorLevel: WORD,
899+
wProcessorRevision: WORD
900+
}
901+
pub type LPSYSTEM_INFO = *mut SYSTEM_INFO;
902+
903+
impl SYSTEM_INFO {
904+
pub fn new() -> SYSTEM_INFO {
905+
SYSTEM_INFO {
906+
wProcessorArchitecture: 0,
907+
wReserved: 0,
908+
dwPageSize: 0,
909+
lpMinimumApplicationAddress: ptr::mut_null(),
910+
lpMaximumApplicationAddress: ptr::mut_null(),
911+
dwActiveProcessorMask: 0,
912+
dwNumberOfProcessors: 0,
913+
dwProcessorType: 0,
914+
dwAllocationGranularity: 0,
915+
wProcessorLevel: 0,
916+
wProcessorRevision: 0
917+
}
918+
}
919+
}
920+
921+
pub struct MEMORY_BASIC_INFORMATION {
922+
BaseAddress: LPVOID,
923+
AllocationBase: LPVOID,
924+
AllocationProtect: DWORD,
925+
RegionSize: SIZE_T,
926+
State: DWORD,
927+
Protect: DWORD,
928+
Type: DWORD
929+
}
930+
pub type LPMEMORY_BASIC_INFORMATION = *mut MEMORY_BASIC_INFORMATION;
931+
}
932+
}
767933
}
768934

769935
#[cfg(target_os = "macos")]
@@ -3093,6 +3259,7 @@ pub mod funcs {
30933259
LPSYSTEM_INFO};
30943260
use libc::types::os::arch::extra::{HANDLE, LPHANDLE};
30953261

3262+
#[cfg(target_arch = "x86")]
30963263
#[abi = "stdcall"]
30973264
extern "stdcall" {
30983265
pub fn GetEnvironmentVariableW(n: LPCWSTR,
@@ -3197,6 +3364,111 @@ pub mod funcs {
31973364
-> LPVOID;
31983365
pub fn UnmapViewOfFile(lpBaseAddress: LPCVOID) -> BOOL;
31993366
}
3367+
3368+
#[cfg(target_arch = "x86_64")]
3369+
extern {
3370+
pub fn GetEnvironmentVariableW(n: LPCWSTR,
3371+
v: LPWSTR,
3372+
nsize: DWORD)
3373+
-> DWORD;
3374+
pub fn SetEnvironmentVariableW(n: LPCWSTR, v: LPCWSTR)
3375+
-> BOOL;
3376+
pub fn GetEnvironmentStringsA() -> LPTCH;
3377+
pub fn FreeEnvironmentStringsA(env_ptr: LPTCH) -> BOOL;
3378+
pub fn GetModuleFileNameW(hModule: HMODULE,
3379+
lpFilename: LPWSTR,
3380+
nSize: DWORD)
3381+
-> DWORD;
3382+
pub fn CreateDirectoryW(lpPathName: LPCWSTR,
3383+
lpSecurityAttributes:
3384+
LPSECURITY_ATTRIBUTES)
3385+
-> BOOL;
3386+
pub fn CopyFileW(lpExistingFileName: LPCWSTR,
3387+
lpNewFileName: LPCWSTR,
3388+
bFailIfExists: BOOL)
3389+
-> BOOL;
3390+
pub fn DeleteFileW(lpPathName: LPCWSTR) -> BOOL;
3391+
pub fn RemoveDirectoryW(lpPathName: LPCWSTR) -> BOOL;
3392+
pub fn SetCurrentDirectoryW(lpPathName: LPCWSTR) -> BOOL;
3393+
pub fn GetLastError() -> DWORD;
3394+
pub fn FindFirstFileW(fileName: *u16, findFileData: HANDLE)
3395+
-> HANDLE;
3396+
pub fn FindNextFileW(findFile: HANDLE, findFileData: HANDLE)
3397+
-> BOOL;
3398+
pub fn FindClose(findFile: HANDLE) -> BOOL;
3399+
pub fn DuplicateHandle(hSourceProcessHandle: HANDLE,
3400+
hSourceHandle: HANDLE,
3401+
hTargetProcessHandle: HANDLE,
3402+
lpTargetHandle: LPHANDLE,
3403+
dwDesiredAccess: DWORD,
3404+
bInheritHandle: BOOL,
3405+
dwOptions: DWORD)
3406+
-> BOOL;
3407+
pub fn CloseHandle(hObject: HANDLE) -> BOOL;
3408+
pub fn OpenProcess(dwDesiredAccess: DWORD,
3409+
bInheritHandle: BOOL,
3410+
dwProcessId: DWORD)
3411+
-> HANDLE;
3412+
pub fn GetCurrentProcess() -> HANDLE;
3413+
pub fn CreateProcessA(lpApplicationName: LPCTSTR,
3414+
lpCommandLine: LPTSTR,
3415+
lpProcessAttributes:
3416+
LPSECURITY_ATTRIBUTES,
3417+
lpThreadAttributes:
3418+
LPSECURITY_ATTRIBUTES,
3419+
bInheritHandles: BOOL,
3420+
dwCreationFlags: DWORD,
3421+
lpEnvironment: LPVOID,
3422+
lpCurrentDirectory: LPCTSTR,
3423+
lpStartupInfo: LPSTARTUPINFO,
3424+
lpProcessInformation:
3425+
LPPROCESS_INFORMATION)
3426+
-> BOOL;
3427+
pub fn WaitForSingleObject(hHandle: HANDLE,
3428+
dwMilliseconds: DWORD)
3429+
-> DWORD;
3430+
pub fn TerminateProcess(hProcess: HANDLE, uExitCode: c_uint)
3431+
-> BOOL;
3432+
pub fn GetExitCodeProcess(hProcess: HANDLE,
3433+
lpExitCode: LPDWORD)
3434+
-> BOOL;
3435+
pub fn GetSystemInfo(lpSystemInfo: LPSYSTEM_INFO);
3436+
pub fn VirtualAlloc(lpAddress: LPVOID,
3437+
dwSize: SIZE_T,
3438+
flAllocationType: DWORD,
3439+
flProtect: DWORD)
3440+
-> LPVOID;
3441+
pub fn VirtualFree(lpAddress: LPVOID,
3442+
dwSize: SIZE_T,
3443+
dwFreeType: DWORD)
3444+
-> BOOL;
3445+
pub fn VirtualLock(lpAddress: LPVOID, dwSize: SIZE_T) -> BOOL;
3446+
pub fn VirtualUnlock(lpAddress: LPVOID, dwSize: SIZE_T)
3447+
-> BOOL;
3448+
pub fn VirtualProtect(lpAddress: LPVOID,
3449+
dwSize: SIZE_T,
3450+
flNewProtect: DWORD,
3451+
lpflOldProtect: LPDWORD)
3452+
-> BOOL;
3453+
pub fn VirtualQuery(lpAddress: LPCVOID,
3454+
lpBuffer: LPMEMORY_BASIC_INFORMATION,
3455+
dwLength: SIZE_T)
3456+
-> SIZE_T;
3457+
pub fn CreateFileMappingW(hFile: HANDLE,
3458+
lpAttributes: LPSECURITY_ATTRIBUTES,
3459+
flProtect: DWORD,
3460+
dwMaximumSizeHigh: DWORD,
3461+
dwMaximumSizeLow: DWORD,
3462+
lpName: LPCTSTR)
3463+
-> HANDLE;
3464+
pub fn MapViewOfFile(hFileMappingObject: HANDLE,
3465+
dwDesiredAccess: DWORD,
3466+
dwFileOffsetHigh: DWORD,
3467+
dwFileOffsetLow: DWORD,
3468+
dwNumberOfBytesToMap: SIZE_T)
3469+
-> LPVOID;
3470+
pub fn UnmapViewOfFile(lpBaseAddress: LPCVOID) -> BOOL;
3471+
}
32003472
}
32013473

32023474
pub mod msvcrt {

0 commit comments

Comments
 (0)