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

Add objects for handling compiler and archiver #527

Merged
merged 8 commits into from
Aug 25, 2021
50 changes: 25 additions & 25 deletions src/fpm.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module fpm
use fpm_strings, only: string_t, operator(.in.), glob, join, string_cat
use fpm_strings, only: string_t, operator(.in.), glob, join, string_cat, fnv_1a
use fpm_backend, only: build_package
use fpm_command_line, only: fpm_build_settings, fpm_new_settings, &
fpm_run_settings, fpm_install_settings, fpm_test_settings
Expand All @@ -9,8 +9,7 @@ module fpm
use fpm_model, only: fpm_model_t, srcfile_t, show_model, &
FPM_SCOPE_UNKNOWN, FPM_SCOPE_LIB, FPM_SCOPE_DEP, &
FPM_SCOPE_APP, FPM_SCOPE_EXAMPLE, FPM_SCOPE_TEST
use fpm_compiler, only: get_module_flags, is_unknown_compiler, get_default_c_compiler, &
get_archiver
use fpm_compiler, only: new_compiler, new_archiver


use fpm_sources, only: add_executable_sources, add_sources_from_dir
Expand All @@ -19,12 +18,9 @@ module fpm
FPM_TARGET_EXECUTABLE, FPM_TARGET_ARCHIVE
use fpm_manifest, only : get_package_data, package_config_t
use fpm_error, only : error_t, fatal_error, fpm_stop
use fpm_manifest_test, only : test_config_t
use,intrinsic :: iso_fortran_env, only : stdin=>input_unit, &
& stdout=>output_unit, &
& stderr=>error_unit
use fpm_manifest_dependency, only: dependency_config_t
use, intrinsic :: iso_fortran_env, only: error_unit
implicit none
private
public :: cmd_build, cmd_run
Expand All @@ -43,10 +39,11 @@ subroutine build_model(model, settings, package, error)

integer :: i, j
type(package_config_t) :: dependency
character(len=:), allocatable :: manifest, lib_dir
character(len=:), allocatable :: manifest, lib_dir, flags

logical :: duplicates_found = .false.
type(string_t) :: include_dir
character(len=16) :: build_name

model%package_name = package%name

Expand All @@ -63,27 +60,30 @@ subroutine build_model(model, settings, package, error)
call filewrite(join_path("build", ".gitignore"),["*"])
end if

if(settings%compiler.eq.'')then
model%fortran_compiler = 'gfortran'
call new_compiler(model%compiler, settings%compiler)
call new_archiver(model%archiver)

if (settings%flag == '') then
flags = model%compiler%get_default_flags(settings%profile == "release")
else
model%fortran_compiler = settings%compiler
endif
flags = settings%flag
select case(settings%profile)
case("release", "debug")
flags = flags // model%compiler%get_default_flags(settings%profile == "release")
end select
end if

model%archiver = get_archiver()
call get_default_c_compiler(model%fortran_compiler, model%c_compiler)
model%c_compiler = get_env('FPM_C_COMPILER',model%c_compiler)
write(build_name, '(z16.16)') fnv_1a(flags)

