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

在generating.module.deps时缺少c++标准的设置 #3873

Closed
24bit-xjkp opened this issue Jun 21, 2023 · 23 comments
Closed

在generating.module.deps时缺少c++标准的设置 #3873

24bit-xjkp opened this issue Jun 21, 2023 · 23 comments
Labels

Comments

@24bit-xjkp
Copy link
Contributor

Xmake 版本

xmake v2.7.9+dev.8b9228762

操作系统版本和架构

Windows 11 22H2

描述问题

在generating.module.deps时没有设置-std=c++23

[  0%]: generating.module.deps MyDetail\MyUtility.ixx
clang -Qunused-arguments -m64 --target=x86_64-w64-mingw32 -Wall -Wextra -DUNICODE -D_UNICODE -D_DLL -DDEBUG -D_DEBUG -DMyStdCStandardSupport -fexceptions -fcxx-exceptions -march=native -E -x c++ MyDetail\MyUtility.ixx -o build\.gens\MyDetail\windows\x64\Debug\rules\modules\cache\c6251745\MyUtility.ixx.i

在编译的时候正确设置了

[  9%]: compiling.module.Debug MyStd.MyUtility
clang -c -x c++-module -fmodule-output=build\.gens\MyDetail\windows\x64\Debug\rules\modules\cache\c6251745\MyStd.MyUtility.pcm -Qunused-arguments -m64 --target=x86_64-w64-mingw32 -Wall -Wextra -std=c++23 -DUNICODE -D_UNICODE -D_DLL -DDEBUG -D_DEBUG -DMyStdCStandardSupport -fexceptions -fcxx-exceptions -march=native -fmodules-cache-path=build\.gens\MyDetail\windows\x64\Debug\rules\modules\cache -o build\.objs\MyDetail\windows\x64\Debug\MyDetail\MyUtility.ixx.obj MyDetail\MyUtility.ixx

但如果我在MyUtility.ixx中对c++标准进行检测就会出错

#if defined(__clang__) || defined(__GNUC__)
#if !(__cplusplus >= 202100L)
#error The compiler does not support C++2b Standard.\
       So it cannot compile this library.
#endif
#elif defined(_MSVC_LANG)
#if !(_MSVC_LANG >= 202004L)
#error The compiler does not support C++2b Standard.\
       So it cannot compile this library.
#endif
#else
#error We do not know the current C++ compiler.\
       Please use msvc/g++/clang++ instead.
#endif
#ifndef __has_include
#error The compiler does not support C++ feature "__has_include"\
       So it cannot compile this library.
#endif
#ifndef __has_cpp_attribute
#error The compiler does not support C++ feature "__has_cpp_attribute"\
       So it cannot compile this library.
#endif
#ifndef __cpp_if_consteval
#error The compiler does not support C++ feature "consteval if"\
       So it cannot compile this library.
#endif
#ifndef __cpp_lib_unreachable
#error The compiler does not support C++ feature "std::unreachable()"\
       So it cannot compile this library.
#endif

编译时出现

[  0%]: generating.module.deps MyDetail\MyUtility.ixx
clang -Qunused-arguments -m64 --target=x86_64-w64-mingw32 -Wall -Wextra -DUNICODE -D_UNICODE -D_DLL -DDEBUG -D_DEBUG -DMyStdCStandardSupport -fexceptions -fcxx-exceptions -march=native -E -x c++ MyDetail\MyUtility.ixx -o build\.gens\MyDetail\windows\x64\Debug\rules\modules\cache\c6251745\MyUtility.ixx.i
In file included from MyDetail\MyUtility.ixx:2:
MyDetail/CppStandardRequire.hpp:7:2: error: The compiler does not support C++2b Standard. So it cannot compile this library.
    7 | #error The compiler does not support C++2b Standard.\
      |  ^
MyDetail/CppStandardRequire.hpp:28:2: error: The compiler does not support C++ feature "consteval if" So it cannot compile this library.
   28 | #error The compiler does not support C++ feature "consteval if"\
      |  ^
MyDetail/CppStandardRequire.hpp:32:2: error: The compiler does not support C++ feature "std::unreachable()" So it cannot compile this library.
   32 | #error The compiler does not support C++ feature "std::unreachable()"\
      |  ^
3 errors generated.

期待的结果

在generating.module.deps时也应该设置c++标准

工程配置

No response

附加信息和错误日志

