Skip to content

Commit

Permalink
Do not pass compiler executable for AppleClang if it is the default (#…
Browse files Browse the repository at this point in the history
…592)

* Do not pass compiler executable for AppleClang if it is the default
  • Loading branch information
jcar87 authored Nov 17, 2023
1 parent bd279af commit c53fbf5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/cmake_conan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
uses: actions/[email protected]
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
Expand Down
17 changes: 16 additions & 1 deletion conan_provider.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions tests/resources/autotools_dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions tests/resources/autotools_dependency/conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[requires]
helloautotools/0.1
20 changes: 19 additions & 1 deletion tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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"""
Expand Down

0 comments on commit c53fbf5

Please sign in to comment.