if (is_unknown_compiler(model%fortran_compiler)) then
if (model%compiler%is_unknown()) then
write(*, '(*(a:,1x))') &
"<WARN>", "Unknown compiler", model%fortran_compiler, "requested!", &
"<WARN>", "Unknown compiler", model%compiler%fc, "requested!", &
"Defaults for this compiler might be incorrect"
end if
model%output_directory = join_path('build',basename(model%fortran_compiler)//'_'//settings%build_name)
model%output_directory = join_path('build',basename(model%compiler%fc)//'_'//build_name)

call get_module_flags(model%fortran_compiler, &
& join_path(model%output_directory,model%package_name), &
& model%fortran_compile_flags)
model%fortran_compile_flags = settings%flag // model%fortran_compile_flags
model%fortran_compile_flags = flags // " " // &
& model%compiler%get_module_flag(join_path(model%output_directory, model%package_name))

allocate(model%packages(model%deps%ndep))

Expand Down Expand Up @@ -191,9 +191,9 @@ subroutine build_model(model, settings, package, error)
if (allocated(error)) return

if (settings%verbose) then
write(*,*)'<INFO> BUILD_NAME: ',settings%build_name
write(*,*)'<INFO> COMPILER: ',settings%compiler
write(*,*)'<INFO> C COMPILER: ',model%c_compiler
write(*,*)'<INFO> BUILD_NAME: ',build_name
write(*,*)'<INFO> COMPILER: ',model%compiler%fc
write(*,*)'<INFO> C COMPILER: ',model%compiler%cc
write(*,*)'<INFO> COMPILER OPTIONS: ', model%fortran_compile_flags
write(*,*)'<INFO> INCLUDE DIRECTORIES: [', string_cat(model%include_dirs,','),']'
end if
Expand Down Expand Up @@ -236,7 +236,7 @@ subroutine check_modules_for_duplicates(model, duplicates_found)
if (allocated(model%packages(k)%sources(l)%modules_provided)) then
do m=1,size(model%packages(k)%sources(l)%modules_provided)
if (model%packages(k)%sources(l)%modules_provided(m)%s.in.modules(:modi-1)) then
write(error_unit, *) "Warning: Module ",model%packages(k)%sources(l)%modules_provided(m)%s, &
write(stderr, *) "Warning: Module ",model%packages(k)%sources(l)%modules_provided(m)%s, &
" in ",model%packages(k)%sources(l)%file_name," is a duplicate"
duplicates_found = .true.
else
Expand Down
44 changes: 8 additions & 36 deletions src/fpm_backend.f90
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ module fpm_backend
use,intrinsic :: iso_fortran_env, only : stdin=>input_unit, stdout=>output_unit, stderr=>error_unit
use fpm_error, only : fpm_stop
use fpm_environment, only: run, get_os_type, OS_WINDOWS
use fpm_filesystem, only: basename, dirname, join_path, exists, mkdir, unix_path
use fpm_filesystem, only: basename, dirname, join_path, exists, mkdir
use fpm_model, only: fpm_model_t
use fpm_targets, only: build_target_t, build_target_ptr, FPM_TARGET_OBJECT, &
FPM_TARGET_C_OBJECT, FPM_TARGET_ARCHIVE, FPM_TARGET_EXECUTABLE
use fpm_strings, only: string_cat, string_t

implicit none

private
Expand Down Expand Up @@ -265,31 +263,19 @@ subroutine build_target(model,target,stat)
select case(target%target_type)

case (FPM_TARGET_OBJECT)
call run(model%fortran_compiler//" -c " // target%source%file_name // target%compile_flags &
// " -o " // target%output_file, echo=.true., exitstat=stat)
call model%compiler%compile_fortran(target%source%file_name, target%output_file, &
& target%compile_flags, stat)

case (FPM_TARGET_C_OBJECT)
call run(model%c_compiler//" -c " // target%source%file_name // target%compile_flags &
// " -o " // target%output_file, echo=.true., exitstat=stat)
call model%compiler%compile_c(target%source%file_name, target%output_file, &
& target%compile_flags, stat)

case (FPM_TARGET_EXECUTABLE)

call run(model%fortran_compiler// " " // target%compile_flags &
//" "//target%link_flags// " -o " // target%output_file, echo=.true., exitstat=stat)
call model%compiler%link(target%output_file, &
& target%compile_flags//" "//target%link_flags, stat)

case (FPM_TARGET_ARCHIVE)

select case (get_os_type())
case (OS_WINDOWS)
call write_response_file(target%output_file//".resp" ,target%link_objects)
call run(model%archiver // target%output_file // " @" // target%output_file//".resp", &
echo=.true., exitstat=stat)

case default
call run(model%archiver // target%output_file // " " // string_cat(target%link_objects," "), &
echo=.true., exitstat=stat)

end select
call model%archiver%make_archive(target%output_file, target%link_objects, stat)

end select

Expand All @@ -301,19 +287,5 @@ subroutine build_target(model,target,stat)

end subroutine build_target

!> Response files allow to read command line options from files.
!> Whitespace is used to separate the arguments, we will use newlines
!> as separator to create readable response files which can be inspected
!> in case of errors.
subroutine write_response_file(name, argv)
character(len=*), intent(in) :: name
type(string_t), intent(in) :: argv(:)
integer :: iarg, io
open(file=name, newunit=io)
do iarg = 1, size(argv)
write(io, '(a)') unix_path(argv(iarg)%s)
end do
close(io)
end subroutine write_response_file

end module fpm_backend
19 changes: 1 addition & 18 deletions src/fpm_command_line.f90
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module fpm_command_line
use fpm_strings, only : lower, split, fnv_1a, to_fortran_name, is_fortran_name
use fpm_filesystem, only : basename, canon_path, which
use fpm_environment, only : run, get_command_arguments_quoted
use fpm_compiler, only : get_default_compile_flags
use fpm_error, only : fpm_stop
use,intrinsic :: iso_fortran_env, only : stdin=>input_unit, &
& stdout=>output_unit, &
Expand Down Expand Up @@ -70,7 +69,6 @@ module fpm_command_line
logical :: show_model=.false.
character(len=:),allocatable :: compiler
character(len=:),allocatable :: profile
character(len=:),allocatable :: build_name
character(len=:),allocatable :: flag
end type

Expand Down Expand Up @@ -113,7 +111,7 @@ module fpm_command_line
& ' ', 'fpm', 'new', 'build', 'run', &
& 'test', 'runner', 'install', 'update', 'list', 'help', 'version' ]

character(len=:), allocatable :: val_runner, val_build, val_compiler, val_flag, val_profile
character(len=:), allocatable :: val_runner, val_compiler, val_flag, val_profile

contains
subroutine get_command_line_settings(cmd_settings)
Expand Down Expand Up @@ -199,7 +197,6 @@ subroutine get_command_line_settings(cmd_settings)
if(specified('runner') .and. val_runner.eq.'')val_runner='echo'
cmd_settings=fpm_run_settings(&
& args=remaining,&
& build_name=val_build,&
& profile=val_profile,&
& compiler=val_compiler, &
& flag=val_flag, &
Expand All @@ -223,7 +220,6 @@ subroutine get_command_line_settings(cmd_settings)

allocate( fpm_build_settings :: cmd_settings )
cmd_settings=fpm_build_settings( &
& build_name=val_build,&
& profile=val_profile,&
& compiler=val_compiler, &
& flag=val_flag, &
Expand Down Expand Up @@ -361,7 +357,6 @@ subroutine get_command_line_settings(cmd_settings)
allocate(install_settings)
install_settings = fpm_install_settings(&
list=lget('list'), &
build_name=val_build, &
profile=val_profile,&
compiler=val_compiler, &
flag=val_flag, &
Expand Down Expand Up @@ -417,7 +412,6 @@ subroutine get_command_line_settings(cmd_settings)
if(specified('runner') .and. val_runner.eq.'')val_runner='echo'
cmd_settings=fpm_test_settings(&
& args=remaining, &
& build_name=val_build, &
& profile=val_profile, &
& compiler=val_compiler, &
& flag=val_flag, &
Expand Down Expand Up @@ -487,17 +481,6 @@ subroutine check_build_vals()

val_flag = " " // sget('flag')
val_profile = sget('profile')
if (val_flag == '') then
call get_default_compile_flags(val_compiler, val_profile == "release", val_flag)
else
select case(val_profile)
case("release", "debug")
call get_default_compile_flags(val_compiler, val_profile == "release", flags)
val_flag = flags // val_flag
end select
end if
allocate(character(len=16) :: val_build)
write(val_build, '(z16.16)') fnv_1a(val_flag)

end subroutine check_build_vals

Expand Down
Loading