Skip to content

Commit

Permalink
custom user presets
Browse files Browse the repository at this point in the history
  • Loading branch information
memsharded committed Jan 22, 2025
1 parent 936c9f5 commit 79be150
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions conan/internal/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"tools.cmake.cmaketoolchain:presets_environment": "String to define wether to add or not the environment section to the CMake presets. Empty by default, will generate the environment section in CMakePresets. Can take values: 'disabled'.",
"tools.cmake.cmaketoolchain:extra_variables": "Dictionary with variables to be injected in CMakeToolchain (potential override of CMakeToolchain defined variables)",
"tools.cmake.cmaketoolchain:enabled_blocks": "Select the specific blocks to use in the conan_toolchain.cmake",
"tools.cmake.cmaketoolchain:user_presets": "Select a different name instead of CMakeUserPresets.json, empty to disable",
"tools.cmake.cmake_layout:build_folder_vars": "Settings and Options that will produce a different build folder and different CMake presets names",
"tools.cmake.cmake_layout:build_folder": "(Experimental) Allow configuring the base folder of the build for local builds",
"tools.cmake.cmake_layout:test_folder": "(Experimental) Allow configuring the base folder of the build for test_package",
Expand Down
4 changes: 3 additions & 1 deletion conan/tools/cmake/toolchain/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ def generate(self):

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

user_presets = self._conanfile.conf.get("tools.cmake.cmaketoolchain:user_presets",
default=self.user_presets_path)
write_cmake_presets(self._conanfile, toolchain_file, self.generator, cache_variables,
self.user_presets_path, self.presets_prefix, buildenv, runenv,
user_presets, self.presets_prefix, buildenv, runenv,
cmake_executable, self.absolute_paths)

def _get_generator(self, recipe_generator):
Expand Down
31 changes: 24 additions & 7 deletions test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ class Conan(ConanFile):
client.save({"conanfile.py": conanfile})
return client


def test_runtime_lib_dirs_single_conf(lib_dir_setup):
client = lib_dir_setup
generator = ""
Expand Down Expand Up @@ -1537,6 +1538,23 @@ def layout(self):
assert os.path.isabs(presets["configurePresets"][0]["toolchainFile"])


def test_customize_cmakeuserpresets():
# https://github.com/conan-io/conan/issues/15639
c = TestClient()
c.save({"conanfile.py": GenConanfile(),
"CMakeLists.txt": ""})
c.run("install . -g CMakeToolchain -of=build")
old = c.load("CMakeUserPresets.json")
os.remove(os.path.join(c.current_folder, "CMakeUserPresets.json"))

c.run("install . -g CMakeToolchain -of=build -c tools.cmake.cmaketoolchain:user_presets=my.json")
new = c.load("my.json")
assert old == new
assert not os.path.exists(os.path.join(c.current_folder, "CMakeUserPresets.json"))
c.run("install . -g CMakeToolchain -of=build -c tools.cmake.cmaketoolchain:user_presets=")
assert not os.path.exists(os.path.join(c.current_folder, "CMakeUserPresets.json"))


def test_output_dirs_gnudirs_local_default():
# https://github.com/conan-io/conan/issues/14733
c = TestClient()
Expand Down Expand Up @@ -1634,7 +1652,7 @@ def test_toolchain_extra_variables():
# Test input from command line passing dict between doble quotes
client.run(textwrap.dedent(r"""
install . -c tools.cmake.cmaketoolchain:extra_variables="{'CMAKE_GENERATOR_INSTANCE': '${GENERATOR_INSTANCE}/buildTools/', 'FOO': 42.2, 'DICT': {'value': 1}, 'CACHE_VAR': {'value': 'hello world', 'cache': True, 'type': 'BOOL', 'docstring': 'test variable'}}"
""")
""")
)

toolchain = client.load("conan_toolchain.cmake")
Expand All @@ -1643,32 +1661,31 @@ def test_toolchain_extra_variables():
assert 'set(DICT 1)' in toolchain
assert 'set(CACHE_VAR "hello world" CACHE BOOL "test variable")' in toolchain


client.run(textwrap.dedent("""
install . -c tools.cmake.cmaketoolchain:extra_variables="{'myVar': {'value': 'hello world', 'cache': 'true'}}"
""") , assert_error=True)
"""), assert_error=True)
assert 'tools.cmake.cmaketoolchain:extra_variables "myVar" "cache" must be a boolean' in client.out

# Test invalid force
client.run(textwrap.dedent("""
install . -c tools.cmake.cmaketoolchain:extra_variables="{'myVar': {'value': 'hello world', 'force': True}}"
""") , assert_error=True)
"""), assert_error=True)
assert 'tools.cmake.cmaketoolchain:extra_variables "myVar" "force" is only allowed for cache variables' in client.out

client.run(textwrap.dedent("""
install . -c tools.cmake.cmaketoolchain:extra_variables="{'myVar': {'value': 'hello world', 'cache': True, 'force': 'true'}}"
""") , assert_error=True)
"""), assert_error=True)
assert 'tools.cmake.cmaketoolchain:extra_variables "myVar" "force" must be a boolean' in client.out

# Test invalid cache variable
client.run(textwrap.dedent("""
install . -c tools.cmake.cmaketoolchain:extra_variables="{'myVar': {'value': 'hello world', 'cache': True}}"
""") , assert_error=True)
"""), assert_error=True)
assert 'tools.cmake.cmaketoolchain:extra_variables "myVar" needs "type" defined for cache variable' in client.out

client.run(textwrap.dedent("""
install . -c tools.cmake.cmaketoolchain:extra_variables="{'myVar': {'value': 'hello world', 'cache': True, 'type': 'INVALID_TYPE'}}"
""") , assert_error=True)
"""), assert_error=True)
assert 'tools.cmake.cmaketoolchain:extra_variables "myVar" invalid type "INVALID_TYPE" for cache variable. Possible types: BOOL, FILEPATH, PATH, STRING, INTERNAL' in client.out

client.run(textwrap.dedent("""
Expand Down

0 comments on commit 79be150

Please sign in to comment.