diff --git a/.github/workflows/cmake_conan.yml b/.github/workflows/cmake_conan.yml index 1ecfee0c..7b50a265 100644 --- a/.github/workflows/cmake_conan.yml +++ b/.github/workflows/cmake_conan.yml @@ -20,6 +20,9 @@ jobs: uses: actions/setup-python@v4.6.0 with: python-version: "3.10" + - name: Install autotools on macOS + run: brew install automake + if: ${{ matrix.os == 'macos-13' }} - name: Install Conan run: pip install conan pytest && conan --version - name: Setup CMake and Ninja diff --git a/conan_provider.cmake b/conan_provider.cmake index 1e50e69e..5229c307 100644 --- a/conan_provider.cmake +++ b/conan_provider.cmake @@ -265,24 +265,39 @@ function(detect_build_type BUILD_TYPE) endif() endfunction() +macro(set_conan_compiler_if_appleclang lang command output_variable) + if(CMAKE_${lang}_COMPILER_ID STREQUAL "AppleClang") + execute_process(COMMAND xcrun --find ${command} + OUTPUT_VARIABLE _xcrun_out OUTPUT_STRIP_TRAILING_WHITESPACE) + if (_xcrun_out STREQUAL "${CMAKE_${lang}_COMPILER}") + set(${output_variable} "") + endif() + unset(_xcrun_out) + endif() +endmacro() + macro(append_compiler_executables_configuration) set(_conan_c_compiler "") set(_conan_cpp_compiler "") if(CMAKE_C_COMPILER) set(_conan_c_compiler "\"c\":\"${CMAKE_C_COMPILER}\",") + set_conan_compiler_if_appleclang(C cc _conan_c_compiler) else() message(WARNING "CMake-Conan: The C compiler is not defined. " "Please define CMAKE_C_COMPILER or enable the C language.") endif() if(CMAKE_CXX_COMPILER) set(_conan_cpp_compiler "\"cpp\":\"${CMAKE_CXX_COMPILER}\"") + set_conan_compiler_if_appleclang(CXX c++ _conan_cpp_compiler) else() message(WARNING "CMake-Conan: The C++ compiler is not defined. " "Please define CMAKE_CXX_COMPILER or enable the C++ language.") endif() - string(APPEND PROFILE "tools.build:compiler_executables={${_conan_c_compiler}${_conan_cpp_compiler}}\n") + if(NOT "x${_conan_c_compiler}${_conan_cpp_compiler}" STREQUAL "x") + string(APPEND PROFILE "tools.build:compiler_executables={${_conan_c_compiler}${_conan_cpp_compiler}}\n") + endif() unset(_conan_c_compiler) unset(_conan_cpp_compiler) endmacro() diff --git a/tests/resources/autotools_dependency/CMakeLists.txt b/tests/resources/autotools_dependency/CMakeLists.txt new file mode 100644 index 00000000..c79c4a83 --- /dev/null +++ b/tests/resources/autotools_dependency/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.24) +project(MyApp CXX) + +set(CMAKE_CXX_STANDARD 17) + +find_package(helloautotools REQUIRED) +add_executable(app main.cpp) +target_link_libraries(app helloautotools::helloautotools) diff --git a/tests/resources/autotools_dependency/conanfile.txt b/tests/resources/autotools_dependency/conanfile.txt new file mode 100644 index 00000000..23e96015 --- /dev/null +++ b/tests/resources/autotools_dependency/conanfile.txt @@ -0,0 +1,2 @@ +[requires] +helloautotools/0.1 diff --git a/tests/test_smoke.py b/tests/test_smoke.py index 77963a2c..7174cdad 100644 --- a/tests/test_smoke.py +++ b/tests/test_smoke.py @@ -62,11 +62,17 @@ def setup_conan_home(conan_home_dir, tmp_path_factory): # Detect default profile run("conan profile detect -vquiet") - # Create hello lib from built-in template + # Create hello lib from built-in CMake template os.chdir(workdir.as_posix()) run("conan new cmake_lib -d name=hello -d version=0.1 -vquiet") run("conan export . -vquiet") + # Create hello-autootols from built-in autotools template + recipe_dir = tmp_path_factory.mktemp(f"temp_autotools") + os.chdir(recipe_dir.as_posix()) + run("conan new autotools_lib -d name=helloautotools -d version=0.1 -vquiet") + run("conan export . -vquiet") + # additional recipes to export from resources, overlay on top of `hello` and export additional_recipes = ['boost', 'bye', 'cmake-module-only', 'cmake-module-with-dependency'] @@ -324,6 +330,18 @@ def test_propagate_non_default_compiler(self, capfd, basic_cmake_project): assert "The C compiler is not defined." not in err assert 'tools.build:compiler_executables={"c":"/usr/bin/clang","cpp":"/usr/bin/clang++"}' in out + @darwin + def test_propagate_compiler_mac_autotools(self, capfd, basic_cmake_project): + """Test that if the compiler is inside an XCode installation, we don't + propagate the path if that's the compiler that would be found by default""" + source_dir, binary_dir = basic_cmake_project + shutil.copytree(src_dir / 'tests' / 'resources' / 'autotools_dependency', source_dir, dirs_exist_ok=True) + run(f"cmake -S {source_dir} -B {binary_dir} -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES={conan_provider} -DCMAKE_BUILD_TYPE=Release", check=False) + out, err = capfd.readouterr() + assert "checking for g++... g++" in err, err + assert "configure: error: C++ compiler cannot create executables" not in err + assert "-- Generating done" in out + class TestProfileCustomization: def test_profile_defaults(self, capfd, basic_cmake_project): """Test the defaults passed for host and build profiles"""