fortran-lang / fpm

Fortran Package Manager (fpm)
https://fpm.fortran-lang.org
MIT License
876 stars 99 forks source link

Preprocess macros not work when building via `fpm build` or `fpm run` #1039

Closed LaplaceSoda closed 1 month ago

LaplaceSoda commented 4 months ago

Description

If we create a new project via fpm and then add following codes in fpm.toml to support openblas:

link = ["openblas"]
....
[dependencies]
stdlib = "*"
[preprocess]
[preprocess.cpp]
macros = ["STDLIB_EXTERNAL_BLAS", "STDLIB_EXTERNAL_LAPACK"]

Then add a flag in subroutine stdlib_dgemm in stdlib_linalg_blas_d.F90 to show if the stdlib_dgemm is called

454    subroutine stdlib_dgemm(transa,transb,m,n,k,alpha,a,lda,b,ldb,beta,c,ldc)
            ......
613           write(*,*)"stdlib_dgemm called!"
614           return
615     end subroutine stdlib_dgemm

And call gemm in main program:

program main
    use stdlib_linalg_blas, only: gemm
    use iso_fortran_env, only: dp => real64
    implicit none
    real(dp) :: AA(3, 4), BB(4, 4), CC(3, 4)
    integer :: i, j
    AA = reshape([1._dp, 5._dp, 9._dp, 2._dp, 6._dp, 10._dp, 3._dp, 7._dp, 11._dp, 4._dp, 8._dp, 12._dp], [3, 4])
    BB = reshape([0._dp, 0._dp, 0._dp, 1._dp, 0._dp, 0._dp, 1._dp, 0._dp, 0._dp, 1._dp, 0._dp, 0._dp, 1._dp, 0._dp, 0._dp, 0._dp], [4, 4])
    call gemm('N', 'N', 3, 4, 4, 1.0_dp, AA, 3, BB, 4, 0._dp, CC, 3)
    write (*, '(4ES8.1)') ((CC(i, j), j=1, 4), i=1, 3)
end program main

Launch the program via fpm run, and the result will show that the dgemm in openblas is not called while the stdlib_dgemm in stdlib is called. image

If we build directly via compiler flags fpm build --flag "-DSTDLIB_EXTERNAL_BLAS -DSTDLIB_EXTERNAL_LAPACK -lopenblas", the result will show that the dgemm in openblas is correctly called.

Expected Behaviour

preprocess macros works normally

Version of fpm

0.10.1, alpha

Platform and Architecture

Windows

Additional Information

No response

perazz commented 3 months ago

Macros are not passed into the dependencies: see

https://github.com/fortran-lang/fpm/blob/f210e9d966381203ac932de985168668731a12f4/example_packages/preprocess_hello_dependency/src/preprocess_hello_dependency.f90

However, you can define dependency-based macros for stdlib like for any other dependency. The only caveat is that you will have to use a git-based formulation at this stage, because the stdlib metapackage does not implement external library finding yet:

[dependencies]
stdlib = { git="https://github.com/fortran-lang/stdlib", branch="stdlib-fpm", preprocess.cpp.macros= ["STDLIB_EXTERNAL_BLAS", "STDLIB_EXTERNAL_LAPACK"] }
perazz commented 2 months ago

@LaplaceSoda do you think we should close this issue?

LaplaceSoda commented 1 month ago

OK,thank you very much.