-
Notifications
You must be signed in to change notification settings - Fork 707
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
Modernize CMake code #734
base: master
Are you sure you want to change the base?
Modernize CMake code #734
Conversation
CMakeLists.txt
Outdated
include(CMakeDependentOption) | ||
cmake_dependent_option(ENABLE_WERROR "Enable -Werror flag" ON "CMAKE_SYSTEM_NAME MATCHES LINUX" OFF) | ||
if (ENABLE_WERROR) | ||
message(STATUS "Enabling -Werror flag") | ||
add_compile_options(-Werror) | ||
endif() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you separated this flag from the others related to LINUX?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The -Werror
blocks my linux builds :) Honestly I don't think any component should ever include this. -Werror
should be up to the toolchain - as the toolchain usually specifies which compile options to use, which warnings to watch for, and whether warnings should be allowed.
I am still working on this PR, I'll likely rebase to Fabio's 3.5.x branch. Stuff just takes time. :) |
In that case maybe i will check this PR later! great job anyway, it has interesting improvements |
46c5cac
to
da94c06
Compare
@kheaactua tried to build this new CMake code and got a failure. I have ubuntu 22.04, gcc version 11.4 the error i got was the following: |
@duartenfonseca I'll take a look! Was this on Fabio's 3.5.x? He did push a change to the network tests a few days back. I'll also fix that conflict that appeared. |
@kheaactua i just did a checkout to your branch and tried to build! |
@kheaactua is the increase of minimum version on cmake is really necessary? from our side, we would like to avoid that |
I raised it to 3.15 to accommodate Conan 2.0. From the author:
Though not yet posted, I've been prepping covesa/capi-conan, covesa/capi-docker, and some others that all use Conan to set up CommonAPI environments. Curious though, what would be desirable about staying at 3.13? With 3.23 vsomeip could use file sets for a cleaner install of the includes Locally I jack it up to 3.30 :) |
da94c06
to
b45251e
Compare
b45251e
to
0c2f5f9
Compare
I understand your reasons, but if we could find a way to keep the CMake changes without increasing from 3.13 would be best. From our internal process, increasing this there would need to be a strong justification for this change. |
I could make Conan apply a patch, that would solve the issue with Conan. I'll give it a try with 3.13 as soon as I can. I may have to remove some of the generator expressions to support 3.13 as well. Also, for me the Docker setup for the network tests fails with CMake 3.22 and below. That said, I would strongly encourage the update.
Just a tonne of updates that help both with QNX and really just in general. |
e2c4cdc
to
9c53ff7
Compare
d56e11e
to
38c935a
Compare
2638cb5
to
74bc25d
Compare
74bc25d
to
429fac5
Compare
list(SORT ${VSOMEIP_NAME}-cfg_SRC) | ||
if(VSOMEIP_ENABLE_MULTIPLE_ROUTING_MANAGERS EQUAL 0) | ||
add_library(${VSOMEIP_NAME}-cfg SHARED ${${VSOMEIP_NAME}-cfg_SRC}) | ||
file(GLOB vsomeip3_cfg_SRC "implementation/configuration/src/*.cpp") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why stop using VSOMEIP_NAME ? if the name one day changes it will be hard to change everywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VSOMEIP_NAME
names sense to control the OUTPUT_NAME
of the artifact, but it doesn't make sense to control the target name in CMake it self.
For instance, in this line
add_library(${VSOMEIP_NAME}-cfg ...)`
this creates a target named ${VSOMEIP_NAME}-cfg
with a default OUTPUT_NAME
of ${VSOMEIP_NAME}-cfg
(where ${VSOMEIP_NAME}
is expanded)
But we never want to target name to change.
Instead, this changes the target to vsomeip-cfg
, and line 399 sets the OUTPUT_NAME
and EXPORT_NAME
.
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") | ||
# This is only relevant for GCC and causes warnings on Clang | ||
set(EXPORTSYMBOLS "-Wl,-export-dynamic -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exportmap.gcc") | ||
set(OS_CXX_FLAGS "${OS_CXX_FLAGS} -pie -Wno-tsan -Wl,-z,relro,-z,now") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need the -Wno-tsan flag, so it shouldn't be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Why do you need it though? Shouldn't this be in the sanitizer section?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This flag caused problems on clang so it was moved to GCC only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k, I restored it in 2d97573
I'm a little confused though, because tsan should only be present if you enable it.. But if you enable it and -Wno-tsan
is always enabled, than tsan won't work.
Are you sure it wouldn't be better to handle this down in the sanitizers section? e.g.
# Sanitizers
if(ENABLE_UNDEFINED_SANITIZER)
add_compile_options(-fsanitize=undefined)
endif()
if(ENABLE_THREAD_SANITIZER)
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
message(WARNING "TSAN isn't enabled on GCC for vsomeip")
else()
add_compile_options(-fsanitize=thread)
endif()
endif()
CMakeLists.txt
Outdated
if(NOT DEFINED _FORTIFY_SOURCE) | ||
set(_FORTIFY_SOURCE 2) | ||
endif() | ||
set(OS_CXX_FLAGS | ||
"${OS_CXX_FLAGS} -D_GLIBCXX_USE_NANOSLEEP -pthread -O -Wall -Wextra -Wformat -Wformat-security -Wconversion -fexceptions -fstrict-aliasing -fstack-protector-strong -fasynchronous-unwind-tables -fno-omit-frame-pointer -D_FORTIFY_SOURCE=${_FORTIFY_SOURCE} -Wformat -Wformat-security -Wpedantic -Werror -fPIE" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the -O flag was removed, it is needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm? The optimization level should be controlled by the CMAKE_BUILD_TYPE
. i.e. building in debug (-Og
), or release (-O2
or -O3
), etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from what i read, there is no CMAKE_BUILD_TYPE that uses optimization flag -O. so in that case the optimization flag should still be added here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not -O
specifically, but the optimization is set by default. The defaults are:
Variable | Flags |
---|---|
CMAKE_CXX_FLAGS |
|
CMAKE_CXX_FLAGS_DEBUG |
-g -O0 |
CMAKE_CXX_FLAGS_RELEASE |
-O3 -DNDEBUG |
CMAKE_CXX_FLAGS_RELWITHDEBINFO |
-O2 -g -DNDEBUG |
CMAKE_CXX_FLAGS_MINSIZEREL |
-Os -DNDEBUG |
(Source Professional CMake: A Practical Guide, 10th edition)
I'd also steer away from -O
it self. It's vague, I think -O
is the same as -O1
. In any case though, it's much better for the end user to be able to control this through a toolchain. Hardcoding an optimization level into the CMake removes the ability for the end user to control their optimization level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k, take a look at a5ececf
CMakeLists.txt
Outdated
add_library(vsomeip3 SHARED) | ||
target_sources(vsomeip3 PRIVATE ${vsomeip3_SRC}) | ||
target_compile_features(vsomeip3 PRIVATE cxx_std_17) | ||
target_include_directories( | ||
vsomeip3 | ||
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/interface> | ||
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/implementation/configuration/include> | ||
INTERFACE $<INSTALL_INTERFACE:${INSTALL_INCLUDE_DIR}> | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why remove the INTERFACE keyword here? this folders are meant to be provided to the targets that link with vsomeip-lib
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is:
-
PUBLIC
: The include directories are added to both the target itself and to any targets that link against it. This means that the include directories are used when compiling the target and are also propagated to consumers of the target. -
INTERFACE
: The include directories are only added to targets that link against the target. They are not used when compiling the target itself, but they are propagated to consumers of the target.
In this case, libvsomeip3 uses these two directories it self, so they should be PUBLIC
rather than an INTERFACE
$<INSTALL_INTERFACE:${INSTALL_INCLUDE_DIR}> | ||
target_compile_definitions( | ||
vsomeip3 | ||
PRIVATE $<$<CXX_COMPILER_ID:MSVC>:VSOMEIP_DLL_COMPILATION> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this a duplicate from lines 463-464?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good eye. It is 100% redundant, I'll fix this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 722fa96
) | ||
target_include_directories(vsomeip3 SYSTEM PUBLIC $<BUILD_INTERFACE:${Boost_INCLUDE_DIRS}>) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a replacement of :
if(${CMAKE_SYSTEM_NAME} MATCHES "QNX") include_directories(${Boost_INCLUDE_DIR} ) else() include_directories(SYSTEM ${Boost_INCLUDE_DIR} ) endif()
?
it seems that it uses a different variable: Boost_INCLUDE_DIRS instead of Boost_INCLUDE_DIR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is the case, couldn't this target_include_directories be merged with the previous one on line 457?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to consult the CMake docs again to answer about Boost_INCLUDE_DIRS
and Boost_INCLUDE_DIR
- these two always confuse me. :)
This is why when possible I try to use the Boost::system
target, but there's one case here where the include directory variable is directly required.
Checking it though
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/interface/compat> | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> # for generated files in build mode | ||
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/implementation/configuration/include> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was this the line that helped you with the internal.hpp file?
if so, what problem did it fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes!
So, in the current version:
configure_file (
"${PROJECT_SOURCE_DIR}/implementation/configuration/include/internal.hpp.in"
"${PROJECT_SOURCE_DIR}/implementation/configuration/include/internal.hpp"
)
This writes internal.hpp
into the source directory. I would argue that this is a build artifact though and should go into the build directory.
Especially because it's in the .gitignore
, when there are changes to this file they're hard to see. When it's in the build directory it's trivial to delete the build directory and get a fresh build. When it's in the source directory though it persists. CMake should keep it updated, but I'm pretty sure what first brought my attention to this was the cases where it wasn't updated.
So the change here is to put it in the build directory:
configure_file(
"${PROJECT_SOURCE_DIR}/implementation/configuration/include/internal.hpp.in"
"${PROJECT_BINARY_DIR}/implementation/configuration/include/internal.hpp"
)
(key being ${PROJECT_BINARY_DIR}
)
And then the line you pointed out adds that path in the build directory to the include path.
The reason it causes me so much trouble is I have it in the binary directory in my forks, but when I switch back and forth from my forks to the upstream code that file re-appears in the source directory, and then overrides the version in the build directory.
By having it in the build directory, my builds seem more robust. Especially when I'm testing certain compiler flags, and have to call clang++
or q++
directly (without CMake) to see the different effects. Having internal.hpp
be in the build directory instead of the source makes this a lot easier.
examples/hello_world/CMakeLists.txt
Outdated
if(ENABLE_SIGNAL_HANDLING) | ||
target_compile_definitions(vsomeip_hello_world_example INTERFACE VSOMEIP_ENABLE_SIGNAL_HANDLING) | ||
endif() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed, since the VSOMEIP_ENABLE_SIGNAL_HANDLING has already been set on the main cmakelists as an add_definitions() command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is in the top level CMake file, but hello_world/CMakeLists.txt
appears to be written to be a self contained project - and nothing uses add_subdirectory
to include it in the build.
test/CMakeLists.txt
Outdated
# remove export symbols from the cxx flags. This is messy, but there's no `remove_link_flags` function | ||
get_target_property(os_link_options OS_INTERFACE INTERFACE_LINK_OPTIONS) | ||
foreach(opt IN ITEMS ${os_link_options}) | ||
if(opt MATCHES ".*version.script.*.gcc") | ||
list(REMOVE_ITEM os_link_options ${opt}) | ||
endif() | ||
endforeach() | ||
unset(opt) | ||
if(os_link_options) | ||
set_target_properties(OS_INTERFACE PROPERTIES INTERFACE_LINK_OPTIONS "${os_link_options}") | ||
endif() | ||
unset(os_link_options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this way, the -Wl,-export-dynamic part of the EXPORT_SYMBOLS was not being removed. my suggestion would be to re-add the EXPORT_SYMBOLS as a list on the main CMakeLists.txt as:
set(EXPORTSYMBOLS
-Wl,-export-dynamic
-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exportmap.gcc)
add that to the OS_INTERFACE:
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# This is only relevant for GCC and causes warnings on Clang
target_link_options(
OS_INTERFACE
INTERFACE
-Wno-tsan -Wl,-z,relro,-z,now
${EXPORTSYMBOLS}
)
and here instead of this foreach loop, use this one:
foreach(symbol ${EXPORTSYMBOLS})
list(REMOVE_ITEM os_link_options ${symbol})
endforeach()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would make it simpler. I'll give it a try
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think it works better. Fixed in bf943c5
38c935a
to
758d37a
Compare
Included a .cmake-format file, and ran applied it to the main CMakeList files in this project.
- Use literal target names instead of variables - Write internal.hpp to build directory instead of source to prevent needless rebuilds - Fixed how this file is includes in source files - Remove hard coded CMAKE_VERBOSE_MAKEFILE - Ran cmake-format - "wrap"ing (ld's --wrap option) the "socket" symbol, see wrappers.cpp - Added install directive to vsomeip/example/hello_world and vsomeip_ctrl - Platform conditional socket lib linkage - hello_world code uses ENABLE_SIGNALS, but its setting was missing in CMake - Improve setup for boost stacktrace, really only helps on Linux and requires the backtrace.h file - Added QNX platform section, though the credential code changes for QNX are not included in this commit. - Use a CACHE variable for VSOMEIP_BASE_PATH. Upstream defaults this to /var for QNX which is a very bad choice. - Followed the 3.5.x example and remove the dependency on routingmanager in the tests, as it's not available on Windows
758d37a
to
a4cf2bf
Compare
Co-authored-by: "Duarte Fonseca" <Duarte.Fonseca@ctw.bmwgroup.com> Co-authored-by: "Matthew Russell" <matthew.g.russell@gmail.com>
Restored wrap options to BSD
Fixed error in ternary
…2→v3 Removed redundant VSOMEIP_DLL_COMPILATION on vsomeip target
…cmake v2→v3 Fixing how tests clean EXPORT_SYMBOLS
…ents - cmake v2→v3 Re-introduced -Wno-tsan
Ensuring -O exists in CMAKE_CXX_FLAGS_RELWITHDEBINFO per Duarte's request
a5ececf
to
fb7e0da
Compare
…mprovements - cmake v2→v3
…eLists improvements - cmake v2→v3 Removed stacktrace interface, this doesn't work well. Also set min boost to 1.66 (same as 3.5.3)
1e8136d
to
38685d1
Compare
The build error is weird.. Though I do get a similar one locally:
Not sure what the cause is, will investigate. |
Status
For the moment this is still a WIP. I would love community feedback on this PR so that we can produce a modern CMake setup that functions well for everyone.
I suspect the cleanest way to organize the commits would be to squash them.
Description
General CMakeLists improvements - cmake v2→v3
.cmake-format
file, and ran applied it to the mainCMakeList
files in this project.if
andforeach
blocks, but I believe overall it does a better job that the default settings. The main benefit is a consistent formatinternal.hpp
to build directory instead of source to prevent needless rebuildsCMAKE_VERBOSE_MAKEFILE
, this should be injected by the user if so desired--wrap
option) the "socket" symbol, seewrappers.cpp
vsomeip/example/hello_world
andvsomeip_ctrl
ENABLE_SIGNALS
, but its setting was missing in CMakebacktrace.h
fileVSOMEIP_BASE_PATH
. Upstream defaults this to/var
for QNX which is a very bad choice.Misc