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

Add ability to detect compiler language standards as features #1715

Closed
xq114 opened this issue Sep 28, 2021 · 4 comments
Closed

Add ability to detect compiler language standards as features #1715

xq114 opened this issue Sep 28, 2021 · 4 comments

Comments

@xq114
Copy link
Contributor

xq114 commented Sep 28, 2021

你在什么场景下需要该功能?

#1712

cmake的target_compile_features支持high level的standard探测和low level的具体feature探测,xmake只支持后者

https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html

另外cmake还支持cuda的standard探测,这个也可以加上,命名可以为cuda_cxx_std_14cuda_cxx_std_17这样

描述可能的解决方案

discussion里面说用flag探测,但这个并不行,例如msvc没有-std:c++11这个flag(最低就是c++14,默认c++17),但探测是否支持c++11应该返回“是”。可以根据编译器版本判断,也可以借助__cplusplus宏来判断。

@waruqi
Copy link
Member

waruqi commented Sep 29, 2021

加上了

configvar_check_features("HAS_CXX_STD_98", "cxx_std_98")
configvar_check_features("HAS_CXX_STD_11", "cxx_std_11", {languages = "c++11"})
configvar_check_features("HAS_CXX_STD_14", "cxx_std_14", {languages = "c++14"})
configvar_check_features("HAS_CXX_STD_17", "cxx_std_17", {languages = "c++17"})
configvar_check_features("HAS_CXX_STD_20", "cxx_std_20", {languages = "c++20"})
configvar_check_features("HAS_C_STD_89", "c_std_89")
configvar_check_features("HAS_C_STD_99", "c_std_99")
configvar_check_features("HAS_C_STD_11", "c_std_11", {languages = "c11"})
configvar_check_features("HAS_C_STD_17", "c_std_17", {languages = "c17"})

cuda 的我没环境,并且不同平台根据 ccbin 探测方式不同,懒得弄了,先这样吧。。

或者可以参考
https://github.com/xmake-io/xmake/tree/dev/xmake/modules/detect/tools/clang
https://github.com/xmake-io/xmake/tree/dev/xmake/modules/detect/tools/gcc
https://github.com/xmake-io/xmake/tree/dev/xmake/modules/detect/tools/cl

提个pr过来,加到 https://github.com/xmake-io/xmake/tree/dev/xmake/modules/detect/tools/nvcc

@waruqi waruqi added this to the v2.5.8 milestone Sep 29, 2021
@xq114
Copy link
Contributor Author

xq114 commented Sep 29, 2021

#1702

这里对option的编译器探测似乎没有好的途径,只能通过

check_snippets("gcc", [[#ifndef __GNUC__ // _MSC_VER, __clang__, __EMSCRIPTEN__, etc
#error
#endif]])

这种来进行,有点复杂;能否提供一个编译器探测功能?

  1. 作为feature提供,configvar_check_features("GCC", "vendor_gcc")这样;
  2. 类似autoconf,提供一个check_decls/configvar_check_decls函数检测declarations,实现也很简单,autoconf的实现是
-- check_decls("...", "decl")
check_snippets("...", [[
void test() {
#ifndef decl
  void(decl);
#endif
}]])

这样定义可以是宏、函数、结构体、全局变量等都行。这样编译器检测可以用

configvar_check_decls("GCC", "__GNUC__")

也很简单

@waruqi
Copy link
Member

waruqi commented Sep 29, 2021

作为feature提供,configvar_check_features("GCC", "vendor_gcc")这样;

是否存在编译器跟 feature 似乎不着边

类似autoconf,提供一个check_decls/configvar_check_decls函数检测declarations,实现也很简单,autoconf的实现是

虽然简单通用,但是 decls 去检测 宏,函数 和类型对用户来讲有歧义,明明叫 decls 却去检测 macros,对于初学者压根想不到

还不如一个api 干一件事情,简单明了,而且目前 check_xxx 已经有专门对 types/funcs 等检测了,就没必要都混到这个接口去。。

直接加个 configvar_check_macros() 专门检测宏定义的就行了。。

检测宏是否定义

configvar_check_macros("HAS_GCC", "__GNUC__")

检测宏没有被定义

configvar_check_macros("NO_GCC", "__GNUC__", {defined = false})

检测宏条件

configvar_check_macros("HAS_CXX20", "__cplusplus >= 202002L", {languages = "c++20"})

@waruqi
Copy link
Member

waruqi commented Sep 29, 2021

@waruqi waruqi closed this as completed Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants