5

我在 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:\project\src\Data\.." /Zi /Gm- /Od /Ob0 /Fd"data.dir\Debug\vc141.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.dir\Debug\" /Fp"data.dir\Debug\data.pch" /diagnostics:classic

并用于链接

/OUT:"M:\project\build\vscode\build\bin\Debug\data.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:\project\build\vscode\build\bin\Debug\data.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"data.dir\Debug\data.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告诉我另一个故事,为什么不应用该属性。

我做错了什么?

4

2 回答 2

6

您需要设置策略:

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

cmake_policy(SET CMP0069 NEW) 
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
于 2020-02-27T13:04:27.003 回答
1

奇怪的是,我在 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

于 2019-06-27T13:13:48.067 回答