即使 check_ipo_supported() 在 CMake 中工作,也不会设置INTERPROCEDURAL_OP



我在CMake 3.14.0中有一个项目,它为Visual Studio 2017 64位生成器构建了一个项目(最低版本为3.10.0,因为其他开发人员可以拥有以前版本的CMake,但大于3.9.0):

cmake_minimum_required (VERSION 3.10.0)
project (data)
add_definitions (-DDATA_EXPORTS)
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/..)
set (PROJECT_SRC
Player.cpp
LLA.cpp
Attitude.cpp
)
add_library (${PROJECT_NAME} SHARED ${PROJECT_SRC})
target_compile_features (${PROJECT_NAME} PUBLIC cxx_std_17)
# Enable IPO
include(CheckIPOSupported)
check_ipo_supported(RESULT iporesult)
if(iporesult)
message (STATUS "IPO supported for project ${PROJECT_NAME}")
set_property(TARGET ${PROJECT_NAME} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()

我添加了一些命令,用于在Visual Studio中添加对LTO的支持。我已经看到我必须检查对IPO的支持,如果可以,我必须设置INTERPROCEDURAL_OPTIMIZATION属性,这就是我所做的

当我运行项目时,我收到以下消息(我也在使用 vcpkg,这就是第一行的原因):

[cmake] IPO supported for project data
...
[cmake] CMake Warning (dev) at D:/Projects/vcpkg/scripts/buildsystems/vcpkg.cmake:198 (_add_library):
[cmake]   Policy CMP0069 is not set: INTERPROCEDURAL_OPTIMIZATION is enforced when
[cmake]   enabled.  Run "cmake --help-policy CMP0069" for policy details.  Use the
[cmake]   cmake_policy command to set the policy and suppress this warning.
[cmake] 
[cmake]   INTERPROCEDURAL_OPTIMIZATION property will be ignored for target 'data'.
[cmake] Call Stack (most recent call first):
[cmake]   src/Data/CMakeLists.txt:18 (add_library)
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.

据我所知,没有为项目启用链接时间优化。我还在Visual Studio中打开了该项目,并检查了项目的命令行,这就是构建的结果:

/GS /TP /W3 /Zc:wchar_t /I"M:projectsrcData.." /Zi /Gm- /Od /Ob0 /Fd"data.dirDebugvc141.pdb" /Zc:inline /fp:precise /D "WIN32" /D "_WINDOWS" /D "DATA_EXPORTS" /D "CMAKE_INTDIR="Debug"" /D "data_EXPORTS" /D "_WINDLL" /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /RTC1 /GR /Gd /MDd /std:c++17 /Fa"Debug/" /EHsc /nologo /Fo"data.dirDebug" /Fp"data.dirDebugdata.pch" /diagnostics:classic

和链接

/OUT:"M:projectbuildvscodebuildbinDebugdata.dll" /MANIFEST /NXCOMPAT /PDB:"M:/project/build/vscode/build/bin/Debug/data.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "comdlg32.lib" "advapi32.lib" /IMPLIB:"M:/project/build/vscode/build/lib/Debug/data.lib" /DEBUG /DLL /MACHINE:X64 /INCREMENTAL /PGD:"M:projectbuildvscodebuildbinDebugdata.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"data.dirDebugdata.dll.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /TLBID:1 

我没有看到 LTO 的任何标志。

我已经运行了策略的命令,这就是结果:

cmake --help-policy CMP0069
CMP0069
-------
``INTERPROCEDURAL_OPTIMIZATION`` is enforced when enabled.
CMake 3.9 and newer prefer to add IPO flags whenever the
``INTERPROCEDURAL_OPTIMIZATION`` target property is enabled and
produce an error if flags are not known to CMake for the current compiler.
Since a given compiler may not support IPO flags in all environments in which
it is used, it is now the project's responsibility to use the
``CheckIPOSupported`` module to check for support before enabling the
``INTERPROCEDURAL_OPTIMIZATION`` target property.  This approach
allows a project to conditionally activate IPO when supported.  It also
allows an end user to set the ``CMAKE_INTERPROCEDURAL_OPTIMIZATION``
variable in an environment known to support IPO even if the project does
not enable the property.
Since CMake 3.8 and lower only honored ``INTERPROCEDURAL_OPTIMIZATION``
for the Intel compiler on Linux, some projects may unconditionally enable the
target property.  Policy ``CMP0069`` provides compatibility with such projects.
This policy takes effect whenever the IPO property is enabled.  The ``OLD``
behavior for this policy is to add IPO flags only for Intel compiler on Linux.
The ``NEW`` behavior for this policy is to add IPO flags for the current
compiler or produce an error if CMake does not know the flags.
This policy was introduced in CMake version 3.9.  CMake version
3.14.0 warns when the policy is not set and uses ``OLD`` behavior.
Use the ``cmake_policy()`` command to set it to ``OLD`` or ``NEW``
explicitly.
.. note::
The ``OLD`` behavior of a policy is
``deprecated by definition``
and may be removed in a future version of CMake.
Examples
^^^^^^^^
Behave like CMake 3.8 and do not apply any IPO flags except for Intel compiler
on Linux:
cmake_minimum_required(VERSION 3.8)
project(foo)
# ...
set_property(TARGET ... PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
Use the ``CheckIPOSupported`` module to detect whether IPO is
supported by the current compiler, environment, and CMake version.
Produce a fatal error if support is not available:
cmake_minimum_required(VERSION 3.9) # CMP0069 NEW
project(foo)
include(CheckIPOSupported)
check_ipo_supported()
# ...
set_property(TARGET ... PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
Apply IPO flags only if compiler supports it:
cmake_minimum_required(VERSION 3.9) # CMP0069 NEW
project(foo)
include(CheckIPOSupported)
# ...
check_ipo_supported(RESULT result)
if(result)
set_property(TARGET ... PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
Apply IPO flags without any checks.  This may lead to build errors if IPO
is not supported by the compiler in the current environment.  Produce an
error if CMake does not know IPO flags for the current compiler:
cmake_minimum_required(VERSION 3.9) # CMP0069 NEW
project(foo)
# ...
set_property(TARGET ... PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)

看来我用对了。

我不明白的是,如果命令check_ipo_supported告诉我另一个故事,为什么不应用该属性。

我做错了什么?

您需要设置策略:

https://cmake.org/cmake/help/git-stage/policy/CMP0069.html

cmake_policy(SET CMP0069 NEW) 
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)

奇怪的是,我在Linux(NixOS 19.03,GCC)上尝试了问题中的代码,它对我来说效果很好。我相信您要打印您当前正在吞咽的诊断消息。它可能有助于解决问题。(它帮助了我,我缺少C++编译器,并且默认情况下check_ipo_supported检查 C 和 C++ IPO 是否正常工作。

check_ipo_supported(RESULT result OUTPUT output)
if(result)
set_property(TARGET foo PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(WARNING "IPO is not supported: ${output}")
endif()

它来自 https://cmake.org/cmake/help/v3.9/module/CheckIPOSupported.html

相关内容

  • 没有找到相关文章

最新更新