diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000000..8ac6b8c498 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "monthly" diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 08b8ed725a..4d97f92f1f 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -22,7 +22,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-11, windows-latest] - gcc_v: [10] # Version of GFortran we want to use. + gcc_v: [10,11,12,13] # Version of GFortran we want to use. include: - os: ubuntu-latest os-arch: linux-x86_64 @@ -43,7 +43,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v1 + uses: actions/checkout@v4 - name: Install GFortran macOS if: contains(matrix.os, 'macos') @@ -194,7 +194,7 @@ jobs: - build steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Download Artifacts uses: actions/download-artifact@v2 @@ -277,7 +277,7 @@ jobs: env: DEPLOY_BRANCH: ${{ secrets.DEPLOY_BRANCH && contains(github.ref, secrets.DEPLOY_BRANCH) && 1 || 0 }} - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 if: ${{ github.event_name == 'push' }} - name: Download Artifacts diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 2ecb4183a1..e3418fe23a 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -6,8 +6,8 @@ jobs: build-and-deploy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v1 + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 with: python-version: '3.x' - name: Install dependencies diff --git a/.github/workflows/meta.yml b/.github/workflows/meta.yml index 5deae0c166..be6a46dcb9 100644 --- a/.github/workflows/meta.yml +++ b/.github/workflows/meta.yml @@ -35,8 +35,6 @@ jobs: mpi: openmpi - os: ubuntu-latest mpi: mpich - - os: windows-latest - mpi: msmpi - os: macos-latest mpi: openmpi - os: macos-latest @@ -45,7 +43,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v1 + uses: actions/checkout@v4 - name: (Ubuntu) setup gcc version if: contains(matrix.os,'ubuntu') @@ -106,16 +104,14 @@ jobs: if: contains(matrix.os,'ubuntu') && contains(matrix.mpi,'intel') timeout-minutes: 1 run: | - wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB - sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB - rm GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB - echo "deb https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list + wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null + echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list sudo apt-get update - name: (Ubuntu) Install Intel oneAPI if: contains(matrix.os,'ubuntu') && contains(matrix.mpi,'intel') - timeout-minutes: 5 - run: sudo apt-get install intel-oneapi-compiler-fortran intel-oneapi-compiler-dpcpp-cpp-and-cpp-classic intel-oneapi-mpi intel-oneapi-mpi-devel intel-oneapi-mkl ninja-build + timeout-minutes: 15 + run: sudo apt-get install intel-oneapi-compiler-dpcpp-cpp-2023.1.0 intel-oneapi-compiler-fortran-2023.1.0 intel-oneapi-mpi-devel ninja-build - name: (Ubuntu) Setup Intel oneAPI environment if: contains(matrix.os,'ubuntu') && contains(matrix.mpi,'intel') @@ -152,6 +148,7 @@ jobs: if: contains(matrix.os,'windows') && contains(matrix.mpi,'msmpi') run: | echo "C:\Program Files\Microsoft MPI\Bin\" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append + echo "/c/Program Files/Microsoft MPI/Bin/" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append echo "MSMPI_BIN=C:\Program Files\Microsoft MPI\Bin\" | Out-File -FilePath $env:GITHUB_ENV -Append - name: (Windows) load OneAPI environment variables @@ -216,9 +213,9 @@ jobs: if: contains(matrix.mpi,'intel') shell: bash run: | - echo "FPM_FC=ifort" >> $GITHUB_ENV - echo "FPM_CC=icc" >> $GITHUB_ENV - echo "FPM_CXX=icpc" >> $GITHUB_ENV + echo "FPM_FC=ifx" >> $GITHUB_ENV + echo "FPM_CC=icx" >> $GITHUB_ENV + echo "FPM_CXX=icpx" >> $GITHUB_ENV - name: (macOS) Use gcc/g++ instead of Clang for C/C++ if: contains(matrix.os,'macOS') diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b4b030d5a9..b14537c8c5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,7 +17,7 @@ jobs: FORMAT: ${{ matrix.format }} steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: fetch-depth: 0 @@ -44,7 +44,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: fetch-depth: 0 @@ -124,7 +124,7 @@ jobs: env: DEPLOY_BRANCH: ${{ secrets.DEPLOY_BRANCH && contains(github.ref, secrets.DEPLOY_BRANCH) && 1 || 0 }} - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 if: ${{ github.event_name == 'push' }} - name: Download Artifacts diff --git a/ci/run_tests.sh b/ci/run_tests.sh index d84a00f1c5..c31f134312 100755 --- a/ci/run_tests.sh +++ b/ci/run_tests.sh @@ -150,6 +150,10 @@ pushd preprocess_cpp_deps "$fpm" build popd +pushd preprocess_cpp_suffix +"$fpm" run +popd + pushd preprocess_per_dependency "$fpm" run popd diff --git a/example_packages/preprocess_cpp_suffix/.gitignore b/example_packages/preprocess_cpp_suffix/.gitignore new file mode 100644 index 0000000000..a007feab07 --- /dev/null +++ b/example_packages/preprocess_cpp_suffix/.gitignore @@ -0,0 +1 @@ +build/* diff --git a/example_packages/preprocess_cpp_suffix/app/main.f90 b/example_packages/preprocess_cpp_suffix/app/main.f90 new file mode 100644 index 0000000000..7d77b56a06 --- /dev/null +++ b/example_packages/preprocess_cpp_suffix/app/main.f90 @@ -0,0 +1,8 @@ +program test_preprocess_suffix + use preprocess_cpp +#ifndef TESTMACRO + stop -1 +#else + stop 0 +#endif +end program test_preprocess_suffix diff --git a/example_packages/preprocess_cpp_suffix/fpm.toml b/example_packages/preprocess_cpp_suffix/fpm.toml new file mode 100644 index 0000000000..6399f1a67d --- /dev/null +++ b/example_packages/preprocess_cpp_suffix/fpm.toml @@ -0,0 +1,7 @@ +name = "preprocess_cpp_suffix" +version = "1" + +[preprocess] +[preprocess.cpp] +macros = ["TESTMACRO", "TESTMACRO2=3", "TESTMACRO3={version}"] +suffixes = ["fpp"] diff --git a/example_packages/preprocess_cpp_suffix/src/preprocess_cpp.fpp b/example_packages/preprocess_cpp_suffix/src/preprocess_cpp.fpp new file mode 100644 index 0000000000..d7ab5d1485 --- /dev/null +++ b/example_packages/preprocess_cpp_suffix/src/preprocess_cpp.fpp @@ -0,0 +1,22 @@ +module preprocess_cpp + implicit none + private + + public :: say_hello +contains + subroutine say_hello + print *, "Hello, preprocess_cpp!" +#ifndef TESTMACRO + This breaks the build. +#endif + +#if TESTMACRO2 != 3 + This breaks the build. +#endif + +#if TESTMACRO3 != 1 + This breaks the build. +#endif + + end subroutine say_hello +end module preprocess_cpp diff --git a/fpm.toml b/fpm.toml index 1da1a00dcf..927889e227 100644 --- a/fpm.toml +++ b/fpm.toml @@ -1,5 +1,5 @@ name = "fpm" -version = "0.9.0" +version = "0.10.0" license = "MIT" author = "fpm maintainers" maintainer = "" @@ -18,6 +18,8 @@ fortran-regex.git = "https://github.com/perazz/fortran-regex" fortran-regex.tag = "1.1.2" jonquil.git = "https://github.com/toml-f/jonquil" jonquil.rev = "4fbd4cf34d577c0fd25e32667ee9e41bf231ece8" +fortran-shlex.git = "https://github.com/perazz/fortran-shlex" +fortran-shlex.tag = "1.0.1" [[test]] name = "cli-test" diff --git a/src/fpm.f90 b/src/fpm.f90 index 8ed5fed0b1..ae8a07d681 100644 --- a/src/fpm.f90 +++ b/src/fpm.f90 @@ -111,37 +111,23 @@ subroutine build_model(model, settings, package, error) model%packages(i)%version = package%version%s() !> Add this dependency's manifest macros - allocate(model%packages(i)%macros(0)) + call model%packages(i)%preprocess%destroy() if (allocated(dependency%preprocess)) then do j = 1, size(dependency%preprocess) - if (dependency%preprocess(j)%name == "cpp") then - if (.not. has_cpp) has_cpp = .true. - if (allocated(dependency%preprocess(j)%macros)) then - model%packages(i)%macros = [model%packages(i)%macros, dependency%preprocess(j)%macros] - end if - else - write(stderr, '(a)') 'Warning: Preprocessor ' // package%preprocess(i)%name // & - ' is not supported; will ignore it' - end if + call model%packages(i)%preprocess%add_config(dependency%preprocess(j)) end do end if !> Add this dependency's package-level macros if (allocated(dep%preprocess)) then do j = 1, size(dep%preprocess) - if (dep%preprocess(j)%name == "cpp") then - if (.not. has_cpp) has_cpp = .true. - if (allocated(dep%preprocess(j)%macros)) then - model%packages(i)%macros = [model%packages(i)%macros, dep%preprocess(j)%macros] - end if - else - write(stderr, '(a)') 'Warning: Preprocessor ' // package%preprocess(i)%name // & - ' is not supported; will ignore it' - end if + call model%packages(i)%preprocess%add_config(dep%preprocess(j)) end do end if + if (model%packages(i)%preprocess%is_cpp()) has_cpp = .true. + if (.not.allocated(model%packages(i)%sources)) allocate(model%packages(i)%sources(0)) if (allocated(dependency%library)) then @@ -150,7 +136,7 @@ subroutine build_model(model, settings, package, error) lib_dir = join_path(dep%proj_dir, dependency%library%source_dir) if (is_dir(lib_dir)) then call add_sources_from_dir(model%packages(i)%sources, lib_dir, FPM_SCOPE_LIB, & - error=error) + with_f_ext=model%packages(i)%preprocess%suffixes, error=error) if (allocated(error)) exit end if end if @@ -188,7 +174,8 @@ subroutine build_model(model, settings, package, error) ! Add sources from executable directories if (is_dir('app') .and. package%build%auto_executables) then call add_sources_from_dir(model%packages(1)%sources,'app', FPM_SCOPE_APP, & - with_executables=.true., error=error) + with_executables=.true., with_f_ext=model%packages(1)%preprocess%suffixes,& + error=error) if (allocated(error)) then return @@ -197,7 +184,8 @@ subroutine build_model(model, settings, package, error) end if if (is_dir('example') .and. package%build%auto_examples) then call add_sources_from_dir(model%packages(1)%sources,'example', FPM_SCOPE_EXAMPLE, & - with_executables=.true., error=error) + with_executables=.true., & + with_f_ext=model%packages(1)%preprocess%suffixes,error=error) if (allocated(error)) then return @@ -206,7 +194,8 @@ subroutine build_model(model, settings, package, error) end if if (is_dir('test') .and. package%build%auto_tests) then call add_sources_from_dir(model%packages(1)%sources,'test', FPM_SCOPE_TEST, & - with_executables=.true., error=error) + with_executables=.true., & + with_f_ext=model%packages(1)%preprocess%suffixes,error=error) if (allocated(error)) then return @@ -216,6 +205,7 @@ subroutine build_model(model, settings, package, error) if (allocated(package%executable)) then call add_executable_sources(model%packages(1)%sources, package%executable, FPM_SCOPE_APP, & auto_discover=package%build%auto_executables, & + with_f_ext=model%packages(1)%preprocess%suffixes, & error=error) if (allocated(error)) then @@ -226,6 +216,7 @@ subroutine build_model(model, settings, package, error) if (allocated(package%example)) then call add_executable_sources(model%packages(1)%sources, package%example, FPM_SCOPE_EXAMPLE, & auto_discover=package%build%auto_examples, & + with_f_ext=model%packages(1)%preprocess%suffixes, & error=error) if (allocated(error)) then @@ -236,6 +227,7 @@ subroutine build_model(model, settings, package, error) if (allocated(package%test)) then call add_executable_sources(model%packages(1)%sources, package%test, FPM_SCOPE_TEST, & auto_discover=package%build%auto_tests, & + with_f_ext=model%packages(1)%preprocess%suffixes, & error=error) if (allocated(error)) then diff --git a/src/fpm/fpm_release.F90 b/src/fpm/fpm_release.F90 index e80050b3ee..fdd88c2781 100644 --- a/src/fpm/fpm_release.F90 +++ b/src/fpm/fpm_release.F90 @@ -18,7 +18,7 @@ type(version_t) function fpm_version() ! Fallback to last known version in case of undefined macro #ifndef FPM_RELEASE_VERSION -# define FPM_RELEASE_VERSION 0.8.0 +# define FPM_RELEASE_VERSION 0.10.0 #endif ! Accept solution from https://stackoverflow.com/questions/31649691/stringify-macro-with-gnu-gfortran diff --git a/src/fpm/manifest/preprocess.f90 b/src/fpm/manifest/preprocess.f90 index f5ea93b0f4..10d8d559b2 100644 --- a/src/fpm/manifest/preprocess.f90 +++ b/src/fpm/manifest/preprocess.f90 @@ -15,6 +15,7 @@ module fpm_manifest_preprocess use fpm_strings, only : string_t, operator(==) use fpm_toml, only : toml_table, toml_key, toml_stat, get_value, get_list, serializable_t, set_value, set_list, & set_string + use,intrinsic :: iso_fortran_env, only : stderr=>error_unit implicit none private @@ -39,11 +40,19 @@ module fpm_manifest_preprocess !> Print information on this instance procedure :: info - + !> Serialization interface procedure :: serializable_is_same => preprocess_is_same procedure :: dump_to_toml procedure :: load_from_toml + !> Operations + procedure :: destroy + procedure :: add_config + + !> Properties + procedure :: is_cpp + procedure :: is_fypp + end type preprocess_config_t @@ -268,4 +277,71 @@ subroutine load_from_toml(self, table, error) end subroutine load_from_toml + !> Clean preprocessor structure + elemental subroutine destroy(this) + class(preprocess_config_t), intent(inout) :: this + + if (allocated(this%name))deallocate(this%name) + if (allocated(this%suffixes))deallocate(this%suffixes) + if (allocated(this%directories))deallocate(this%directories) + if (allocated(this%macros))deallocate(this%macros) + + end subroutine destroy + + !> Add preprocessor settings + subroutine add_config(this,that) + class(preprocess_config_t), intent(inout) :: this + type(preprocess_config_t), intent(in) :: that + + if (.not.that%is_cpp()) then + write(stderr, '(a)') 'Warning: Preprocessor ' // that%name // & + ' is not supported; will ignore it' + return + end if + + if (.not.allocated(this%name)) this%name = that%name + + ! Add macros + if (allocated(that%macros)) then + if (allocated(this%macros)) then + this%macros = [this%macros, that%macros] + else + allocate(this%macros, source = that%macros) + end if + endif + + ! Add suffixes + if (allocated(that%suffixes)) then + if (allocated(this%suffixes)) then + this%suffixes = [this%suffixes, that%suffixes] + else + allocate(this%suffixes, source = that%suffixes) + end if + endif + + ! Add directories + if (allocated(that%directories)) then + if (allocated(this%directories)) then + this%directories = [this%directories, that%directories] + else + allocate(this%directories, source = that%directories) + end if + endif + + end subroutine add_config + + ! Check cpp + logical function is_cpp(this) + class(preprocess_config_t), intent(in) :: this + is_cpp = .false. + if (allocated(this%name)) is_cpp = this%name == "cpp" + end function is_cpp + + ! Check cpp + logical function is_fypp(this) + class(preprocess_config_t), intent(in) :: this + is_fypp = .false. + if (allocated(this%name)) is_fypp = this%name == "fypp" + end function is_fypp + end module fpm_manifest_preprocess diff --git a/src/fpm_compiler.F90 b/src/fpm_compiler.F90 index 3031d50d11..f173267659 100644 --- a/src/fpm_compiler.F90 +++ b/src/fpm_compiler.F90 @@ -199,6 +199,9 @@ module fpm_compiler flag_intel_fixed_form = " -fixed", & flag_intel_standard_compliance = " -standard-semantics" +character(*), parameter :: & + flag_intel_llvm_check = " -check all,nouninit" + character(*), parameter :: & flag_intel_backtrace_win = " /traceback", & flag_intel_warn_win = " /warn:all", & @@ -435,7 +438,7 @@ subroutine get_debug_compile_flags(id, flags) case(id_intel_llvm_nix) flags = & flag_intel_warn//& - flag_intel_check//& + flag_intel_llvm_check//& flag_intel_limit//& flag_intel_debug//& flag_intel_byterecl//& diff --git a/src/fpm_environment.f90 b/src/fpm_environment.f90 index 39152ab4ad..aba65e77bd 100644 --- a/src/fpm_environment.f90 +++ b/src/fpm_environment.f90 @@ -56,11 +56,11 @@ integer function get_os_type() result(r) !! found on specific system types only. !! !! Returns OS_UNKNOWN if the operating system cannot be determined. - character(len=32) :: val - integer :: length, rc - logical :: file_exists - logical, save :: first_run = .true. - integer, save :: ret = OS_UNKNOWN + character(len=255) :: val + integer :: length, rc + logical :: file_exists + logical, save :: first_run = .true. + integer, save :: ret = OS_UNKNOWN !$omp threadprivate(ret, first_run) if (.not. first_run) then diff --git a/src/fpm_filesystem.F90 b/src/fpm_filesystem.F90 index 177ee85fea..ca521346b3 100644 --- a/src/fpm_filesystem.F90 +++ b/src/fpm_filesystem.F90 @@ -654,7 +654,7 @@ end function unix_path !! integer :: iostat !! character(len=:),allocatable :: line, iomsg !! open(unit=stdin,pad='yes') -!! INFINITE: do +!! INFINITE: do !! call getline(stdin,line,iostat,iomsg) !! if(iostat /= 0) exit INFINITE !! write(*,'(a)')'['//line//']' diff --git a/src/fpm_meta.f90 b/src/fpm_meta.f90 index 827af3ca3c..3265b26e47 100644 --- a/src/fpm_meta.f90 +++ b/src/fpm_meta.f90 @@ -3,21 +3,22 @@ !> This is a wrapper data type that encapsulate all pre-processing information !> (compiler flags, linker libraries, etc.) required to correctly enable a package !> to use a core library. -!> !> -!>### Available core libraries +!> +!>### Available core libraries !> !> - OpenMP !> - MPI !> - fortran-lang stdlib !> - fortran-lang minpack -!> +!> !> !> @note Core libraries are enabled in the [build] section of the fpm.toml manifest !> !> module fpm_meta -use fpm_strings, only: string_t, len_trim, remove_newline_characters +use fpm_strings, only: string_t, len_trim, remove_newline_characters, str_begins_with_str, & + str_ends_with use fpm_error, only: error_t, fatal_error, syntax_error, fpm_stop use fpm_compiler use fpm_model @@ -29,6 +30,8 @@ module fpm_meta use fpm_filesystem, only: run, get_temp_filename, getline, exists, canon_path, is_dir, get_dos_path use fpm_versioning, only: version_t, new_version, regex_version_from_text use fpm_os, only: get_absolute_path +use shlex_module, only: shlex_split => split +use regex_module, only: regex use iso_fortran_env, only: stdout => output_unit implicit none @@ -106,6 +109,8 @@ module fpm_meta integer, parameter, private :: LANG_C = 2 integer, parameter, private :: LANG_CXX = 3 +character(*), parameter :: LANG_NAME(*) = [character(7) :: 'Fortran','C','C++'] + contains !> Return a name for the MPI library @@ -434,10 +439,8 @@ subroutine resolve_metapackage_model(model,package,settings,error) ! Dependencies are added to the package config, so they're properly resolved ! into the dependency tree later. ! Flags are added to the model (whose compiler needs to be already initialized) - if (model%compiler%is_unknown()) then - call fatal_error(error,"compiler not initialized: cannot build metapackages") - return - end if + if (model%compiler%is_unknown()) & + write(stdout,'(a)') ' compiler not initialized: metapackages may not be available' ! OpenMP if (package%meta%openmp%on) then @@ -634,7 +637,13 @@ logical function msmpi_init(this,compiler,error) result(found) call get_absolute_path('C:\Program Files\Microsoft MPI\Bin\mpiexec.exe',bindir,error) endif - ! Do a third attempt: search for mpiexec.exe in PATH location + ! Third attempt for bash-style shell + if (len_trim(bindir)<=0 .or. allocated(error)) then + if (verbose) print *, '+ %MSMPI_BIN% empty, searching /c/Program Files/Microsoft MPI/Bin/ ...' + call get_absolute_path('/c/Program Files/Microsoft MPI/Bin/mpiexec.exe',bindir,error) + endif + + ! Do a fourth attempt: search for mpiexec.exe in PATH location if (len_trim(bindir)<=0 .or. allocated(error)) then if (verbose) print *, '+ C:\Program Files\Microsoft MPI\Bin\ not found. searching %PATH%...' @@ -647,7 +656,7 @@ logical function msmpi_init(this,compiler,error) result(found) endif - if (allocated(error) .or. .not.exists(bindir)) then + if (allocated(error)) then call fatal_error(error,'MS-MPI error: MS-MPI Runtime directory is missing. '//& 'check environment variable %MSMPI_BIN% or that the folder is in %PATH%.') return @@ -983,28 +992,25 @@ subroutine init_mpi_from_wrappers(this,compiler,mpilib,fort_wrapper,c_wrapper,cx call destroy(this) ! Get linking flags - if (mpilib/=MPI_TYPE_INTEL) then - this%link_flags = mpi_wrapper_query(mpilib,fort_wrapper,'link',verbose,error) + this%link_flags = mpi_wrapper_query(mpilib,fort_wrapper,'link',verbose,error) + if (allocated(error)) return - ! We fix OpenMPI's Fortran wrapper bug (https://github.com/open-mpi/ompi/issues/11636) - !call fix_openmpi_link_flags(this%link_flags,compiler,mpilib,fort_wrapper,c_wrapper,cxx_wrapper,error) + ! Remove useless/dangerous flags + call filter_link_arguments(compiler,this%link_flags) - if (allocated(error)) return - this%has_link_flags = len_trim(this%link_flags)>0 - endif + this%has_link_flags = len_trim(this%link_flags)>0 ! Request to use libs in arbitrary order if (this%has_link_flags .and. compiler%is_gnu() .and. os_is_unix() .and. get_os_type()/=OS_MACOS) then this%link_flags = string_t(' -Wl,--start-group '//this%link_flags%s) end if - ! Add language-specific flags - call set_language_flags(mpilib,fort_wrapper,this%has_fortran_flags,this%fflags,verbose,error) + call set_language_flags(compiler,mpilib,fort_wrapper,this%has_fortran_flags,this%fflags,verbose,error) if (allocated(error)) return - call set_language_flags(mpilib,c_wrapper,this%has_c_flags,this%cflags,verbose,error) + call set_language_flags(compiler,mpilib,c_wrapper,this%has_c_flags,this%cflags,verbose,error) if (allocated(error)) return - call set_language_flags(mpilib,cxx_wrapper,this%has_cxx_flags,this%cxxflags,verbose,error) + call set_language_flags(compiler,mpilib,cxx_wrapper,this%has_cxx_flags,this%cxxflags,verbose,error) if (allocated(error)) return ! Get library version @@ -1021,7 +1027,8 @@ subroutine init_mpi_from_wrappers(this,compiler,mpilib,fort_wrapper,c_wrapper,cx contains - subroutine set_language_flags(mpilib,wrapper,has_flags,flags,verbose,error) + subroutine set_language_flags(compiler,mpilib,wrapper,has_flags,flags,verbose,error) + type(compiler_t), intent(in) :: compiler integer, intent(in) :: mpilib type(string_t), intent(in) :: wrapper logical, intent(inout) :: has_flags @@ -1041,6 +1048,8 @@ subroutine set_language_flags(mpilib,wrapper,has_flags,flags,verbose,error) if (verbose) print *, '+ MPI language flags from wrapper <',wrapper%s,'>: flags=',flags%s + call filter_build_arguments(compiler,flags) + endif end subroutine set_language_flags @@ -1055,13 +1064,16 @@ subroutine mpi_compiler_match(language,wrappers,compiler,which_one,mpilib,error) integer, intent(out) :: which_one, mpilib type(error_t), allocatable, intent(out) :: error - integer :: i + integer :: i, same_vendor, vendor_mpilib type(string_t) :: screen character(128) :: msg_out type(compiler_t) :: mpi_compiler - which_one = 0 - mpilib = MPI_TYPE_NONE + which_one = 0 + same_vendor = 0 + mpilib = MPI_TYPE_NONE + + if (verbose) print *, '+ Trying to match available ',LANG_NAME(language),' MPI wrappers to ',compiler%fc,'...' do i=1,size(wrappers) @@ -1070,6 +1082,8 @@ subroutine mpi_compiler_match(language,wrappers,compiler,which_one,mpilib,error) screen = mpi_wrapper_query(mpilib,wrappers(i),'compiler',verbose=.false.,error=error) if (allocated(error)) return + if (verbose) print *, ' Wrapper ',wrappers(i)%s,' lib=',MPI_TYPE_NAME(mpilib),' uses ',screen%s + select case (language) case (LANG_FORTRAN) ! Build compiler type. The ID is created based on the Fortran name @@ -1080,7 +1094,6 @@ subroutine mpi_compiler_match(language,wrappers,compiler,which_one,mpilib,error) which_one = i return end if - case (LANG_C) ! For other languages, we can only hope that the name matches the expected one if (screen%s==compiler%cc .or. screen%s==compiler%fc) then @@ -1094,8 +1107,20 @@ subroutine mpi_compiler_match(language,wrappers,compiler,which_one,mpilib,error) end if end select + ! Because the intel mpi library does not support llvm_ compiler wrappers yet, + ! we must check for that manually + if (is_intel_classic_option(language,same_vendor,screen,compiler,mpi_compiler)) then + same_vendor = i + vendor_mpilib = mpilib + end if end do + ! Intel compiler: if an exact match is not found, attempt closest wrapper + if (which_one==0 .and. same_vendor>0) then + which_one = same_vendor + mpilib = vendor_mpilib + end if + ! None of the available wrappers matched the current Fortran compiler write(msg_out,1) size(wrappers),compiler%fc call fatal_error(error,trim(msg_out)) @@ -1103,6 +1128,28 @@ subroutine mpi_compiler_match(language,wrappers,compiler,which_one,mpilib,error) end subroutine mpi_compiler_match +!> Because the Intel mpi library does not support llvm_ compiler wrappers yet, +!> we must save the Intel-classic option and later manually replace it +logical function is_intel_classic_option(language,same_vendor_ID,screen_out,compiler,mpi_compiler) + integer, intent(in) :: language,same_vendor_ID + type(string_t), intent(in) :: screen_out + type(compiler_t), intent(in) :: compiler,mpi_compiler + + if (same_vendor_ID/=0) then + is_intel_classic_option = .false. + else + select case (language) + case (LANG_FORTRAN) + is_intel_classic_option = mpi_compiler%is_intel() .and. compiler%is_intel() + case (LANG_C) + is_intel_classic_option = screen_out%s=='icc' .and. compiler%cc=='icx' + case (LANG_CXX) + is_intel_classic_option = screen_out%s=='icpc' .and. compiler%cc=='icpx' + end select + end if + +end function is_intel_classic_option + !> Return library version from the MPI wrapper command type(version_t) function mpi_version_get(mpilib,wrapper,error) integer, intent(in) :: mpilib @@ -1395,6 +1442,7 @@ type(string_t) function mpi_wrapper_query(mpilib,wrapper,command,verbose,error) end if ! Take out the first command from the whole line + call remove_newline_characters(screen) call split(screen%s,tokens,delimiters=' ') screen%s = trim(adjustl(tokens(1))) @@ -1441,6 +1489,7 @@ type(string_t) function mpi_wrapper_query(mpilib,wrapper,command,verbose,error) select case (mpilib) case (MPI_TYPE_OPENMPI); cmdstr = string_t('--showme:link') case (MPI_TYPE_MPICH); cmdstr = string_t('-link-info') + case (MPI_TYPE_INTEL); cmdstr = string_t('-show') case default call fatal_error(error,unsupported_msg) return @@ -1458,7 +1507,7 @@ type(string_t) function mpi_wrapper_query(mpilib,wrapper,command,verbose,error) select case (mpilib) case (MPI_TYPE_OPENMPI) call remove_newline_characters(screen) - case (MPI_TYPE_MPICH) + case (MPI_TYPE_MPICH,MPI_TYPE_INTEL) ! MPICH reports the full command including the compiler name. Remove it if so call remove_newline_characters(screen) call split(screen%s,tokens) @@ -1598,4 +1647,123 @@ type(string_t) function mpi_wrapper_query(mpilib,wrapper,command,verbose,error) end function mpi_wrapper_query +!> Check if input is a useful linker argument +logical function is_link_argument(compiler,string) + type(compiler_t), intent(in) :: compiler + character(*), intent(in) :: string + + select case (compiler%id) + case (id_intel_classic_windows,id_intel_llvm_windows) + is_link_argument = string=='/link' & + .or. str_begins_with_str(string,'/LIBPATH')& + .or. str_ends_with(string,'.lib') ! always .lib whether static or dynamic + case default + + ! fix OpenMPI's Fortran wrapper bug (https://github.com/open-mpi/ompi/issues/11636) here + is_link_argument = ( str_begins_with_str(string,'-L') & + .or. str_begins_with_str(string,'-l') & + .or. str_begins_with_str(string,'-Xlinker') & + .or. string=='-pthread' & + .or. (str_begins_with_str(string,'-W') .and. & + (string/='-Wall') .and. (.not.str_begins_with_str(string,'-Werror'))) ) & + .and. .not. ( & + (get_os_type()==OS_MACOS .and. index(string,'-commons,use_dylibs')>0) ) + end select + +end function is_link_argument + +!> From build, remove optimization and other unnecessary flags +subroutine filter_build_arguments(compiler,command) + type(compiler_t), intent(in) :: compiler + type(string_t), intent(inout) :: command + character(len=:), allocatable :: tokens(:) + + integer :: i,n,re_i,re_l + logical, allocatable :: keep(:) + logical :: keep_next + character(len=:), allocatable :: module_flag,include_flag + + if (len_trim(command)<=0) return + + ! Split command into arguments + tokens = shlex_split(command%s) + + module_flag = get_module_flag(compiler,"") + include_flag = get_include_flag(compiler,"") + + n = size(tokens) + allocate(keep(n),source=.false.) + keep_next = .false. + + do i=1,n + + if (get_os_type()==OS_MACOS .and. index(tokens(i),'-commons,use_dylibs')>0) then + keep(i) = .false. + keep_next = .false. + elseif (str_begins_with_str(tokens(i),'-D') .or. & + str_begins_with_str(tokens(i),'-f') .or. & + str_begins_with_str(tokens(i),'-I') .or. & + str_begins_with_str(tokens(i),module_flag) .or. & + str_begins_with_str(tokens(i),include_flag) .or. & + tokens(i)=='-pthread' .or. & + (str_begins_with_str(tokens(i),'-W') .and. tokens(i)/='-Wall' .and. .not.str_begins_with_str(tokens(i),'-Werror')) & + ) then + keep(i) = .true. + if (tokens(i)==module_flag .or. tokens(i)==include_flag .or. tokens(i)=='-I') keep_next = .true. + elseif (keep_next) then + keep(i) = .true. + keep_next = .false. + end if + end do + + ! Backfill + command = string_t("") + do i=1,n + if (.not.keep(i)) cycle + + command%s = command%s//' '//trim(tokens(i)) + end do + + +end subroutine filter_build_arguments + +!> From the linker flags, remove optimization and other unnecessary flags +subroutine filter_link_arguments(compiler,command) + type(compiler_t), intent(in) :: compiler + type(string_t), intent(inout) :: command + character(len=:), allocatable :: tokens(:) + + integer :: i,n + logical, allocatable :: keep(:) + logical :: keep_next + + if (len_trim(command)<=0) return + + ! Split command into arguments + tokens = shlex_split(command%s) + + n = size(tokens) + allocate(keep(n),source=.false.) + keep_next = .false. + + do i=1,n + if (is_link_argument(compiler,tokens(i))) then + keep(i) = .true. + if (tokens(i)=='-L' .or. tokens(i)=='-Xlinker') keep_next = .true. + elseif (keep_next) then + keep(i) = .true. + keep_next = .false. + end if + end do + + ! Backfill + command = string_t("") + do i=1,n + if (.not.keep(i)) cycle + command%s = command%s//' '//trim(tokens(i)) + end do + +end subroutine filter_link_arguments + + end module fpm_meta diff --git a/src/fpm_model.f90 b/src/fpm_model.f90 index 609e84cfec..3a8d70df74 100644 --- a/src/fpm_model.f90 +++ b/src/fpm_model.f90 @@ -42,6 +42,7 @@ module fpm_model use fpm_toml, only: serializable_t, toml_table, toml_stat, set_value, set_list, get_value, & & get_list, add_table, toml_key, add_array, set_string use fpm_error, only: error_t, fatal_error +use fpm_manifest_preprocess, only: preprocess_config_t implicit none private @@ -155,7 +156,7 @@ module fpm_model type(srcfile_t), allocatable :: sources(:) !> List of macros. - type(string_t), allocatable :: macros(:) + type(preprocess_config_t) :: preprocess !> Package version number. character(:), allocatable :: version diff --git a/src/fpm_sources.f90 b/src/fpm_sources.f90 index 0165249f50..69c724eec6 100644 --- a/src/fpm_sources.f90 +++ b/src/fpm_sources.f90 @@ -25,12 +25,16 @@ module fpm_sources !> Wrapper to source parsing routines. !> Selects parsing routine based on source file name extension -function parse_source(source_file_path,error) result(source) +function parse_source(source_file_path,custom_f_ext,error) result(source) character(*), intent(in) :: source_file_path + type(string_t), optional, intent(in) :: custom_f_ext(:) type(error_t), allocatable, intent(out) :: error type(srcfile_t) :: source + type(string_t), allocatable :: f_ext(:) - if (str_ends_with(lower(source_file_path), fortran_suffixes)) then + call list_fortran_suffixes(f_ext,custom_f_ext) + + if (str_ends_with(lower(source_file_path), f_ext)) then source = parse_f_source(source_file_path, error) @@ -42,7 +46,7 @@ function parse_source(source_file_path,error) result(source) source = parse_c_source(source_file_path,error) - end if + endif if (allocated(error)) then return @@ -50,8 +54,31 @@ function parse_source(source_file_path,error) result(source) end function parse_source +!> List fortran suffixes, including optional ones +subroutine list_fortran_suffixes(suffixes,with_f_ext) + type(string_t), allocatable, intent(out) :: suffixes(:) + !> Additional user-defined (preprocessor) extensions that should be treated as Fortran sources + type(string_t), intent(in), optional :: with_f_ext(:) + + integer :: ndefault,nuser,i + + ndefault = size(fortran_suffixes) + nuser = 0; if (present(with_f_ext)) nuser = size(with_f_ext) + + allocate(suffixes(ndefault + nuser)) + do i=1,ndefault + suffixes(i) = string_t(fortran_suffixes(i)) + end do + if (present(with_f_ext)) then + do i=1,nuser + suffixes(ndefault+i) = string_t(with_f_ext(i)%s) + end do + endif + +end subroutine list_fortran_suffixes + !> Add to `sources` by looking for source files in `directory` -subroutine add_sources_from_dir(sources,directory,scope,with_executables,recurse,error) +subroutine add_sources_from_dir(sources,directory,scope,with_executables,with_f_ext,recurse,error) !> List of `[[srcfile_t]]` objects to append to. Allocated if not allocated type(srcfile_t), allocatable, intent(inout), target :: sources(:) !> Directory in which to search for source files @@ -60,6 +87,8 @@ subroutine add_sources_from_dir(sources,directory,scope,with_executables,recurse integer, intent(in) :: scope !> Executable sources (fortran `program`s) are ignored unless `with_executables=.true.` logical, intent(in), optional :: with_executables + !> Additional user-defined (preprocessor) extensions that should be treated as Fortran sources + type(string_t), intent(in), optional :: with_f_ext(:) !> Whether to recursively search subdirectories, default is `.true.` logical, intent(in), optional :: recurse !> Error handling @@ -69,7 +98,7 @@ subroutine add_sources_from_dir(sources,directory,scope,with_executables,recurse logical, allocatable :: is_source(:), exclude_source(:) logical :: recurse_ type(string_t), allocatable :: file_names(:) - type(string_t), allocatable :: src_file_names(:) + type(string_t), allocatable :: src_file_names(:),f_ext(:) type(string_t), allocatable :: existing_src_files(:) type(srcfile_t), allocatable :: dir_sources(:) @@ -87,10 +116,15 @@ subroutine add_sources_from_dir(sources,directory,scope,with_executables,recurse allocate(existing_src_files(0)) end if + ! Get legal fortran suffixes + call list_fortran_suffixes(f_ext,with_f_ext) + is_source = [(.not.(is_hidden_file(basename(file_names(i)%s))) .and. & .not.(canon_path(file_names(i)%s) .in. existing_src_files) .and. & - (str_ends_with(lower(file_names(i)%s), fortran_suffixes) .or. & + (str_ends_with(lower(file_names(i)%s), f_ext) .or. & str_ends_with(lower(file_names(i)%s), c_suffixes) ),i=1,size(file_names))] + + src_file_names = pack(file_names,is_source) allocate(dir_sources(size(src_file_names))) @@ -98,7 +132,7 @@ subroutine add_sources_from_dir(sources,directory,scope,with_executables,recurse do i = 1, size(src_file_names) - dir_sources(i) = parse_source(src_file_names(i)%s,error) + dir_sources(i) = parse_source(src_file_names(i)%s,with_f_ext,error) if (allocated(error)) return dir_sources(i)%unit_scope = scope @@ -129,7 +163,7 @@ end subroutine add_sources_from_dir !> Add to `sources` using the executable and test entries in the manifest and !> applies any executable-specific overrides such as `executable%name`. !> Adds all sources (including modules) from each `executable%source_dir` -subroutine add_executable_sources(sources,executables,scope,auto_discover,error) +subroutine add_executable_sources(sources,executables,scope,auto_discover,with_f_ext,error) !> List of `[[srcfile_t]]` objects to append to. Allocated if not allocated type(srcfile_t), allocatable, intent(inout), target :: sources(:) !> List of `[[executable_config_t]]` entries from manifest @@ -138,6 +172,8 @@ subroutine add_executable_sources(sources,executables,scope,auto_discover,error) integer, intent(in) :: scope !> If `.false.` only executables and tests specified in the manifest are added to `sources` logical, intent(in) :: auto_discover + !> Additional user-defined (preprocessor) extensions that should be treated as Fortran sources + type(string_t), intent(in), optional :: with_f_ext(:) !> Error handling type(error_t), allocatable, intent(out) :: error @@ -150,7 +186,7 @@ subroutine add_executable_sources(sources,executables,scope,auto_discover,error) do i=1,size(exe_dirs) call add_sources_from_dir(sources,exe_dirs(i)%s, scope, & - with_executables=auto_discover, recurse=.false., error=error) + with_executables=auto_discover, with_f_ext=with_f_ext,recurse=.false., error=error) if (allocated(error)) then return @@ -180,7 +216,7 @@ subroutine add_executable_sources(sources,executables,scope,auto_discover,error) ! Add if not already discovered (auto_discovery off) associate(exe => executables(i)) - exe_source = parse_source(join_path(exe%source_dir,exe%main),error) + exe_source = parse_source(join_path(exe%source_dir,exe%main),with_f_ext,error) exe_source%exe_name = exe%name if (allocated(exe%link)) then exe_source%link_libraries = exe%link diff --git a/src/fpm_strings.f90 b/src/fpm_strings.f90 index 759fec8f54..e7c92beaf2 100644 --- a/src/fpm_strings.f90 +++ b/src/fpm_strings.f90 @@ -77,6 +77,7 @@ module fpm_strings interface str_ends_with procedure :: str_ends_with_str procedure :: str_ends_with_any + procedure :: str_ends_with_any_string end interface str_ends_with interface str @@ -128,6 +129,23 @@ pure logical function str_ends_with_any(s, e) result(r) end function str_ends_with_any +!> Test if a CHARACTER string ends with any of an array of string suffixs +pure logical function str_ends_with_any_string(s, e) result(r) + character(*), intent(in) :: s + type(string_t), intent(in) :: e(:) + + integer :: i + + r = .true. + do i=1,size(e) + + if (str_ends_with(s,trim(e(i)%s))) return + + end do + r = .false. + +end function str_ends_with_any_string + !> test if a CHARACTER string begins with a specified prefix pure logical function str_begins_with_str(s, e, case_sensitive) result(r) character(*), intent(in) :: s, e diff --git a/src/fpm_targets.f90 b/src/fpm_targets.f90 index df0d58810f..7c8c97b775 100644 --- a/src/fpm_targets.f90 +++ b/src/fpm_targets.f90 @@ -33,6 +33,7 @@ module fpm_targets use fpm_strings, only: string_t, operator(.in.), string_cat, fnv_1a, resize, lower, str_ends_with use fpm_compiler, only: get_macros use fpm_sources, only: get_exe_name_with_suffix +use fpm_manifest_preprocess, only: preprocess_config_t implicit none private @@ -233,7 +234,7 @@ subroutine build_target_list(targets,model) sources(i)%unit_type==FPM_UNIT_CSOURCE), & output_name = get_object_name(sources(i)), & features = model%packages(j)%features, & - macros = model%packages(j)%macros, & + preprocess = model%packages(j)%preprocess, & version = model%packages(j)%version) @@ -247,7 +248,7 @@ subroutine build_target_list(targets,model) call add_target(targets,package=model%packages(j)%name,source = sources(i), & type = FPM_TARGET_CPP_OBJECT, & output_name = get_object_name(sources(i)), & - macros = model%packages(j)%macros, & + preprocess = model%packages(j)%preprocess, & version = model%packages(j)%version) if (with_lib .and. sources(i)%unit_scope == FPM_SCOPE_LIB) then @@ -280,7 +281,7 @@ subroutine build_target_list(targets,model) output_name = get_object_name(sources(i)), & source = sources(i), & features = model%packages(j)%features, & - macros = model%packages(j)%macros & + preprocess = model%packages(j)%preprocess & ) if (sources(i)%unit_scope == FPM_SCOPE_APP) then @@ -413,7 +414,7 @@ end subroutine collect_exe_link_dependencies !> Allocate a new target and append to target list subroutine add_target(targets, package, type, output_name, source, link_libraries, & - & features, macros, version) + & features, preprocess, version) type(build_target_ptr), allocatable, intent(inout) :: targets(:) character(*), intent(in) :: package integer, intent(in) :: type @@ -421,7 +422,7 @@ subroutine add_target(targets, package, type, output_name, source, link_librarie type(srcfile_t), intent(in), optional :: source type(string_t), intent(in), optional :: link_libraries(:) type(fortran_features_t), intent(in), optional :: features - type(string_t), intent(in), optional :: macros(:) + type(preprocess_config_t), intent(in), optional :: preprocess character(*), intent(in), optional :: version integer :: i @@ -450,7 +451,9 @@ subroutine add_target(targets, package, type, output_name, source, link_librarie if (present(source)) new_target%source = source if (present(link_libraries)) new_target%link_libraries = link_libraries if (present(features)) new_target%features = features - if (present(macros)) new_target%macros = macros + if (present(preprocess)) then + if (allocated(preprocess%macros)) new_target%macros = preprocess%macros + endif if (present(version)) new_target%version = version allocate(new_target%dependencies(0))