Skip to content

Add CsWin32PlatformTarget to generate arch-specific APIs in AnyCPU projects - #1835

Merged
Jevan Saks (jevansaks) merged 3 commits into
mainfrom
copilot/specify-api-architecture-settings
Oct 1, 2026
Merged

Jevan Saks (jevansaks) merged 3 commits into
mainfrom
copilot/specify-api-architecture-settings

Conversation

Copilot AI commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

AnyCPU projects such as WinForms cannot generate arch-specific Win32 APIs (Pack=1 structs, Get/SetWindowLongPtr, …), even when they know one architecture's definitions are fine for their use. This PR adds an MSBuild property that overrides the architecture used to select those APIs. The compiled assembly stays AnyCPU.

<PropertyGroup>
  <PlatformTarget>AnyCPU</PlatformTarget>
  <CsWin32PlatformTarget>x64</CsWin32PlatformTarget>
</PropertyGroup>

Changes

  • Source generator
    • Reads build_property.CsWin32PlatformTarget and generates with that platform by passing the compilation to the generator via WithOptions(...WithPlatform(...)). No public API change.
    • Accepted values, case-insensitive: AnyCPU, x86, x64, arm64. Any other value raises the new error PInvoke014 and nothing is generated.
  • PInvoke005: warning → error ⚠️ breaking
    • The message now suggests setting PlatformTarget or <CsWin32PlatformTarget>.
    • AnyCPU projects that build today with only this warning will now fail to build.
    • Module.* wildcard requests still silently skip arch-specific APIs.
  • Build-task mode (CsWin32RunAsBuildTask)
    • The targets file passes $(CsWin32PlatformTarget) to the generator, falling back to $(PlatformTarget).
    • Unrecognized values fail the build with PInvoke014, because the CLI would otherwise silently fall back to AnyCPU.
  • CLI (CsWin32Generator)
    • The platform-incompatible handler now also catches wrapped PlatformIncompatibleExceptions, e.g. when the requested API depends on an arch-specific type.
    • Its error now includes the underlying reason and the same suggestion as PInvoke005.
  • Plumbing / docs
    • CsWin32PlatformTarget added as a CompilerVisibleProperty.
    • AnalyzerReleases.Unshipped.md updated: PInvoke005 is now Error, and PInvoke014 is added.
    • ArchSpecificAPIs.md has a new section on the property, with a warning that the user is responsible for layout correctness on other architectures.

Reviewer notes

  • In build-task mode, changing only CsWin32PlatformTarget does not invalidate GenerateCsWin32Code's incremental Inputs/Outputs, so the generator won't re-run. PlatformTarget already behaves the same way, so this PR leaves it unaddressed.

Copilot AI and others added 2 commits October 1, 2026 17:25
Co-authored-by: jevansaks <20667023+jevansaks@users.noreply.github.com>
Co-authored-by: jevansaks <20667023+jevansaks@users.noreply.github.com>
Copilot AI changed the title [WIP] Add capability to specify API architecture in settings json Add CsWin32PlatformTarget to generate arch-specific APIs in AnyCPU projects Oct 1, 2026
@jevansaks
Jevan Saks (jevansaks) marked this pull request as ready for review October 1, 2026 18:13
@jevansaks
Jevan Saks (jevansaks) merged commit 54e0a83 into main Oct 1, 2026
20 checks passed
@jevansaks
Jevan Saks (jevansaks) deleted the copilot/specify-api-architecture-settings branch October 1, 2026 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider specifying a given architecture for APIs when targetting AnyCPU

3 participants