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

Consider adding cmakeExecutable to presets #15447

Merged
merged 6 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions conan/tools/cmake/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@


def write_cmake_presets(conanfile, toolchain_file, generator, cache_variables,
user_presets_path=None, preset_prefix=None, buildenv=None, runenv=None):
user_presets_path=None, preset_prefix=None, buildenv=None, runenv=None,
cmake_executable=None):
preset_path, preset_data = _CMakePresets.generate(conanfile, toolchain_file, generator,
cache_variables, preset_prefix, buildenv, runenv)
cache_variables, preset_prefix, buildenv, runenv,
cmake_executable)
_IncludingPresets.generate(conanfile, preset_path, user_presets_path, preset_prefix, preset_data)


class _CMakePresets:
""" Conan generated main CMakePresets.json inside the generators_folder
"""
@staticmethod
def generate(conanfile, toolchain_file, generator, cache_variables, preset_prefix, buildenv, runenv):
def generate(conanfile, toolchain_file, generator, cache_variables, preset_prefix, buildenv, runenv,
cmake_executable):
cache_variables = cache_variables or {}
if platform.system() == "Windows" and generator == "MinGW Makefiles":
if "CMAKE_SH" not in cache_variables:
Expand Down Expand Up @@ -61,12 +64,13 @@ def generate(conanfile, toolchain_file, generator, cache_variables, preset_prefi
_CMakePresets._insert_preset(data, "testPresets", test_preset)
configure_preset = _CMakePresets._configure_preset(conanfile, generator, cache_variables,
toolchain_file, multiconfig,
preset_prefix, buildenv)
preset_prefix, buildenv,
cmake_executable)
# Conan generated presets should have only 1 configurePreset, no more, overwrite it
data["configurePresets"] = [configure_preset]
else:
data = _CMakePresets._contents(conanfile, toolchain_file, cache_variables, generator,
preset_prefix, buildenv, runenv)
preset_prefix, buildenv, runenv, cmake_executable)

preset_content = json.dumps(data, indent=4)
save(preset_path, preset_content)
Expand All @@ -85,14 +89,14 @@ def _insert_preset(data, preset_type, preset):

