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

GH-45325: [C++] Improved error message when using clang++ without clang #45326

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

WillAyd
Copy link
Contributor

@WillAyd WillAyd commented Jan 21, 2025

Rationale for this change

This resolves a rather cryptic error message encountered when trying to compile with clang++

What changes are included in this PR?

Changing the CMake configuration that adds flags to only add clang-specific command line arguments when clang is being used alongside clang++

Are these changes tested?

Locally yes

Are there any user-facing changes?

No - this is for developers

Copy link

⚠️ GitHub issue #45325 has been automatically assigned in GitHub to PR creator.

@github-actions github-actions bot added the awaiting review Awaiting review label Jan 21, 2025
@WillAyd WillAyd force-pushed the fix-clang++-compile branch from cec542b to bd6aa35 Compare January 21, 2025 23:18
@WillAyd WillAyd changed the title GH-45325: [C++] Fix compilation issue with clang++ without clang GH-45325: [C++] Improved error message when using clang++ without clang Jan 21, 2025
Copy link
Member

@assignUser assignUser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the fix is correct but given that this is an issue that was completely resolved in ccache 3.4 in 2018 (ccache/ccache#188) we should be good just removing that entire thing!

@github-actions github-actions bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Jan 21, 2025
@assignUser
Copy link
Member

(my comment was about the earlier version of the fix ^^)

@WillAyd
Copy link
Contributor Author

WillAyd commented Jan 21, 2025

Yea thanks. We can also remove that and that helps with the configure stage, but I noticed that the program still fails to compile when you mix compilers

@WillAyd WillAyd force-pushed the fix-clang++-compile branch 2 times, most recently from dd6538b to 51e2039 Compare January 21, 2025 23:42
Comment on lines 26 to 30
if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(UsingClang TRUE)
else()
set(UsingClang FALSE)
endif()

# Check for Clang C++ compiler
if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(UsingClangPlusPlus TRUE)
else()
set(UsingClangPlusPlus FALSE)
endif()

if(${UsingClangPlusPlus} AND NOT ${UsingClang})
message(FATAL_ERROR "When using clang++ you must also use clang")
endif()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use if(NOT CMAKE_C_COMPILER_ID STREQUAL CMAKE_CXX_COMPILER_ID)?

Copy link
Member

@assignUser assignUser Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah that's nice:

CC=gcc CXX=clang++ cmake .                                      
-- The C compiler identification is GNU 13.3.0
-- The CXX compiler identification is Clang 18.1.3
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:6 (message):
  Use same compiler for C and C++

@github-actions github-actions bot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Jan 21, 2025
@WillAyd WillAyd force-pushed the fix-clang++-compile branch from 51e2039 to fd0e85a Compare January 22, 2025 01:50
@github-actions github-actions bot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Jan 22, 2025
@@ -23,6 +23,12 @@ include(CheckCXXSourceCompiles)

message(STATUS "System processor: ${CMAKE_SYSTEM_PROCESSOR}")

if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this check?
In general, we can't mix C/C++ compilers from different vendores, right?
(Can we mix g++ and clang?)

Copy link
Contributor Author

@WillAyd WillAyd Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea mixing g++ and clang is the problem, or vice versa. If you just set -DCMAKE_CXX_COMPILER=clang++ then clang++ will be used for cpp files and gcc will be used for c (assuming gcc is the default)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but wouldn't the following catch that?
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL CMAKE_C_COMPILER_ID)
Why only perform the check if the CMAKE_CXX_COMPILER_ID is clang?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see what you are saying. I don't know CMake well enough to know if those labels are always expected to be the same. Happy to make that change.

This originally started by locating the "bug" in SetupCxxFlags.cmake, hence the condition in the first place

@github-actions github-actions bot added awaiting changes Awaiting changes and removed awaiting change review Awaiting change review labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants