Loading...
Searching...
No Matches
OS

Definition

This module provides portable runtime operating-system detection facilities used throughout the fpx preprocessor.

Supported platforms include:

  • Linux distributions
  • macOS
  • Native Microsoft Windows
  • Cygwin
  • Solaris/OpenSolaris
  • FreeBSD
  • OpenBSD

Detection is performed lazily on first use and cached using OpenMP threadprivate storage, ensuring negligible overhead for repeated queries.

The implementation relies primarily on environment variables, with fallback detection through the presence of well-known operating-system specific files.

This strategy is designed to work reliably in native installations, containers, WSL environments, and most cross-compilation setups.

Detection Model
OS identification is attempted in the following order:
  1. Environment variable OSTYPE
  2. Environment variable OS
  3. Operating-system specific filesystem probes
  4. Fallback to OS_UNKNOWN

Examples

  1. Basic OS detection:
    integer :: my_os
    my_os = get_os_type()
    print *, 'Running on: ', os_name(my_os)
    !> prints e.g. 'Running on: Linux'
  2. Conditional compilation based on OS:
    !platform specific system call
    if (os_is_unix()) then
    call system('gcc --version')
    else
    call execute_command_line('gfortran --version')
    end if
  3. Using the cached value explicitly:
    integer :: os_type
    os_type = get_os_type() ! detects and caches
    print *, os_is_unix(os_type) ! fast, no re-detection
  4. Module constants
if (get_os_type() == os_windows) then
...
end if

Variables

◆ os_cygwin

integer, parameter, public os_cygwin = 4

Cygwin POSIX environment on Windows.

Definition at line 92 of file os.f90.

◆ os_freebsd

integer, parameter, public os_freebsd = 6

FreeBSD and its direct derivatives.

Definition at line 98 of file os.f90.

◆ os_linux

integer, parameter, public os_linux = 1

Linux (any distribution, including GNU/Linux).

Definition at line 83 of file os.f90.

◆ os_macos

integer, parameter, public os_macos = 2

macOS (Darwin-based Apple operating system)

Definition at line 86 of file os.f90.

◆ os_openbsd

integer, parameter, public os_openbsd = 7

OpenBSD.

Definition at line 101 of file os.f90.

◆ os_solaris

integer, parameter, public os_solaris = 5

Oracle Solaris / OpenSolaris derivatives.

Definition at line 95 of file os.f90.

◆ os_unknown

integer, parameter, public os_unknown = 0

Unknown / undetected operating system.

Definition at line 80 of file os.f90.

◆ os_windows

integer, parameter, public os_windows = 3

Microsoft Windows (native, 32-bit or 64-bit).

Definition at line 89 of file os.f90.

◆ os_windowsx86

integer, parameter, public os_windowsx86 = 8

Native Microsoft Windows running on 32-bit x86 architecture.

This value is returned when the operating system is identified as Windows and the PROCESSOR_ARCHITECTURE environment variable indicates an x86 target.

It can be used when architecture-specific behavior is required.

Definition at line 111 of file os.f90.

Methods

◆ get_os_type()

integer function, public get_os_type

Determine the current operating system type Returns one of the OS_* constants.

Thread Safety
The detected value is cached independently for each OpenMP thread using threadprivate storage. Concurrent calls therefore incur no synchronization overhead after the first query on each thread.

Detection strategy:

  1. Environment variable OSTYPE (common on Unix-like systems)
  2. Environment variable OS (set on Windows)
  3. 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, ...)

Examples

select case (get_os_type())
case (os_windows)
print *, 'Windows'
case (os_linux)
print *, 'Linux'
end select

Definition at line 188 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]osOptional OS identifier; if absent get_os_type() is called
Returns
.true. if OS is not Windows, .false. otherwise

Examples

if (os_is_unix()) then
call execute_command_line('uname -a')
end if

Definition at line 316 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. Accepted values include:

  • OS_UNKNOWN
  • OS_LINUX
  • OS_MACOS
  • OS_WINDOWS
  • OS_WINDOWSx86
  • OS_CYGWIN
  • OS_SOLARIS
  • OS_FREEBSD
  • OS_OPENBSD Useful for logging, error messages, or user output.
    Parameters
    [in]osOS identifier from get_os_type()
    Returns
    Allocated character string with the OS name
    Examples
print *, os_name(os_linux)
!> prints: Linux

Definition at line 140 of file os.f90.