Skip to content

Commit 0c561b0

Browse files
committed
Apply suggestion: move run to filesystem and use getline
fpm_environment::run is moved to fpm_filesystem so that it can use the getline function to retrieve redirected output from file
1 parent b0115d1 commit 0c561b0

File tree

8 files changed

+84
-85
lines changed

8 files changed

+84
-85
lines changed

src/fpm.f90

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ module fpm
44
use fpm_command_line, only: fpm_build_settings, fpm_new_settings, &
55
fpm_run_settings, fpm_install_settings, fpm_test_settings
66
use fpm_dependency, only : new_dependency_tree
7-
use fpm_environment, only: run, get_env
8-
use fpm_filesystem, only: is_dir, join_path, number_of_rows, list_files, exists, basename, filewrite, mkdir
7+
use fpm_environment, only: get_env
8+
use fpm_filesystem, only: is_dir, join_path, number_of_rows, list_files, exists, &
9+
basename, filewrite, mkdir, run
910
use fpm_model, only: fpm_model_t, srcfile_t, show_model, &
1011
FPM_SCOPE_UNKNOWN, FPM_SCOPE_LIB, FPM_SCOPE_DEP, &
1112
FPM_SCOPE_APP, FPM_SCOPE_EXAMPLE, FPM_SCOPE_TEST

src/fpm/cmd/new.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ module fpm_cmd_new
5454
!> be the first go-to for a CLI utility).
5555

5656
use fpm_command_line, only : fpm_new_settings
57-
use fpm_environment, only : run, OS_LINUX, OS_MACOS, OS_WINDOWS
57+
use fpm_environment, only : OS_LINUX, OS_MACOS, OS_WINDOWS
5858
use fpm_filesystem, only : join_path, exists, basename, mkdir, is_dir
59-
use fpm_filesystem, only : fileopen, fileclose, filewrite, warnwrite
59+
use fpm_filesystem, only : fileopen, fileclose, filewrite, warnwrite, run
6060
use fpm_strings, only : join, to_fortran_name
6161
use fpm_error, only : fpm_stop
6262
use,intrinsic :: iso_fortran_env, only : stderr=>error_unit

src/fpm_backend.F90

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ module fpm_backend
2929

3030
use,intrinsic :: iso_fortran_env, only : stdin=>input_unit, stdout=>output_unit, stderr=>error_unit
3131
use fpm_error, only : fpm_stop
32-
use fpm_environment, only: run, get_os_type, OS_WINDOWS
33-
use fpm_filesystem, only: basename, dirname, join_path, exists, mkdir, LINE_BUFFER_LEN
32+
use fpm_filesystem, only: basename, dirname, join_path, exists, mkdir, run, getline
3433
use fpm_model, only: fpm_model_t
3534
use fpm_strings, only: string_t, operator(.in.)
3635
use fpm_targets, only: build_target_t, build_target_ptr, FPM_TARGET_OBJECT, &
@@ -349,13 +348,13 @@ subroutine print_build_log(target)
349348
type(build_target_t), intent(in), target :: target
350349

351350
integer :: fh, ios
352-
character(LINE_BUFFER_LEN) :: line
351+
character(:), allocatable :: line
353352

354353
if (exists(target%output_log_file)) then
355354

356355
open(newunit=fh,file=target%output_log_file,status='old')
357356
do
358-
read(fh, '(A)', iostat=ios) line
357+
call getline(fh, line, ios)
359358
if (ios /= 0) exit
360359
write(*,'(A)') trim(line)
361360
end do

src/fpm_command_line.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ module fpm_command_line
2929
use M_CLI2, only : set_args, lget, sget, unnamed, remaining, specified
3030
use M_CLI2, only : get_subcommand, CLI_RESPONSE_FILE
3131
use fpm_strings, only : lower, split, fnv_1a, to_fortran_name, is_fortran_name
32-
use fpm_filesystem, only : basename, canon_path, which
33-
use fpm_environment, only : run, get_command_arguments_quoted
32+
use fpm_filesystem, only : basename, canon_path, which, run
33+
use fpm_environment, only : get_command_arguments_quoted
3434
use fpm_error, only : fpm_stop
3535
use,intrinsic :: iso_fortran_env, only : stdin=>input_unit, &
3636
& stdout=>output_unit, &

src/fpm_compiler.f90

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
! Unisys ? ? ? ? ? discontinued
2828
module fpm_compiler
2929
use fpm_environment, only: &
30-
run, &
3130
get_env, &
3231
get_os_type, &
3332
OS_LINUX, &
@@ -39,7 +38,7 @@ module fpm_compiler
3938
OS_OPENBSD, &
4039
OS_UNKNOWN
4140
use fpm_filesystem, only: join_path, basename, get_temp_filename, delete_file, unix_path, &
42-
& getline
41+
& getline, run
4342
use fpm_strings, only: string_cat, string_t
4443
implicit none
4544
public :: compiler_t, new_compiler, archiver_t, new_archiver

src/fpm_environment.f90

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module fpm_environment
1111
private
1212
public :: get_os_type
1313
public :: os_is_unix
14-
public :: run
1514
public :: get_env
1615
public :: get_command_arguments_quoted
1716
public :: separator
@@ -157,74 +156,6 @@ logical function os_is_unix(os) result(unix)
157156
unix = build_os /= OS_WINDOWS
158157
end function os_is_unix
159158

