Skip to content

Commit

Permalink
fix: Fortran api (#421)
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
  • Loading branch information
LecrisUT committed Feb 7, 2024
1 parent a648e41 commit 383d1ff
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ GitHub release pages and in the git history.

## \[Unreleased\]

### Fortran API

- [\[#421\]] - Allow importing Fortran API when library/consumer compilers differ
- [\[#421\]] - Fix building Fortran API from exported `spglib_f08.F90`

### Fixes

- [\[#426\]] - Fix Windows installation path
Expand Down Expand Up @@ -2123,5 +2128,6 @@ in bravais.c.
```

[setuptools-scm]: https://setuptools-scm.readthedocs.io/en/latest/extending/#available-implementations
[\[#421\]]: https://github.com/spglib/spglib/pull/421
[\[#422\]]: https://github.com/spglib/spglib/pull/422
[\[#426\]]: https://github.com/spglib/spglib/pull/426
19 changes: 18 additions & 1 deletion cmake/SpglibConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,26 @@ if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/PackageCompsHelper.cmake)
message(WARNING "Missing helper file PackageCompsHelper.cmake")
set(Spglib_FOUND FALSE)
return()
endif()
endif ()

include(${CMAKE_CURRENT_LIST_DIR}/PackageCompsHelper.cmake)
find_package_with_comps(PACKAGE Spglib PRINT LOAD_ALL_DEFAULT HAVE_GLOBAL_SHARED_STATIC)

check_required_components(Spglib)

# For Fortran targets, check that the modules are usable with the current compiler
if (CMAKE_Fortran_COMPILER AND TARGET Spglib::fortran_mod)
# TODO: CMake 3.25 use the modern try_compile signature. Remove the explicit CMakeScratch
try_compile(spglib_fortran_try_compile ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeScratch/spglib_fortran
SOURCES ${CMAKE_CURRENT_LIST_DIR}/try_compile.f90
LINK_LIBRARIES Spglib::fortran_mod
)
if (spglib_fortran_try_compile)
# If the compilation was successful, use the module version of the library
add_library(Spglib::fortran ALIAS Spglib::fortran_mod)
else ()
# Otherwise, assume it was because of incompatible compiler
# Add the bundled `.f90` files as sources instead
add_library(Spglib::fortran ALIAS Spglib::fortran_include)
endif ()
endif ()
35 changes: 30 additions & 5 deletions fortran/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ add_library(Spglib_fortran)
add_library(Spglib::fortran ALIAS Spglib_fortran)
configure_file(spglib_version.f90.in spglib_version.f90)

# This target is only used in the SpglibConfig.cmake
add_library(Spglib_fortran_include INTERFACE)
target_sources(Spglib_fortran_include INTERFACE
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/spglib_f08.F90>"
)
set_target_properties(Spglib_fortran_include PROPERTIES
EXPORT_NAME fortran_include
)

# Concatenate the contents of the fortran file so it can be compiled from source
execute_process(COMMAND ${CMAKE_COMMAND} -E cat
${CMAKE_CURRENT_BINARY_DIR}/spglib_version.f90
${CMAKE_CURRENT_SOURCE_DIR}/spglib_f08.F90
OUTPUT_VARIABLE spglib_f08_concat
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/spglib_f08.F90 ${spglib_f08_concat})

# Use the separate files for the project itself so that they are properly re-built
target_sources(Spglib_fortran PRIVATE
spglib_f08.F90
${CMAKE_CURRENT_BINARY_DIR}/spglib_version.f90
Expand All @@ -12,14 +31,16 @@ target_sources(Spglib_fortran PRIVATE
set_target_properties(Spglib_fortran PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
EXPORT_NAME fortran
EXPORT_NAME fortran_mod
OUTPUT_NAME spglib_f08
PUBLIC_HEADER spglib_f08.F90)
set_target_properties(Spglib_fortran PROPERTIES
PUBLIC_HEADER spglib_f08.F90)
)
set_target_properties(Spglib_fortran_include PROPERTIES
PUBLIC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/spglib_f08.F90
)
target_include_directories(Spglib_fortran PUBLIC
"$<BUILD_INTERFACE:${CMAKE_Fortran_MODULE_DIRECTORY}>")
target_link_libraries(Spglib_fortran PUBLIC Spglib_symspg)
target_link_libraries(Spglib_fortran_include INTERFACE Spglib_symspg)
# Note: Fortran wrapper is not linked to OpenMP library because it should not be defining any such setup

# Install
Expand All @@ -43,7 +64,7 @@ if (SPGLIB_INSTALL)
endif ()
target_include_directories(Spglib_fortran PUBLIC
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR}>")
install(TARGETS Spglib_fortran
install(TARGETS Spglib_fortran Spglib_fortran_include
EXPORT SpglibTargets-fortran
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Spglib_Runtime
NAMELINK_COMPONENT Spglib_Development
Expand All @@ -52,6 +73,10 @@ if (SPGLIB_INSTALL)
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT Spglib_Runtime
)
export_components(COMPONENT fortran LIB_TYPE ${SPGLIB_LIB_TYPE})
install(FILES try_compile.f90
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Spglib
COMPONENT Spglib_Development
)

# Maybe it is handled automatically
install(FILES ${CMAKE_Fortran_MODULE_DIRECTORY}/spglib_f08.mod
Expand Down
5 changes: 5 additions & 0 deletions fortran/try_compile.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
program try_compile
use spglib_f08

print *, version
end program try_compile

0 comments on commit 383d1ff

Please sign in to comment.