FetchContent is not setting include path

1k Views Asked by At

I'm trying to link a fortran project against jsonfortran. Below is a mwe which fails because the module file cannot be found. This happens when the include path is not properly set.

main.F90

program thing_program
  use json_module, only : json_ck
  implicit none

  character(kind=json_ck, len=:), allocatable :: json_string

  json_string = "{}"
  write(*, *) json_string
  
end program thing_program

CMakeLists.txt

cmake_minimum_required(VERSION 3.21)

project(
  myproj
  VERSION 0.0.0
  LANGUAGES Fortran
)

# don't build json-fortran docs
set(SKIP_DOC_GEN ON CACHE INTERNAL "")

#json fortran uses the compiler id as an identifier
string(TOLOWER ${CMAKE_Fortran_COMPILER_ID} compiler_id)

include(FetchContent)
FetchContent_Declare(jsonfortran-${compiler_id}
  GIT_REPOSITORY https://github.com/jacobwilliams/json-fortran.git
  GIT_TAG 3ab8f98209871875325c6985dd0e50085d1c82c2 # 8.3.0
  # if the project is installed system-wide, use that instead of building it ourselves
  FIND_PACKAGE_ARGS NAMES jsonfortran-${compiler_id}
)

FetchContent_MakeAvailable(jsonfortran-${compiler_id})

add_executable( thing main.F90 )

target_link_libraries(thing 
  PUBLIC 
    jsonfortran-${compiler_id}
)

Before using FetchContent, I was able to add jsonfortran with find_package, but I had to use target_link_libraries(thing PUBLIC jsonfortran-${compiler_id}::jsonfortran) to get cmake to generate properly. After using FetchContent, that same call to target_link_libraries fails, but works if I change it to this target_link_libraries(thing PUBLIC jsonfortran-${compiler_id}).

I'm guessing that the includes directory isn't being properly set so the fortran mod files cannot be found because the call to target_link_libraries is somehow incorrect, but I am not sure how to make it function properly. Any idea what's going on?

The actual error, if you're interested:

[  6%] Building Fortran object CMakeFiles/thing.dir/main.F90.o
/Users/kshores/Downloads/thing/main.F90:2:7:

    2 |   use json_module, only : json_ck
      |       1
Fatal Error: Cannot open module file 'json_module.mod' for reading at (1): No such file or directory
compilation terminated.

Update: The mod files are created and placed into _deps/jsonfortran-gnu-build when they should be placed into _deps/jsonfortran-gnu-build/include. With make VERBOSE=1, I see that in fact the include directory is added -I/Users/kshores/Downloads/thing/build/_deps/jsonfortran-gnu-build/include, but the mod files are not there for some reason.

0

There are 0 best solutions below