Skip to content

Commit 1884df7

Browse files
committed
call program
1 parent 6fc413a commit 1884df7

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

cmake/compilers.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ if(NOT f08contig)
7373
endif()
7474

7575
if(CMAKE_Fortran_COMPILER_ID STREQUAL Intel)
76-
set(FFLAGS -stand f18 -traceback -warn)
76+
set(FFLAGS -stand f18 -implicitnone -traceback -warn)
7777

7878
if(CMAKE_BUILD_TYPE STREQUAL Debug)
7979
list(APPEND FFLAGS -debug extended -check all)

system/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ if(NOT hasParent)
77
include(../cmake/compilers.cmake)
88
endif()
99

10-
if(NOT f08command)
10+
if(hasParent AND NOT f08command)
1111
return()
12+
elseif(NOT f08command)
13+
message(FATAL_ERROR "Fortran 2008 execute_command_line necessary for these programs.")
1214
endif()
1315

16+
add_library(osdet os_detect.f90)
17+
target_compile_options(osdet PRIVATE ${FFLAGS})
18+
1419
add_executable(gitrev gitrev.f90)
1520
target_compile_options(gitrev PRIVATE ${FFLAGS})
1621
target_link_libraries(gitrev ${FLIBS})
@@ -24,3 +29,7 @@ add_test(NAME FortranCompiler COMMAND complog)
2429
add_executable(playsound play_sound.f90)
2530
target_compile_options(playsound PRIVATE ${FFLAGS})
2631
target_link_libraries(playsound ${FLIBS})
32+
33+
add_executable(callpython call_python_script.f90)
34+
target_compile_options(callpython PRIVATE ${FFLAGS})
35+
target_link_libraries(callpython osdet ${FLIBS})

system/call_python_script.f90

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
program call_python_script
2+
!! demos calling a Python script, without crashing Fortran program if Python interpreter is not found.
3+
!! this technique works in general, as Fortran will crash if calling a non-existant program.
4+
use os_detect, only: getos
5+
use, intrinsic:: iso_fortran_env, only: stderr=>error_unit
6+
7+
implicit none
8+
9+
integer :: ierr
10+
character(:), allocatable :: cmd, oscmd, os
11+
character(*), parameter :: exe='python3'
12+
13+
cmd = "import psutil; print(f'{psutil.virtual_memory().available} MB RAM free')"
14+
15+
os = getos()
16+
if (os=='windows') then
17+
oscmd = 'where '//exe
18+
elseif(os=='unix') then
19+
oscmd = 'which '//exe
20+
else
21+
error stop 'unknown os '//os
22+
endif
23+
24+
call execute_command_line(oscmd, exitstat=ierr)
25+
26+
if (ierr==0) then
27+
call execute_command_line(exe//' -c "'//cmd//'"', exitstat=ierr)
28+
endif
29+
if (ierr/=0) write(stderr,*) 'Python psutil not available'
30+
31+
end program

system/os_detect.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module os_detect
2+
3+
implicit none
4+
5+
contains
6+
7+
function getos()
8+
!! heuristic detection of operating system based on de factor environment variables
9+
character(256) :: buf
10+
character(:), allocatable :: getos
11+
12+
call get_environment_variable("HOME", buf)
13+
if (len_trim(buf) > 0) then
14+
getos = "unix"
15+
return
16+
endif
17+
18+
call get_environment_variable("USERPROFILE", buf)
19+
if (len_trim(buf) > 0) then
20+
getos = "windows"
21+
return
22+
endif
23+
24+
end function getos
25+
26+
end module os_detect

0 commit comments

Comments
 (0)