diff options
-rw-r--r-- | fpm/src/fpm.f90 | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/fpm/src/fpm.f90 b/fpm/src/fpm.f90 index 85597c0..cc5666b 100644 --- a/fpm/src/fpm.f90 +++ b/fpm/src/fpm.f90 @@ -3,10 +3,66 @@ implicit none private public :: print_help, cmd_build +integer, parameter :: OS_LINUX = 1 +integer, parameter :: OS_MACOS = 2 +integer, parameter :: OS_WINDOWS = 3 + contains +integer function get_os_type() result(r) +! Determine the OS type +! +! Returns one of OS_LINUX, OS_MACOS, OS_WINDOWS. +! +! Currently we use the $HOME and $HOMEPATH environment variables to determine +! the OS type. That is not 100% accurate in all cases, but it seems to be good +! enough for now. See the following issue for a more robust solution: +! +! https://github.com/fortran-lang/fpm/issues/144 +! +character(len=100) :: val +integer stat +! Only Windows define $HOMEPATH by default and we test its value to improve the +! chances of it working even if a user defines $HOMEPATH on Linux or macOS. +call get_environment_variable("HOMEPATH", val, status=stat) +if (stat == 0 .and. val(1:7) == "\Users\") then + r = OS_WINDOWS + return +end if + +! We assume that $HOME=/home/... is Linux, $HOME=/Users/... is macOS, otherwise +! we assume Linux. This is only a heuristic and can easily fail. +call get_environment_variable("HOME", val, status=stat) +if (stat == 1) then + print *, "$HOME does not exist" + error stop +end if +if (stat /= 0) then + print *, "get_environment_variable() failed" + error stop +end if +if (val(1:6) == "/home/") then + r = OS_LINUX +else if (val(1:7) == "/Users/") then + r = OS_MACOS +else + ! This will happen on HPC systems that typically do not use either /home nor + ! /Users for $HOME. Those systems are typically Linux, so for now we simply + ! set Linux here. + r = OS_LINUX +end if +end function + subroutine print_help() print *, "Fortran Package Manager (fpm)" +select case (get_os_type()) + case (OS_LINUX) + print *, "OS Type: Linux" + case (OS_MACOS) + print *, "OS Type: macOS" + case (OS_WINDOWS) + print *, "OS Type: Windows" +end select end subroutine subroutine run(cmd) |