Skip to content

Commit b4a266f

Browse files
committed
Implement --flag option for Fortran fpm
1 parent 8cbbd80 commit b4a266f

File tree

3 files changed

+350
-246
lines changed

3 files changed

+350
-246
lines changed

fpm/src/fpm.f90

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module fpm
99
use fpm_model, only: fpm_model_t, srcfile_t, show_model, &
1010
FPM_SCOPE_UNKNOWN, FPM_SCOPE_LIB, FPM_SCOPE_DEP, &
1111
FPM_SCOPE_APP, FPM_SCOPE_EXAMPLE, FPM_SCOPE_TEST
12-
use fpm_compiler, only: add_compile_flag_defaults
12+
use fpm_compiler, only: get_module_flags
1313

1414

1515
use fpm_sources, only: add_executable_sources, add_sources_from_dir
@@ -64,7 +64,10 @@ subroutine build_model(model, settings, package, error)
6464

6565
model%output_directory = join_path('build',basename(model%fortran_compiler)//'_'//settings%build_name)
6666

67-
call add_compile_flag_defaults(settings%build_name, basename(model%fortran_compiler), model)
67+
call get_module_flags(model%fortran_compiler, &
68+
& join_path(model%output_directory,model%package_name), &
69+
& model%fortran_compile_flags)
70+
model%fortran_compile_flags = settings%flag // model%fortran_compile_flags
6871
if(settings%verbose)then
6972
write(*,*)'<INFO>COMPILER OPTIONS: ', model%fortran_compile_flags
7073
endif

fpm/src/fpm_command_line.f90

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ module fpm_command_line
2727
OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_WINDOWS, &
2828
OS_CYGWIN, OS_SOLARIS, OS_FREEBSD
2929
use M_CLI2, only : set_args, lget, sget, unnamed, remaining, specified
30-
use fpm_strings, only : lower, split
30+
use fpm_strings, only : lower, split, fnv_1a
3131
use fpm_filesystem, only : basename, canon_path, to_fortran_name
32+
use fpm_compiler, only : get_default_compile_flags
3233
use,intrinsic :: iso_fortran_env, only : stdin=>input_unit, &
3334
& stdout=>output_unit, &
3435
& stderr=>error_unit
@@ -65,6 +66,7 @@ module fpm_command_line
6566
logical :: show_model=.false.
6667
character(len=:),allocatable :: compiler
6768
character(len=:),allocatable :: build_name
69+
character(len=:),allocatable :: flag
6870
end type
6971

7072
type, extends(fpm_build_settings) :: fpm_run_settings
@@ -106,7 +108,7 @@ module fpm_command_line
106108
& ' ', 'fpm', 'new', 'build', 'run', &
107109
& 'test', 'runner', 'install', 'update', 'list', 'help', 'version' ]
108110

109-
character(len=:), allocatable :: val_runner, val_build, val_compiler
111+
character(len=:), allocatable :: val_runner, val_build, val_compiler, val_flag
110112

111113
contains
112114
subroutine get_command_line_settings(cmd_settings)
@@ -157,6 +159,7 @@ subroutine get_command_line_settings(cmd_settings)
157159
& --example F&
158160
& --runner " " &
159161
& --compiler "'//get_env('FPM_COMPILER','gfortran')//'" &
162+
& --flag:: " "&
160163
& --verbose F&
161164
& --',help_run,version_text)
162165

@@ -192,6 +195,7 @@ subroutine get_command_line_settings(cmd_settings)
192195
& args=remaining,&
193196
& build_name=val_build,&
194197
& compiler=val_compiler, &
198+
& flag=val_flag, &
195199
& example=lget('example'), &
196200
& list=lget('list'),&
197201
& name=names,&
@@ -204,6 +208,7 @@ subroutine get_command_line_settings(cmd_settings)
204208
& --list F &
205209
& --show-model F &
206210
& --compiler "'//get_env('FPM_COMPILER','gfortran')//'" &
211+
& --flag:: " "&
207212
& --verbose F&
208213
& --',help_build,version_text)
209214

@@ -213,6 +218,7 @@ subroutine get_command_line_settings(cmd_settings)
213218
cmd_settings=fpm_build_settings( &
214219
& build_name=val_build,&
215220
& compiler=val_compiler, &
221+
& flag=val_flag, &
216222
& list=lget('list'),&
217223
& show_model=lget('show-model'),&
218224
& verbose=lget('verbose') )
@@ -339,6 +345,7 @@ subroutine get_command_line_settings(cmd_settings)
339345
call set_args('--release F --no-rebuild F --verbose F --prefix " " &
340346
& --list F &
341347
& --compiler "'//get_env('FPM_COMPILER','gfortran')//'" &
348+
& --flag:: " "&
342349
& --libdir "lib" --bindir "bin" --includedir "include"', &
343350
help_install, version_text)
344351

