Skip to content

Commit

Permalink
Allow windows, windows-core, and others to be compiled and used i…
Browse files Browse the repository at this point in the history
…n `no_std` environments. (#3047)
  • Loading branch information
sivadeilra authored May 20, 2024
1 parent 78a4034 commit 1172cf2
Show file tree
Hide file tree
Showing 747 changed files with 73,932 additions and 73,819 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
run: rustup component add clippy
- name: Fix environment
uses: ./.github/actions/fix-environment
- name: Clippy no_std
run: cargo clippy -p no_std
- name: Clippy riddle
run: cargo clippy -p riddle
- name: Clippy sample_bits
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ jobs:
uses: ./.github/actions/fix-environment
- name: Clean
run: cargo clean
- name: Test no_std
run: cargo test -p no_std --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test riddle
run: cargo test -p riddle --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test sample_bits
Expand Down Expand Up @@ -154,10 +156,10 @@ jobs:
run: cargo test -p test_array --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_bcrypt
run: cargo test -p test_bcrypt --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_bstr
run: cargo test -p test_bstr --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Clean
run: cargo clean
- name: Test test_bstr
run: cargo test -p test_bstr --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_calling_convention
run: cargo test -p test_calling_convention --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_cfg_generic
Expand Down Expand Up @@ -256,10 +258,10 @@ jobs:
run: cargo test -p test_sys --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_targets
run: cargo test -p test_targets --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_unions
run: cargo test -p test_unions --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Clean
run: cargo clean
- name: Test test_unions
run: cargo test -p test_unions --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_variant
run: cargo test -p test_variant --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_wdk
Expand Down
10 changes: 5 additions & 5 deletions crates/libs/bindgen/src/rust/com_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef, kind: metadata::Interface
quote! {
#features
pub unsafe fn #name<#generics>(&self, #params) -> windows_core::Result<T> #where_clause {
let mut result__ = std::ptr::null_mut();
let mut result__ = core::ptr::null_mut();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args).and_then(||windows_core::Type::from_abi(result__))
}
}
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef, kind: metadata::Interface
quote! {
#features
pub unsafe fn #name<#generics>(&self, #params) -> windows_core::Result<#return_type> #where_clause {
let mut result__ = std::mem::zeroed();
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args).#map
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef, kind: metadata::Interface
quote! {
#features
pub unsafe fn #name<#generics>(&self, #params) -> windows_core::Result<#return_type> #where_clause {
let mut result__ = std::mem::zeroed();
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args);
windows_core::Type::from_abi(result__)
}
Expand All @@ -102,15 +102,15 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef, kind: metadata::Interface
let map = if metadata::type_is_blittable(&return_type) {
quote! { result__ }
} else {
quote! { std::mem::transmute(result__) }
quote! { core::mem::transmute(result__) }
};

let return_type = writer.type_name(&return_type);

