@@ -12,7 +12,8 @@ function(preprocess preproc preprocopts srcext trgext srcfiles trgfiles)
1212
1313 set (_trgfiles)
1414 foreach (srcfile IN LISTS srcfiles)
15- string (REGEX REPLACE "\\ .${srcext} $" ".${trgext} " trgfile ${srcfile} )
15+ get_filename_component (filename ${srcfile} NAME )
16+ string (REGEX REPLACE "\\ .${srcext} $" ".${trgext} " trgfile ${filename} )
1617 add_custom_command (
1718 OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /${trgfile}
1819 COMMAND ${preproc} ${preprocopts} ${CMAKE_CURRENT_SOURCE_DIR} /${srcfile} ${CMAKE_CURRENT_BINARY_DIR} /${trgfile}
@@ -47,3 +48,65 @@ function (fypp_f90pp fyppopts fyppfiles F90files)
4748 set (${F90files} ${_F90files} PARENT_SCOPE)
4849endfunction ()
4950
51+ # Helper function to configure stdlib targets
52+ #
53+ # It preprocesses the given fypp and fypp+cpp files, combines them with the
54+ # regular Fortran files, and creates a library target with the given name.
55+ # Args:
56+ # target_name [in]: Name of the library target to create
57+ # regular_sources_var [in]: Regular Fortran sources
58+ # fypp_files_var [in]: Sources to be preprocessed with fypp
59+ # cpp_files_var [in]: Sources to be preprocessed with fypp and cpp
60+ #
61+ function (configure_stdlib_target target_name regular_sources_var fypp_files_var cpp_files_var)
62+ #### Pre-process: .fpp -> .f90 via Fypp
63+ fypp_f90("${fyppFlags} " "${${fypp_files_var} }" ${target_name} _fypp_outFiles)
64+ #### Pre-process: .fypp -> .F90 via Fypp (for C preprocessor directives)
65+ fypp_f90pp("${fyppFlags} " "${${cpp_files_var} }" ${target_name} _cpp_outFiles)
66+ 67+ list (APPEND all_sources ${${target_name} _fypp_outFiles})
68+ list (APPEND all_sources ${${target_name} _cpp_outFiles})
69+ list (APPEND all_sources ${${regular_sources_var} })
70+ 71+ add_library (${target_name} ${all_sources} )
72+ add_library (${PROJECT_NAME} ::${target_name} ALIAS ${target_name} )
73+ 74+ set_target_properties (
75+ ${target_name}
76+ PROPERTIES
77+ POSITION_INDEPENDENT_CODE ON
78+ WINDOWS_EXPORT_ALL_SYMBOLS ON
79+ )
80+ 81+ if (CMAKE_Fortran_COMPILER_ID STREQUAL GNU AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 10.0)
82+ target_compile_options (
83+ ${target_name}
84+ PRIVATE
85+ $<$<COMPILE_LANGUAGE:Fortran>:-fno-range-check>
86+ )
87+ endif ()
88+ 89+ set (LIB_MOD_DIR ${CMAKE_CURRENT_BINARY_DIR} /mod_files/${target_name} /)
90+ set (INSTALL_MOD_DIR "${CMAKE_INSTALL_MODULEDIR} /${target_name} " )
91+ # We need the module directory before we finish the configure stage since the
92+ # build interface might resolve before the module directory is generated by CMake
93+ if (NOT EXISTS "${LIB_MOD_DIR} " )
94+ file (MAKE_DIRECTORY "${LIB_MOD_DIR} " )
95+ endif ()
96+ 97+ set_target_properties (${target_name} PROPERTIES
98+ Fortran_MODULE_DIRECTORY ${LIB_MOD_DIR}
99+ )
100+ target_include_directories (${target_name} PUBLIC
101+ $<BUILD_INTERFACE:${LIB_MOD_DIR} >
102+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_MODULEDIR} >
103+ )
104+ 105+ install (TARGETS ${target_name}
106+ EXPORT ${PROJECT_NAME} -targets
107+ RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR} "
108+ ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR} "
109+ LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR} "
110+ )
111+ install (DIRECTORY ${LIB_MOD_DIR} DESTINATION "${INSTALL_MOD_DIR} " )
112+ endfunction ()
0 commit comments