How can I check in CMake whether my system is POSIX-conformant?

327 Views Asked by At

I want to try and build a source file only if I'm on a POSIX system (let's ignore cross-building for the sake of this question). I know how to check that I'm on a UNIX system; I know how to check for the presence of header files; but - how do I check for full POSIX conformance?

Notes:

  • For the sake of specificity, I want to check for POSIX.1-2008 and POSIX.1c.
  • I care about conformance in practice, not official certification.
  • You may assume CMake 3.25 or later (but if you can avoid that assumption, that's also nice).
1

There are 1 best solutions below

2
starball On

One way basic way to do this is just check CMAKE_HOST_SYSTEM_NAME (assuming you really want the host system name and not the target system name (CMAKE_HOST_NAME)):

Name of the OS CMake is running on.

On systems that have the uname command, this variable is set to the output of uname -s. Linux, Windows, and Darwin for macOS are the values found on the big three operating systems.

You can look at the doc comment in Modules/CMakeDetermineSystem.cmake to see the list of possible values.

The list of well-known operating systems that are POSIX certified is pretty short (https://en.wikipedia.org/wiki/POSIX#POSIX-certified). Of those that are listed in the Wikipedia page and the CMakeDetermineSystem.cmake, I see AIX, HP-UX, Darwin (macOS), SCO_SV (OpenServer 5), UnixWare,

I don't see INTEGRITY, VxWorks, or z/OS in the list of documented possible values, but that might just be a lapse in documentation and not necessarily that they aren't supported. From https://en.wikipedia.org/wiki/Uname#Examples, I see that uname -s for "z/OS USS" gives OS/390. That table doesn't have info for INTEGRITY or VxWare though.

Related on the point of there not being a lot of POSIX-certified OSes: Why are most Linux distributions not POSIX-compliant?

Something like this:

set(POSIX_SYSTEM_NAMES "AIX;HP-UX;Darwin;SCO_SV;UnixWare;OS/360")
if(NOT ("${CMAKE_HOST_SYSTEM_NAME}" IN_LIST POSIX_SYSTEM_NAMES))
  message(FATAL_ERROR "This system is not a fully POSIX compliant system")
endif()