Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds x86 build #624

Merged
merged 11 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,31 @@ jobs:
- os: windows-latest
rust: nightly
other: x86_64-pc-windows-msvc
- os: windows-latest
rust: stable
other: i686-pc-windows-msvc
- os: windows-latest
rust: nightly
other: i686-pc-windows-msvc
- os: ubuntu-latest
rust: stable
other: i686-unknown-linux-gnu
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
profile: minimal
override: true
components: rustfmt
- run: rustup update --no-self-update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- run: rustup target add ${{ matrix.other }}
- run: rustup component add rustfmt || echo "rustfmt not available"

- name: build
- name: build linux
run: cargo build
if: matrix.os == 'ubuntu-latest'

- name: test
run: cargo test --all
- name: build windows
run: cargo build --all --target ${{ matrix.other }}
if: matrix.os == 'windows-latest'

- name: test windows
run: cargo test --all --target ${{ matrix.other }}
if: matrix.os == 'windows-latest'

- name: fmt
Expand Down
Binary file modified .windows/x64/TestComponent.dll
Binary file not shown.
Binary file modified .windows/x86/TestComponent.dll
Binary file not shown.
2 changes: 2 additions & 0 deletions crates/gen/src/parser/element_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ impl ElementType {
| Self::ComInterface(_)
| Self::Delegate(_) => false,
Self::Struct(def) => def.is_blittable(),
Self::Array((kind, _)) => kind.is_blittable(),
_ => true,
}
}
Expand Down Expand Up @@ -470,6 +471,7 @@ impl ElementType {
s.0.fields().any(|f| f.signature().is_explicit())
}
}
Self::Array((kind, _)) => kind.is_explicit(),
_ => false,
}
}
Expand Down
26 changes: 21 additions & 5 deletions crates/gen/src/types/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ impl Struct {
.iter()
.any(|(_, signature, _)| signature.is_explicit());

// TODO: workaround for getting windows-docs building
let has_complex_array = fields
.iter()
.any(|(_, signature, _)| match &signature.kind {
ElementType::Array((signature, _)) => {
!signature.is_blittable() || signature.kind.is_nullable()
}
_ => false,
});

let runtime_type = if is_winrt {
let signature = Literal::byte_string(&self.type_signature().as_bytes());

Expand Down Expand Up @@ -238,7 +248,7 @@ impl Struct {
None
});

let compare = if is_union | has_union {
let compare = if is_union | has_union | has_complex_array {
quote! {}
} else {
let compare = fields
Expand Down Expand Up @@ -274,7 +284,7 @@ impl Struct {
}
};

let default = if is_union || has_union {
let default = if is_union || has_union || has_complex_array {
quote! {}
} else {
let defaults = if is_handle {
Expand Down Expand Up @@ -319,7 +329,7 @@ impl Struct {
}
};

let debug = if is_union || has_union {
let debug = if is_union || has_union || has_complex_array {
quote! {}
} else {
let debug_name = self.0.name();
Expand All @@ -330,8 +340,14 @@ impl Struct {
.enumerate()
.filter_map(|(index, (_, signature, name))| {
// TODO: there must be a simpler way to implement Debug just to exclude this type.
if let ElementType::Callback(_) = signature.kind {
return None;
match &signature.kind {
ElementType::Callback(_) => return None,
ElementType::Array((kind, _)) => {
if let ElementType::Callback(_) = kind.kind {
return None;
}
}
_ => {}
}

let field = name.as_str();
Expand Down
1 change: 1 addition & 0 deletions crates/gen/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn workspace_dir() -> std::path::PathBuf {
.arg("metadata")
.arg("--format-version=1")
.arg("--no-deps")
.arg("--offline")
.output()
.expect("Failed to run `cargo metadata`");

Expand Down
36 changes: 15 additions & 21 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ pub fn build(stream: TokenStream) -> TokenStream {

let workspace_windows_dir = gen::workspace_windows_dir();

let mut source = workspace_windows_dir.clone();
source.push(ARCHITECTURE);
let source = source.to_str().expect("Invalid workspace architecture dir");

let mut destination = workspace_windows_dir.clone();
destination.pop();
destination.push("target");
Expand All @@ -67,14 +63,11 @@ pub fn build(stream: TokenStream) -> TokenStream {

path.push("windows.rs");
let mut file = ::std::fs::File::create(&path).expect("Failed to create windows.rs");
let bytes = #tokens.as_bytes();
file.write_all(bytes).expect("Could not write generated code to output file");
file.write_all(#tokens.as_bytes()).expect("Could not write generated code to output file");

if bytes.len() < 100_000_000 {
let mut cmd = ::std::process::Command::new("rustfmt");
cmd.arg(&path);
let _ = cmd.output();
}
let mut cmd = ::std::process::Command::new("rustfmt");
cmd.arg(&path);
let _ = cmd.output();

fn copy(source: &::std::path::PathBuf, destination: &mut ::std::path::PathBuf) {
if let ::std::result::Result::Ok(files) = ::std::fs::read_dir(source) {
Expand Down Expand Up @@ -114,7 +107,17 @@ pub fn build(stream: TokenStream) -> TokenStream {

if ::std::path::PathBuf::from(#workspace_windows_dir).exists() {
println!("cargo:rerun-if-changed={}", #workspace_windows_dir);
let source = ::std::path::PathBuf::from(#source);
let mut source = ::std::path::PathBuf::from(#workspace_windows_dir);

// The `target_arch` cfg is not set for build scripts so we need to sniff it out from the environment variable.
source.push(match ::std::env::var("CARGO_CFG_TARGET_ARCH").expect("No `CARGO_CFG_TARGET_ARCH` env variable set").as_str() {
"x86_64" => "x64",
"x86" => "x86",
"arm" => "arm",
"aarch64" => "arm64",
unexpected => panic!("Unexpected `{}` architecture set by `CARGO_CFG_TARGET_ARCH`", unexpected),
});

let destination = ::std::path::PathBuf::from(#destination);
let profile = ::std::env::var("PROFILE").expect("No `PROFILE` env variable set");
copy_to_profile(&source, &destination, &profile);
Expand Down Expand Up @@ -147,12 +150,3 @@ pub(crate) fn namespace_literal_to_rough_namespace(namespace: &str) -> String {
}
result
}

#[cfg(target_arch = "x86_64")]
const ARCHITECTURE: &str = "x64";
#[cfg(target_arch = "x86")]
const ARCHITECTURE: &str = "x86";
#[cfg(target_arch = "arm")]
const ARCHITECTURE: &str = "arm";
#[cfg(target_arch = "aarch64")]
const ARCHITECTURE: &str = "arm64";
38 changes: 32 additions & 6 deletions examples/clock/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ impl Window {
(*this).handle = window;

// TODO: https://github.com/microsoft/win32metadata/issues/331
SetWindowLongPtrA(window, GWLP_USERDATA, this as _);
SetWindowLongA(window, GWLP_USERDATA, this as _);
} else {
let this = GetWindowLongPtrA(window, GWLP_USERDATA) as *mut Self;
let this = GetWindowLongA(window, GWLP_USERDATA) as *mut Self;

if !this.is_null() {
return (*this).message_handler(message, wparam, lparam);
Expand Down Expand Up @@ -727,9 +727,35 @@ fn create_swapchain(device: &ID3D11Device, window: HWND) -> Result<IDXGISwapChai
}

// TODO: workaround for https://github.com/microsoft/win32metadata/issues/142
#[link(name = "user32")]
extern "system" {
fn SetWindowLongPtrA(window: HWND, index: i32, value: isize) -> isize;

fn GetWindowLongPtrA(window: HWND, index: i32) -> isize;
#[allow(non_snake_case)]
unsafe fn SetWindowLongA(window: HWND, index: i32, value: isize) -> isize {
#[link(name = "user32")]
extern "system" {
#[cfg(target_pointer_width = "32")]
#[link_name = "SetWindowLongA"]
fn SetWindowLongA(window: HWND, index: i32, value: i32) -> isize;

#[cfg(target_pointer_width = "64")]
#[link_name = "SetWindowLongPtrA"]
fn SetWindowLongA(window: HWND, index: i32, value: isize) -> isize;
}

SetWindowLongA(window, index, value as _)
}

#[allow(non_snake_case)]
unsafe fn GetWindowLongA(window: HWND, index: i32) -> isize {
#[link(name = "user32")]
extern "system" {
#[cfg(target_pointer_width = "32")]
#[link_name = "GetWindowLongA"]
fn GetWindowLongA(window: HWND, index: i32) -> i32;

#[cfg(target_pointer_width = "64")]
#[link_name = "GetWindowLongPtrA"]
fn GetWindowLongA(window: HWND, index: i32) -> isize;
}

GetWindowLongA(window, index) as _
}