Skip to content

Add Microsoft.DotNet.CMake.Sdk MSBuild SDK for integrating native builds into MSBuild. - #4533

Merged
markwilkie merged 17 commits into
dotnet:masterfrom
jkoritzinsky:cmake-sdk
Apr 27, 2020
Merged

markwilkie merged 17 commits into
dotnet:masterfrom
jkoritzinsky:cmake-sdk

Conversation

@jkoritzinsky

Copy link
Copy Markdown
Member

Adds a new MSBuild SDK for driving CMake invocations and easily referencing native built assets from managed projects. Additionally, it provides a mechanism for a managed project to reference a native project and have its assets copied to the output directory. It also has a feature to support outputting a script that runs the configure and build steps as MSBuild would have run them to more easily enable supporting standing up native builds on platforms where MSBuild is not currently available (bringup scenarios).

TODO: Open a PR into dotnet/arcade-services to recognize Microsoft.DotNet.CMake.Sdk as an MSBuild SDK for Maestro++

Fixes #4008

cc: @hughbe, @AaronRobinsonMSFT

Also @jkotas, can you take a look at the design here for supporting bringup scenarios and let me know if there's anything more needed to better support bringup? I'd like to enable us to use this for all native build scenarios, but I know there are concerns around the team about supporting new platforms.

<CMakeBuildDependsOn></CMakeBuildDependsOn>
</PropertyGroup>

<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

@alexperovich alexperovich Dec 10, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this SDK really need to import the entire .NET sdk with the C# and VB compilation support? Can we just import Microsoft.Common.props/targets to get the necessary ProjectReference and PackageReference stuff?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I should be able to just import Microsoft.Common.props/targets. Does that also enable importing of Directory.Build.props/targets? The reason I initially included the Microsoft.NET.Sdk was to get all of the niceties, not just PackageReference.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yea, Directory.Build.props/targets support is in the common props/targets, along with PackageReferences.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think I can remove this without breaking a scenario. When I tried to remove this locally, I lost the automatic setting of the Configuration property as well as all output path properties, which this SDK uses as defaults unless otherwise specified.

@jkotas

jkotas commented Dec 10, 2019

Copy link
Copy Markdown
Member

cc @janvorli

<ItemGroup Condition="'$(_IsMultiConfigurationGenerator)' != 'true'">

<!-- ############################################################### -->
<!-- The following is required because the single configuration -->

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This does not look pretty. Do we have this in the existing "run cmake from .cmd/.sh" solution?

If we have this, where is it?
If we do not have this, can we do the same?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes we do. This particular file is used by the CoreCLR test build only. It's located at https://github.com/dotnet/runtime/blob/master/src/coreclr/tests/src/Directory.Build.targets#L113-L170.

This MSBuild file is only used for the "managed project needs native assets in its output directory and we don't want to include all CMake outputs or have to duplicate our directory structure in our install directory" scenario. Basically the CoreCLR test tree and likely the WinForms test tree scenarios, not any product build scenarios.

This version is a significantly cleaned-up version of that implementation that also supports single-configuration generators on Windows (such as NMake or Ninja) instead of on Unix platforms only.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This particular file is used by the CoreCLR test build only.

