Skipping/selecting platform specific source files

I’m looking for a good way to select when platform specific source files should be included in the build. If I for example have “foo_linux.cpp” and “foo_windows.cpp”, I would like to select the appropriate version with as little extra work as possible. Currently I have if() statements in my CMakeLists.txt calling target_sources() differently depending on platform.

But I was hoping to be able to have files more or less automatically selected based for example on filename suffixes (as in my example names above).

Is there some “best practice” in this area with CMake?

Johan Holmberg

I use CMAKE_SYSTEM_NAME for that.
In particular, I set custom variable PLATFORM_SUFFIX in a central place based on the CMAKE_SYSTEM_NAME (and few others in my case). Later I can use it in the source list like:
add_executable(myexe main.cpp something${PLATFORM_SUFFIX}.cpp).