project_options icon indicating copy to clipboard operation
project_options copied to clipboard

Fix Hardening options and LTO for multi-configuration generators

Open FeignClaims opened this issue 1 year ago • 2 comments

fix #259 by using generator expressions and CMAKE_INTERPROCEDURAL_OPTIMIZATION_<CONFIG>.

FeignClaims avatar May 19 '24 07:05 FeignClaims

From my personal test on MacOS M1 (unfortunately not implementable in Github Actions yet), check_ipo_supported() falsely failed because it can't find clang-scan-deps for "" -format=p1689.

The problem is that CMake 3.23 and below run the check with the current environment and toolchain settings. Since we specify cmake_minimum_required(VERSION 3.20) in Index.cmake, it performs as if cmake is v3.20, but CMake supports C++ Modules only after 3.28.

Here're two possible solutions:

  • (chosen) In Index.cmake, use cmake_minimum_required(VERSION 3.24) instead of cmake_minimum_required(VERSION 3.20), which makes check_ipo_supported() prefer to honor the calling project's flags.
  • Instead of calling enable_interprocedural_optimization() inside Index.cmake, force users to call enable_interprocedural_optimization(<prefix>_project_options) themselves in their project.
error log
CMake Warning at build/clang/_deps/_sample_project_project_options-src/src/Optimization.cmake:20 (message):
  Interprocedural Optimization is not supported.  Not using it.  Here is the
  error log: Change Dir:
  '/Users/feignclaims/code/cpp/cpp_conan_template/build/clang/CMakeFiles/_CMakeLTOTest-CXX/bin'




  Run Build Command(s): /opt/homebrew/bin/ninja -v

  [1/8] "" -format=p1689 -- /opt/homebrew/opt/llvm/bin/clang++
  -DCMAKE_INTDIR=\"Debug\" -stdlib=libc++ -L/opt/homebrew/opt/llvm/lib/c++
  -Wno-unused-command-line-argument -g -std=c++26 -flto=thin -arch arm64
  -isysroot
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk
  -fPIC -x c++
  /Users/feignclaims/code/cpp/cpp_conan_template/build/clang/CMakeFiles/_CMakeLTOTest-CXX/src/foo.cpp
  -c -o CMakeFiles/foo.dir/Debug/foo.cpp.o -resource-dir
  "/opt/homebrew/Cellar/llvm/18.1.5/lib/clang/18" -MT
  CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi -MD -MF
  CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi.d >
  CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi.tmp && mv
  CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi.tmp
  CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi

  FAILED: CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi

  "" -format=p1689 -- /opt/homebrew/opt/llvm/bin/clang++
  -DCMAKE_INTDIR=\"Debug\" -stdlib=libc++ -L/opt/homebrew/opt/llvm/lib/c++
  -Wno-unused-command-line-argument -g -std=c++26 -flto=thin -arch arm64
  -isysroot
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk
  -fPIC -x c++
  /Users/feignclaims/code/cpp/cpp_conan_template/build/clang/CMakeFiles/_CMakeLTOTest-CXX/src/foo.cpp
  -c -o CMakeFiles/foo.dir/Debug/foo.cpp.o -resource-dir
  "/opt/homebrew/Cellar/llvm/18.1.5/lib/clang/18" -MT
  CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi -MD -MF
  CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi.d >
  CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi.tmp && mv
  CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi.tmp
  CMakeFiles/foo.dir/Debug/foo.cpp.o.ddi

  /bin/sh: : command not found

  [2/8] "" -format=p1689 -- /opt/homebrew/opt/llvm/bin/clang++
  -DCMAKE_INTDIR=\"Debug\" -stdlib=libc++ -L/opt/homebrew/opt/llvm/lib/c++
  -Wno-unused-command-line-argument -g -std=c++26 -flto=thin -arch arm64
  -isysroot
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk
  -fPIE -x c++
  /Users/feignclaims/code/cpp/cpp_conan_template/build/clang/CMakeFiles/_CMakeLTOTest-CXX/src/main.cpp
  -c -o CMakeFiles/boo.dir/Debug/main.cpp.o -resource-dir
  "/opt/homebrew/Cellar/llvm/18.1.5/lib/clang/18" -MT
  CMakeFiles/boo.dir/Debug/main.cpp.o.ddi -MD -MF
  CMakeFiles/boo.dir/Debug/main.cpp.o.ddi.d >
  CMakeFiles/boo.dir/Debug/main.cpp.o.ddi.tmp && mv
  CMakeFiles/boo.dir/Debug/main.cpp.o.ddi.tmp
  CMakeFiles/boo.dir/Debug/main.cpp.o.ddi

  FAILED: CMakeFiles/boo.dir/Debug/main.cpp.o.ddi

  "" -format=p1689 -- /opt/homebrew/opt/llvm/bin/clang++
  -DCMAKE_INTDIR=\"Debug\" -stdlib=libc++ -L/opt/homebrew/opt/llvm/lib/c++
  -Wno-unused-command-line-argument -g -std=c++26 -flto=thin -arch arm64
  -isysroot
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk
  -fPIE -x c++
  /Users/feignclaims/code/cpp/cpp_conan_template/build/clang/CMakeFiles/_CMakeLTOTest-CXX/src/main.cpp
  -c -o CMakeFiles/boo.dir/Debug/main.cpp.o -resource-dir
  "/opt/homebrew/Cellar/llvm/18.1.5/lib/clang/18" -MT
  CMakeFiles/boo.dir/Debug/main.cpp.o.ddi -MD -MF
  CMakeFiles/boo.dir/Debug/main.cpp.o.ddi.d >
  CMakeFiles/boo.dir/Debug/main.cpp.o.ddi.tmp && mv
  CMakeFiles/boo.dir/Debug/main.cpp.o.ddi.tmp
  CMakeFiles/boo.dir/Debug/main.cpp.o.ddi

  /bin/sh: : command not found

  ninja: build stopped: subcommand failed.



Call Stack (most recent call first):
  build/clang/_deps/_sample_project_project_options-src/src/Index.cmake:212 (enable_interprocedural_optimization)
  build/clang/_deps/_sample_project_project_options-src/src/DynamicProjectOptions.cmake:257 (project_options)
  cmake/sample_project_custom_project_options.cmake:30 (dynamic_project_options)
  CMakeLists.txt:12 (include)

FeignClaims avatar May 19 '24 08:05 FeignClaims

All vcpkg-related cross tests failed after requiring CMake 3.24 as the minium version.

I compared the logs from the last test with this one. Maybe this is caused by ~~github action caches~~... or something else

~~Help wanted.~~ Fixed after setting the POLICY CMP0138 instead of bumping cmake to 3.24.

FeignClaims avatar May 19 '24 08:05 FeignClaims

You can set a POLICY without the need to bump the minimum version:

if(POLICY CMP0138)
  cmake_policy(SET CMP0138 NEW)
endif()

ddassie-texa avatar Jun 10 '24 08:06 ddassie-texa

You can set a POLICY without the need to bump the minimum version:您可以设置 POLICY 而无需提高最低版本:

if(POLICY CMP0138)
  cmake_policy(SET CMP0138 NEW)
endif()

I forgot this solution, thanks.

FeignClaims avatar Jun 10 '24 08:06 FeignClaims

ci on Test (macos-13, gcc, true, true) fails, which refers to the known issue of github runners actions/runner-images#9997. Therefore, it's not related to this pr.

Given that the pull request performs well in other tests, I believe it is now suitable for review and possibly ready for merging.

FeignClaims avatar Jun 10 '24 08:06 FeignClaims