@@ -349,6 +356,7 @@ subroutine get_command_line_settings(cmd_settings)
349356
list=lget('list'), &
350357
build_name=val_build, &
351358
compiler=val_compiler, &
359+
flag=val_flag, &
352360
no_rebuild=lget('no-rebuild'), &
353361
verbose=lget('verbose'))
354362
call get_char_arg(install_settings%prefix, 'prefix')
@@ -373,6 +381,7 @@ subroutine get_command_line_settings(cmd_settings)
373381
& --release F&
374382
& --runner " " &
375383
& --compiler "'//get_env('FPM_COMPILER','gfortran')//'" &
384+
& --flag:: " "&
376385
& --verbose F&
377386
& --',help_test,version_text)
378387

@@ -402,6 +411,7 @@ subroutine get_command_line_settings(cmd_settings)
402411
& args=remaining, &
403412
& build_name=val_build, &
404413
& compiler=val_compiler, &
414+
& flag=val_flag, &
405415
& example=.false., &
406416
& list=lget('list'), &
407417
& name=names, &
@@ -455,7 +465,17 @@ subroutine check_build_vals()
455465
val_compiler='gfortran'
456466
endif
457467

458-
val_build=trim(merge('release','debug ',lget('release')))
468+
val_flag = sget('flag')
469+
if (val_flag == '') then
470+
call get_default_compile_flags(val_compiler, lget('release'), val_flag)
471+
else
472+
if (lget('release')) then
473+
write(stdout,'(a)') &
474+
'<WARNING> --release ignored since explicit --flag argument provided'
475+
end if
476+
end if
477+
allocate(character(len=16) :: val_build)
478+
write(val_build, '(z16.16)') fnv_1a(val_flag)
459479

460480
end subroutine check_build_vals
461481