Would it be better for this to stay next to CoreCLR tests only if it is only used by them?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The WinForms scenario will also use this feature (thats what #4008 is about). Also, I plan to use this feature in the dotnet/benchmarks repository to enable interop testing without having to flow the native test assets. If it weren't for the WinForms scenario, these targets would likely live in the dotnet/runtime repo.

@janvorli

Copy link
Copy Markdown
Member

@jkoritzinsky can you please share the bringup script it generates so that I can get some idea about it?

<_VSNativeCompilerComponentName>Microsoft.VisualStudio.Component.VC.Tools.$(_VSNativeCompilerComponentArchName)</_VSNativeCompilerComponentName>
</PropertyGroup>

<Target Name="ResolveVSGenerator" Condition="'$(CMakeGenerator)' == 'Visual Studio'">

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do not see equivalent resolve steps for clang/gcc in your change (like https://github.com/dotnet/runtime/blob/master/src/coreclr/src/pal/tools/find-gcc.sh). How are those going to work?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Paths to those scripts will be passed into the CMake SDK by setting the CMakeCompilerSearchScript property. The value of that property will be run before the configure step and before the build step and inserted into the bringup script before the configure and build commands respectively.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be better for this to be symmetric between platforms? Why to hardcode stuff for Windows, but not for Unix?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How would we be able to choose clang compiler version to use? I have many versions installed on my devbox. And how about all the other command line options that our current build.cmd/.sh scripts have?

@jkoritzinsky jkoritzinsky Dec 11, 2019 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We'd add support in what would be the coreclr.cmakeproj project file to allow you to pass MSBuild properties to configure which compiler to use as matches for the current parameters into build.sh. The PGO related arguments could be easily converted over to MSBuild properties. The ninja parameter converts directly to a /p:CMakeGenerator=Ninja argument. And we could wrap these properties with command line switches on coreclr.sh or build.sh in dotnet/runtime.

As mentioned in one of the top-level comments below, this isn't particularly useful in the CoreCLR build at the moment, but if/when we move towards Arcadifying the build, this will enable us to allow developers to use the all-up build scripts to build the native component without extra layers of wrapping when leaving MSBuild to build the native project (Bash->MSBuild->CMake) instead of going Bash->MSBuild->Bash->CMake.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There's a significant difference IMO in how we have to put in the work to find compilers on Windows vs Unix. On Windows you need to find a VS installation (likely with vswhere), and find the VC tools, which is significantly more convoluted than looking for a program on PATH. These targets also ensure that the VS install we pick actually has the toolset required. Our current scripts don't validate that you have the right toolsets for the architecture you're targeting, so you'll get a less clean error output.

Additionally, the reasoning behind not including the find-gcc.sh and find-clang.sh scripts here was for bringup reasons. If they were in the package, then we'd have to have a way to restore the package to get the scripts on the machine where we're building, which isn't possible, copy the files to the output, or inline them into the bringup script. My initial thought was to keep the find-gcc and find-clang scripts local to dotnet/runtime so the bringup script generation could be as simple as possible (one file, only call a script the user specifies and call cmake and cmake --build).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can the find-vs.bat, find-gcc.bat or find-clang.bat scripts can be under runtime\eng, potentially part of the file set that arcade copies around? It would make the solution symmetric, not part of any package, and potentially usable by any repo.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's a great idea! We should definitely be able to move them into eng/common and use them from this SDK.

@jkotas

jkotas commented Dec 10, 2019 •

Copy link
Copy Markdown
Member

can you take a look at the design here for supporting bringup scenarios

@janvorli was concerned about the bringup scenarios.

My primary concern about this is the layer of complexity that this adds.

I understand why you may want to use this for small tests where you would like to build a random little helper unmanaged binaries with as little ceremony as possible. I am fine with that.

I do not understand why this makes the unmanaged product build better. It is just more stuff that can go wrong and somebody needs to understand.

@jkoritzinsky

Copy link
Copy Markdown
Member Author

I haven't ported over the CoreCLR build to use this yet, but I can post the bringup script that is generated when I use this on Windows for the build of the hosting components:

cmake -G "Visual Studio 16 2019" -A x64 -B "D:\source\runtime\artifacts\obj\win-x64.Debug\/corehost" -DCLI_CMAKE_COMMON_HOST_VER=5.0.0-dev.19610.1 -DCLI_CMAKE_HOST_VER=5.0.0-dev.19610.1 -DCLI_CMAKE_HOST_FXR_VER=5.0.0-dev.19610.1 -DCLI_CMAKE_HOST_POLICY_VER=5.0.0-dev.19610.1 -DCLI_CMAKE_PKG_RID=win-x64 -DCLI_CMAKE_COMMIT_HASH=7788dbef9a8572face75b96f232416b2e3e1f7f3 -DCLI_CMAKE_PLATFORM_ARCH_x64=1 -DCLI_CMAKE_RESOURCE_DIR=D:\source\runtime\artifacts\obj\win-x64.Debug\/hostResourceFiles -DCMAKE_INSTALL_PREFIX=D:\source\runtime\artifacts\bin\/win-x64.Debug
      

        
      cmake --build "D:\source\runtime\artifacts\obj\win-x64.Debug\/corehost" --target install  --config Debug --

If I had passed in a script such as the find-gcc.sh script via the CMakeCompilerSearchScript property like <CMakeCompilerSearchScript>. find-gcc.sh 5 0</CMakeCompilerSearchScript>, the bringup script would look as follows:


. find-gcc.sh 5 0
cmake -G "Visual Studio 16 2019" -A x64 -B "D:\source\runtime\artifacts\obj\win-x64.Debug\/corehost" -DCLI_CMAKE_COMMON_HOST_VER=5.0.0-dev.19610.1 -DCLI_CMAKE_HOST_VER=5.0.0-dev.19610.1 -DCLI_CMAKE_HOST_FXR_VER=5.0.0-dev.19610.1 -DCLI_CMAKE_HOST_POLICY_VER=5.0.0-dev.19610.1 -DCLI_CMAKE_PKG_RID=win-x64 -DCLI_CMAKE_COMMIT_HASH=7788dbef9a8572face75b96f232416b2e3e1f7f3 -DCLI_CMAKE_PLATFORM_ARCH_x64=1 -DCLI_CMAKE_RESOURCE_DIR=D:\source\runtime\artifacts\obj\win-x64.Debug\/hostResourceFiles -DCMAKE_INSTALL_PREFIX=D:\source\runtime\artifacts\bin\/win-x64.Debug
      

        . find-gcc.sh 5 0
      cmake --build "D:\source\runtime\artifacts\obj\win-x64.Debug\/corehost" --target install  --config Debug --

@jkoritzinsky

Copy link
Copy Markdown
Member Author

The main reasons I had for using this to make the product build better:

  • We could remove all of the ceremony we have around getting the paths to the PGO data and writing it out to disk just to read back in to pass to CMake. We could do this work entirely within MSBuild in a project using this SDK and avoid all of the issues there.
  • We remove the need to maintain build.cmd and build.sh scripts for each native build that have completely different CLI designs for each native component (coreclr, libraries, corehost) to pass down their various CMake defines by enabling a well-defined pattern for passing them to CMake.
  • This SDK effectively removes an additional process layer from a fully Arcadified build system that has native components by directly invoking CMake instead of invoking a script that invokes CMake. In an Arcadified build, there has to be an MSBuild project front end to the native build anyway, which is currently backed by a batch/bash script that calls cmake. This will enable a good amount of simplification in the libraries and corehost builds in dotnet/runtime, and if/when coreclr's build is Arcadified, this will help there as well.
  • (Less important) It allows us to easily enable users to use their CMake generator of choice across the whole dotnet/runtime with one MSBuild property and no additional work (for example Ninja on Linux).

I agree that this doesn't add much today to coreclr's build, but if/when we go through the Arcadification process for coreclr, this becomes more valuable.

@janvorli

Copy link
Copy Markdown
Member

I have an additional question about some more logic. How are we going to do the target distro rid detection, CPU type detection and the OS detection that we now have in the _build-commons.sh?

@jkoritzinsky

Copy link
Copy Markdown
Member Author

We're going to have to solve those questions as part of Arcadifiying coreclr. They don't need to be done as part of this SDK.

Just to clarify in case there's confusion, using this SDK in the current product build would only replace the following portions of src/coreclr/build.cmd and src/coreclr/build.sh:

  • In src/coreclr/build.sh, the build_native and build_cross_architecture_components functions and lines 704-708
  • In src/coreclr/build.cmd, lines 464-520 and lines 528-602.

As I mentioned previously, there isn't a good value proposition for moving the CoreCLR build to using this SDK until/if we Arcadify coreclr's build. Many of the other items you mentioned can either be done within MSBuild (as is done for corehost's build) or added to the eng/build.sh and eng/build.cmd scripts that wrap the Arcade-based build of dotnet/runtime.

@janvorli

Copy link
Copy Markdown
Member

One more thought about coreclr. More than 50% of the build.sh script deals with native build. It looks like we could strip dealing with the managed stuff (managed assemblies build, package creation, optimization data restoration) and leave just the stuff relevant for native components build. We have a similar thing in the former corefx where the build-native.sh script basically does just that.
Then such a script can stay and can be used for both the bringup and the native portion of the build. It seems to me that there is not a big difference between calling a .sh / .cmd script and just invoking cmake.

@jkoritzinsky

Copy link
Copy Markdown
Member Author

That's something we can definitely do. I personally feel that we have a lot of duplication between a "native logic only" build.sh/.cmd in coreclr, the build-native.sh/.cmd scripts in libraries (formerly corefx) and the build.sh/.cmd in installer (formerly core-setup) that we could coalesce with this SDK. Alternatively, we could also coalesce the differences without the SDK by updating the scripts to share more.

Keeping the .sh and .cmd scripts exclusively for bringup scenarios feels like we might be optimizing our development for a few weeks/month or two every few years instead of the more common scenarios when we aren't doing bringup on new platforms. Bringup on new platforms doesn't require all of the options in the scripts as we have today to support all of our supported platforms; the compiler version and the CMake parameters can be hard-coded and the invocation could be a 3 line script (excluding return code handling) to do the whole build instead of the hundereds of lines of logic in each of the build/build-native scripts. As a result, we have a lot of additional complexity in our scripts that are just duplicating logic that in many cases is already in MSBuild.

@jkotas

jkotas commented Dec 11, 2019

Copy link
Copy Markdown
Member

fully Arcadified build system that

Many of the Arcade build system designs and ideas are managed code centric. I do not believe they work well for repos with large unmanaged components.

We are having this discussion because we do not have shared understanding where we want to land. It may be best to first get a shared understanding where we want to land (e.g what does Arcadification looks like - for both CoreCLR and Mono) and then work back from there.

I like the old CoreCLR build system better - for the same max process depth reason I have mentioned in dotnet/runtime#672 (comment).

I believe that this (max process depth 3) is better:

+ build.sh
   + cmake
   + make
      + clang
   + msbuild
      + csc

than this (max process depth 4):

+ build.sh
   + msbuild
      + cmake
      + make
         + clang
      + csc

@janvorli

Copy link
Copy Markdown
Member

I don't want to optimize for the bringup scenarios, but I would also like to not to have to reinvent the wheel that we already had and was working fine when someone wants to bring up a new platform. I see the scripts as a nice and simple to debug way to prepare cmake command line arguments. I expect only a very minor changes to these scripts when bringing up new platforms and also a very minor changes to these scripts over the time. During the last years, we were mostly just adding support for a new clang version (until your recent changes, of course).
It would definitely be great to share most of the scripts between the coreclr, libraries and installer. Half step towards that was already done when the _build-commons.sh was created in coreclr to enable sharing code between the product and test builds.
So overall, it seems that it would be beneficial to keep stripped down scripts with common parts shared (just to build the native parts) for coreclr, libraries and installer. We would call those scripts from the msbuild scripts of the the arcadified build instead of the cmake, but that we can still call from the command line directly (which I expect to be doing myself most of the time, as I rarely change stuff in the managed world).

@jkoritzinsky

Copy link
Copy Markdown
Member Author

fully Arcadified build system that

Many of the Arcade build system designs and ideas are managed code centric. I do not believe they work well for repos with large unmanaged components.

We are having this discussion because we do not have shared understanding where we want to land. It may be best to first get a shared understanding where we want to land (e.g what does Arcadification looks like - for both CoreCLR and Mono) and then work back from there.

I agree. We should make sure we understand what Arcadification looks like for CoreCLR before making a decision on what to do for the native portion.

I like the old CoreCLR build system better - for the same max process depth reason I have mentioned in dotnet/runtime#672 (comment).

I believe that this (max process depth 3) is better:

+ build.sh
   + cmake
   + make
      + clang
   + msbuild
      + csc

than this (max process depth 4):

+ build.sh
   + msbuild
      + cmake
      + make
         + clang
      + csc

I've been thinking about the process tree from the outer build.sh script that builds the full repository. Currently when you build via that, the tree below is the process tree for invoking cmake (this is also the tree for invoking cmake from libraries and installer today):

+ ./build.sh
   + msbuild
        + ./src/coreclr/build.sh
             + cmake
                 + make
                      + clang

Building from the src/coreclr/build.sh script obviously cuts that down to that node in the tree and below (make is under cmake ever since we moved to using cmake --build to drive the native build).

The proposed solution would make the process tree from the root build script:

+ ./build.sh
   + msbuild
         + cmake
             + make
                  + clang

Bringing down the process tree size by one.

I don't want to optimize for the bringup scenarios, but I would also like to not to have to reinvent the wheel that we already had and was working fine when someone wants to bring up a new platform. I see the scripts as a nice and simple to debug way to prepare cmake command line arguments. I expect only a very minor changes to these scripts when bringing up new platforms and also a very minor changes to these scripts over the time. During the last years, we were mostly just adding support for a new clang version (until your recent changes, of course).
It would definitely be great to share most of the scripts between the coreclr, libraries and installer. Half step towards that was already done when the _build-commons.sh was created in coreclr to enable sharing code between the product and test builds.
So overall, it seems that it would be beneficial to keep stripped down scripts with common parts shared (just to build the native parts) for coreclr, libraries and installer. We would call those scripts from the msbuild scripts of the the arcadified build instead of the cmake, but that we can still call from the command line directly (which I expect to be doing myself most of the time, as I rarely change stuff in the managed world).

One option here would be to use the SDK for supported platforms, and have a bringup_build_native.sh similar to how we have the bringup_runtest.sh. However, the utility of this depends on how we decide to arcadify the coreclr product and test builds, which are discussions we still need to have as mentioned above.

I just want to make sure that this SDK has enough utility to present as a possible option during our CoreCLR Arcadification discussions and that it has the needed features to enable developers of CoreCLR to easily build the product if we were to choose it as an option, either now or in the future.

@tmat

tmat commented Dec 11, 2019

Copy link
Copy Markdown
Member

I agree with @jkotas on having cmake build on the same level as msbuild.

+ build.sh
   + cmake
   + make
      + clang
   + msbuild
      + csc

Arcade does not require msbuild to drive everything. The downside of not building everything via msbuild is serialization of the builds (first all cmake "projects" will build, then all msbuild projects). But that's given by having 2 different build systems (make, msbuild) that don't communicate dependencies with each other.

@jkoritzinsky

Copy link
Copy Markdown
Member Author

If we go that route of running the CMake builds completely outside of MSBuild, then I agree that we would not need or want to use this SDK for product builds.

This SDK is still useful for the CoreCLR test tree, WinForms' test tree, and as a mechanism to create native assets in dotnet/performance.

@wjk

wjk commented Dec 11, 2019

Copy link
Copy Markdown
Contributor

My 2¢ on this PR: Would it be possible to publish this SDK to the main NuGet feed? As currently written, it would be useful for many other projects than just .NET Core. In fact, I have been meaning to research writing this exact library myself for some time for use in some of my mixed-language C++/C# projects. Once again, you beat me to it! 😃 Thanks!

@jkoritzinsky

Copy link
Copy Markdown
Member Author

If the project is referenced by a .sln file
Can't do this: Project 'C:\Users\hughbe\source\repos\Arcade-CMake\NativeTests\NativeTests.proj' has an unknown project type and cannot be added to the solution file. Contact your SDK provider for support.

If you only have a reference to it via a NativeProjectReference, then I see why it would be manual

My point, however, is that the behaviour with ProjectReference is a non-manual process.

Yes that is true, but the behavior with ProjectReference has additional side-effects that we don't want.

We don't want to do the same thing as ProjectReference for restore because we don't want a NativeProjectReference added to the restore graph. That would add dependencies to a final Pack output as well as bring through transitive package references into the references and have a number of other reaching effects.

Given that we can't reference it from sln and we don't want to treat NativeProjectReference as ProjectReference, how can we get it to autogenerate project.assets.json on build?

I'll see if I can make something work.

So what happened with Platform is something is passing it as a global property, so your changes can't override it. I'll try to track down what's happening there.

Thanks - any advice with the vswhere.exe issue?

Looks like in the time between when I started this PR and now Arcade changed how they discover vswhere. I'll need to adapt my design to support the change.

I've figured it out. The Platform property is being set by the root sln file. I suggest running your build with dotnet build /p:Platform=x86 to set it from the root

Any idea why build.cmd -platform x64 doesn't work?

Looks like your solution doesn't have an x64 platform configuration set up. The solution file needs to have that set up separately in VS in the platform drop down so that it propagates through correctly.

…ces so a single restore from the root will restore everything.
@jkoritzinsky

Copy link
Copy Markdown
Member Author

@hughbe I've fixed the vswhere lookup and hooked into the regular restore process.

@jkoritzinsky

Copy link
Copy Markdown
Member Author

@markwilkie anywhere in particular you'd like me to add a link to this from the Arcade docs?


- CMakeLists
- The root CMakeLists.txt of the native build.
- The root CMakeLists.txt of the native build. This is required.

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.

is there an argument for making this CMakeLists.txt by defau;t?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I personally don't like that implicit behavior since it's such a small entry. I want to be able to look at a project file and see at a glance what I'm building. If it's default it adds more cognitive load. I feel that this is different than a csproj because the csproj is actually building the output, whereas this SDK is just driving something else getting built. If that something else isn't explicitly in the project, then it makes it harder to reason about.

@hughbe

hughbe commented Mar 12, 2020

Copy link
Copy Markdown
Contributor

Looks like your solution doesn't have an x64 platform configuration set up. The solution file needs to have that set up separately in VS in the platform drop down so that it propagates through correctly.

Are you sure? I'm seeing it in the sln file


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Windows.Forms.Interop.Tests", "System.Windows.Forms.Interop.Tests\System.Windows.Forms.Interop.Tests.csproj", "{03C4545B-2C9B-41E5-B957-EDE3B713EE60}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Debug|x64 = Debug|x64
		Debug|x86 = Debug|x86
		Release|Any CPU = Release|Any CPU
		Release|x64 = Release|x64
		Release|x86 = Release|x86
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Debug|x64.ActiveCfg = Debug|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Debug|x64.Build.0 = Debug|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Debug|x86.ActiveCfg = Debug|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Debug|x86.Build.0 = Debug|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Release|Any CPU.Build.0 = Release|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Release|x64.ActiveCfg = Release|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Release|x64.Build.0 = Release|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Release|x86.ActiveCfg = Release|Any CPU
		{03C4545B-2C9B-41E5-B957-EDE3B713EE60}.Release|x86.Build.0 = Release|Any CPU
	EndGlobalSection
EndGlobal

I've figured it out. The Platform property is being set by the root sln file. I suggest running your build with dotnet build /p:Platform=x86 to set it from the root. That will also ensure that the rest of your projects will be consistent and will build for x86.

I get the errors running build.cmd from root... Also when I run build.cmd -platform x86, so I'm not sure this is actually the problem

@jkoritzinsky

jkoritzinsky commented Mar 12, 2020 •

Copy link
Copy Markdown
Member Author

I see the issue now. In the solution file, it's set to build the System.Windows.Forms.Interop.Tests assembly as AnyCPU even when platform is set to x86 or x64. If you look in the GlobalSection(ProjectConfigurationPlatforms) section towards the bottom, you'll see that it maps each of the platforms to AnyCPU. When I change the GlobalSection(ProjectConfigurationPlatforms) section to map x64 to x64 and x86 to x86, it builds the correct architecture with the -platform parameter.

@alexperovich alexperovich left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have any concerns with the current implementation. Please get signoff from @jkotas as well.

@jkotas

jkotas commented Apr 9, 2020

Copy link
Copy Markdown
Member

No concerns from me. I do not know much about the art of writing .NET SDKs, so I am able to comment on whether the details are right.

@hughbe

hughbe commented Apr 19, 2020

Copy link
Copy Markdown
Contributor

Would love to get this merged in and used. @jkoritzinsky would you mind helping out with dotnet/winforms#1932 if I encounter any problems? Thanks so much!

@jkoritzinsky

Copy link
Copy Markdown
Member Author

@mmitche can I get signoff or review on this PR?

cc: @markwilkie

@markwilkie

Copy link
Copy Markdown
Contributor

Adding @jaredpar as there was a cmake discussion earlier that this might help with.

<PropertyGroup>
<CMakeOutputDir Condition="'$(CMakeOutputDir)' != ''">$([MSBuild]::NormalizePath($(CMakeOutputDir)))</CMakeOutputDir>
<CMakeOutputDir Condition="'$(CMakeOutputDir)' == ''">$([MSBuild]::NormalizePath($(IntermediateOutputPath)))</CMakeOutputDir>
<CMakeOutputDir>$(CMakeOutputDir.TrimEnd('\'))</CMakeOutputDir>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does forward slash on non-windows platforms need to be trimmed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes. The forward slash was causing the script to escape the following space and was breaking the script parsing. By removing the slash, we stop that escaping.

@jkoritzinsky

Copy link
Copy Markdown
Member Author

I believe this is ready to be merged. Can someone with merge permissions merge this in?

@markwilkie
markwilkie merged commit d4a1100 into dotnet:master Apr 27, 2020
@RussKie

RussKie commented Apr 30, 2020

Copy link
Copy Markdown
Contributor

When should we expect this to be available? It doesn't look like it is available in 20200424.11
Do we need to do anything special to opt in?

/cc: @hughbe

@ghost

ghost commented Apr 30, 2020

Copy link
Copy Markdown

As this was merged in 4/27, you shouldn't see it in a version from 4/24. The change is available in the latest build that we are sending to repos tomorrow.

@RussKie

RussKie commented Apr 30, 2020

Copy link
Copy Markdown
Contributor

Hah, silly me, should have made a connection that the version contains the date

@ghost

ghost commented Apr 30, 2020 •

Copy link
Copy Markdown

Also, you can always check the repo's README to see which version is the latest that we are sending to consumer repos after it goes through our validation pipeline.

@hughbe

hughbe commented May 1, 2020

Copy link
Copy Markdown
Contributor

I have found an error using this on my Mac: dotnet build /P:Platform=x64 has the following error

CMake Error at CMakeLists.txt:2 (project):
  Generator

    Unix Makefiles

  does not support platform specification, but platform

    x64

Seems like we are incorrectly passing a -A platform parameter on Mac (Unix?) when we should not be doing so

<ItemGroup>
<_CMakeArguments Include="-G &quot;$(CMakeGenerator)&quot;" />
<_CMakeArguments Condition="'$(CMakeToolset)' != ''" Include="-T $(CMakeToolset)" />
<_CMakeArguments Condition="'$(_CMakePassArchitectureToGenerator)' != ''" Include="-A $(Platform)" />

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.

we actually set _CMakePassArchitectureToGenerator to False, so this test fails.

@hughbe

hughbe commented May 1, 2020

Copy link
Copy Markdown
Contributor

Upon fixing the architecture parameter (#5388), I get the following errors:

/Users/hugh/.nuget/packages/microsoft.dotnet.cmake.sdk/5.0.0-beta.20228.4/build/Microsoft.DotNet.CMake.Sdk.targets(172,5): error MSB3073: The command " [/Users/hugh/Documents/GitHub/winforms/src/System.Windows.Forms/tests/NativeTests/NativeTests.proj]
/Users/hugh/.nuget/packages/microsoft.dotnet.cmake.sdk/5.0.0-beta.20228.4/build/Microsoft.DotNet.CMake.Sdk.targets(172,5): error MSB3073:        [/Users/hugh/Documents/GitHub/winforms/src/System.Windows.Forms/tests/NativeTests/NativeTests.proj]
/Users/hugh/.nuget/packages/microsoft.dotnet.cmake.sdk/5.0.0-beta.20228.4/build/Microsoft.DotNet.CMake.Sdk.targets(172,5): error MSB3073:       . $([MSBuild]::MakeRelative('$(MSBuildProjectDirectory)', '$(RepositoryEngineeringDir)common/native/find-native-compiler.sh') $(CMakeCompilerToolchain) $(CMakeCompilerMajorVersion) $(CMakeCompilerMinorVersion) [/Users/hugh/Documents/GitHub/winforms/src/System.Windows.Forms/tests/NativeTests/NativeTests.proj]
/Users/hugh/.nuget/packages/microsoft.dotnet.cmake.sdk/5.0.0-beta.20228.4/build/Microsoft.DotNet.CMake.Sdk.targets(172,5): error MSB3073:        [/Users/hugh/Documents/GitHub/winforms/src/System.Windows.Forms/tests/NativeTests/NativeTests.proj]
/Users/hugh/.nuget/packages/microsoft.dotnet.cmake.sdk/5.0.0-beta.20228.4/build/Microsoft.DotNet.CMake.Sdk.targets(172,5): error MSB3073:        [/Users/hugh/Documents/GitHub/winforms/src/System.Windows.Forms/tests/NativeTests/NativeTests.proj]
/Users/hugh/.nuget/packages/microsoft.dotnet.cmake.sdk/5.0.0-beta.20228.4/build/Microsoft.DotNet.CMake.Sdk.targets(172,5): error MSB3073:        cmake -G "Unix Makefiles" -B "/Users/hugh/Documents/GitHub/winforms/artifacts/obj/NativeTests/x64/Debug/" -S "" -DCMAKE_INSTALL_PREFIX=/Users/hugh/Documents/GitHub/winforms/artifacts/bin/NativeTests/x64/Debug/ -DCMAKE_BUILD_TYPE=Debug [/Users/hugh/Documents/GitHub/winforms/src/System.Windows.Forms/tests/NativeTests/NativeTests.proj]
/Users/hugh/.nuget/packages/microsoft.dotnet.cmake.sdk/5.0.0-beta.20228.4/build/Microsoft.DotNet.CMake.Sdk.targets(172,5): error MSB3073:       " exited with code 1. [/Users/hugh/Documents/GitHub/winforms/src/System.Windows.Forms/tests/NativeTests/NativeTests.proj]

I confirmed that the generator script worked. However, the code that doesn't work is the CMakeCompilerSearchScript defined as

<Target Name="_ResolveNonMSVCCompilerToolchain" Condition="'$(CMakeCompilerToolchain)' != 'MSVC' and '$(CMakeCompilerSearchScript)' == ''">
  <Error Condition="$([MSBuild]::IsOsPlatform(Windows))" Text="This SDK does not support using non-MSVC toolchains on Windows." />
  <PropertyGroup>
    <CMakeCompilerSearchScript>
      . $([MSBuild]::MakeRelative('$(MSBuildProjectDirectory)', '$(RepositoryEngineeringDir)common/native/find-native-compiler.sh') $(CMakeCompilerToolchain) $(CMakeCompilerMajorVersion) $(CMakeCompilerMinorVersion)
    </CMakeCompilerSearchScript>
  </PropertyGroup>
</Target>

There appears to be two bugs here:

  1. Missing parenthesis
  2. We are calling the method with "." when we can just execute it directly.

Updated #5388

@hughbe

hughbe commented May 1, 2020 •

Copy link
Copy Markdown
Contributor

Also, running dotnet clean on a c# (managed) project, I would also expect any NativeProjectReference items to be cleaned as well

@jkoritzinsky
jkoritzinsky deleted the cmake-sdk branch June 25, 2020 05:51
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.

Need a common way to reference CMakeLists.txt projects from a csproj