@staticmethod
def _contents(conanfile, toolchain_file, cache_variables, generator, preset_prefix, buildenv,
runenv):
runenv, cmake_executable):
"""
Contents for the CMakePresets.json
It uses schema version 3 unless it is forced to 2
"""
multiconfig = is_multi_configuration(generator)
conf = _CMakePresets._configure_preset(conanfile, generator, cache_variables, toolchain_file,
multiconfig, preset_prefix, buildenv)
multiconfig, preset_prefix, buildenv, cmake_executable)
build = _CMakePresets._build_preset_fields(conanfile, multiconfig, preset_prefix)
test = _CMakePresets._test_preset_fields(conanfile, multiconfig, preset_prefix, runenv)
ret = {"version": 3,
Expand All @@ -106,7 +110,7 @@ def _contents(conanfile, toolchain_file, cache_variables, generator, preset_pref

@staticmethod
def _configure_preset(conanfile, generator, cache_variables, toolchain_file, multiconfig,
preset_prefix, buildenv):
preset_prefix, buildenv, cmake_executable):
build_type = conanfile.settings.get_safe("build_type")
name = _CMakePresets._configure_preset_name(conanfile, multiconfig)
if preset_prefix:
Expand All @@ -124,6 +128,9 @@ def _configure_preset(conanfile, generator, cache_variables, toolchain_file, mul
if buildenv:
ret["environment"] = buildenv

if cmake_executable:
ret["cmakeExecutable"] = cmake_executable

if is_msvc(conanfile):
# We can force the generator Visual even if it is Ninja, to define the toolset
toolset = GenericSystemBlock.get_toolset("Visual", conanfile)
Expand Down
19 changes: 17 additions & 2 deletions conan/tools/cmake/toolchain/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ def content(self):
content = relativize_generated_file(content, self._conanfile, "${CMAKE_CURRENT_LIST_DIR}")
return content

def _find_cmake_exe(self):
for req in self._conanfile.dependencies.direct_build.values():
if req.ref.name == "cmake":
czoido marked this conversation as resolved.
Show resolved Hide resolved
for bindir in req.cpp_info.bindirs:
cmake_path = os.path.join(bindir, "cmake")
cmake_exe_path = os.path.join(bindir, "cmake.exe")

if os.path.exists(cmake_path):
return cmake_path
elif os.path.exists(cmake_exe_path):
return cmake_exe_path

def generate(self):
"""
This method will save the generated files to the conanfile.generators_folder
Expand Down Expand Up @@ -215,7 +227,7 @@ def generate(self):
else:
cache_variables[name] = value

buildenv, runenv = None, None
buildenv, runenv, cmake_executable = None, None, None

if self._conanfile.conf.get("tools.cmake.cmaketoolchain:presets_environment", default="",
check_type=str, choices=("disabled", "")) != "disabled":
Expand All @@ -228,8 +240,11 @@ def generate(self):
runenv = {name: value for name, value in
run_env.items(variable_reference="$penv{{{name}}}")}

cmake_executable = self._conanfile.conf.get("tools.cmake:cmake_program", None) or self._find_cmake_exe()

write_cmake_presets(self._conanfile, toolchain, self.generator, cache_variables,
self.user_presets_path, self.presets_prefix, buildenv, runenv)
self.user_presets_path, self.presets_prefix, buildenv, runenv,
cmake_executable)

def _get_generator(self, recipe_generator):
# Returns the name of the generator to be used by CMake
Expand Down
57 changes: 57 additions & 0 deletions conans/test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,3 +1222,60 @@ def test_presets_njobs():
c.run('install . -g CMakeToolchain -c tools.build:jobs=42')
presets = json.loads(c.load("CMakePresets.json"))
assert presets["buildPresets"][0]["jobs"] == 42


def test_add_cmakeexe_to_presets():
c = TestClient()

tool = textwrap.dedent(r"""
import os
from conan import ConanFile
from conan.tools.files import chdir, save
class Tool(ConanFile):
name = "cmake"
version = "3.27"
settings = "os", "compiler", "arch", "build_type"
def package(self):
with chdir(self, self.package_folder):
save(self, "bin/{}", "")
""")

profile = textwrap.dedent("""
include(default)
[platform_tool_requires]
cmake/3.27
""")

consumer = textwrap.dedent("""
[tool_requires]
cmake/3.27
[layout]
cmake_layout
""")

cmake_exe = "cmake.exe" if platform.system() == "Windows" else "cmake"

c.save({"tool.py": tool.format(cmake_exe),
"conanfile.txt": consumer,
"myprofile": profile})
c.run("create tool.py")
c.run("install . -g CMakeToolchain -g CMakeDeps")

presets_path = os.path.join("build", "Release", "generators", "CMakePresets.json") \
if platform.system() != "Windows" else os.path.join("build", "generators", "CMakePresets.json")
presets = json.loads(c.load(presets_path))

assert cmake_exe == os.path.basename(presets["configurePresets"][0].get("cmakeExecutable"))

# if we set "tools.cmake:cmake_program" that will have preference
c.run("install . -g CMakeToolchain -g CMakeDeps -c tools.cmake:cmake_program='/other/path/cmake'")
presets = json.loads(c.load(presets_path))

assert '/other/path/cmake' == presets["configurePresets"][0].get("cmakeExecutable")

# if we have a platform_tool_requires it will not be set because it is filtered before
# so it will not be in direct_build dependencies
c.run("install . -g CMakeToolchain -g CMakeDeps -pr:h=./myprofile")

presets = json.loads(c.load(presets_path))
assert presets["configurePresets"][0].get("cmakeExecutable") is None