quote! {
#features
pub unsafe fn #name<#generics>(&self, #params) -> #return_type #where_clause {
let mut result__ = std::mem::zeroed();
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args);
#map
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ impl From<IDispatch> for windows_core::VARIANT {
unsafe {
Self::from_raw(windows_core::imp::VARIANT {
Anonymous: windows_core::imp::VARIANT_0 {
Anonymous: windows_core::imp::VARIANT_0_0 { vt: 9, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: windows_core::imp::VARIANT_0_0_0 { pdispVal: std::mem::transmute(value) } },
Anonymous: windows_core::imp::VARIANT_0_0 { vt: 9, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: windows_core::imp::VARIANT_0_0_0 { pdispVal: core::mem::transmute(value) } },
},
})
}
Expand All @@ -15,7 +15,7 @@ impl From<IDispatch> for windows_core::PROPVARIANT {
unsafe {
Self::from_raw(windows_core::imp::PROPVARIANT {
Anonymous: windows_core::imp::PROPVARIANT_0 {
Anonymous: windows_core::imp::PROPVARIANT_0_0 { vt: 9, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: windows_core::imp::PROPVARIANT_0_0_0 { pdispVal: std::mem::transmute(value) } },
Anonymous: windows_core::imp::PROPVARIANT_0_0 { vt: 9, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: windows_core::imp::PROPVARIANT_0_0_0 { pdispVal: core::mem::transmute(value) } },
},
})
}
Expand All @@ -28,7 +28,7 @@ impl TryFrom<&windows_core::VARIANT> for IDispatch {
let from = from.as_raw();
unsafe {
if from.Anonymous.Anonymous.vt == 9 && !from.Anonymous.Anonymous.Anonymous.pdispVal.is_null() {
let dispatch: &IDispatch = std::mem::transmute(&from.Anonymous.Anonymous.Anonymous.pdispVal);
let dispatch: &IDispatch = core::mem::transmute(&from.Anonymous.Anonymous.Anonymous.pdispVal);
Ok(dispatch.clone())
} else {
Err(windows_core::Error::from_hresult(windows_core::imp::TYPE_E_TYPEMISMATCH))
Expand All @@ -43,7 +43,7 @@ impl TryFrom<&windows_core::PROPVARIANT> for IDispatch {
let from = from.as_raw();
unsafe {
if from.Anonymous.Anonymous.vt == 9 && !from.Anonymous.Anonymous.Anonymous.pdispVal.is_null() {
let dispatch: &IDispatch = std::mem::transmute(&from.Anonymous.Anonymous.Anonymous.pdispVal);
let dispatch: &IDispatch = core::mem::transmute(&from.Anonymous.Anonymous.Anonymous.pdispVal);
Ok(dispatch.clone())
} else {
Err(windows_core::Error::from_hresult(windows_core::imp::TYPE_E_TYPEMISMATCH))
Expand Down
10 changes: 5 additions & 5 deletions crates/libs/bindgen/src/rust/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn gen_win_function(writer: &Writer, namespace: &str, def: metadata::MethodDef)
#[inline]
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<T> #where_clause {
#link
let mut result__ = std::ptr::null_mut();
let mut result__ = core::ptr::null_mut();
#name(#args).and_then(||windows_core::Type::from_abi(result__))
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ fn gen_win_function(writer: &Writer, namespace: &str, def: metadata::MethodDef)
#[inline]
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<#return_type> #where_clause {
#link
let mut result__ = std::mem::zeroed();
let mut result__ = core::mem::zeroed();
#name(#args).#map
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ fn gen_win_function(writer: &Writer, namespace: &str, def: metadata::MethodDef)
#[inline]
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<#return_type> #where_clause {
#link
let mut result__ = std::mem::zeroed();
let mut result__ = core::mem::zeroed();
#name(#args);
windows_core::Type::from_abi(result__.assume_init())
}
Expand All @@ -136,7 +136,7 @@ fn gen_win_function(writer: &Writer, namespace: &str, def: metadata::MethodDef)
let map = if metadata::type_is_blittable(&return_type) {
quote! { result__ }
} else {
quote! { std::mem::transmute(result__) }
quote! { core::mem::transmute(result__) }
};

let return_type = writer.type_name(&return_type);
Expand All @@ -146,7 +146,7 @@ fn gen_win_function(writer: &Writer, namespace: &str, def: metadata::MethodDef)
#[inline]
pub unsafe fn #name<#generics>(#params) -> #return_type #where_clause {
#link
let mut result__ = std::mem::zeroed();
let mut result__ = core::mem::zeroed();
#name(#args);
#map
}
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/rust/implements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef) -> TokenStream {
impl #type_ident {
pub fn new<'a, T: #impl_ident>(this: &'a T) -> windows_core::ScopedInterface<'a, Self> {
let this = windows_core::ScopedHeap { vtable: &#implvtbl_ident::<T>::VTABLE as *const _ as *const _, this: this as *const _ as *const _ };
let this = std::mem::ManuallyDrop::new(Box::new(this));
unsafe { windows_core::ScopedInterface::new(std::mem::transmute(&this.vtable)) }
let this = core::mem::ManuallyDrop::new(Box::new(this));
unsafe { windows_core::ScopedInterface::new(core::mem::transmute(&this.vtable)) }
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/rust/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ fn gen_win_interface(writer: &Writer, def: metadata::TypeDef) -> TokenStream {

tokens.combine(&quote! {
#features
impl<#constraints> std::ops::Deref for #ident {
impl<#constraints> core::ops::Deref for #ident {
type Target = #base;
fn deref(&self) -> &Self::Target {
unsafe { std::mem::transmute(self) }
unsafe { core::mem::transmute(self) }
}
}
});
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/bindgen/src/rust/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ fn gen_struct_with_name(writer: &Writer, def: metadata::TypeDef, struct_name: &s
quote! {}
} else if !writer.sys && flags.contains(metadata::TypeAttributes::ExplicitLayout) && !metadata::field_is_copyable(f, def) {
let ty = writer.type_default_name(&ty);
quote! { pub #name: std::mem::ManuallyDrop<#ty>, }
quote! { pub #name: core::mem::ManuallyDrop<#ty>, }
} else if !writer.sys && !flags.contains(metadata::TypeAttributes::WindowsRuntime) && !metadata::field_is_blittable(f, def) {
if let metadata::Type::Win32Array(ty, len) = ty {
let ty = writer.type_default_name(&ty);
quote! { pub #name: [std::mem::ManuallyDrop<#ty>; #len], }
quote! { pub #name: [core::mem::ManuallyDrop<#ty>; #len], }
} else {
let ty = writer.type_default_name(&ty);
quote! { pub #name: std::mem::ManuallyDrop<#ty>, }
quote! { pub #name: core::mem::ManuallyDrop<#ty>, }
}
} else {
let ty = writer.type_default_name(&ty);
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/rust/winrt_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef, generic_types: &[metadata
_ => {
if signature.return_type.is_winrt_array() {
let return_type = writer.type_name(&signature.return_type);
quote! { windows_core::Array::<#return_type>::set_abi_len(std::mem::transmute(&mut result__)), result__.as_mut_ptr() as *mut _ as _ }
quote! { windows_core::Array::<#return_type>::set_abi_len(core::mem::transmute(&mut result__)), result__.as_mut_ptr() as *mut _ as _ }
} else {
quote! { &mut result__ }
}
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef, generic_types: &[metadata
};

quote! {
let mut result__ = std::mem::zeroed();
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).#vname)(windows_core::Interface::as_raw(this), #args #return_arg)
.#map
}
Expand Down
10 changes: 5 additions & 5 deletions crates/libs/bindgen/src/rust/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,16 @@ impl Writer {
quote! { *mut core::ffi::c_void }
}
metadata::Type::String => {
quote! { std::mem::MaybeUninit<windows_core::HSTRING> }
quote! { core::mem::MaybeUninit<windows_core::HSTRING> }
}
metadata::Type::Name(metadata::TypeName::BSTR) => {
quote! { std::mem::MaybeUninit<windows_core::BSTR> }
quote! { core::mem::MaybeUninit<windows_core::BSTR> }
}
metadata::Type::Name(metadata::TypeName::VARIANT) => {
quote! { std::mem::MaybeUninit<windows_core::VARIANT> }
quote! { core::mem::MaybeUninit<windows_core::VARIANT> }
}
metadata::Type::Name(metadata::TypeName::PROPVARIANT) => {
quote! { std::mem::MaybeUninit<windows_core::PROPVARIANT> }
quote! { core::mem::MaybeUninit<windows_core::PROPVARIANT> }
}
metadata::Type::Win32Array(kind, len) => {
let name = self.type_abi_name(kind);
Expand All @@ -273,7 +273,7 @@ impl Writer {
if metadata::type_def_is_blittable(*def) {
tokens
} else {
quote! { std::mem::MaybeUninit<#tokens> }
quote! { core::mem::MaybeUninit<#tokens> }
}
}
metadata::TypeKind::Delegate => {
Expand Down
26 changes: 13 additions & 13 deletions crates/libs/bindgen/src/winmd/writer/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ pub fn write(mut tables: Vec<u8>, mut strings: Vec<u8>, mut blobs: Vec<u8>) -> V
let mut guids = vec![0; 16]; // zero guid
let size_of_streams = tables.len() + guids.len() + strings.len() + blobs.len();

let mut dos: metadata::IMAGE_DOS_HEADER = std::mem::zeroed();
let mut dos: metadata::IMAGE_DOS_HEADER = core::mem::zeroed();
dos.e_magic = metadata::IMAGE_DOS_SIGNATURE;
dos.e_lfarlc = 64;
dos.e_lfanew = std::mem::size_of::<metadata::IMAGE_DOS_HEADER>() as i32;
dos.e_lfanew = core::mem::size_of::<metadata::IMAGE_DOS_HEADER>() as i32;

let mut file: metadata::IMAGE_FILE_HEADER = std::mem::zeroed();
let mut file: metadata::IMAGE_FILE_HEADER = core::mem::zeroed();
file.Machine = metadata::IMAGE_FILE_MACHINE_I386;
file.NumberOfSections = 1;
file.SizeOfOptionalHeader = std::mem::size_of::<metadata::IMAGE_OPTIONAL_HEADER32>() as u16;
file.SizeOfOptionalHeader = core::mem::size_of::<metadata::IMAGE_OPTIONAL_HEADER32>() as u16;
file.Characteristics = metadata::IMAGE_FILE_DLL | metadata::IMAGE_FILE_32BIT_MACHINE | metadata::IMAGE_FILE_EXECUTABLE_IMAGE;

let mut optional: metadata::IMAGE_OPTIONAL_HEADER32 = std::mem::zeroed();
let mut optional: metadata::IMAGE_OPTIONAL_HEADER32 = core::mem::zeroed();
optional.Magic = metadata::IMAGE_NT_OPTIONAL_HDR32_MAGIC;
optional.MajorLinkerVersion = 11;
optional.SizeOfInitializedData = 1024;
Expand All @@ -39,13 +39,13 @@ pub fn write(mut tables: Vec<u8>, mut strings: Vec<u8>, mut blobs: Vec<u8>) -> V
optional.LoaderFlags = 0x100000;
optional.NumberOfRvaAndSizes = 16;

let mut section: metadata::IMAGE_SECTION_HEADER = std::mem::zeroed();
let mut section: metadata::IMAGE_SECTION_HEADER = core::mem::zeroed();
section.Name = *b".text\0\0\0";
section.Characteristics = 0x4000_0020;
section.VirtualAddress = SECTION_ALIGNMENT;

let mut clr: metadata::IMAGE_COR20_HEADER = std::mem::zeroed();
clr.cb = std::mem::size_of::<metadata::IMAGE_COR20_HEADER>() as u32;
let mut clr: metadata::IMAGE_COR20_HEADER = core::mem::zeroed();
clr.cb = core::mem::size_of::<metadata::IMAGE_COR20_HEADER>() as u32;
clr.MajorRuntimeVersion = 2;
clr.MinorRuntimeVersion = 5;
clr.Flags = 1;
Expand All @@ -65,17 +65,17 @@ pub fn write(mut tables: Vec<u8>, mut strings: Vec<u8>, mut blobs: Vec<u8>) -> V
type GuidsHeader = StreamHeader<8>;
type BlobsHeader = StreamHeader<8>;

let size_of_stream_headers = std::mem::size_of::<TablesHeader>() + std::mem::size_of::<StringsHeader>() + std::mem::size_of::<GuidsHeader>() + std::mem::size_of::<BlobsHeader>();
let size_of_image = optional.FileAlignment as usize + std::mem::size_of::<metadata::IMAGE_COR20_HEADER>() + std::mem::size_of::<metadata::METADATA_HEADER>() + size_of_stream_headers + size_of_streams;
let size_of_stream_headers = core::mem::size_of::<TablesHeader>() + core::mem::size_of::<StringsHeader>() + core::mem::size_of::<GuidsHeader>() + core::mem::size_of::<BlobsHeader>();
let size_of_image = optional.FileAlignment as usize + core::mem::size_of::<metadata::IMAGE_COR20_HEADER>() + core::mem::size_of::<metadata::METADATA_HEADER>() + size_of_stream_headers + size_of_streams;

optional.SizeOfImage = round(size_of_image, optional.SectionAlignment as usize) as u32;
section.Misc.VirtualSize = size_of_image as u32 - optional.FileAlignment;
section.SizeOfRawData = round(section.Misc.VirtualSize as usize, optional.FileAlignment as usize) as u32;

optional.DataDirectory[14] = metadata::IMAGE_DATA_DIRECTORY { VirtualAddress: SECTION_ALIGNMENT, Size: std::mem::size_of::<metadata::IMAGE_COR20_HEADER>() as u32 };
optional.DataDirectory[14] = metadata::IMAGE_DATA_DIRECTORY { VirtualAddress: SECTION_ALIGNMENT, Size: core::mem::size_of::<metadata::IMAGE_COR20_HEADER>() as u32 };
section.PointerToRawData = optional.FileAlignment;
clr.MetaData.VirtualAddress = SECTION_ALIGNMENT + std::mem::size_of::<metadata::IMAGE_COR20_HEADER>() as u32;
clr.MetaData.Size = section.Misc.VirtualSize - std::mem::size_of::<metadata::IMAGE_COR20_HEADER>() as u32;
clr.MetaData.VirtualAddress = SECTION_ALIGNMENT + core::mem::size_of::<metadata::IMAGE_COR20_HEADER>() as u32;
clr.MetaData.Size = section.Misc.VirtualSize - core::mem::size_of::<metadata::IMAGE_COR20_HEADER>() as u32;

let mut buffer = Vec::<u8>::new();

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/winmd/writer/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait Write {

impl Write for Vec<u8> {
unsafe fn write_header<T: Sized>(&mut self, value: &T) {
self.extend_from_slice(std::slice::from_raw_parts(value as *const _ as _, std::mem::size_of::<T>()));
self.extend_from_slice(std::slice::from_raw_parts(value as *const _ as _, core::mem::size_of::<T>()));
}

fn write_u8(&mut self, value: u8) {
Expand Down
4 changes: 4 additions & 0 deletions crates/libs/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ windows-interface = { path = "../interface", version = "0.56.0" }
[dev-dependencies.windows-bindgen]
version = "0.56.0"
path = "../bindgen"

[features]
default = ["std"]
std = []
Loading

0 comments on commit 1172cf2

Please sign in to comment.