如何区分clang和clang-cl? [英] How to differentiate between clang and clang-cl?

查看:85
本文介绍了如何区分clang和clang-cl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CMake的新手,在这里遇到了一些麻烦,想知道是否有针对该问题的优雅"解决方案,或者只是一个简单的解决方案.

I'm new to CMake and I've run into a little bit of a snag here, wondering if there is an "elegant" solution to this problem, or maybe there's just an easy solution.

作为示例,为设置编译器标志,我正在执行以下操作:

As an example, for setting up compiler flags I'm doing the following:

target_compile_options(${PROJECT_NAME}
    PUBLIC
        $<$<CXX_COMPILER_ID:GNU>:"-some_gnu_flag">
        $<$<CXX_COMPILER_ID:Clang>:"-some_clang_flag">
        $<$<CXX_COMPILER_ID:MSVC>:"/some_msvc_flag">
)

除了我尝试使用clang-cl作为编译器进行构建时,即:

This works perfectly as expected, except for when I try a build using clang-cl as a compiler, ie:

cmake .. -G "Visual Studio 15 2017 Win64" -T "LLVM-vs2014"

CXX ID报告为Clang(毕竟它是 clang),但我不想使用Clang标志,实际上我想使用MSVC标志,因为clang-cl是设计好的将取代MSVC cl-因此只接受MSVC样式标志.

The CXX ID is reported as Clang (it is clang after all) but I don't want to be using Clang flags, I actually want to be using MSVC flags since clang-cl is designed to be a drop in replacement for MSVCs cl - and hence only accepts MSVC style flags.

那么,在不创建一些混乱代码的情况下,有哪些好的解决方案呢?我知道我可能可以进行一堆if()检查并设置一些变量,但是我试图遵循现代cmake"约定,因此为什么我要使用生成器表达式.

So what are some good solutions to this without creating some messy code? I know I could probably do a bunch of if() checks and set some variable, but I was trying to stick to "modern cmake" conventions, hence why I was using generator expressions to begin with.

推荐答案

这是我当前的解决方案".我找到了CMAKE_CXX_SIMULATE_ID变量,该变量可以保存我想要的信息-尽管这对我来说仍然有点脏.

This is my current "solution". I found the CMAKE_CXX_SIMULATE_ID variable which holds the information I want - although this still feels a bit dirty to me.

# Get compiler info
set(CXX_FLAGS_STYLE_GNU OFF)
set(CXX_FLAGS_STYLE_MSVC OFF)
set(CXX_FLAGS_STYLE_CLANGCL OFF)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    set(CXX_FLAGS_STYLE_GNU ON)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
    if ("${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
        set(CXX_FLAGS_STYLE_CLANGCL ON)
    else ()
        set(CXX_FLAGS_STYLE_GNU ON)
    endif ()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
    set(CXX_FLAGS_STYLE_MSVC ON)
else ()
    message(FATAL_ERROR "Unsupported compiler!")
endif ()

# Compiler flags
target_compile_options(${PROJECT_NAME}
    PRIVATE
        ## GCC/Clang Flags
        $<$<BOOL:${CXX_FLAGS_STYLE_GNU}>:"-someflag">
        ## MSVC flags
        $<$<BOOL:${CXX_FLAGS_STYLE_MSVC}>:"/someflag">
        ## CLANG-CL flags
        $<$<BOOL:${CXX_FLAGS_STYLE_CLANGCL}>:"-someflag">
)

这篇关于如何区分clang和clang-cl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