Skip to content

Commit e8c0854

Browse files
committed
Merge branch 'main' into backend-output
2 parents b1b6a7b + 68061db commit e8c0854

File tree

7 files changed

+125
-22
lines changed

7 files changed

+125
-22
lines changed

ci/run_tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,9 @@ pushd c_header_only
9898
"$fpm" build
9999
popd
100100

101+
pushd c_main
102+
"$fpm" run
103+
popd
104+
101105
# Cleanup
102106
rm -rf ./*/build

example_packages/c_main/app/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int
2+
main (void)
3+
{
4+
return 0;
5+
}

example_packages/c_main/fpm.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name = "c-main"
2+
3+
[[executable]]
4+
name = "c-main"
5+
main = "main.c"

src/fpm/cmd/new.f90

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ module fpm_cmd_new
5656
use fpm_command_line, only : fpm_new_settings
5757
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, run
59+
use fpm_filesystem, only : fileopen, fileclose, filewrite, warnwrite, which, run
6060
use fpm_strings, only : join, to_fortran_name
6161
use fpm_error, only : fpm_stop
62+
6263
use,intrinsic :: iso_fortran_env, only : stderr=>error_unit
6364
implicit none
6465
private
@@ -572,9 +573,58 @@ subroutine cmd_new(settings)
572573
call create_verified_basic_manifest(join_path(settings%name, 'fpm.toml'))
573574
endif
574575
! assumes git(1) is installed and in path
575-
call run('git init ' // settings%name)
576+
if(which('git').ne.'')then
577+
call run('git init ' // settings%name)
578+
endif
576579
contains
577580

581+
function git_metadata(what) result(returned)
582+
!> get metadata values such as email address and git name from git(1) or return appropriate default
583+
use fpm_filesystem, only : get_temp_filename, getline
584+
character(len=*), intent(in) :: what ! keyword designating what git metatdata to query
585+
character(len=:), allocatable :: returned ! value to return for requested keyword
586+
character(len=:), allocatable :: command
587+
character(len=:), allocatable :: temp_filename
588+
character(len=:), allocatable :: iomsg
589+
character(len=:), allocatable :: temp_value
590+
integer :: stat, unit
591+
temp_filename = get_temp_filename()
592+
! for known keywords set default value for RETURNED and associated git(1) command for query
593+
select case(what)
594+
case('uname')
595+
returned = "Jane Doe"
596+
command = "git config --get user.name > " // temp_filename
597+
case('email')
598+
returned = "[email protected]"
599+
command = "git config --get user.email > " // temp_filename
600+
case default
601+
write(stderr,'(*(g0,1x))')&
602+
& '<ERROR> *git_metadata* unknown metadata name ',trim(what)
603+
returned=''
604+
return
605+
end select
606+
! Execute command if git(1) is in command path
607+
if(which('git')/='')then
608+
call run(command, exitstat=stat)
609+
if (stat /= 0) then ! If command failed just return default
610+
return
611+
else ! Command did not return an error so try to read expected output file
612+
open(file=temp_filename, newunit=unit,iostat=stat)
613+
if(stat == 0)then
614+
! Read file into a scratch variable until status of doing so is checked
615+
call getline(unit, temp_value, stat, iomsg)
616+
if (stat == 0 .and. temp_value /= '') then
617+
! Return output from successful command
618+
returned=temp_value
619+
endif
620+
endif
621+
! Always do the CLOSE because a failed open has unpredictable results.
622+
! Add IOSTAT so a failed close does not cause program to stop
623+
close(unit, status="delete",iostat=stat)
624+
endif
625+
endif
626+
end function git_metadata
627+
578628
subroutine create_verified_basic_manifest(filename)
579629
!> create a basic but verified default manifest file
580630
use fpm_toml, only : toml_table, toml_serializer, set_value
@@ -603,9 +653,9 @@ subroutine create_verified_basic_manifest(filename)
603653
call set_value(table, "name", BNAME)
604654
call set_value(table, "version", "0.1.0")
605655
call set_value(table, "license", "license")
606-
call set_value(table, "author", "Jane Doe")
607-
call set_value(table, "maintainer", "[email protected]")
608-
call set_value(table, "copyright", 'Copyright '//date(1:4)//', Jane Doe')
656+
call set_value(table, "author", git_metadata('uname'))
657+
call set_value(table, "maintainer", git_metadata('email'))
658+
call set_value(table, "copyright", 'Copyright '//date(1:4)//', '//git_metadata('uname'))
609659
! continue building of manifest
610660
! ...
611661
call new_package(package, table, error=error)

src/fpm_command_line.f90

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ module fpm_command_line
3131
use fpm_strings, only : lower, split, fnv_1a, to_fortran_name, is_fortran_name
3232
use fpm_filesystem, only : basename, canon_path, which, run
3333
use fpm_environment, only : get_command_arguments_quoted
34-
use fpm_error, only : fpm_stop
34+
use fpm_error, only : fpm_stop, error_t
35+
use fpm_os, only : get_current_directory
3536
use,intrinsic :: iso_fortran_env, only : stdin=>input_unit, &
3637
& stdout=>output_unit, &
3738
& stderr=>error_unit
39+
3840
implicit none
3941

4042
private
@@ -179,6 +181,7 @@ subroutine get_command_line_settings(cmd_settings)
179181
character(len=*), parameter :: fc_env = "FC", cc_env = "CC", ar_env = "AR", &
180182
& fflags_env = "FFLAGS", cflags_env = "CFLAGS", ldflags_env = "LDFLAGS", &
181183
& fc_default = "gfortran", cc_default = " ", ar_default = " ", flags_default = " "
184+
type(error_t), allocatable :: error
182185

183186
call set_help()
184187
! text for --version switch,
@@ -317,9 +320,13 @@ subroutine get_command_line_settings(cmd_settings)
317320
& help_new, version_text)
318321
select case(size(unnamed))
319322
case(1)
320-
write(stderr,'(*(7x,g0,/))') &
321-
& '<USAGE> fpm new NAME [[--lib|--src] [--app] [--test] [--example]]|[--full|--bare] [--backfill]'
322-
call fpm_stop(1,'directory name required')
323+
if(lget('backfill'))then
324+
name='.'
325+
else
326+
write(stderr,'(*(7x,g0,/))') &
327+
& '<USAGE> fpm new NAME [[--lib|--src] [--app] [--test] [--example]]|[--full|--bare] [--backfill]'
328+
call fpm_stop(1,'directory name required')
329+
endif
323330
case(2)
324331
name=trim(unnamed(2))
325332
case default
@@ -328,6 +335,13 @@ subroutine get_command_line_settings(cmd_settings)
328335
call fpm_stop(2,'only one directory name allowed')
329336
end select
330337
!*! canon_path is not converting ".", etc.
338+
if(name.eq.'.')then
339+
call get_current_directory(name, error)
340+
if (allocated(error)) then
341+
write(stderr, '("[Error]", 1x, a)') error%message
342+
stop 1
343+
endif
344+
endif
331345
name=canon_path(name)
332346
if( .not.is_fortran_name(to_fortran_name(basename(name))) )then
333347
write(stderr,'(g0)') [ character(len=72) :: &
@@ -336,6 +350,7 @@ subroutine get_command_line_settings(cmd_settings)
336350
call fpm_stop(4,' ')
337351
endif
338352

353+
339354
allocate(fpm_new_settings :: cmd_settings)
340355
if (any( specified([character(len=10) :: 'src','lib','app','test','example','bare'])) &
341356
& .and.lget('full') )then
@@ -1079,7 +1094,7 @@ subroutine set_help()
10791094
' fpm new A --full # create example/ and an annotated fpm.toml as well', &
10801095
' fpm new A --bare # create no directories ', &
10811096
' create any missing files in current directory ', &
1082-
' fpm new `pwd` --full --backfill ', &
1097+
' fpm new --full --backfill ', &
10831098
'' ]
10841099
help_test=[character(len=80) :: &
10851100
'NAME ', &

src/fpm_compiler.f90

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module fpm_compiler
3939
OS_UNKNOWN
4040
use fpm_filesystem, only: join_path, basename, get_temp_filename, delete_file, unix_path, &
4141
& getline, run
42-
use fpm_strings, only: string_cat, string_t
42+
use fpm_strings, only: split, string_cat, string_t
4343
implicit none
4444
public :: compiler_t, new_compiler, archiver_t, new_archiver
4545
public :: debug
@@ -60,6 +60,8 @@ module fpm_compiler
6060
id_nvhpc, &
6161
id_nag, &
6262
id_flang, &
63+
id_flang_new, &
64+
id_f18, &
6365
id_ibmxl, &
6466
id_cray, &
6567
id_lahey, &
@@ -383,7 +385,8 @@ function get_include_flag(self, path) result(flags)
383385
case default
384386
flags = "-I "//path
385387

386-
case(id_caf, id_gcc, id_f95, id_cray, id_nvhpc, id_pgi, id_flang, &
388+
case(id_caf, id_gcc, id_f95, id_cray, id_nvhpc, id_pgi, &
389+
& id_flang, id_flang_new, id_f18, &
387390
& id_intel_classic_nix, id_intel_classic_mac, &
388391
& id_intel_llvm_nix, id_lahey, id_nag, id_ibmxl, &
389392
& id_lfortran)
@@ -410,6 +413,9 @@ function get_module_flag(self, path) result(flags)
410413
case(id_nvhpc, id_pgi, id_flang)
411414
flags = "-module "//path
412415

416+
case(id_flang_new, id_f18)
417+
flags = "-module-dir "//path
418+
413419
case(id_intel_classic_nix, id_intel_classic_mac, &
414420
& id_intel_llvm_nix)
415421
flags = "-module "//path
@@ -446,7 +452,7 @@ subroutine get_default_c_compiler(f_compiler, c_compiler)
446452
case(id_intel_llvm_nix,id_intel_llvm_windows)
447453
c_compiler = 'icx'
448454

449-
case(id_flang)
455+
case(id_flang, id_flang_new, id_f18)
450456
c_compiler='clang'
451457

452458
case(id_ibmxl)
@@ -470,22 +476,26 @@ function get_compiler_id(compiler) result(id)
470476
character(len=*), intent(in) :: compiler
471477
integer(kind=compiler_enum) :: id
472478

473-
character(len=:), allocatable :: command, output
479+
character(len=:), allocatable :: full_command, full_command_parts(:), command, output
474480
integer :: stat, io
475481

476482
! Check whether we are dealing with an MPI compiler wrapper first
477483
if (check_compiler(compiler, "mpifort") &
478484
& .or. check_compiler(compiler, "mpif90") &
479485
& .or. check_compiler(compiler, "mpif77")) then
480486
output = get_temp_filename()
481-
call run(compiler//" -showme:command > "//output//" 2>&1", &
487+
call run(compiler//" -show > "//output//" 2>&1", &
482488
& echo=.false., exitstat=stat)
483489
if (stat == 0) then
484490
open(file=output, newunit=io, iostat=stat)
485-
if (stat == 0) call getline(io, command, stat)
491+
if (stat == 0) call getline(io, full_command, stat)
486492
close(io, iostat=stat)
487493

488494
! If we get a command from the wrapper, we will try to identify it
495+
call split(full_command, full_command_parts, delimiters=' ')
496+
if(size(full_command_parts) > 0)then
497+
command = trim(full_command_parts(1))
498+
endif
489499
if (allocated(command)) then
490500
id = get_id(command)
491501
if (id /= id_unknown) return
@@ -557,6 +567,16 @@ function get_id(compiler) result(id)
557567
return
558568
end if
559569

570+
if (check_compiler(compiler, "flang-new")) then
571+
id = id_flang_new
572+
return
573+
end if
574+
575+
if (check_compiler(compiler, "f18")) then
576+
id = id_f18
577+
return
578+
end if
579+
560580
if (check_compiler(compiler, "flang")) then
561581
id = id_flang
562582
return

src/fpm_sources.f90

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,23 @@ subroutine add_executable_sources(sources,executables,scope,auto_discover,error)
163163
if (allocated(executables(i)%link)) then
164164
sources(j)%link_libraries = executables(i)%link
165165
end if
166+
sources(j)%unit_type = FPM_UNIT_PROGRAM
166167
cycle exe_loop
167168

168169
end if
169170

170171
end do
171172

172173
! Add if not already discovered (auto_discovery off)
173-
exe_source = parse_source(join_path(executables(i)%source_dir,executables(i)%main),error)
174-
exe_source%exe_name = executables(i)%name
175-
if (allocated(executables(i)%link)) then
176-
exe_source%link_libraries = executables(i)%link
177-
end if
178-
exe_source%unit_scope = scope
174+
associate(exe => executables(i))
175+
exe_source = parse_source(join_path(exe%source_dir,exe%main),error)
176+
exe_source%exe_name = exe%name
177+
if (allocated(exe%link)) then
178+
exe_source%link_libraries = exe%link
179+
end if
180+
exe_source%unit_type = FPM_UNIT_PROGRAM
181+
exe_source%unit_scope = scope
182+
end associate
179183

180184
if (allocated(error)) return
181185

0 commit comments

Comments
 (0)