Skip to content

Commit

Permalink
Autoformat build! macros with new windows_fmt crate (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Jun 7, 2021
1 parent 8e7ce3d commit 227c0f8
Show file tree
Hide file tree
Showing 39 changed files with 285 additions and 263 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ jobs:
git diff --exit-code || (echo '::error::Generated bindings are out-of-date. Make sure to update them by running `cargo run -p windows_bindings`'; exit 1)
shell: bash
if: matrix.os == 'windows-latest' && matrix.rust == 'stable'

format-macro:
runs-on: windows-latest
name: Format `build!` macros
steps:
- uses: actions/checkout@v2
- name: Invoke `windows_fmt`
run: cargo r -p windows_fmt
- name: Diff formatting result
shell: bash
run: git diff --exit-code || (echo '::error::Some `build!` macros were improperly formatted. Please run `cargo run -p windows_fmt` and push again'; exit 1)
30 changes: 14 additions & 16 deletions crates/bindings/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
use std::io::prelude::*;

fn main() -> std::io::Result<()> {
let tokens = windows_macros::generate!(
let tokens = windows_macros::generate! {
Windows::Foundation::{IReference, IStringable, PropertyValue},
Windows::Win32::System::Com::{
CoCreateGuid, CoTaskMemAlloc, CoTaskMemFree, CLSIDFromProgID, CoInitializeEx, CoCreateInstance,
IAgileObject, COINIT_MULTITHREADED, COINIT_APARTMENTTHREADED, CLSCTX_ALL,
CLSIDFromProgID, CoCreateGuid, CoCreateInstance, CoInitializeEx, CoTaskMemAlloc,
CoTaskMemFree, IAgileObject, CLSCTX_ALL, COINIT_APARTMENTTHREADED,
COINIT_MULTITHREADED,
},
Windows::Win32::System::Diagnostics::Debug::{
GetLastError, FormatMessageW, FORMAT_MESSAGE_ALLOCATE_BUFFER, FORMAT_MESSAGE_FROM_SYSTEM,
FORMAT_MESSAGE_IGNORE_INSERTS,
FormatMessageW, GetLastError, FORMAT_MESSAGE_ALLOCATE_BUFFER,
FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS,
},
Windows::Win32::System::Memory::{
GetProcessHeap, HeapAlloc, HeapFree, HEAP_NONE,
},
Windows::Win32::System::OleAutomation::{BSTR, GetErrorInfo, IErrorInfo, SetErrorInfo},
Windows::Win32::System::Memory::{GetProcessHeap, HeapAlloc, HeapFree, HEAP_NONE},
Windows::Win32::System::OleAutomation::{GetErrorInfo, IErrorInfo, SetErrorInfo, BSTR},
Windows::Win32::System::SystemServices::{
GetProcAddress, LoadLibraryA, FreeLibrary, CO_E_NOTINITIALIZED, E_POINTER,
},
Windows::Win32::System::Threading::{
CreateEventA, SetEvent, WaitForSingleObject,
FreeLibrary, GetProcAddress, LoadLibraryA, CO_E_NOTINITIALIZED, E_POINTER,
},
Windows::Win32::System::WindowsProgramming::CloseHandle,
Windows::Win32::System::Threading::{CreateEventA, SetEvent, WaitForSingleObject},
Windows::Win32::System::WinRT::{
IRestrictedErrorInfo, ILanguageExceptionErrorInfo2, IWeakReference, IWeakReferenceSource,
ILanguageExceptionErrorInfo2, IRestrictedErrorInfo, IWeakReference,
IWeakReferenceSource,
},
);
Windows::Win32::System::WindowsProgramming::CloseHandle,
};

let mut path = windows_gen::workspace_dir();
path.push("src");
Expand Down
7 changes: 7 additions & 0 deletions crates/fmt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "windows_fmt"
version = "0.0.0"
edition = "2018"

[dependencies]
windows_gen = { path = "../gen" }
60 changes: 60 additions & 0 deletions crates/fmt/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::ffi::OsStr;
use std::io::Result;
use std::io::Write;
use std::path::*;
use std::process::Command;
use std::process::Stdio;

fn format_file(file: &Path, pattern: &str) -> Result<()> {
const SHIM: &str = "use format_build_macro::{";

let contents = std::fs::read(file)?;
let contents = String::from_utf8(contents).expect("Failed to parse UTF-8");

// Replace macro call with our `use` pattern
let contents = contents.replace(pattern, SHIM);

// Spawn `rustfmt` and pipe string through it, instead of writing it to disk
let mut child = Command::new("rustfmt")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("Failed to spawn `rustfmt`");
let mut stdin = child.stdin.take().expect("Failed to open stdin");
stdin.write_all(contents.as_bytes())?;
drop(stdin);

// Read formatted output from `rustfmt`
let output = child.wait_with_output().expect("Failed to read stdout");
let contents = String::from_utf8(output.stdout).expect("Failed to parse UTF-8");

// Some build macros contain a single item, where curly braces are removed. Those
// cannot easily be formatted yet.
if contents.contains(SHIM) {
std::fs::write(file, contents.replace(SHIM, pattern))?;
}

Ok(())
}

fn walk_path(path: &Path) -> Result<()> {
if path.is_dir() {
for entry in path.read_dir()? {
walk_path(&entry?.path())?;
}
} else if path.file_name() == Some(OsStr::new("build.rs")) {
format_file(path, "windows::build! {")?;
}

Ok(())
}

fn main() -> Result<()> {
let dir = windows_gen::workspace_dir();
walk_path(&dir)?;

format_file(
&dir.join("crates/bindings/src/main.rs"),
"let tokens = windows_macros::generate! {",
)
}
4 changes: 2 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ In the build.rs file add the following:

```rust
fn main() {
windows::build!(
windows::build! {
// Note that we're using the `Intl` namespace which is nested inside the `Win32` namespace
// which itself is inside the `Windows` namespace.
Windows::Win32::Globalization::{ISpellChecker, SpellCheckerFactory, ISpellCheckerFactory, CORRECTIVE_ACTION, IEnumSpellingError, ISpellingError},
Windows::Win32::System::SystemServices::{BOOL, PWSTR, S_FALSE},
Windows::Win32::System::Com::IEnumString
)
};
}
```

Expand Down
6 changes: 3 additions & 3 deletions examples/buffer/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
windows::build!(
Windows::Foundation::{MemoryBuffer, IMemoryBufferReference},
windows::build! {
Windows::Foundation::{IMemoryBufferReference, MemoryBuffer},
Windows::Win32::System::WinRT::IMemoryBufferByteAccess,
);
};
}
45 changes: 23 additions & 22 deletions examples/clock/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
fn main() {
windows::build!(
windows::build! {
Windows::Foundation::Numerics::Matrix3x2,
Windows::Win32::Graphics::Direct2D::{
CLSID_D2D1Shadow, D2D1CreateFactory, ID2D1Bitmap1, ID2D1Device, ID2D1DeviceContext,
ID2D1Effect, ID2D1Factory1, ID2D1SolidColorBrush, ID2D1StrokeStyle, D2D1_ALPHA_MODE_IGNORE,
D2D1_ALPHA_MODE_PREMULTIPLIED, D2D1_BITMAP_OPTIONS_CANNOT_DRAW, D2D1_BITMAP_OPTIONS_TARGET,
D2D1_BITMAP_PROPERTIES1, D2D1_BRUSH_PROPERTIES, D2D1_CAP_STYLE_ROUND,
D2D1_CAP_STYLE_TRIANGLE, D2D1_COLOR_F, D2D1_COMPOSITE_MODE_SOURCE_OVER,
D2D1_DEBUG_LEVEL_INFORMATION, D2D1_DEVICE_CONTEXT_OPTIONS_NONE, D2D1_ELLIPSE,
D2D1_FACTORY_OPTIONS, D2D1_FACTORY_TYPE_SINGLE_THREADED, D2D1_INTERPOLATION_MODE_LINEAR,
D2D1_PIXEL_FORMAT, D2D1_STROKE_STYLE_PROPERTIES, D2D1_UNIT_MODE_DIPS, D2D_POINT_2F,
D2D_RECT_F, D2D_SIZE_F, D2D_SIZE_U,
ID2D1Effect, ID2D1Factory1, ID2D1SolidColorBrush, ID2D1StrokeStyle,
D2D1_ALPHA_MODE_IGNORE, D2D1_ALPHA_MODE_PREMULTIPLIED, D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
D2D1_BITMAP_OPTIONS_TARGET, D2D1_BITMAP_PROPERTIES1, D2D1_BRUSH_PROPERTIES,
D2D1_CAP_STYLE_ROUND, D2D1_CAP_STYLE_TRIANGLE, D2D1_COLOR_F,
D2D1_COMPOSITE_MODE_SOURCE_OVER, D2D1_DEBUG_LEVEL_INFORMATION,
D2D1_DEVICE_CONTEXT_OPTIONS_NONE, D2D1_ELLIPSE, D2D1_FACTORY_OPTIONS,
D2D1_FACTORY_TYPE_SINGLE_THREADED, D2D1_INTERPOLATION_MODE_LINEAR, D2D1_PIXEL_FORMAT,
D2D1_STROKE_STYLE_PROPERTIES, D2D1_UNIT_MODE_DIPS, D2D_POINT_2F, D2D_RECT_F,
D2D_SIZE_F, D2D_SIZE_U,
},
Windows::Win32::Graphics::Direct3D11::{
D3D11CreateDevice, ID3D11Device, D3D11_CREATE_DEVICE_BGRA_SUPPORT,
D3D11_CREATE_DEVICE_DEBUG, D3D11_SDK_VERSION, D3D_DRIVER_TYPE, D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D11_CREATE_DEVICE_DEBUG, D3D11_SDK_VERSION, D3D_DRIVER_TYPE,
D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP,
},
Windows::Win32::Graphics::Dxgi::{
CreateDXGIFactory1, IDXGIDevice, IDXGIFactory2, IDXGIFactory7, IDXGIOutput, IDXGISurface,
IDXGISwapChain1, DXGI_ERROR_UNSUPPORTED, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_UNKNOWN,
DXGI_PRESENT_TEST, DXGI_SAMPLE_DESC, DXGI_SWAP_CHAIN_DESC1,
CreateDXGIFactory1, IDXGIDevice, IDXGIFactory2, IDXGIFactory7, IDXGIOutput,
IDXGISurface, IDXGISwapChain1, DXGI_ERROR_UNSUPPORTED, DXGI_FORMAT_B8G8R8A8_UNORM,
DXGI_FORMAT_UNKNOWN, DXGI_PRESENT_TEST, DXGI_SAMPLE_DESC, DXGI_SWAP_CHAIN_DESC1,
DXGI_SWAP_CHAIN_FULLSCREEN_DESC, DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL,
DXGI_USAGE_RENDER_TARGET_OUTPUT,
},
Expand All @@ -37,13 +38,13 @@ fn main() {
UI_ANIMATION_UPDATE_RESULT,
},
Windows::Win32::UI::WindowsAndMessaging::{
CreateWindowExA, DefWindowProcA, DispatchMessageA, GetMessageA, GetWindowLongPtrA,
LoadCursorW, PeekMessageA, PostQuitMessage, RegisterClassA, SetWindowLongPtrA,
SetWindowLongA, GetWindowLongA,
CREATESTRUCTA, CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, GWLP_USERDATA, HWND, IDC_HAND,
LPARAM, MSG, PM_REMOVE, SIZE_MINIMIZED, WINDOW_LONG_PTR_INDEX, WM_ACTIVATE, WM_DESTROY,
WM_DISPLAYCHANGE, WM_NCCREATE, WM_PAINT, WM_QUIT, WM_SIZE, WM_USER, WNDCLASSA, WPARAM,
WS_OVERLAPPEDWINDOW, WS_VISIBLE,
CreateWindowExA, DefWindowProcA, DispatchMessageA, GetMessageA, GetWindowLongA,
GetWindowLongPtrA, LoadCursorW, PeekMessageA, PostQuitMessage, RegisterClassA,
SetWindowLongA, SetWindowLongPtrA, CREATESTRUCTA, CS_HREDRAW, CS_VREDRAW,
CW_USEDEFAULT, GWLP_USERDATA, HWND, IDC_HAND, LPARAM, MSG, PM_REMOVE, SIZE_MINIMIZED,
WINDOW_LONG_PTR_INDEX, WM_ACTIVATE, WM_DESTROY, WM_DISPLAYCHANGE, WM_NCCREATE,
WM_PAINT, WM_QUIT, WM_SIZE, WM_USER, WNDCLASSA, WPARAM, WS_OVERLAPPEDWINDOW,
WS_VISIBLE,
},
);
};
}
7 changes: 3 additions & 4 deletions examples/com_uri/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
fn main() {
windows::build!(
Windows::Win32::System::OleAutomation::BSTR,
Windows::Win32::System::Com::CreateUri,
);
windows::build! {
Windows::Win32::System::Com::CreateUri, Windows::Win32::System::OleAutomation::BSTR,
};
}
5 changes: 1 addition & 4 deletions examples/core_app/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
fn main() {
windows::build!(
Windows::ApplicationModel::Core::*,
Windows::UI::Core::*,
);
windows::build! {Windows::ApplicationModel::Core::*, Windows::UI::Core::*};
}
68 changes: 15 additions & 53 deletions examples/d3d12/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,22 @@
fn main() {
windows::build!(
Windows::Win32::Graphics::Direct3D12::*,
Windows::Win32::Graphics::Dxgi::*,
windows::build! {
Windows::Win32::Graphics::Direct3D11::{
D3D_FEATURE_LEVEL_11_0,
D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST
D3D_FEATURE_LEVEL_11_0, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST,
},
Windows::Win32::Graphics::Direct3D12::*,
Windows::Win32::Graphics::Dxgi::*,
Windows::Win32::Graphics::Hlsl::*,
Windows::Win32::System::SystemServices::{GetModuleHandleA, HINSTANCE, PSTR},
Windows::Win32::System::Threading::{CreateEventA, WaitForSingleObject},
Windows::Win32::System::WindowsProgramming::INFINITE,
Windows::Win32::UI::DisplayDevices::RECT,
Windows::Win32::UI::WindowsAndMessaging::{
AdjustWindowRect,
CREATESTRUCTA,
CreateWindowExA,
CS_HREDRAW,
CS_VREDRAW,
CW_USEDEFAULT,
DefWindowProcA,
DispatchMessageA,
GetWindowLongA,
GetWindowLongPtrA,
GWLP_USERDATA,
IDC_ARROW,
LoadCursorW,
MSG,
PeekMessageA,
PM_REMOVE,
PostQuitMessage,
RegisterClassExA,
SetWindowLongA,
SetWindowLongPtrA,
ShowWindow,
SW_SHOW,
TranslateMessage,
WM_CREATE,
WM_DESTROY,
WM_KEYDOWN,
WM_KEYUP,
WM_PAINT,
WM_QUIT,
WNDCLASS_STYLES,
WNDCLASSEXA,
WS_OVERLAPPEDWINDOW,
},
Windows::Win32::System::SystemServices::{
GetModuleHandleA,
HINSTANCE,
PSTR,
},
Windows::Win32::System::Threading::{
CreateEventA,
WaitForSingleObject
},
Windows::Win32::System::WindowsProgramming::{
INFINITE
},
Windows::Win32::UI::DisplayDevices::{
RECT
AdjustWindowRect, CreateWindowExA, DefWindowProcA, DispatchMessageA, GetWindowLongA,
GetWindowLongPtrA, LoadCursorW, PeekMessageA, PostQuitMessage, RegisterClassExA,
SetWindowLongA, SetWindowLongPtrA, ShowWindow, TranslateMessage, CREATESTRUCTA,
CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, GWLP_USERDATA, IDC_ARROW, MSG, PM_REMOVE,
SW_SHOW, WM_CREATE, WM_DESTROY, WM_KEYDOWN, WM_KEYUP, WM_PAINT, WM_QUIT, WNDCLASSEXA,
WNDCLASS_STYLES, WS_OVERLAPPEDWINDOW,
},
);
};
}
4 changes: 2 additions & 2 deletions examples/enum_windows/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
windows::build!(
windows::build! {
Windows::Win32::UI::WindowsAndMessaging::{EnumWindows, GetWindowTextW}
);
};
}
6 changes: 3 additions & 3 deletions examples/event/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fn main() {
windows::build!(
windows::build! {
Windows::Win32::System::Threading::{
CreateEventW, SetEvent, WaitForSingleObject, WAIT_OBJECT_0,
},
Windows::Win32::System::WindowsProgramming::CloseHandle
);
Windows::Win32::System::WindowsProgramming::CloseHandle,
};
}
4 changes: 2 additions & 2 deletions examples/message_box/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
windows::build!(
windows::build! {
Windows::Win32::UI::WindowsAndMessaging::{MessageBoxA, MB_OK}
);
};
}
10 changes: 5 additions & 5 deletions examples/ocr/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
fn main() {
windows::build!(
Windows::Graphics::Imaging::{SoftwareBitmap, BitmapDecoder},
Windows::Media::Ocr::{OcrEngine, OcrResult},
Windows::Storage::{FileAccessMode, StorageFile},
windows::build! {
Windows::Foundation::IAsyncOperation,
Windows::Graphics::Imaging::{BitmapDecoder, SoftwareBitmap},
Windows::Media::Ocr::{OcrEngine, OcrResult},
Windows::Storage::Streams::IRandomAccessStream,
);
Windows::Storage::{FileAccessMode, StorageFile},
};
}
22 changes: 10 additions & 12 deletions examples/overlapped/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
fn main() {
windows::build!(
Windows::Win32::Storage::FileSystem::{
CreateFileA, ReadFile, FILE_GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
},
Windows::Win32::System::SystemServices::GetOverlappedResult,
Windows::Win32::System::Threading::{
CreateEventA, WaitForSingleObject, WAIT_OBJECT_0,
},
Windows::Win32::System::Diagnostics::Debug::{GetLastError, ERROR_IO_PENDING},
Windows::Win32::System::WindowsProgramming::CloseHandle,
);
windows::build! {
Windows::Win32::Storage::FileSystem::{
CreateFileA, ReadFile, FILE_FLAG_OVERLAPPED, FILE_GENERIC_READ, FILE_SHARE_READ,
OPEN_EXISTING,
},
Windows::Win32::System::Diagnostics::Debug::{GetLastError, ERROR_IO_PENDING},
Windows::Win32::System::SystemServices::GetOverlappedResult,
Windows::Win32::System::Threading::{CreateEventA, WaitForSingleObject, WAIT_OBJECT_0},
Windows::Win32::System::WindowsProgramming::CloseHandle,
};
}
8 changes: 4 additions & 4 deletions examples/rss/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
fn main() {
windows::build!(
windows::build! {
Windows::Foundation::Collections::IVector,
Windows::Foundation::{IAsyncOperationWithProgress, Uri},

Windows::Web::Syndication::{
ISyndicationText, RetrievalProgress, SyndicationClient, SyndicationFeed, SyndicationItem,
ISyndicationText, RetrievalProgress, SyndicationClient, SyndicationFeed,
SyndicationItem,
},
);
};
}
Loading

0 comments on commit 227c0f8

Please sign in to comment.