Skip to content

Add target-specific CMake project references - #17514

Merged
jkoritzinsky merged 7 commits into
mainfrom
jkoritzinsky-cmake-sdk-target-build
Nov 19, 2026
Merged

jkoritzinsky merged 7 commits into
mainfrom
jkoritzinsky-cmake-sdk-target-build

Conversation

@jkoritzinsky

@jkoritzinsky jkoritzinsky commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

CMake SDK consumers need to build and consume selected targets from a shared CMake project, while native build tools must not operate on the same build tree concurrently.

This change adds CMakeProjectReference with CMakeTargets, selects artifacts directly from the CMake File API target descriptions, and serializes configure/build execution with a user-scoped cross-process mutex. Existing NativeProjectReference directory-based artifact handling remains supported.

Validation:

  • CMake SDK Debug build
  • CMake SDK Release build and package
  • MSBuild target parsing validation

To double check:

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 8, 2026 21:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

There are build-blocking issues (unused using with warnings-as-errors, and invalid MSBuild metadata method usage in ProjectReference.targets) that must be corrected before merge.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review tier: Lite
Findings: 2 High severity · 1 Medium severity

New issues introduced by this change (3)
Severity Finding
High severity src/​Microsoft.DotNet.CMake.Sdk/​sdk/​ProjectReference.targets — The %(CMakeProjectReference.CMakeTargets.Replace(...)) syntax is not a valid MSBuild metadata…
High severity src/​Microsoft.DotNet.CMake.Sdk/​src/​ExecWithMutex.cs — System.IO is not used in this file, and the repo builds with TreatWarningsAsErrors=true, so…
Medium severity src/​Microsoft.DotNet.CMake.Sdk/​src/​GetCMakeArtifactsFromFileApi.cs — When CMakeTargets is specified, this task doesn’t use config.Directories, but it still fails if…
What changed in this PR

This PR extends the CMake SDK’s project reference model so consumers can build/copy artifacts from specific CMake targets (via the CMake File API) while preventing concurrent native configure/build operations on the same build tree using a cross-process, user-scoped mutex.

Changes:

  • Add CMakeProjectReference with CMakeTargets metadata and integrate it into restore/build/copy flows.
  • Update File API artifact discovery to support selecting artifacts by CMake target name(s) in addition to source-directory matching.
  • Introduce ExecWithMutex and use it to serialize CMake Configure/CoreCompile execution per build output directory.
File Description
src/​Microsoft.DotNet.CMake.Sdk/​src/​GetCMakeArtifactsFromFileApi.cs Adds target-based artifact discovery path and updates logging/messages accordingly.
src/​Microsoft.DotNet.CMake.Sdk/​src/​ExecWithMutex.cs New task to run Exec under a named cross-process mutex (CurrentUserOnly on non-NETFRAMEWORK).
src/​Microsoft.DotNet.CMake.Sdk/​sdk/​ProjectReference.targets Adds CMakeProjectReference item type and wires restore/build/copy behavior for target-based references.
src/​Microsoft.DotNet.CMake.Sdk/​README.md Documents the new CMakeProjectReference usage and behavior.
src/​Microsoft.DotNet.CMake.Sdk/​Microsoft.DotNet.CMake.Sdk.csproj Adds dependency needed for deriving from Exec (Microsoft.Build.Tasks.Core).
src/​Microsoft.DotNet.CMake.Sdk/​build/​Microsoft.DotNet.CMake.Sdk.targets Uses ExecWithMutex to serialize configure/build execution per $(CMakeOutputDir).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 9, 2026 17:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

MSBuild property passing currently breaks multi-target CMakeTargets (semicolon parsing) and target selection can silently succeed on typos, risking missing native outputs without failing the build.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review tier: Lite
Findings: 1 High severity · 1 Medium severity

New issues introduced by this change (2)
Severity Finding
High severity src/​Microsoft.DotNet.CMake.Sdk/​sdk/​ProjectReference.targets — CMakeTargets is documented/treated as semicolon-separated, but it’s being embedded directly into…
Medium severity src/​Microsoft.DotNet.CMake.Sdk/​src/​GetCMakeArtifactsFromFileApi.cs — When CMakeTargets is specified, the task silently succeeds even if one or more requested targets do…
Issues resolved since last review (3)
Severity Finding
Medium severity src/​Microsoft.DotNet.CMake.Sdk/​src/​GetCMakeArtifactsFromFileApi.cs — When CMakeTargets is specified, this task doesn’t use config.Directories, but it still fails if… View resolved comment
High severity src/​Microsoft.DotNet.CMake.Sdk/​src/​ExecWithMutex.cs — System.IO is not used in this file, and the repo builds with TreatWarningsAsErrors=true, so… View resolved comment
High severity src/​Microsoft.DotNet.CMake.Sdk/​sdk/​ProjectReference.targets — The %(CMakeProjectReference.CMakeTargets.Replace(...)) syntax is not a valid MSBuild metadata… View resolved comment
Suppressed comments (2)

Previously missed (1) — in code that hasn't changed since the last review.