clang version 17.0.0 (https://github.com/llvm/llvm-project.git 5e9173c43a9b97c8614e36d6f754317f731e71e9)
Target: x86_64-w64-windows-msvc
Thread model: posix
InstalledDir: D:/clang/bin
@24bit-xjkp 24bit-xjkp added the bug label Jun 21, 2023
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Title: Lack of C++ standard settings when generating.module.deps

@waruqi
Copy link
Member

waruqi commented Jun 28, 2023

不能加,故意删掉的,通过 -E 预编译去生成依赖图,添加 -std=c++ 会导致 clang 去找 bmi 模块,会报错 模块找不到,因为 bmi 还没被编译。连模块依赖扫描都过不了。

test/test.cpp:3:8: fatal error: module 'my_module' not found
import my_module;

1 error generated.

相关代码和注释:

exclude -fmodule* and -std=c++/gnu++* flags because,
when they are set clang try to find bmi of imported modules but they don't exists a this point of compilation

-- exclude -fmodule* and -std=c++/gnu++* flags because, when they are set clang try to find bmi of imported modules but they don't exists a this point of compilation
table.remove_if(compflags, function(_, flag)
return flag:startswith("-fmodule") or flag:startswith("-std=c++") or flag:startswith("-std=gnu++")
end)

除非使用最新版本的 clang 并且带有 clang-scan-deps,让 xmake 尝试用 clang-scan-deps 去生成模块依赖图,就没有这个问题,如果没 clang-scan-deps, xmake 会 fallback 到内置的模块依赖图实现,走的 clang -E 预编译,有限制。

然而 clang 的 clang-scan-deps 目前对 c++modules 支持不是很好,headerunit 什么的 也没很好支持。。所以你可以尝试,也可以耐心等待 clang 后续版本对 c++modules 的改进。

@24bit-xjkp
Copy link
Contributor Author

除非使用最新版本的 clang 并且带有 clang-scan-deps,让 xmake 尝试用 clang-scan-deps 去生成模块依赖图,就没有这个问题,如果没 clang-scan-deps, xmake 会 fallback 到内置的模块依赖图实现,走的 clang -E 预编译,有限制。

我用的clang17是有clang-scan-deps.exe的,不过xmake并没有去调用。这里需要加什么设置么?

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Unless you use the latest version of clang with clang-scan-deps, let xmake try to use clang-scan-deps to generate the module dependency graph, there will be no such problem, if there is no clang-scan-deps, xmake will fallback to the built-in Module dependency graph implementation, pre-compiled by clang -E, has limitations.

The clang17 I used has clang-scan-deps.exe, but xmake did not call it. Do I need to add any settings here?

@waruqi
Copy link
Member

waruqi commented Jun 28, 2023

你调下这里,看下为啥没进去。。

if has_clangscandepssupport(target) and not target:policy("build.c++.clang.fallbackscanner") then

function has_clangscandepssupport(target)

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


You tune down here and see why you didn't go in. .

if has_clangscandepssupport(target) and not target:policy("build.c++.clang.fallbackscanner") then

function has_clangscandepssupport(target)

@24bit-xjkp
Copy link
Contributor Author

不能加,故意删掉的,通过 -E 预编译去生成依赖图,添加 -std=c++ 会导致 clang 去找 bmi 模块,会报错 模块找不到,因为 bmi 还没被编译。连模块依赖扫描都过不了。

我用clang并没有出问题。

clang -Qunused-arguments -m64 -g -Wall -Wextra -DUNICODE -D_UNICODE -D_DLL -DDEBUG -D_DEBUG -DMyStdCStandardSupport -fexceptions -fcxx-exceptions -march=native -E -x c++ MyDetail\MyMemory.ixx -o build\.gens\MyDetail\windows\x64\Debug\rules\modules\cache\c6251745\MyMemory.ixx.i -std=c++23

这个可以正常编译,而且我当中也使用了其他模块

# 1 "D:/gcc/include/sec_api/crtdbg_s.h" 1 3
# 10 "D:/gcc/include/sec_api/crtdbg_s.h" 3
# 1 "D:/gcc/include/crtdbg.h" 1 3
# 11 "D:/gcc/include/sec_api/crtdbg_s.h" 2 3
# 225 "D:/gcc/include/crtdbg.h" 2 3
# 24 "MyDetail\\MyMemory.ixx" 2
# 36 "MyDetail\\MyMemory.ixx"
export module MyStd.MyMemory;
import MyStd.MyError;
import MyStd.MyUtility;

clang是最新的开发版

clang version 17.0.0 (https://github.com/llvm/llvm-project.git 5e9173c43a9b97c8614e36d6f754317f731e71e9)

@24bit-xjkp
Copy link
Contributor Author

你调下这里,看下为啥没进去。。

看起来探测的路径错了

checkinfo: cannot runv(.\clang-scan-deps.exe --version), No such file or directory
checking for .\clang-scan-deps ... no

这个clang-scan-deps在D:/clang/bin下,.\这个路径应该不对。

waruqi added a commit that referenced this issue Jun 30, 2023
@waruqi
Copy link
Member

waruqi commented Jun 30, 2023

你调下这里,看下为啥没进去。。

看起来探测的路径错了

checkinfo: cannot runv(.\clang-scan-deps.exe --version), No such file or directory
checking for .\clang-scan-deps ... no

这个clang-scan-deps在D:/clang/bin下,.\这个路径应该不对。

再试试,xmake update -s dev

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


You tune down here to see why you didn't go in. .

Looks like the probed path is wrong

checkinfo: cannot runv(.\clang-scan-deps.exe --version), No such file or directory
checking for .\clang-scan-deps ... no

This clang-scan-deps is under D:/clang/bin, the path of .\ should be wrong.

Try again, xmake update -s dev

@waruqi
Copy link
Member

waruqi commented Jun 30, 2023

不能加,故意删掉的,通过 -E 预编译去生成依赖图,添加 -std=c++ 会导致 clang 去找 bmi 模块,会报错 模块找不到,因为 bmi 还没被编译。连模块依赖扫描都过不了。

我用clang并没有出问题。

clang -Qunused-arguments -m64 -g -Wall -Wextra -DUNICODE -D_UNICODE -D_DLL -DDEBUG -D_DEBUG -DMyStdCStandardSupport -fexceptions -fcxx-exceptions -march=native -E -x c++ MyDetail\MyMemory.ixx -o build\.gens\MyDetail\windows\x64\Debug\rules\modules\cache\c6251745\MyMemory.ixx.i -std=c++23

这个可以正常编译,而且我当中也使用了其他模块

# 1 "D:/gcc/include/sec_api/crtdbg_s.h" 1 3
# 10 "D:/gcc/include/sec_api/crtdbg_s.h" 3
# 1 "D:/gcc/include/crtdbg.h" 1 3
# 11 "D:/gcc/include/sec_api/crtdbg_s.h" 2 3
# 225 "D:/gcc/include/crtdbg.h" 2 3
# 24 "MyDetail\\MyMemory.ixx" 2
# 36 "MyDetail\\MyMemory.ixx"
export module MyStd.MyMemory;
import MyStd.MyError;
import MyStd.MyUtility;

clang是最新的开发版

clang version 17.0.0 (https://github.com/llvm/llvm-project.git 5e9173c43a9b97c8614e36d6f754317f731e71e9)

我暂时没这个版本 clang 16 我这不行

@24bit-xjkp
Copy link
Contributor Author

xmake update -s dev后正确调用了clang-scan-deps

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


clang-scan-deps is called correctly after xmake update -s dev

@24bit-xjkp
Copy link
Contributor Author

我发现了预编译头和模块混用时有一个顺序问题

[  0%]: generating.module.deps Test\src\test.cpp
[  0%]: generating.module.deps MyContainer\MySimd.ixx
[  0%]: generating.module.deps MyDetail\MyError.ixx
[  0%]: generating.module.deps MyDetail\MyMemory.ixx
[  0%]: generating.module.deps MyDetail\MyUtility.ixx
[  0%]: generating.module.deps MyDetail\TypeConversions.ixx
[  0%]: compiling.Debug MyDetail\BasicDefinitions.hpp
[  5%]: compiling.module.Debug MyStd.MyUtility
[  5%]: compiling.module.Debug MyStd.TypeConversions
[ 17%]: compiling.module.Debug MyStd.MyError
[ 23%]: compiling.module.Debug MyStd.MyMemory
[ 47%]: compiling.module.Debug MyStd.MySimd
[ 82%]: compiling.Debug Test\src\test.cpp
[ 88%]: linking.Debug Test

这里先进行了generating,后编译了预编译头。若预编译头不存在则编译失败。
应该把编译头文件提前,因为头文件中不能使用模块,所以可以在generating前进行。

还有一个问题,传递给clang-scan-deps的参数漏了预编译头

-- MyDetail/xmake.lua
target("MyDetail")
    set_kind("object")
    set_pcxxheader("BasicDefinitions.hpp", {public = true})
    add_files("*.ixx")
target_end()
-- Test/xmake.lua
target("Test")
    set_kind("binary")
    add_files("src/test.cpp")
    add_deps("MyDetail")
target_end()

构建时出现如下错误

clang-scan-deps --format=p1689 -- clang -x c++ -c Test\src\test.cpp -o build\.objs\Test\cross\none\Debug\Test\src\test.cpp.o -Qunused-arguments -g -Wall -Wextra -std=c++23 -DUNICODE -D_UNICODE -D_DLL -DDEBUG -D_DEBUG -DMyStdCStandardSupport -fexceptions -fcxx-exceptions -include MyDetail\BasicDefinitions.hpp -include-pch -march=native
error: Error while scanning dependencies for Test\src\test.cpp:
error: unable to read PCH file -march=native: 'No such file or directory'
fatal error: PCH file '-march=native' not found: module file not found

实际上-include-pch后面应该加上 build\.objs\MyDetail\cross\none\Debug\MyDetail\BasicDefinitions.hpp.pch,但这里漏了。这是否也和顺序错误有关?

@24bit-xjkp 24bit-xjkp reopened this Jul 2, 2023
@waruqi
Copy link
Member

waruqi commented Jul 2, 2023

目前模块不支持预编译头,另外都走模块了,再用预编译也没啥意义,提升不大,问题更多,原本模块各家编译器都支持还不完整。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


At present, the module does not support precompiled headers. In addition, it is a module. It is meaningless to use precompiled. The improvement is not big, and there are more problems.

@24bit-xjkp
Copy link
Contributor Author

另外都走模块了,再用预编译也没啥意义

这不是还没支持import头文件;的语法嘛,暂且只能用用预编译头了,不然还是每个模块预处理一次头文件。而且如果需要用到宏的话还是只能预编译头,保留还是有一定作用的。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


In addition, all modules are taken, and it is meaningless to use precompilation

Isn't it that the syntax of import header files is not yet supported? For the time being, only precompiled headers can be used, otherwise each module should preprocess the header files once. And if you need to use macros, you can only precompile the header, and reservation still has a certain effect.

@waruqi
Copy link
Member

waruqi commented Jul 2, 2023

另外都走模块了,再用预编译也没啥意义

这不是还没支持import头文件;的语法嘛,暂且只能用用预编译头了,不然还是每个模块预处理一次头文件。而且如果需要用到宏的话还是只能预编译头,保留还是有一定作用的。

headerunit ?。。我记得 gcc/clang/msvc 都支持了才对。。

https://github.com/xmake-io/xmake/tree/master/tests/projects/c%2B%2B/modules/user_headerunit

$ xmake f --toolchain=llvm --sdk=~/projects/working/qvmfix/llvm/llvm-
roject/llvm/build -c
checking for platform ... macosx
checking for architecture ... x86_64
checking for Xcode directory ... /Applications/Xcode.app
checking for SDK version of Xcode for macosx (x86_64) ... 13.0
ruki-2:user_headerunit ruki$ xmake -r
[  0%]: generating.module.deps src/main.cpp
[  0%]: generating.module.deps src/hello.mpp
[ 11%]: compiling.headerunit.release header.hpp
[ 33%]: compiling.module.release hello
[ 66%]: compiling.release src/main.cpp
[ 77%]: linking.release user_headerunit
[100%]: build ok, spent 0.688s

不过 clang 对 stl headerunit 似乎还没很好支持

@24bit-xjkp
Copy link
Contributor Author

headerunit ?。。我记得 gcc/clang/msvc 都支持了才对。

然而

[  0%]: generating.module.deps src\main.cpp
clang-scan-deps --format=p1689 -- clang -x c++ -c src\main.cpp -o build\.objs\user_headerunit\windows\x64\release\src\main.cpp.obj -Qunused-arguments -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -std=c++20 -fexceptions -fcxx-exceptions -DNDEBUG
error: Error while scanning dependencies for src\main.cpp:
src\main.cpp:2:8: error: header file "header.hpp" (aka 'src/header.hpp') cannot be imported because it is not known to be a header unit

编译的是xmake\tests\projects\c++\modules\user_headerunit。这么看我是不是还需要设置什么才能正常使用?

@waruqi
Copy link
Member

waruqi commented Jul 2, 2023

headerunit ?。。我记得 gcc/clang/msvc 都支持了才对。

然而

[  0%]: generating.module.deps src\main.cpp
clang-scan-deps --format=p1689 -- clang -x c++ -c src\main.cpp -o build\.objs\user_headerunit\windows\x64\release\src\main.cpp.obj -Qunused-arguments -m64 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -std=c++20 -fexceptions -fcxx-exceptions -DNDEBUG
error: Error while scanning dependencies for src\main.cpp:
src\main.cpp:2:8: error: header file "header.hpp" (aka 'src/header.hpp') cannot be imported because it is not known to be a header unit

编译的是xmake\tests\projects\c++\modules\user_headerunit。这么看我是不是还需要设置什么才能正常使用?

clang 的 clang-scan-deps 对 headerunit 支持不好。。。禁用就行了,我这不用 clang-scan-deps,就能正常编译

#3853 (comment)

@24bit-xjkp
Copy link
Contributor Author

你说得对,只能等待clang-scan-deps的改进了

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


You are right, can only wait for the improvement of clang-scan-deps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants