C# SDK style projects Any CPU

I can now generate SDK style projects for my C# projects in my c++/C# program. This has enabled me to use .NET6, which is nice.

But my one problem is that the sln file generated for Visual Studio forces all of the projects to be “Any CPU”, like:

		{22FBEAD8-11FF-33A0-86DB-03A20F5BD573}.Debug|x64.ActiveCfg = Debug|Any CPU
		{22FBEAD8-11FF-33A0-86DB-03A20F5BD573}.Debug|x64.Build.0 = Debug|Any CPU
		{22FBEAD8-11FF-33A0-86DB-03A20F5BD573}.Release|x64.ActiveCfg = Release|Any CPU
		{22FBEAD8-11FF-33A0-86DB-03A20F5BD573}.Release|x64.Build.0 = Release|Any CPU
		{22FBEAD8-11FF-33A0-86DB-03A20F5BD573}.MinSizeRel|x64.ActiveCfg = MinSizeRel|Any CPU
		{22FBEAD8-11FF-33A0-86DB-03A20F5BD573}.MinSizeRel|x64.Build.0 = MinSizeRel|Any CPU
		{22FBEAD8-11FF-33A0-86DB-03A20F5BD573}.RelWithDebInfo|x64.ActiveCfg = RelWithDebInfo|Any CPU
		{22FBEAD8-11FF-33A0-86DB-03A20F5BD573}.RelWithDebInfo|x64.Build.0 = RelWithDebInfo|Any CPU

Seems this is hard-coded and there are no options to override it:

        // On VS 19 and above, always map .NET SDK projects to "Any CPU".
        if (target->IsDotNetSdkTarget() &&
            this->GetVersion() >= VSVersion::VS16 &&
            !this->IsReservedTarget(target->GetName())) {
          mapping = "Any CPU";
        }
        this->WriteProjectConfigurations(fout, *vcprojName, *target, configs,
                                         configsPartOfDefaultBuild, mapping);

Is there any way to remove this or override? I need to build with x64 because I’m building my native binaries with x64.

Right now my only idea is to hack it with a find-and-replace in the generated sln from “Any CPU” to “x64”, but that seems…less than ideal.

1 Like

This would likely require a new feature to plumb a target selection option through here. Maybe CMAKE_CSharp_TARGET_CPU that defaults to “Any CPU”?

Cc: @brad.king

See CMake Issue 23513.

Glad to see this is being looked at. As it is, the solution generated is broken for C++ and C# DOTNET_SDK projects. Visual Studio will show an error message about missing configuration mappings for projects (because the “Any CPU” in the solution file), but the configuration manager UI shows all the projects with “x64” as I’d expect. Then when building, some of the C# projects are mysteriously “skipped”. Based on some other Stack Overflow posts, they get skipped because of the “Any CPU” configuration in the solution file (so removing them and adding them back to the solution is a trick I saw).

I would love to get this project working so I can have native x64 DLL’s that are consumed with P/invoke from my .NET6 managed code all built in one Visual Studio solution from my cmake project.

For the time being, I have an ugly hack near the top of my cmakelist:

option(FIX_ANY_CPU "Enable work-around to fix AnyCPU generated in sln, must run cmake once without this first" OFF)
option(RECURSIVE_GENERATE "Recursive call to cmake" OFF)
if(FIX_ANY_CPU AND NOT RECURSIVE_GENERATE)
    message(CHECK_START "Recursive generate")
    execute_process(COMMAND ${CMAKE_COMMAND}
        -G "${CMAKE_GENERATOR}"
        -T "${CMAKE_GENERATOR_TOOLSET}"
        -A "${CMAKE_GENERATOR_PLATFORM}" 
        -DRECURSIVE_GENERATE:BOOL=ON 
        ${CMAKE_SOURCE_DIR}
        RESULT_VARIABLE recursive_result)
    if (${recursive_result} EQUAL 0)
        message(CHECK_PASS "done")
    else()
        message(CHECK_FAIL "failed!")
        message(FATAL_ERROR "Recursive generate failed!")
    endif()

    # post-generate steps here
    message(CHECK_START "Fixing the csproj mess of AnyCPU...")
    execute_process(COMMAND
        powershell -Command "(Get-Content ${CMAKE_BINARY_DIR}/project_name.sln) -replace 'Any CPU', 'x64' | Out-File ${CMAKE_BINARY_DIR}/project_name.sln"
        RESULT_VARIABLE any_cpu_fix_result)
    if (${any_cpu_fix_result} EQUAL 0)
        message(CHECK_PASS "done")
    else()
        message(CHECK_FAIL "failed!")
    endif()

    # exit without doing anything else, since it already happened
    return()
endif()
#rest of the cmake list is here...

Two years on, SDK-style projects still generate these broken solutions. I generate a solution with SDK-style projects, but get a warning when I open it, and the projects get skipped on build.