Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add predefined macro PROJECT_ROOT #792

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions example_packages/preprocess_project_root/app/main.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
!> from https://stackoverflow.com/a/46342008
#ifdef __GFORTRAN__
# define STRINGIFY_START(X) "&
# define STRINGIFY_END(X) &X"
#else /* default stringification */
# define STRINGIFY_(X) #X
# define STRINGIFY_START(X) &
# define STRINGIFY_END(X) STRINGIFY_(X)
#endif

program preprocess_project_root
implicit none
character(len=*), parameter :: root = STRINGIFY_START(PROJECT_ROOT)
STRINGIFY_END(PROJECT_ROOT)

print *, root

end program preprocess_project_root
4 changes: 4 additions & 0 deletions example_packages/preprocess_project_root/fpm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "preprocess_project_root"

[preprocess]
[preprocess.cpp]
11 changes: 11 additions & 0 deletions example_packages/preprocess_project_root/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Preprocess_project_root
If you have activated preprocess option in `fpm.toml` via
```toml
[preprocess]
[preprocess.cpp]
```
some predefined variables will be added to macro definitions. In this example, the macro `PROJECT_ROOT=\path\to\project` is passed to the Fortran source code.

In practice, the value of `PROJECT_ROOT`, which is literally `\path\to\project`, is simply pasted without quoted as a valid string literal. So the code using `PROJECT_ROOT` macro may not compile. You can refer to [Stringify macro with GNU gfortran](https://stackoverflow.com/a/46342008) to solve this problem.


18 changes: 18 additions & 0 deletions src/fpm_compiler.f90
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module fpm_compiler
& getline, run
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_os, only: get_current_directory
use fpm_error, only: error_t
implicit none
public :: compiler_t, new_compiler, archiver_t, new_archiver, get_macros
Expand Down Expand Up @@ -485,6 +486,23 @@ function get_macros(id, macros_list, version) result(macros)

end do

!> add predefined variables
call set_predefined_macro()

contains

!> append predefined variable as macro definition
subroutine set_predefined_macro()
type(error_t), allocatable :: error
character(len=:), allocatable :: project_root

!> add PROJECT_ROOT
call get_current_directory(project_root, error)
if (.not. allocated(project_root)) project_root = ''
macros = macros//' '//macro_definition_symbol//"PROJECT_ROOT="//project_root

end subroutine set_predefined_macro

end function get_macros

function get_include_flag(self, path) result(flags)
Expand Down