Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-main.f90 auto executables: fix in fpm install #1036

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ci/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ test ! -x ./build/gfortran_*/app/unused
test ! -x ./build/gfortran_*/test/unused_test
popd

pushd auto_with_nondefault_main
"$fpm" build
"$fpm" install --prefix=./installed
test -x ./installed/bin/non_default_name
popd

pushd tree_shake
"$fpm" build
"$fpm" run
Expand Down
2 changes: 2 additions & 0 deletions example_packages/auto_with_nondefault_main/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/*
installed/*
3 changes: 3 additions & 0 deletions example_packages/auto_with_nondefault_main/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# auto_with_nondefault_main
Install auto-executable with non-default source name
fpm install --prefix=/path/to/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
program main
print *, 'hello, world!'
end program main
5 changes: 5 additions & 0 deletions example_packages/auto_with_nondefault_main/fpm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = "auto_with_nondefault_main"
[build]
auto-executables = true
[library]

10 changes: 2 additions & 8 deletions src/fpm.f90
Original file line number Diff line number Diff line change
Expand Up @@ -765,17 +765,11 @@ logical function should_be_run(settings,run_scope,exe_target)

integer :: j

if (exe_target%target_type == FPM_TARGET_EXECUTABLE .and. &
allocated(exe_target%dependencies)) then
if (exe_target%is_executable_target(run_scope)) then

associate(exe_source => exe_target%dependencies(1)%ptr%source)

if (exe_source%unit_scope/=run_scope) then

! Other scope
should_be_run = .false.

elseif (size(settings%name) == 0 .or. .not.settings%list) then
if (size(settings%name) == 0 .or. .not.settings%list) then

! No list of targets
should_be_run = .true.
Expand Down
48 changes: 22 additions & 26 deletions src/fpm/cmd/install.f90
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ subroutine cmd_install(settings)
type(installer_t) :: installer
type(string_t), allocatable :: list(:)
logical :: installable
integer :: ntargets

call get_package_data(package, "fpm.toml", error, apply_defaults=.true.)
call handle_error(error)
Expand All @@ -40,18 +41,17 @@ subroutine cmd_install(settings)
call targets_from_sources(targets, model, settings%prune, error)
call handle_error(error)

call install_info(output_unit, settings%list, targets, ntargets)
if (settings%list) return

installable = (allocated(package%library) .and. package%install%library) &
.or. allocated(package%executable)
.or. allocated(package%executable) .or. ntargets>0

if (.not.installable) then
call fatal_error(error, "Project does not contain any installable targets")
call handle_error(error)
end if

if (settings%list) then
call install_info(output_unit, targets)
return
end if

if (.not.settings%no_rebuild) then
call build_package(targets,model,verbose=settings%verbose)
end if
Expand All @@ -73,18 +73,20 @@ subroutine cmd_install(settings)
end if
end if

if (allocated(package%executable)) then
if (allocated(package%executable) .or. ntargets>0) then
call install_executables(installer, targets, error)
call handle_error(error)
end if

end subroutine cmd_install

subroutine install_info(unit, targets)
subroutine install_info(unit, verbose, targets, ntargets)
integer, intent(in) :: unit
logical, intent(in) :: verbose
type(build_target_ptr), intent(in) :: targets(:)
integer, intent(out) :: ntargets

integer :: ii, ntargets
integer :: ii
type(string_t), allocatable :: install_target(:), temp(:)

allocate(install_target(0))
Expand All @@ -96,12 +98,16 @@ subroutine install_info(unit, targets)
install_target = [install_target, temp]

ntargets = size(install_target)

if (verbose) then

write(unit, '("#", *(1x, g0))') &
"total number of installable targets:", ntargets
do ii = 1, ntargets
write(unit, '("-", *(1x, g0))') install_target(ii)%s
end do
write(unit, '("#", *(1x, g0))') &
"total number of installable targets:", ntargets
do ii = 1, ntargets
write(unit, '("-", *(1x, g0))') install_target(ii)%s
end do

endif

end subroutine install_info

Expand Down Expand Up @@ -129,7 +135,7 @@ subroutine install_executables(installer, targets, error)
integer :: ii

do ii = 1, size(targets)
if (is_executable_target(targets(ii)%ptr)) then
if (targets(ii)%ptr%is_executable_target(FPM_SCOPE_APP)) then
call installer%install_executable(targets(ii)%ptr%output_file, error)
if (allocated(error)) exit
end if
Expand All @@ -138,20 +144,10 @@ subroutine install_executables(installer, targets, error)

end subroutine install_executables

elemental function is_executable_target(target_ptr) result(is_exe)
type(build_target_t), intent(in) :: target_ptr
logical :: is_exe
is_exe = target_ptr%target_type == FPM_TARGET_EXECUTABLE .and. &
allocated(target_ptr%dependencies)
if (is_exe) then
is_exe = target_ptr%dependencies(1)%ptr%source%unit_scope == FPM_SCOPE_APP
end if
end function is_executable_target

subroutine handle_error(error)
type(error_t), intent(in), optional :: error
if (present(error)) then
call fpm_stop(1,error%message)
call fpm_stop(1,'*cmd_install* error: '//error%message)
end if
end subroutine handle_error

Expand Down
7 changes: 5 additions & 2 deletions src/fpm_targets.f90
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ module fpm_targets

!> Version number
character(:), allocatable :: version

contains

procedure :: is_executable_target

end type build_target_t

Expand Down Expand Up @@ -1043,7 +1047,7 @@ end subroutine filter_executable_targets


elemental function is_executable_target(target_ptr, scope) result(is_exe)
type(build_target_t), intent(in) :: target_ptr
class(build_target_t), intent(in) :: target_ptr
integer, intent(in) :: scope
logical :: is_exe
is_exe = target_ptr%target_type == FPM_TARGET_EXECUTABLE .and. &
Expand Down Expand Up @@ -1101,5 +1105,4 @@ function get_feature_flags(compiler, features) result(flags)
end if
end function get_feature_flags


end module fpm_targets
Loading