diff --git a/conan_provider.cmake b/conan_provider.cmake index 1d3ca4c4..2e7b51ad 100644 --- a/conan_provider.cmake +++ b/conan_provider.cmake @@ -210,6 +210,30 @@ function(conan_install) # success set_property(GLOBAL PROPERTY CONAN_INSTALL_SUCCESS TRUE) endif() + + # set tool path + set(CONAN_TOOL_PATH) + string(JSON NUMBER_OF_NODES LENGTH ${conan_stdout} graph nodes) + math(EXPR NUMBER_OF_NODES "${NUMBER_OF_NODES}-1") + foreach(INDEX RANGE ${NUMBER_OF_NODES}) + string(JSON CONTEXT GET ${conan_stdout} graph nodes ${INDEX} context) + if(NOT CONTEXT STREQUAL "build") + continue() + endif() + + string(JSON BINDIRS GET ${conan_stdout} graph nodes ${INDEX} cpp_info root bindirs) + string(JSON BINDIRS_NODES LENGTH ${BINDIRS}) + if(BINDIRS_NODES EQUAL 0) + continue() + endif() + math(EXPR BINDIRS_NODES "${BINDIRS_NODES}-1") + foreach(BINDIRS_INDEX RANGE ${BINDIRS_NODES}) + string(JSON BINDIR GET ${BINDIRS} ${BINDIRS_INDEX}) + list(APPEND CONAN_TOOL_PATH "${BINDIR}") + endforeach() + endforeach() + set(CONAN_TOOL_PATH ${CONAN_TOOL_PATH} CACHE STRING "Path to build tools") + message(STATUS "CMake-Conan: CONAN_TOOL_PATH=${CONAN_TOOL_PATH}") endfunction() diff --git a/tests/resources/tool/CMakeLists.txt b/tests/resources/tool/CMakeLists.txt new file mode 100644 index 00000000..95447119 --- /dev/null +++ b/tests/resources/tool/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.24) +project(MyToolApp CXX) + +set(CMAKE_CXX_STANDARD 17) +find_package(hello REQUIRED) +find_program( + NINJA_EXECUTABLE ninja + PATHS ${CONAN_TOOL_PATH} REQUIRED + NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH) diff --git a/tests/resources/tool/conanfile.txt b/tests/resources/tool/conanfile.txt new file mode 100644 index 00000000..804a4492 --- /dev/null +++ b/tests/resources/tool/conanfile.txt @@ -0,0 +1,5 @@ +[requires] +hello/0.1 + +[tool_requires] +ninja/1.11.1 diff --git a/tests/test_smoke.py b/tests/test_smoke.py index 3afff32c..7f6d1794 100644 --- a/tests/test_smoke.py +++ b/tests/test_smoke.py @@ -1,4 +1,5 @@ import os +import re import platform import shutil import subprocess @@ -134,6 +135,26 @@ def test_reconfigure_on_conanfile_changes(self, capfd, chdir_build): assert all(expected in out for expected in expected_conan_install_outputs) +class TestTool: + @pytest.fixture(scope="class", autouse=True) + def tool_setup(self): + "Layout for tool test" + src_dir = Path(__file__).parent.parent + shutil.copytree(src_dir / 'tests' / 'resources' / 'tool', ".", dirs_exist_ok=True) + yield + + @unix + def test_tool_path(self, capfd, chdir_build): + "Tools are available in CMake" + generator = "-GNinja" if platform.system() == "Windows" else "" + + run(f"cmake --fresh .. -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=conan_provider.cmake -DCMAKE_BUILD_TYPE=Release {generator}") + out, _ = capfd.readouterr() + regex = re.compile(r".*--CMake-Conan:CONAN_TOOL_PATH=\/.*\/bin.*") + out_without_whitespace = "".join(out.split()) + assert regex.match(out_without_whitespace) + + class TestSubdir: @pytest.fixture(scope="class", autouse=True) def subdir_setup(self):