@@ -516,17 +536,17 @@ subroutine set_help()
516536
' ']
517537
help_list_dash = [character(len=80) :: &
518538
' ', &
519-
' build [--compiler COMPILER_NAME] [--release] [--list] ', &
539+
' build [--compiler COMPILER_NAME] [--release] [--flag FFLAGS] [--list] ', &
520540
' help [NAME(s)] ', &
521541
' new NAME [[--lib|--src] [--app] [--test] [--example]]| ', &
522542
' [--full|--bare][--backfill] ', &
523543
' update [NAME(s)] [--fetch-only] [--clean] [--verbose] ', &
524544
' list [--list] ', &
525-
' run [[--target] NAME(s) [--example] [--release] [--all] [--runner "CMD"] ', &
526-
' [--compiler COMPILER_NAME] [--list] [-- ARGS] ', &
527-
' test [[--target] NAME(s)] [--release] [--runner "CMD"] [--list] ', &
545+
' run [[--target] NAME(s) [--example] [--release] [--flag FFLAGS] [--all] ', &
546+
' [--runner "CMD"] [--compiler COMPILER_NAME] [--list] [-- ARGS] ', &
547+
' test [[--target] NAME(s)] [--release] [--flag FFLAGS] [--runner "CMD"] [--list]', &
528548
' [--compiler COMPILER_NAME] [-- ARGS] ', &
529-
' install [--release] [--no-rebuild] [--prefix PATH] [options] ', &
549+
' install [--release] [--flag FFLAGS] [--no-rebuild] [--prefix PATH] [options] ', &
530550
' ']
531551
help_usage=[character(len=80) :: &
532552
'' ]
@@ -632,24 +652,26 @@ subroutine set_help()
632652
' ', &
633653
' Their syntax is ', &
634654
' ', &
635-
' build [--release] [--list] [--compiler COMPILER_NAME] ', &
655+
' build [--release] [--flag FFLAGS] [--list] [--compiler COMPILER_NAME]', &
636656
' new NAME [[--lib|--src] [--app] [--test] [--example]]| ', &
637-
' [--full|--bare][--backfill] ', &
657+
' [--full|--bare][--backfill] ', &
638658
' update [NAME(s)] [--fetch-only] [--clean] ', &
639-
' run [[--target] NAME(s)] [--release] [--list] [--example] [--all] ', &
640-
' [--runner "CMD"] [--compiler COMPILER_NAME] [-- ARGS] ', &
641-
' test [[--target] NAME(s)] [--release] [--list] ', &
659+
' run [[--target] NAME(s)] [--release] [--flag FFLAGS] [--list] [--example]', &
660+
' [--all] [--runner "CMD"] [--compiler COMPILER_NAME] [-- ARGS] ', &
661+
' test [[--target] NAME(s)] [--release] [--flag FFLAGS] [--list] ', &
642662
' [--runner "CMD"] [--compiler COMPILER_NAME] [-- ARGS] ', &
643663
' help [NAME(s)] ', &
644664
' list [--list] ', &
645-
' install [--release] [--no-rebuild] [--prefix PATH] [options] ', &
665+
' install [--release] [--flag FFLAGS] [--no-rebuild] [--prefix PATH] [options]', &
646666
' ', &
647667
'SUBCOMMAND OPTIONS ', &
648668
' --release Builds or runs in release mode (versus debug mode). fpm(1)', &
649-
' Defaults to using common compiler debug flags and building', &
650-
' in "build/*_debug/". When this flag is present build ', &
651-
' output goes into "build/*_release/" and common compiler ', &
652-
' optimization flags are used. ', &
669+
' Defaults to using common compiler debug flags. ', &
670+
' When this flag is present common compiler optimization flags', &
671+
' are used.', &
672+
' --flag FFLAGS Use compile arguments provided in FFLAGS rather than', &
673+
' defaults from debug or release mode, module flags are', &
674+
' provided by fpm(1) and must not be present in FFLAGS', &
653675
' --list List candidates instead of building or running them. On ', &
654676
' the fpm(1) command this shows a brief list of subcommands.', &
655677
' --runner CMD Provides a command to prefix program execution paths. ', &
@@ -708,8 +730,9 @@ subroutine set_help()
708730
' run(1) - the fpm(1) subcommand to run project applications ', &
709731
' ', &
710732
'SYNOPSIS ', &
711-
' fpm run [[--target] NAME(s)[--release][--compiler COMPILER_NAME] ', &
712-
' [--runner "CMD"] [--example] [--list] [--all] [-- ARGS] ', &
733+
' fpm run [[--target] NAME(s) [--release] [--flag FFLAGS]', &
734+
' [--compiler COMPILER_NAME] [--runner "CMD"] [--example]', &
735+
' [--list] [--all] [-- ARGS]', &
713736
' ', &
714737
' fpm run --help|--version ', &
715738
' ', &
@@ -734,6 +757,7 @@ subroutine set_help()
734757
' --all Run all examples or applications. An alias for --target ''*''. ', &
735758
' --example Run example programs instead of applications. ', &
736759
' --release selects the optimized build instead of the debug build. ', &
760+
' --flags FFLAGS selects compile arguments for the build', &
737761
' --compiler COMPILER_NAME Specify a compiler name. The default is ', &
738762
' "gfortran" unless set by the environment ', &
739763
' variable FPM_COMPILER. ', &
@@ -774,7 +798,7 @@ subroutine set_help()
774798
' build(1) - the fpm(1) subcommand to build a project ', &
775799
' ', &
776800
'SYNOPSIS ', &
777-
' fpm build [--release][--compiler COMPILER_NAME] [-list] ', &
801+
' fpm build [--release] [--flags FFLAGS] [--compiler COMPILER_NAME] [-list]', &
778802
' ', &
779803
' fpm build --help|--version ', &
780804
' ', &
@@ -798,6 +822,7 @@ subroutine set_help()
798822
'OPTIONS ', &
799823
' --release build in build/*_release instead of build/*_debug with ', &
800824
' high optimization instead of full debug options. ', &
825+
' --flags FFLAGS selects compile arguments for the build', &
801826
' --compiler COMPILER_NAME Specify a compiler name. The default is ', &
802827
' "gfortran" unless set by the environment ', &
803828
' variable FPM_COMPILER. ', &
@@ -954,8 +979,8 @@ subroutine set_help()
954979
' test(1) - the fpm(1) subcommand to run project tests ', &
955980
' ', &
956981
'SYNOPSIS ', &
957-
' fpm test [[--target] NAME(s)][--release][--compiler COMPILER_NAME ] ', &
958-
' [--runner "CMD"] [--list][-- ARGS] ', &
982+
' fpm test [[--target] NAME(s)] [--release] [--flag FFLAGS]', &
983+
' [--compiler COMPILER_NAME ] [--runner "CMD"] [--list][-- ARGS]', &
959984
' ', &
960985
' fpm test --help|--version ', &
961986
' ', &
@@ -971,8 +996,8 @@ subroutine set_help()
971996
' any single character and "*" represents any string. ', &
972997
' Note The glob string normally needs quoted to ', &
973998
' protect the special characters from shell expansion.', &
974-
' --release selects the optimized build instead of the debug ', &
975-
' build. ', &
999+
' --release selects the optimized build instead of the debug build. ', &
1000+
' --flags FFLAGS selects compile arguments for the build', &
9761001
' --compiler COMPILER_NAME Specify a compiler name. The default is ', &
9771002
' "gfortran" unless set by the environment ', &
9781003
' variable FPM_COMPILER. ', &
@@ -1021,7 +1046,7 @@ subroutine set_help()
10211046
' install(1) - install fpm projects', &
10221047
'', &
10231048
'SYNOPSIS', &
1024-
' fpm install [--release] [--list] [--no-rebuild] [--prefix DIR]', &
1049+
' fpm install [--release] [--flag FFLAGS] [--list] [--no-rebuild] [--prefix DIR]', &
10251050
' [--bindir DIR] [--libdir DIR] [--includedir DIR]', &
10261051
' [--verbose]', &
10271052
'', &
@@ -1036,6 +1061,7 @@ subroutine set_help()
10361061
' --list list all installable targets for this project,', &
10371062
' but do not install any of them', &
10381063
' --release selects the optimized build instead of the debug build', &
1064+
' --flags FFLAGS selects compile arguments for the build', &
10391065
' --no-rebuild do not rebuild project before installation', &
10401066
' --prefix DIR path to installation directory (requires write access),', &
10411067
' the default prefix on Unix systems is $HOME/.local', &

0 commit comments

Comments
 (0)