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

Replace CMAKE_CXX_FLAGS with better option (warning config) #4

Open
ricab opened this issue May 8, 2018 · 6 comments
Open

Replace CMAKE_CXX_FLAGS with better option (warning config) #4

ricab opened this issue May 8, 2018 · 6 comments
Labels
wontfix This will not be worked on

Comments

@ricab
Copy link
Owner

ricab commented May 8, 2018

(issue corresponding to review suggestion here)

CMAKE_CXX_FLAGS is used to configure compiler warnings in cmake as part of the testing procedure, (following the this advice). I am not aware of a warning API in cmake and I know of no option that does not involve passing explicit flags to the compiler. So, AFAIU, better options to configure compiler warnings would still require explicit flags.

With that in mind, I see 2 contending alternatives to the current approach:

  • use target_compile_options on each target
  • use add_compile_options once

To be discussed. Suggestions of better options are welcome.

@ricab
Copy link
Owner Author

ricab commented May 8, 2018

@Quincunx271, could you please provide or point me to something explaining why this is a bad habit even when compiler flags are needed? Will clients somehow be affected when #3 is done? And what better option did you envisage?

@Quincunx271
Copy link

First off, I am not really that experienced with CMake, but I do have an idea of what the best practices are. It's totally possible that there are better practices / reasons than what I know.

If you want the library to be consumable via add_subdirectory(...), you do not want to pollute things like CMAKE_CXX_FLAGS. Also, I personally find it much easier to understand when CMAKE_CXX_FLAGS isn't explicitly touched in the CMakeLists.txt

You could use add_compile_options, but I would personally use target_compile_options. You could write a cmake function to do it for you if you don't want to repeat yourself:

function(scope_guard_compile_options TARGET)
  target_compile_options(${TARGET}
    # options go here
  )
endfunction()

For setting up warnings, I usually do this:

target_compile_options(my-target
  PRIVATE
    $<$<CXX_COMPILER_ID:Gnu>:
      # g++ warning flags
    >
    $<$<CXX_COMPILER_ID:Clang>:
      # clang warning flags
    >
    $<$<CXX_COMPILER_ID:MSVC>:
      # MSVC warning flags
    >
)

@OvermindDL1
Copy link

For 'best practices' from the big cmake dev's themselves see: https://cgold.readthedocs.io/en/latest/

@ricab
Copy link
Owner Author

ricab commented May 8, 2018

@OvermindDL1: thanks again for the pointer. I did not find anything relevant to this issue there though.

@Quincunx271:

First off, I am not really that experienced with CMake, but I do have an idea of what the best practices are. It's totally possible that there are better practices / reasons than what I know.

Pretty much the same here. I share the impression that CMake abstractions are better than direct compiler flags in general, but I don't see an alternative here. And I looked at the time... Perhaps add_compile_options or target_compile_options are more elegant than changing CMAKE_CXX_FLAGS... I wish I could see the right argument for that.

If you want the library to be consumable via add_subdirectory(...), you do not want to pollute things like CMAKE_CXX_FLAGS.

Hmm, well it was not really meant for that – it is a top-level CMakeLists.txt which would require other changes anyway to be used like that. BTW, I intend to configure warnings only in the BUILD_TESTING case when #3 is done.

easier to understand when CMAKE_CXX_FLAGS isn't explicitly touched in the CMakeLists.txt

Again, I agree in general, but because there are usually better cmake abstractions. Perhaps add_compile_options is more elegant here, but it feels pretty much like a matter of taste.

I would personally use target_compile_options

Come to think of it, add_compile_options seems preferable to configuring each individual target the same way, as it conveys right away that the options are to be applied in all targets, wouldn't you say? What would be the point of having a function that was called for all targets anyway?

@OvermindDL1
Copy link

Honestly, as long as the library is accessible via something like Hunter (or any of the other cmake dependency systems, though hunter is the only one that only requires cmake and nothing else) then it becomes trivial to use. For that I think all it needs is to be able to 'install' properly with all necessary header files and config files and such available.

@ricab
Copy link
Owner Author

ricab commented May 9, 2018

@OvermindDL1 point taken

@ricab ricab added the wontfix This will not be worked on label Jan 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants