Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define compiler variables in CMakePresets.json #16762

Merged
merged 3 commits into from
Aug 27, 2024

Conversation

petwu
Copy link
Contributor

@petwu petwu commented Aug 1, 2024

Changelog: Feature: Define CMAKE_<LANG>_COMPILER variables in CMakePresets.json.
Docs: Omit
Fixes #13136

The compiler variables are set to the same values as in the cmake_toolchain.cmake, and only when defining tools.build:compiler_executables.

I've tested it with both VS an VS Code, which successfully initialize the correct vcvars environment and select the correct compiler as specified by the toolset version, host and target architecture. From a non-IDE perspective, this does influence the CMake configuration in any way, as the compiler variables would be set in the toolchain to the same value anyway.

profile
[settings]
os=Windows
arch=x86
compiler=msvc
compiler.version=193
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.cppstd=17

[conf]
tools.cmake.cmaketoolchain:toolset_arch=x64
tools.build:compiler_executables={"c": "cl.exe", "cpp": "cl.exe"}
conanfile.txt
[generators]
CMakeToolchain

[layout]
cmake_layout
CMakePresets.json after conan install -pr profile -s build_type=Debug .
{
    "version": 3,
    "vendor": {
        "conan": {}
    },
    "cmakeMinimumRequired": {
        "major": 3,
        "minor": 15,
        "patch": 0
    },
    "configurePresets": [
        {
            "name": "conan-debug",
            "displayName": "'conan-debug' config",
            "description": "'conan-debug' configure using 'Ninja' generator",
            "generator": "Ninja",
            "cacheVariables": {
                "CMAKE_POLICY_DEFAULT_CMP0091": "NEW",
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_C_COMPILER": "cl.exe",
                "CMAKE_CXX_COMPILER": "cl.exe"
            },
            "toolset": {
                "value": "v143,host=x64",
                "strategy": "external"
            },
            "architecture": {
                "value": "x86",
                "strategy": "external"
            },
            "toolchainFile": "generators\\conan_toolchain.cmake",
            "binaryDir": "C:\\TMP\\conan-feat-presets-with-compiler\\build\\Debug"
        }
    ],
    "buildPresets": [
        {
            "name": "conan-debug",
            "configurePreset": "conan-debug",
            "jobs": 24
        }
    ],
    "testPresets": [
        {
            "name": "conan-debug",
            "configurePreset": "conan-debug"
        }
    ]
}
  • Refer to the issue that supports this Pull Request.
  • If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
  • I've read the Contributing guide.
  • I've followed the PEP8 style guides for Python code.
  • I've opened another PR in the Conan docs repo to the develop branch, documenting this one.

@CLAassistant
Copy link

CLAassistant commented Aug 1, 2024

CLA assistant check
All committers have signed the CLA.

@petwu petwu force-pushed the feature/cmake-presets-compiler branch from b5ec82e to c09a252 Compare August 1, 2024 14:11
@petwu petwu force-pushed the feature/cmake-presets-compiler branch from c09a252 to 7f694c9 Compare August 1, 2024 14:12
@memsharded memsharded added this to the 2.7.0 milestone Aug 2, 2024
Copy link
Member

@memsharded memsharded left a comment

Choose a reason for hiding this comment

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

I think this can be moved forward for next release, thanks very much for your contribution!

Asking @jcar87 for an extra review, thanks!

Copy link
Contributor

@jcar87 jcar87 left a comment

Choose a reason for hiding this comment

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

LGTM!

@memsharded, I would consider passing cl.exe to both C and CXX compilers if compiler=msvc, the cmake generator is Ninja, even when tools.build.compiler_executables is undefined.

@@ -158,6 +158,13 @@ def _configure_preset(conanfile, generator, cache_variables, toolchain_file, mul
"strategy": "external"
}

# Set the compiler like in the toolchain. Some IDEs like VS or VSCode require the compiler
# being set to cl.exe in order to activate the environment using vcvarsall.bat according to
Copy link
Contributor

Choose a reason for hiding this comment

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

interesting! I've not found this documented for VS Code, but have verified that it indeed works that way. Thanks so much for figuring this out! :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought it was documented somewhere, but I couldn't find it either and can't remember how I learned about it in the first place. But I have used this trick successfully for some time now and seeing this comment this has been a thing for at least 3 years.

[settings]
os=Windows
arch=x86_64
compiler=clang
Copy link
Contributor

Choose a reason for hiding this comment

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

I can see the output is as expected, but wouldn't he compiler executables be clang or clang-cl.exe instead?

Copy link
Member

Choose a reason for hiding this comment

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

I think this is being directly overriden by tools.build:compiler_executables and most likely the intention of the test was to have compiler=msvc, can you please confirm @petwu?

I'll check if it makes sense or is possible to introduce some check for this.

Copy link
Member

Choose a reason for hiding this comment

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

I suspect this is derived from copy&paste the test above called test_set_cmake_lang_compilers_and_launchers

Copy link
Member

Choose a reason for hiding this comment

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

Exactly, I have pushed some changes to the test to define compiler=msvc instead.
Indeed, the compiler_executables is being written no matter what the compiler setting is, it is the responsibility of the user to define the profiles consistently. I have checked if it is possible to introduce some check that made sense for potential misalignments like this one (defining compiler_executable with cl.exe for other compiler not msvc, but it looks a bit too dirty, so leaving it out of this PR, might reconsider later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, I just copied the test above. Setting compiler=clang and then saying your executable is called cl.exe of course doesn't make sense in a real-world scenario. The test should just verify that the compiler_executable setting ended up in the generated preset.

@memsharded memsharded merged commit 60df72c into conan-io:develop2 Aug 27, 2024
2 checks passed
czoido added a commit to czoido/conan that referenced this pull request Sep 11, 2024
czoido added a commit that referenced this pull request Sep 11, 2024
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.

[feature] CMakePresets support for msvc toolset version
4 participants