2

I want to run my unit tests in all available C++ versions, so I have a directory structure like

tests/
    component/
        tst_component.cpp
        cxx11/
        cxx14/
        cxx17/
        cxx20/

And I compile tst_component.cpp in each of the cxxNN subdirs in C++NN using CONFIG += c++NN. This works well if the compiler supports all these (with 1z for 17 and 2a for 20, for older qmakes). But on our CI, I have one compiler that's too old to understand the -std=c++2a option that CONFIG+=c++2a adds to the command line, so I'd like to conditionally exclude the cxx20 subdir when the compiler doesn't support (even a subset of) c++20.

I know of

CONFIG(QT_CONFIG, c++2a):SUBDIRS += cxx20

but that's testing whether Qt was built in C++20 mode, which is not what I want. It's what I'm using atm, to get the change through the CI, but it means, of course, that the C++20 mode isn't actually tested on the CI until someone installs a Qt built with C++20.

Is there a way that's independent of how Qt is built and doesn't involve maintaining a compiler version whitelist in the build system by hand?

EDIT:20210609 Something like a config test: I try to compile something with CONFIG+=c++2a and if that works, some flag is set (like have_cxx20, enabling me to say have_cxx20:SUBDIRS+=cxx20)?

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90

1 Answers1

2

qmake has a compilation test feature, which you can use to compile a simple source file with a defined set of building flags. See https://doc.qt.io/qt-5/qmake-test-function-reference.html#qtcompiletest-test for the reference, and here is a skeleton for such a project:

mainproject/
   ├── config.tests
   │   └── test
   │       ├── test.cpp
   │       └── test.pro
   ├── main.cpp
   └── mainproject.pro

mainproject/config.tests/test/test.pro:

SOURCES = test.cpp ## can be just a simple main() function, or even testing some c++20 specific features
QMAKE_CXXFLAGS+= -std=c++20

mainproject/mainproject.pro

load(configure)
qtCompileTest(test) {
    message("CPP20 test passed")
} else {
    message("CPP20 test failed")
}

TARGET = mainproject
SOURCES += main.cpp
zgyarmati
  • 1,135
  • 8
  • 15