使用 CMake 通过 NVCC 传递到 MSVC 的混淆标志

2024-03-24

我有一个 CMake 文件,用于在 Windows 上构建一些 CUDA (NVCC/MSVC)。我正在尝试将 MSVC 警告级别设置为/W4, using:

add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options=/W4>")

使用 CMake 3.9 构建时,我收到以下警告:

(CudaBuildCore target) ->
  cl : Command line warning D9025: overriding '/W4' with '/W3'

在 CMake 3.15 中,策略更改为不自动设置/W3在 CUDA 标志中,但使用该版本我得到:

(CudaBuildCore target) ->
  cl : Command line warning D9025: overriding '/W4' with '/W1'

如果我执行构建步骤--verbose我在 3.9 中看到以下内容:

  "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\nvcc.exe" -gencode=arch=compute_30,code=\"compute_30,compute_30\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64" -x cu  -IC:\Users\eddi\Documents\temp\thur\sw\include -IC:\Users\eddi\Documents\temp\thur\sw\shared\common\include -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include"     --keep-dir x64\Debug -maxrregcount=0  --machine 64 --compile -cudart static --compiler-options=/W4 --compiler-options= -Xcompiler="/EHsc -Zi -Ob0" -g   -D_WINDOWS -D"FISH_BUILD_TYPE=\"DEBUG\"" -D"CMAKE_INTDIR=\"Debug\"" -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Fdfish.dir\Debug\fish.pdb /FS /Zi /RTC1 /MDd /GR" -o fish.dir\Debug\fish_cuda.obj "C:\Users\eddi\Documents\temp\thur\sw\fish_cuda.cpp"

3.15:

  "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\nvcc.exe" -gencode=arch=compute_30,code=\"compute_30,compute_30\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64" -x cu  -IC:\Users\eddi\Documents\temp\thur\sw\include -IC:\Users\eddi\Documents\temp\thur\sw\shared\common\include -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include"     --keep-dir x64\Debug -maxrregcount=0  --machine 64 --compile -cudart static --compiler-options=/W4 --compiler-options= -Xcompiler="/EHsc -Zi -Ob0" -g   -D_WINDOWS -D"FISH_BUILD_TYPE=\"DEBUG\"" -D"CMAKE_INTDIR=\"Debug\"" -D"CMAKE_INTDIR=\"Debug\"" -D_MBCS -Xcompiler "/EHsc /W1 /nologo /Od /Fdfish.dir\Debug\fish.pdb /FS /Zi /RTC1 /MDd /GR" -o fish.dir\Debug\fish_cuda.obj "C:\Users\eddi\Documents\temp\thur\sw\fish_cuda.cpp"

剧透:除了-Xcompiler "/EHsc /W3 /nologo /Od /Fdfish.dir\Debug\fish.pdb /FS /Zi /RTC1 /MDd /GR"部分。

尽我所能,我找不到引入这组标志的位置(由 CMake 等人),因此无法开始研究如何改变行为。


编辑1:为故事增添色彩...... 我 grep 了我的 CMake 安装/nologo并发现了几个.cmake包含以下行的文件:

   35:   set(CMAKE_CL_NOLOGO "/nologo")

我将它们全部更改为

   35:   set(CMAKE_CL_NOLOGO "/nologo_EDD")

但它们都没有出现在令人困惑的选项集中。那么要么我错过了一些东西,要么它们是从 CMake 之外的东西引入的?


编辑2: @squareskittles 说:

您可以从 CMAKE_CXX_FLAGS_INIT 变量中删除默认标志

公平地说,我可以看到其中的逻辑:它很简单,并且直接解决了我眼前的问题。但这似乎有点生硬,并且没有首先说明标志的来源。

不管怎样,抛开反对意见不谈,我在我的脚本中添加了这个丑陋的东西:

message( "--------------------------- ${CMAKE_CXX_FLAGS_INIT}")
string (REGEX REPLACE "/W[0-4]" "" CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT}")
message( "--------------------------- ${CMAKE_CXX_FLAGS_INIT}")

使用 CMake 3.9,我看到:

---------------------------   /DWIN32 /D_WINDOWS /W3 /GR /EHsc
---------------------------   /DWIN32 /D_WINDOWS  /GR /EHsc

正如所希望的那样。但在 3.15 中,我看到:

---------------------------   /DWIN32 /D_WINDOWS /GR /EHsc
---------------------------   /DWIN32 /D_WINDOWS /GR /EHsc

ie, the /W3标志一开始就不存在(更不用说/W1标志)由于政策变化。但更糟糕的是,这两种情况仍然存在主要问题:

nvcc.exe ... --compiler-options=/W4 -Xcompiler="/EHsc -Zi -Ob0" ... -Xcompiler "/EHsc /W3 /nologo /Od /Fdfish.dir\Debug\fish.pdb /FS /Zi /RTC1 /MDd /GR" ...

EDIT3: 一些进展!通过这个简单的改变:

#add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options=/W4>")
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=/W4>")

没有更多的抱怨!令人困惑的标志仍然存在,但现在阅读:

-Xcompiler "/EHsc /W4 /nologo /Od /Ffish.dir\Debug\fish.pdb
  /FS /Zi /RTC1 /MDd /GR"

IE。 CMake 发现了/W4并覆盖其默认值。

但尽管搜索了 CMake 源代码,我仍然没有确定这一切是在哪里完成的。


EDIT4:MSVC 参与了这一切。 “神秘”选项是由 MSVC 从.vcxproj文件。如果我使用-Xcompile=/W4方法,项目 XML 包含:

<CudaCompile>
  ...
  <AdditionalOptions>%(AdditionalOptions) -Xcompiler="/EHsc -Zi -Ob1"</AdditionalOptions>
  ...
  <Warning>W4</Warning>
 </CudaCompile>

但有了原版,--compiler-options=/W4 I get:

<CudaCompile>
   <AdditionalOptions>%(AdditionalOptions) --compiler-options=/W4 -Xcompiler="/EHsc -Zi -Ob1"</AdditionalOptions>
   ...
</CudaCompile>

这一切都有道理;如果没有指定警告级别,MSVC 必须默认为 W1,因此会受到投诉。

另外,还有一个事后线索:构建步骤有:

-Xcompiler="/EHsc -Zi -Ob0"
-Xcompiler "/EHsc /W1 /nologo /Od ..."

注意一个有等于而一个没有implying他们来自不同的地方。

我认为我的调查目前已经足够了。


对我自己的一个问题的长篇大论的总结:

I had:

add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:--compiler-options=/W4>")

但发现:

add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=/W4>")

更合适。

基本上,CMake/NVCC/MSVC 管道了解-Xcompiler选项覆盖了默认值,但没有意识到--compiler-options是一个等效的语句,导致命令行中的指令不明确。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 CMake 通过 NVCC 传递到 MSVC 的混淆标志 的相关文章

随机推荐