From 996419b0fd56aaf284e4cbe99581f0ef2fc8071d Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sun, 2 Oct 2022 13:43:41 +0530 Subject: [PATCH 1/2] feat: ability to read project major, minor & patch macros --- example_packages/version_test/fpm.toml | 5 +++ .../version_test/src/version_test.f90 | 22 ++++++++++ src/fpm_compiler.f90 | 44 ++++++++++++++++++- src/fpm_targets.f90 | 5 ++- 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 example_packages/version_test/fpm.toml create mode 100644 example_packages/version_test/src/version_test.f90 diff --git a/example_packages/version_test/fpm.toml b/example_packages/version_test/fpm.toml new file mode 100644 index 0000000000..f5b6f87ef9 --- /dev/null +++ b/example_packages/version_test/fpm.toml @@ -0,0 +1,5 @@ +name = "version_test" +version = "0.1.2" + +[preprocess] +[preprocess.cpp] diff --git a/example_packages/version_test/src/version_test.f90 b/example_packages/version_test/src/version_test.f90 new file mode 100644 index 0000000000..499b4bdd63 --- /dev/null +++ b/example_packages/version_test/src/version_test.f90 @@ -0,0 +1,22 @@ +module version_test + implicit none + private + + public :: say_hello +contains + subroutine say_hello + print *, "Hello, version_test!" +#if PROJECT_VERSION_MAJOR != 0 + This breaks the build. +#endif + +#if PROJECT_VERSION_MINOR != 1 + This breaks the build. +#endif + +#if PROJECT_VERSION_PATCH != 2 + This breaks the build. +#endif + + end subroutine say_hello +end module version_test diff --git a/src/fpm_compiler.f90 b/src/fpm_compiler.f90 index 6f19ad1ada..3df445a5ca 100644 --- a/src/fpm_compiler.f90 +++ b/src/fpm_compiler.f90 @@ -44,7 +44,7 @@ module fpm_compiler use fpm_manifest, only : package_config_t use fpm_error, only: error_t implicit none -public :: compiler_t, new_compiler, archiver_t, new_archiver, get_macros +public :: compiler_t, new_compiler, archiver_t, new_archiver, get_macros, get_version_macros public :: debug enum, bind(C) @@ -432,6 +432,48 @@ subroutine set_preprocessor_flags (id, flags, package) end subroutine set_preprocessor_flags +!> Extracts the PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR & PROJECT_VERSION_PATCH macros. +function get_version_macros(id, version) result(version_macros) + !> Compiler id. + integer(compiler_enum), intent(in) :: id + + !> Version number of the target. + character(len=:), allocatable, intent(in) :: version + + character(len=:), allocatable :: version_macros + character(len=:), allocatable :: macro_definition_symbol + character(:), allocatable :: version_parts(:) + integer :: i + + !> Set macro defintion symbol on the basis of compiler used + select case(id) + case default + macro_definition_symbol = "-D" + case (id_intel_classic_windows, id_intel_llvm_windows) + macro_definition_symbol = "/D" + end select + + !> Check if version macros are not allocated. + if (.not.allocated(version_macros)) then + version_macros = " " + end if + + + !> Extract the Major, Minor & Patch number from Version Number. + call split(version, version_parts, delimiters='.') + + do i = 1, size(version_parts) + if (i == 1) then + version_macros = version_macros//macro_definition_symbol//'PROJECT_VERSION_MAJOR'//'='//version_parts(i) + else if (i == 2) then + version_macros = version_macros//' '//macro_definition_symbol//'PROJECT_VERSION_MINOR'//'='//version_parts(i) + else if (i == 3) then + version_macros = version_macros//' '//macro_definition_symbol//'PROJECT_VERSION_PATCH'//'='//version_parts(i) + end if + end do + +end function get_version_macros + !> This function will parse and read the macros list and !> return them as defined flags. function get_macros(id, macros_list, version) result(macros) diff --git a/src/fpm_targets.f90 b/src/fpm_targets.f90 index ca88bb5e35..e8fa917480 100644 --- a/src/fpm_targets.f90 +++ b/src/fpm_targets.f90 @@ -30,7 +30,7 @@ module fpm_targets use fpm_environment, only: get_os_type, OS_WINDOWS, OS_MACOS use fpm_filesystem, only: dirname, join_path, canon_path use fpm_strings, only: string_t, operator(.in.), string_cat, fnv_1a, resize -use fpm_compiler, only: get_macros +use fpm_compiler, only: get_macros, get_version_macros implicit none private @@ -803,6 +803,9 @@ subroutine resolve_target_linking(targets, model) target%compile_flags = target%compile_flags // get_macros(model%compiler%id, & target%macros, & target%version) + + !> Get Version Macro flags. + target%compile_flags = target%compile_flags // get_version_macros(model%compiler%id, target%version) if (len(global_include_flags) > 0) then target%compile_flags = target%compile_flags//global_include_flags From 8186856b33761f343df8dec481c5b063d7659dfb Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sat, 12 Nov 2022 14:38:30 +0530 Subject: [PATCH 2/2] fix: use version_t for version parsing --- src/fpm_compiler.f90 | 51 +++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/src/fpm_compiler.f90 b/src/fpm_compiler.f90 index 3df445a5ca..ffb059e35e 100644 --- a/src/fpm_compiler.f90 +++ b/src/fpm_compiler.f90 @@ -43,6 +43,8 @@ module fpm_compiler use fpm_strings, only: split, string_cat, string_t, str_ends_with, str_begins_with_str use fpm_manifest, only : package_config_t use fpm_error, only: error_t +use fpm_versioning, only: version_t, new_version + implicit none public :: compiler_t, new_compiler, archiver_t, new_archiver, get_macros, get_version_macros public :: debug @@ -457,20 +459,6 @@ function get_version_macros(id, version) result(version_macros) if (.not.allocated(version_macros)) then version_macros = " " end if - - - !> Extract the Major, Minor & Patch number from Version Number. - call split(version, version_parts, delimiters='.') - - do i = 1, size(version_parts) - if (i == 1) then - version_macros = version_macros//macro_definition_symbol//'PROJECT_VERSION_MAJOR'//'='//version_parts(i) - else if (i == 2) then - version_macros = version_macros//' '//macro_definition_symbol//'PROJECT_VERSION_MINOR'//'='//version_parts(i) - else if (i == 3) then - version_macros = version_macros//' '//macro_definition_symbol//'PROJECT_VERSION_PATCH'//'='//version_parts(i) - end if - end do end function get_version_macros @@ -484,7 +472,10 @@ function get_macros(id, macros_list, version) result(macros) character(len=:), allocatable :: macros character(len=:), allocatable :: macro_definition_symbol character(:), allocatable :: valued_macros(:) - + + type(version_t) :: version_object + + call new_version(version_object, version) integer :: i @@ -518,6 +509,36 @@ function get_macros(id, macros_list, version) result(macros) !> Check if the value of macro ends with '}' character. if (str_ends_with(trim(valued_macros(size(valued_macros))), "}")) then + !> Check if the string contains "version%major as substring. + if (index(valued_macros(size(valued_macros)), "version%major") /= 0) then + if (len(macros) == 0) then + macros = macros//macro_definition_symbol//trim(valued_macros(1))//'='//version_object%num(1) + else + macros = macros//' '//macro_definition_symbol//trim(valued_macros(1))//'='//version_object%num(1) + end if + cycle + end if + + !> Check if the string contains "version%minor" as substring. + if (index(valued_macros(size(valued_macros)), "version%minor") /= 0) then + if (len(macros) == 0) then + macros = macros//macro_definition_symbol//trim(valued_macros(1))//'='//version_object%num(2) + else + macros = macros//' '//macro_definition_symbol//trim(valued_macros(1))//'='//version_object%num(2) + end if + cycle + end if + + !> Check if the string contains "version%patch" as substring. + if (index(valued_macros(size(valued_macros)), "version%patch") /= 0) then + if (len(macros) == 0) then + macros = macros//macro_definition_symbol//trim(valued_macros(1))//'='//version_object%num(3) + else + macros = macros//' '//macro_definition_symbol//trim(valued_macros(1))//'='//version_object%num(3) + end if + cycle + end if + !> Check if the string contains "version" as substring. if (index(valued_macros(size(valued_macros)), "version") /= 0) then