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

Specify sanitizer parameters in CMake #76

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

wusatosi
Copy link
Member

@wusatosi wusatosi commented Nov 14, 2024

This PR attempts adopts @bretbrownjr 's suggestion in #44 (review).

You can use variables like CMAKE_CXX_FLAGS_Debug_INIT to tune what a Debug or Release build entails, for what it's worth. It's maybe a little nicer to use those variables instead of CMAKE_CXX_FLAGS. But not a huge deal.

https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_CONFIG_INIT.html

However, instead of implementing a toolchain file, this PR introduces a apply_sanitzer util.

The reason for not adopting a toolchain file is because CMAKE_CXX_COMPILER_ID is not resolved during toolchain execution, which means we are unable to customize sanitizer use according to compiler support.

This PR updates CI script to use the new sanitizer utility.

This sanitizer utility exports an undocumented option of BEMAN_BUILDSYS_SANITIZER and is not to be used outside of internal build script/ preset/ CI. Using BEMAN_BUILDSYS as its namespace so that this functionality could be copied over across projects easily.

This PR fixes #65 as it does not add leak sanitizer when apple clang is detected, this needs testing (I think @camio have a mac environment?).

Preset change will be filed after #82 is merged.

cmake/toolchain.cmake Outdated Show resolved Hide resolved
Copy link
Member

@steve-downey steve-downey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"We need to verify CMAKE_CXX_COMPILER_ID for g++ on macos is AppleClang."

Confirm what the compiler identification is for the default false g++ on Darwin is.

Marking "Request changes" so this doesn't get landed prematurely.

cmake/toolchain.cmake Outdated Show resolved Hide resolved
@ClausKlein

This comment was marked as resolved.

@wusatosi

This comment was marked as resolved.

@steve-downey
Copy link
Member

So, yes:
The CXX compiler identification is AppleClang 16.0.0.16000026

@steve-downey
Copy link
Member

Not sure if picking the toolchain for Darwin better, or making the generic toolchain smarter would work better, but either should work?

@ClausKlein
Copy link

Wait, that can't work!

iMac:exemplar clausklein$ cmake --preset gcc-debug  --trace-expand --trace-source=toolchain.cmake
Put cmake in trace mode, but with variables expanded.
Put cmake in trace mode, but output only lines of a specified file. Multiple options are allowed.
Preset CMake variables:

  BEMAN_BUILDSYS_SANITIZER="ASan"
  CMAKE_BUILD_TYPE="Debug"
  CMAKE_CXX_COMPILER="g++"
  CMAKE_CXX_STANDARD="20"
  CMAKE_TOOLCHAIN_FILE="cmake/toolchain.cmake"

/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(3):  set(CMAKE_C_FLAGS_RELEASE_INIT -O3 )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(4):  set(CMAKE_CXX_FLAGS_RELEASE_INIT -O3 )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(6):  set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT -O3 )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(7):  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT -O3 )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(16):  if(DEFINED BEMAN_BUILDSYS_SANITIZER )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(17):  if(BEMAN_BUILDSYS_SANITIZER STREQUAL ASan )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(19):  set(SANITIZER_FLAGS -fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(24):  if(NOT CMAKE_CXX_COMPILER_ID )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(25):  message(WARNING toolchain is used before CMAKE_CXX_COMPILER_ID was set! )
CMake Warning at cmake/toolchain.cmake:25 (message):
  toolchain is used before CMAKE_CXX_COMPILER_ID was set!
Call Stack (most recent call first):
  build/gcc-debug/CMakeFiles/3.31.0-dirty/CMakeSystem.cmake:6 (include)
  CMakeLists.txt:5 (project)


/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(27):  if(APPLE )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(28):  message(STATUS Using GCC on macOS; excluding -fsanitize=leak )
-- Using GCC on macOS; excluding -fsanitize=leak
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(44):  list(APPEND CMAKE_C_FLAGS_DEBUG_INIT -fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(45):  list(APPEND CMAKE_CXX_FLAGS_DEBUG_INIT -fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined )
-- The CXX compiler identification is AppleClang 16.0.0.16000026
-- Detecting CXX compiler ABI info
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(3):  set(CMAKE_C_FLAGS_RELEASE_INIT -O3 )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(4):  set(CMAKE_CXX_FLAGS_RELEASE_INIT -O3 )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(6):  set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT -O3 )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(7):  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT -O3 )
/Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/toolchain.cmake(16):  if(DEFINED BEMAN_BUILDSYS_SANITIZER )
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
Examples to be built: identity_direct_usage;identity_as_default_projection
-- Configuring done (2.4s)

@wusatosi
Copy link
Member Author

Not sure if picking the toolchain for Darwin better, or making the generic toolchain smarter would work better, but either should work?

I am leaning on having separate tool chain files and having like a central tool chain dispatch logic based on compiler and platform.

But then I realized there's not that much variance in building across platforms and compilers, at least in exemplar, to warrant separate files.

@ClausKlein
Copy link

But then I realized there's not that much variance in building across platforms and compilers, at least in exemplar, to warrant separate files.

That is why often I use the project_options
see i.e.: https://github.com/aminya/project_options/blob/main/src/Sanitizers.cmake

@wusatosi
Copy link
Member Author

That is why often I use the project_options see i.e.: https://github.com/aminya/project_options/blob/main/src/Sanitizers.cmake

That's project looks fantastic!

I can bring this up in weekly sync and see if we want to use this.
Clang-tidy support, coverage, doxygen (and potentially vcpkg) are improvement features we would love to have, it would be fantastic if we can get all these done with this dependency.

@wusatosi
Copy link
Member Author

Ah I think tool chain file is executed before project(), so CMAKE_CXX_COMPILER_ID maybe unset?

@wusatosi wusatosi force-pushed the toolchain branch 2 times, most recently from 5f20499 to 117fcd9 Compare November 15, 2024 04:30
@wusatosi wusatosi force-pushed the toolchain branch 2 times, most recently from a3f18b3 to b000f40 Compare November 15, 2024 04:49
@wusatosi wusatosi changed the title Add CMake toolchain file Specify sanitizer parameters in CMake Nov 15, 2024
@ClausKlein
Copy link

Ah I think tool chain file is executed before project(), so CMAKE_CXX_COMPILER_ID maybe unset?

Yes, and call N times for each LANGUAGES enabled in project.

@wusatosi

This comment was marked as resolved.

@wusatosi
Copy link
Member Author

So I decide to not implement a toolchain file instead adding a generic apply_sanitizer util cmake script under the cmake directory. I will update the PR description accordingly.

I added a macos run of gcc-debug to confirm we are building correctly.

@steve-downey you may review and unblock this PR

@ClausKlein
Copy link

ClausKlein commented Nov 15, 2024

FYI: I have just added project_options as demo here:
bemanproject/execution26#80

@wusatosi
Copy link
Member Author

nudging @steve-downey @neatudarius @camio to review as #80 maybe improved if this is merged.

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

Successfully merging this pull request may close these issues.

clang++: error: unsupported option '-fsanitize=leak' for target 'x86_64-apple-darwin23.6.0'
3 participants