jacobwilliams / json-fortran

A Modern Fortran JSON API
https://jacobwilliams.github.io/json-fortran/
Other
332 stars 83 forks source link

Incorrect mod locations when using FetchContent #526

Closed K20shores closed 3 months ago

K20shores commented 1 year ago

Given this CMakeLists.txt file

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
)

and this fortran program

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

I cannot build the cmake project. That project attempts to include json-fortran with FetchContent. This is the error message

[ 53%] 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

If I look into build/_deps/jsonfortran-gnu-build, I can see that the mod files are there instead of in build/_deps/jsonfortran-gnu-build/include which is odd because the output of message(STATUS ${MODULE_DIR}) inside of the json-fortran CMakeLists.txt points to /Users/kshores/Downloads/thing/build/_deps/jsonfortran-gnu-build/include.

When I make VERBOSE=1 and look at the command that copies the mod files to the include directory, this is what it is

/opt/homebrew/Cellar/cmake/3.25.1/bin/cmake -E cmake_copy_f90_mod _deps/jsonfortran-gnu-build/json_kinds.mod _deps/jsonfortran-gnu-build/CMakeFiles/jsonfortran.dir/json_kinds.mod.stamp GNU

I realize that this could be a bug in cmake, but I thought I'd drop this here just in case one of the maintainers might know offhand what could be causing this. If I figure it out, I'll make a PR.

K20shores commented 1 year ago

Also reported to kitware here

K20shores commented 1 year ago

The linked kitware issue solves the problem. BUT if I do a make install in a project that includes json-fortran with FetchContent, json-fortran also gets installed because there is no check to make sure the project is not the top level proejct.

jacobwilliams commented 3 months ago

Fixed in #557