Skip to content

Commit 49f2ff3

Browse files
authored
Merge pull request #775 from fortran-lang/gnikit/issue774
bug: C preprocessor does not propagate directives to executables
2 parents e3fff65 + 9d0b31d commit 49f2ff3

File tree

7 files changed

+40
-3
lines changed

7 files changed

+40
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
build/*
2+
3+
# Visual Studio Code
4+
.vscode/

ci/run_tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ pushd c_main
122122
"$fpm" run
123123
popd
124124

125+
pushd c_main_preprocess
126+
"$fpm" build --c-flag "-DVAL"
127+
popd
128+
125129
pushd app_with_c
126130
"$fpm" run
127131
popd
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdio.h>
2+
3+
#include "val.h"
4+
5+
int main() {
6+
printf("%d\n", variable);
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name = "c_main_preprocess"
2+
[library]
3+
include-dir = ["src"]
4+
5+
[[executable]]
6+
name="main-c"
7+
main="main.c"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "val.h"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _VAL_H_
2+
#define _VAL_H_
3+
4+
#ifdef VAL
5+
const int variable = 1;
6+
#endif
7+
#endif

src/fpm_targets.f90

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module fpm_targets
2929
use fpm_model
3030
use fpm_environment, only: get_os_type, OS_WINDOWS, OS_MACOS
3131
use fpm_filesystem, only: dirname, join_path, canon_path
32-
use fpm_strings, only: string_t, operator(.in.), string_cat, fnv_1a, resize
32+
use fpm_strings, only: string_t, operator(.in.), string_cat, fnv_1a, resize, lower, str_ends_with
3333
use fpm_compiler, only: get_macros
3434
implicit none
3535

@@ -189,7 +189,7 @@ subroutine build_target_list(targets,model)
189189
!> The package model from which to construct the target list
190190
type(fpm_model_t), intent(inout), target :: model
191191

192-
integer :: i, j, n_source
192+
integer :: i, j, n_source, exe_type
193193
character(:), allocatable :: xsuffix, exe_dir
194194
logical :: with_lib
195195

@@ -268,7 +268,15 @@ subroutine build_target_list(targets,model)
268268

269269
case (FPM_UNIT_PROGRAM)
270270

271-
call add_target(targets,package=model%packages(j)%name,type = FPM_TARGET_OBJECT,&
271+
if (str_ends_with(lower(sources(i)%file_name), [".c"])) then
272+
exe_type = FPM_TARGET_C_OBJECT
273+
else if (str_ends_with(lower(sources(i)%file_name), [".cpp", ".cc "])) then
274+
exe_type = FPM_TARGET_CPP_OBJECT
275+
else ! Default to a Fortran object
276+
exe_type = FPM_TARGET_OBJECT
277+
end if
278+
279+
call add_target(targets,package=model%packages(j)%name,type = exe_type,&
272280
output_name = get_object_name(sources(i)), &
273281
source = sources(i) &
274282
)

0 commit comments

Comments
 (0)