|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | + |
| 12 | +// To avoid problems with Windows UAC installer detection heuristics, |
| 13 | +// Rust-produced executables need an application manifest. |
| 14 | +// For details, see issue #10512. |
| 15 | + |
| 16 | +// No-op on other platforms. |
| 17 | + |
| 18 | +use driver::session::Session; |
| 19 | +use std::path::Path; |
| 20 | + |
| 21 | +#[cfg(not(windows))] |
| 22 | +pub fn postprocess_executable(_sess: Session, _filename: &Path) {} |
| 23 | + |
| 24 | +#[cfg(windows)] |
| 25 | +pub fn postprocess_executable(sess: Session, filename: &Path) { |
| 26 | + |
| 27 | + let default_manifest = concat!( |
| 28 | + "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>", |
| 29 | + "<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>", |
| 30 | + " <trustInfo xmlns='urn:schemas-microsoft-com:asm.v3'>", |
| 31 | + " <security>", |
| 32 | + " <requestedPrivileges>", |
| 33 | + " <requestedExecutionLevel level='asInvoker' uiAccess='false' />", |
| 34 | + " </requestedPrivileges>", |
| 35 | + " </security>", |
| 36 | + " </trustInfo>", |
| 37 | + "</assembly>"); |
| 38 | + |
| 39 | + match windows::embed_manifest(filename, default_manifest) { |
| 40 | + Ok(_) => (), |
| 41 | + Err(msg) => sess.err(format!("Could not embed application manifest: {}", msg)) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[cfg(windows)] |
| 46 | +mod windows { |
| 47 | + use std::libc::types::os::arch::extra::{BOOL,WORD,DWORD,HANDLE,LPCWSTR,LPCVOID}; |
| 48 | + use std::libc::consts::os::extra::FALSE; |
| 49 | + use std::cast::transmute; |
| 50 | + use std::os; |
| 51 | + |
| 52 | + // FIXME #9053: should import as_utf16_p from std rather than re-defining here |
| 53 | + //use std::os::win32::as_utf16_p; |
| 54 | + fn as_utf16_p<T>(s: &str, f: |*u16| -> T) -> T { |
| 55 | + let mut t = s.to_utf16(); |
| 56 | + // Null terminate before passing on. |
| 57 | + t.push(0u16); |
| 58 | + t.as_imm_buf(|buf, _len| f(buf)) |
| 59 | + } |
| 60 | + |
| 61 | + #[link_name = "kernel32"] |
| 62 | + extern "system" { |
| 63 | + pub fn BeginUpdateResourceW(pFileName: LPCWSTR, |
| 64 | + bDeleteExistingResources: BOOL) -> HANDLE; |
| 65 | + pub fn UpdateResourceW(hUpdate: HANDLE, |
| 66 | + lpType: LPCWSTR, |
| 67 | + lpName: LPCWSTR, |
| 68 | + wLanguage: WORD, |
| 69 | + lpData: LPCVOID, |
| 70 | + cbData: DWORD) -> BOOL; |
| 71 | + pub fn EndUpdateResourceW(hUpdate: HANDLE, |
| 72 | + fDiscard: BOOL) -> BOOL; |
| 73 | + } |
| 74 | + |
| 75 | + fn MAKEINTRESOURCEW(id: int) -> LPCWSTR { |
| 76 | + unsafe{ transmute(id) } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn embed_manifest(filename: &Path, |
| 80 | + manifest: &str) -> Result<(),~str> { |
| 81 | + unsafe { |
| 82 | + let hUpdate = as_utf16_p(filename.as_str().unwrap(), |path| { |
| 83 | + BeginUpdateResourceW(path, FALSE) |
| 84 | + }); |
| 85 | + if hUpdate.is_null() { |
| 86 | + return Err(format!("failure in BeginUpdateResourceW: {}", os::last_os_error())); |
| 87 | + } |
| 88 | + |
| 89 | + let ok = manifest.as_imm_buf(|p, len| { |
| 90 | + UpdateResourceW(hUpdate, |
| 91 | + MAKEINTRESOURCEW(24), // RT_MANIFEST |
| 92 | + MAKEINTRESOURCEW(1), // CREATEPROCESS_MANIFEST_RESOURCE_ID |
| 93 | + 0, // LANG_NEUTRAL, SUBLANG_NEUTRAL |
| 94 | + p as LPCVOID, |
| 95 | + len as u32) |
| 96 | + }); |
| 97 | + if ok == FALSE { |
| 98 | + return Err(format!("failure in UpdateResourceW: {}", os::last_os_error())); |
| 99 | + } |
| 100 | + |
| 101 | + let ok = EndUpdateResourceW(hUpdate, FALSE); |
| 102 | + if ok == FALSE { |
| 103 | + return Err(format!("failure in EndUpdateResourceW: {}", os::last_os_error())); |
| 104 | + } |
| 105 | + Ok(()) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments