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

Tests tweaks #38

Merged
merged 3 commits into from
Nov 13, 2021
Merged

Tests tweaks #38

merged 3 commits into from
Nov 13, 2021

Conversation

Tachi107
Copy link
Contributor

The changes are not that big, but I decided to split them in multiple commits because every one of them does a different thing. They are all related though, so I prepared a single PR. Here's their description (from the commit messages):

test: make tests runnable with ctest

This helps with automated testing, as tests can be ran with ctest or by "building" the target test

I also changed the way the C++ standard version is handled; the tests require C++17, and this was previously accomplished by manually passing the -std=c++17 flag, while the "CMake way" of doing this is to set the CXX_STANDARD property. Unfortunately the value 17 of the property was only added in CMake 3.8, so when using older CMake versions the flag is still passed manually. I've also set the CXX_EXTENSIONS and CXX_STANDARD_REQUIRED to false and true, respectively, to ensure that -std=c++17 gets passed instead of -std=gnu++17 on GCC-like compilers and that CMake doesn't fallback to older standards versions when the requested standard is not available.

build: use system GTest when available

This reduces compile times and allows tests to be ran in environments without internet access (e.g. when building a Debian package).

CMake will look for an installed copy of GoogleTest on the system, and will use it if found, and will fallback to download and unpack it otherwise.

There are a lot of checks involved since CMake changed the way FindGTest works in newer CMake versions, and they are needed to ensure 3.1 compatibility.

build: use -pthread when possible

Using -pthread is the "right way" of enabling threading support in GCC and similar, and this can accomplished with the THREADS_PREFER_PTHREAD_FLAG option. Its use is also recommended by the CMake devs, see cmake.org/cmake/help/latest/module/FindThreads.html

More words than code changes :)

Copy link
Member

@flobernd flobernd left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you. Let's wait for @athre0z to review as well.

Copy link
Member

@athre0z athre0z left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM! :)

Unfortunately the value 17 of the property was only added in CMake 3.8, so when using older CMake versions the flag is still passed manually

Yes, this was why I originally went with the target_compile_options approach. However, like mentioned in the comments, I don't think this is necessary any longer.

CMakeLists.txt Outdated
Comment on lines 264 to 269
# CXX_STANDARD 17 is only available in CMake 3.8 and newer
if (CMAKE_VERSION VERSION_GREATER "3.7")
set_target_properties("Test${test}" PROPERTIES CXX_STANDARD 17)
elseif(NOT MSVC)
target_compile_options("Test${test}" PRIVATE "-std=c++17")
endif ()
Copy link
Member

Choose a reason for hiding this comment

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

Feel free to just raise the minimum required version to 3.8. Even the old-stable versions of both Ubuntu and Debian have newer packages these days, so I don't think we have to support even older stuff any longer.

I'd also assume this to be broken on e.g. CMake 3.7.1, because that would technically be greater than 3.7?

The documentation says:

omitted components are treated as zero

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Feel free to just raise the minimum required version to 3.8

Oh ok, I'll update the PR.

I'd also assume this to be broken on e.g. CMake 3.7.1, because that would technically be greater than 3.7?

Yeah you're right, I missed that

Copy link
Member

Choose a reason for hiding this comment

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

I think Gtest anyways started complaining about our old CMake version last time I configured/generated from scratch ^^

Copy link
Contributor Author

@Tachi107 Tachi107 Nov 13, 2021

Choose a reason for hiding this comment

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

I think Gtest anyways started complaining about our old CMake version last time I configured/generated from scratch

That's because GTest 1.10 uses a deprecated minimum required version, see google/googletest#3040 and google/googletest#3523

CMakeLists.txt Outdated
Comment on lines 70 to 76
# CMake 3.20 and upstream GTestConfig.cmake
if (TARGET GTest::gtest)
add_library(gtest ALIAS GTest::gtest)
# CMake 3.5 and newer
elseif (TARGET GTest::GTest)
add_library(gtest ALIAS GTest::GTest)
# Older CMake versions
else ()
add_library(gtest INTERFACE)
find_package(Threads REQUIRED)
target_include_directories(gtest INTERFACE "${GTEST_INCLUDE_DIRS}")
target_link_libraries(gtest INTERFACE "${GTEST_LIBRARIES}" Threads::Threads)
endif ()
Copy link
Member

Choose a reason for hiding this comment

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

Like with the other comment, feel free to just raise the required CMake version. The oldest non-EOL Ubuntu version (18.04) has CMake 3.10.2, oldest non-EOL Debian (buster) has 3.13.4. Since these two distros are typically the slowest to upgrade, it's pretty safe to assume that all other important ones have newer versions already. I'm not particularly concerned with the commercial extended support releases.

This helps with automated testing, as tests can be ran with `ctest` or
by "building" the target `test`

I also changed the way the C++ standard version is handled; the tests
require C++17, and this was previously accomplished by manually passing
the `-std=c++17` flag, while the "CMake way" of doing this is to set the
`CXX_STANDARD` property. Unfortunately the value `17` of the property
was only added in CMake 3.8, so I bumped the minimum required version.
I've also set the `CXX_EXTENSIONS` and `CXX_STANDARD_REQUIRED` to false
and true, respectively, to ensure that `-std=c++17` gets passed instead
of `-std=gnu++17` on GCC-like compilers and that CMake doesn't fallback
to older standards versions when the requested standard is not available
This reduces compile times and allows tests to be ran in environments
without internet access (e.g. when building a Debian package).

CMake will look for an installed copy of GoogleTest on the system, and
will use it if found, and will fallback to download and unpack it
otherwise.
Using -pthread is the "right way" of enabling threading support in GCC
and similar, and this can accomplished with the
THREADS_PREFER_PTHREAD_FLAG option. Its use is also recommended by the
CMake devs, see
https://cmake.org/cmake/help/latest/module/FindThreads.html#variable:THREADS_PREFER_PTHREAD_FLAG
@athre0z athre0z merged commit 8983325 into zyantific:master Nov 13, 2021
@Tachi107 Tachi107 deleted the tests-tweaks branch November 13, 2021 23:13
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.

3 participants