Operating system detection utilities for the fpx preprocessor This lightweight module provides reliable runtime detection of the current operating system on Unix-like platforms (Linux, macOS, FreeBSD, OpenBSD, Solaris) and Windows (including Cygwin, MSYS, and native Windows). Detection is performed only once per thread (using OpenMP threadprivate storage) and then cached for fast subsequent calls. The implementation first checks common environment variables (OSTYPE, OS), then falls back to the presence of OS-specific files. This makes it robust across native systems, containers, WSL, Cygwin, and cross-compilation environments.
- Basic OS detection:
integer :: my_os
my_os = get_os_type()
print *, 'Running on: ', os_name(my_os)
- Conditional compilation based on OS:
if (os_is_unix()) then
call system('gcc --version')
else
call execute_command_line('gfortran --version')
end if
...
- Using the cached value explicitly:
integer :: os_type
os_type = get_os_type()
print *, os_is_unix(os_type)
...