Skip to content

Add --with-conditional to wrap single-file output in a preprocessor conditional#772

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-with-conditional
Jul 14, 2026
Merged

Add --with-conditional to wrap single-file output in a preprocessor conditional#772
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-with-conditional

Conversation

@tannergooding

Copy link
Copy Markdown
Member

There's no way today to wrap a generated file in a preprocessor conditional. --headerFile prepends a fixed header, but nothing emits a leading #if/trailing #endif.

This adds --with-conditional <symbol> / -wcond which wraps single-file C# output in a leading #if <symbol> and a trailing #endif:

#if VERSION_2_OR_NEWER

namespace ClangSharp.Test
{
    public partial struct MyStruct
    {
        public int x;
    }
}
#endif

The motivating scenario (#365) is Unity, where files can't be conditionally excluded at the project level, so the consumer generates each struct variant separately and selects one via a build symbol. XML output is intentionally unaffected.


Scoped to single-file C# output on purpose, which is exactly the run-once-per-variant flow in the issue. Wrapping multi-file output is a larger design — you'd want the symbol to vary per file (a remap), where-as a single global symbol is trivial. Left as a follow-up if someone actually needs the multi-file variant.

Adds a golden test (ConditionalOutputTest).

Fixes #365

…onditional

Wraps single-file C# output in a leading '#if <symbol>' and trailing '#endif' so consumers
that can't conditionally exclude files at the project level (e.g. Unity) can generate variants
and select one via a build symbol. XML output is unaffected.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding
tannergooding merged commit 9ba8570 into dotnet:main Jul 14, 2026
14 checks passed
@tannergooding
tannergooding deleted the tannergooding-with-conditional branch July 14, 2026 20:57
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.

[PInvokeGenerator] Provide a mechanism to wrap a generated file in a conditional

1 participant