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)
...
◆ os_cygwin
| integer, parameter, public os_cygwin = 4 |
Cygwin POSIX environment on Windows.
Definition at line 61 of file os.f90.
◆ os_freebsd
| integer, parameter, public os_freebsd = 6 |
FreeBSD and its direct derivatives.
Definition at line 67 of file os.f90.
◆ os_linux
| integer, parameter, public os_linux = 1 |
Linux (any distribution, including GNU/Linux)
Definition at line 52 of file os.f90.
◆ os_macos
| integer, parameter, public os_macos = 2 |
macOS (Darwin-based Apple operating system)
Definition at line 55 of file os.f90.
◆ os_openbsd
| integer, parameter, public os_openbsd = 7 |
OpenBSD.
Definition at line 70 of file os.f90.
◆ os_solaris
| integer, parameter, public os_solaris = 5 |
Oracle Solaris / OpenSolaris derivatives.
Definition at line 64 of file os.f90.
◆ os_unknown
| integer, parameter, public os_unknown = 0 |
Unknown / undetected operating system.
Definition at line 49 of file os.f90.
◆ os_windows
| integer, parameter, public os_windows = 3 |
Microsoft Windows (native, 32-bit or 64-bit)
Definition at line 58 of file os.f90.
◆ os_windowsx86
| integer, parameter, public os_windowsx86 = 8 |
Microsoft Windows — explicitly 32-bit (x86) architecture.
Mainly useful when different behavior is needed between 32-bit and 64-bit Windows
Definition at line 75 of file os.f90.
◆ get_os_type()
| integer function, public get_os_type |
Determine the current operating system type Returns one of the OS_* constants. Detection is performed only on the first call and cached in threadprivate storage for subsequent fast access.
Detection strategy:
- Environment variable
OSTYPE (common on Unix-like systems)
- Environment variable
OS (set on Windows)
- Presence of OS-specific files (/etc/os-release, /usr/bin/sw_vers, etc.)
Returns OS_UNKNOWN if no reliable indicator is found.
- Returns
- OS identifier (OS_LINUX, OS_MACOS, OS_WINDOWS, ...)
Remarks
Definition at line 119 of file os.f90.
◆ os_is_unix()
| logical function, public os_is_unix |
( |
integer, intent(in), optional | os | ) |
|
Return .true. if the current (or supplied) OS is Unix-like Convenience wrapper that returns .true. for any non-Windows platform. Useful for writing portable code that needs different handling on Windows.
- Parameters
-
| [in] | os | Optional OS identifier; if absent get_os_type() is called |
- Returns
- .true. if OS is not Windows, .false. otherwise
Remarks
Definition at line 239 of file os.f90.
◆ os_name()
| pure character(:) function, allocatable, public os_name |
( |
integer, intent(in) | os | ) |
|
Return a human-readable string describing the OS type flag Converts any of the OS_* integer constants into its corresponding name. Useful for logging, error messages, or user output.
- Parameters
-
| [in] | os | OS identifier from get_os_type() |
- Returns
- Allocated character string with the OS name
Remarks
Definition at line 87 of file os.f90.