160-
!> echo command string and pass it to the system for execution
161-
subroutine run(cmd,echo,exitstat,verbose,redirect)
162-
character(len=*), intent(in) :: cmd
163-
logical,intent(in),optional :: echo
164-
integer, intent(out),optional :: exitstat
165-
logical, intent(in), optional :: verbose
166-
character(*), intent(in), optional :: redirect
167-
168-
logical :: echo_local, verbose_local
169-
character(:), allocatable :: redirect_str
170-
character(1000) :: line
171-
integer :: stat, fh, ios
172-
173-
174-
if(present(echo))then
175-
echo_local=echo
176-
else
177-
echo_local=.true.
178-
end if
179-
180-
if(present(verbose))then
181-
verbose_local=verbose
182-
else
183-
verbose_local=.true.
184-
end if
185-
186-
if (present(redirect)) then
187-
redirect_str = ">"//redirect//" 2>&1"
188-
else
189-
if(verbose_local)then
190-
! No redirection but verbose output
191-
redirect_str = ""
192-
else
193-
! No redirection and non-verbose output
194-
if (os_is_unix()) then
195-
redirect_str = ">/dev/null 2>&1"
196-
else
197-
redirect_str = ">NUL 2>&1"
198-
end if
199-
end if
200-
end if
201-
202-
if(echo_local) print *, '+ ', cmd
203-
204-
call execute_command_line(cmd//redirect_str, exitstat=stat)
205-
206-
if (verbose_local.and.present(redirect)) then
207-
208-
open(newunit=fh,file=redirect,status='old')
209-
do
210-
read(fh, '(A)', iostat=ios) line
211-
if (ios /= 0) exit
212-
write(*,'(A)') trim(line)
213-
end do
214-
close(fh)
215-
216-
end if
217-
218-
if (present(exitstat)) then
219-
exitstat = stat
220-
else
221-
if (stat /= 0) then
222-
call fpm_stop(1,'*run*:Command failed')
223-
end if
224-
end if
225-
226-
end subroutine run
227-
228159
!> get named environment variable value. It it is blank or
229160
!! not set return the optional default value
230161
function get_env(NAME,DEFAULT) result(VALUE)

src/fpm_filesystem.F90

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module fpm_filesystem
55
use fpm_environment, only: get_os_type, &
66
OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_WINDOWS, &
77
OS_CYGWIN, OS_SOLARIS, OS_FREEBSD, OS_OPENBSD
8-
use fpm_environment, only: separator, get_env
8+
use fpm_environment, only: separator, get_env, os_is_unix
99
use fpm_strings, only: f_string, replace, string_t, split, notabs
1010
use iso_c_binding, only: c_char, c_ptr, c_int, c_null_char, c_associated, c_f_pointer
1111
use fpm_error, only : fpm_stop
@@ -15,7 +15,7 @@ module fpm_filesystem
1515
mkdir, exists, get_temp_filename, windows_path, unix_path, getline, delete_file
1616
public :: fileopen, fileclose, filewrite, warnwrite, parent_dir
1717
public :: read_lines, read_lines_expanded
18-
public :: which, LINE_BUFFER_LEN
18+
public :: which, run, LINE_BUFFER_LEN
1919

2020
integer, parameter :: LINE_BUFFER_LEN = 1000
2121

@@ -850,4 +850,73 @@ function which(command) result(pathname)
850850
enddo SEARCH
851851
end function which
852852

853+
!> echo command string and pass it to the system for execution
854+
subroutine run(cmd,echo,exitstat,verbose,redirect)
855+
character(len=*), intent(in) :: cmd
856+
logical,intent(in),optional :: echo
857+
integer, intent(out),optional :: exitstat
858+
logical, intent(in), optional :: verbose
859+
character(*), intent(in), optional :: redirect
860+
861+
logical :: echo_local, verbose_local
862+
character(:), allocatable :: redirect_str
863+
character(:), allocatable :: line
864+
integer :: stat, fh, ios
865+
866+
867+
if(present(echo))then
868+
echo_local=echo
869+
else
870+
echo_local=.true.
871+
end if
872+
873+
if(present(verbose))then
874+
verbose_local=verbose
875+
else
876+
verbose_local=.true.
877+
end if
878+
879+
if (present(redirect)) then
880+
redirect_str = ">"//redirect//" 2>&1"
881+
else
882+
if(verbose_local)then
883+
! No redirection but verbose output
884+
redirect_str = ""
885+
else
886+
! No redirection and non-verbose output
887+
if (os_is_unix()) then
888+
redirect_str = ">/dev/null 2>&1"
889+
else
890+
redirect_str = ">NUL 2>&1"
891+
end if
892+
end if
893+
end if
894+
895+
if(echo_local) print *, '+ ', cmd
896+
897+
call execute_command_line(cmd//redirect_str, exitstat=stat)
898+
899+
if (verbose_local.and.present(redirect)) then
900+
901+
open(newunit=fh,file=redirect,status='old')
902+
do
903+
call getline(fh, line, ios)
904+
if (ios /= 0) exit
905+
write(*,'(A)') trim(line)
906+
end do
907+
close(fh)
908+
909+
end if
910+
911+
if (present(exitstat)) then
912+
exitstat = stat
913+
else
914+
if (stat /= 0) then
915+
call fpm_stop(1,'*run*:Command failed')
916+
end if
917+
end if
918+
919+
end subroutine run
920+
921+
853922
end module fpm_filesystem

test/new_test/new_test.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
program new_test
22
use,intrinsic :: iso_fortran_env, only : stdin=>input_unit, stdout=>output_unit, stderr=>error_unit
33
use fpm_filesystem, only : is_dir, list_files, exists, windows_path, join_path, &
4-
dirname
4+
dirname, run
55
use fpm_strings, only : string_t, operator(.in.)
6-
use fpm_environment, only : run, get_os_type
6+
use fpm_environment, only : get_os_type
77
use fpm_environment, only : OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_CYGWIN, OS_SOLARIS, OS_FREEBSD, OS_OPENBSD, OS_WINDOWS
88
implicit none
99
type(string_t), allocatable :: file_names(:)

0 commit comments

Comments
 (0)