src/Microsoft.DotNet.CMake.Sdk/src/GetCMakeArtifactsFromFileApi.cs:158

  • The comment above this block notes that CMake’s File API “routinely emits” "."/".." segments, but dirSource is only canonicalized when it is not rooted. If CMake returns an absolute directory path containing "."/"..", the comparison against normalizedSourceDir can fail unexpectedly. Canonicalize dirSource for both rooted and non-rooted cases.

src/Microsoft.DotNet.CMake.Sdk/sdk/ProjectReference.targets:110

  • ReferencedCMakeTargets is passed through the MSBuild task Properties list without escaping. If CMakeTargets contains multiple targets separated by ';', only the first target will be assigned to ReferencedCMakeTargets and the remaining segments will be parsed as additional (invalid) properties, breaking artifact selection/copy.
    <MSBuild Projects="$(MSBuildProjectFile)"
             Targets="CopyNativeProjectBinariesFromFileApi"
             Properties="ReferencedCMakeProject=%(CMakeProjectReferenceNormalized.Identity);
                         ReferencedCMakeTargets=%(CMakeProjectReferenceNormalized.CMakeTargets);
                         %(CMakeProjectReferenceNormalized.AdditionalProperties)"

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 9, 2026 22:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The MSBuild invocation that builds CMake project references can batch incorrectly (cross-product) and may build the wrong projects with the wrong CMakeBuildTarget values.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review tier: Lite
Findings: 1 High severity · 1 Medium severity

New issues introduced by this change (1)
Severity Finding
High severity src/​Microsoft.DotNet.CMake.Sdk/​sdk/​ProjectReference.targets — The MSBuild invocation for CMake project references is batching over…
Pre-existing issues (1)
Severity Finding
Medium severity src/​Microsoft.DotNet.CMake.Sdk/​src/​GetCMakeArtifactsFromFileApi.cs — When CMakeTargets is specified, the task silently succeeds even if one or more requested targets do… View comment
Issues resolved since last review (1)
Severity Finding
High severity src/​Microsoft.DotNet.CMake.Sdk/​sdk/​ProjectReference.targets — CMakeTargets is documented/treated as semicolon-separated, but it’s being embedded directly into… View resolved comment

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 24, 2026 17:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The configure/build mutex scope and target handling issues must be addressed.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
Resolved since last review (2)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Use case-sensitive matching for CMake targets

src/​Microsoft.DotNet.CMake.Sdk/​src/​GetCMakeArtifactsFromFileApi.cs:138

CMake target names are case-sensitive, but this lookup uses OrdinalIgnoreCase (and the selection below uses StringComparer.OrdinalIgnoreCase). A request whose casing differs from the real target can be treated as found here even though the generated cmake --build --target command fails on case-sensitive generators. Match with ordinal/case-sensitive comparisons consistently.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: fd5eb201-1a6d-47ae-aa7a-0f7ecd7c06ae
Copilot AI lite review requested due to automatic review settings November 18, 2026 19:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active critical and moderate findings remain, including a compile-breaking unresolved TaskEnvironment reference and incorrect mutex-path and CMake target comparisons.

3 open issues
1 resolved since last review

Review effort: Lite

Share a build-tree mutex across path aliases and protect concurrent File API operations. Match CMake targets case-sensitively without rejecting generator-provided targets.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: fd5eb201-1a6d-47ae-aa7a-0f7ecd7c06ae
Copilot AI lite review requested due to automatic review settings November 18, 2026 22:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new CMake project-reference path lacks an end-to-end MSBuild integration test with multiple target references, leaving metadata batching, target builds, and artifact copying insufficiently verified.

2 open issues
2 resolved since last review

Review effort: Lite

@davidwrighton

Copy link
Copy Markdown
Member

Would it be practical to record a configure fingerprint in the build tree and validate it before cmake --build? Configure could compute a fingerprint from the effective configure inputs (the expanded configure command and relevant compiler/toolchain environment), then atomically write it only after configure succeeds. The build step could compare its expected fingerprint while holding the same build-tree mutex, and fail with a clear error if another invocation has reconfigured the tree differently.

That would preserve the current per-command locking while preventing a build from silently using another invocation's configuration. The fingerprint needs to cover all configuration-affecting inputs, and the check must remain under the lock through the build. It would not by itself detect stale CMake cache values for settings omitted by a later invocation, so that limitation should be explicit. Alternatively, configure and build could be one locked transaction, at the cost of serializing configure as well.

Note

This comment was generated with GitHub Copilot.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: fd5eb201-1a6d-47ae-aa7a-0f7ecd7c06ae
Copilot AI lite review requested due to automatic review settings November 19, 2026 19:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical concurrency issue remains because the configure fingerprint is written after releasing the mutex, allowing another build to alter the CMake tree before the fingerprint is persisted.

2 open issues
1 resolved since last review

Review effort: Lite

@jkoritzinsky
jkoritzinsky enabled auto-merge (squash) November 19, 2026 19:45
@jkoritzinsky
jkoritzinsky merged commit 3894d7c into main Nov 19, 2026
11 of 13 checks passed
@jkoritzinsky
jkoritzinsky deleted the jkoritzinsky-cmake-sdk-target-build branch November 19, 2026 20:48
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 20, 2026
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.

3 participants