我(尝试)利用CMake的生成器表达式来支持基于Fortran项目的编译器供应商的 生成时 编译器标志配置。
不幸的是,虽然相关的documentation描述了如何在C和C ++项目中完成(分别参见C_COMPILER_ID
和CXX_COMPILER_ID
逻辑表达式),但没有提到Fortran模拟。
我已确认没有Fortran_COMPILER_ID
生成器表达式,在使用时会发出以下错误:
CMake Error at CMakeLists.txt:<line number> (target_compile_options):
Error evaluating generator expression:
$<Fortran_COMPILER_ID:GNU>
Expression did not evaluate to a known generator expression
是否可以为Fortran项目估算此行为?
编辑:
期望行为的最低范例
cmake_minimum_required( VERSION 3.2 )
project( myProject LANGUAGES Fortran )
set( fortran_module_directory "${CMAKE_BINARY_DIR}/modules" CACHE PATH "directory for fortran modules" )
file( MAKE_DIRECTORY ${fortran_module_directory} )
set( CMAKE_Fortran_MODULE_DIRECTORY ${fortran_module_directory} )
set( myProject_GNU_DEBUG_flags "flag1" "flag2" )
set( myProject_GNU_RELEASE_flags "flag3" "flag4" )
set( myProject_Intel_DEBUG_flags "flag5" "flag6")
set( myProject_Intel_RELEASE_flags "flag7" "flag8" )
# other compilers
add_library( myProject STATIC
${CMAKE_CURRENT_SOURCE_DIR}/src/mySource.f90
${CMAKE_CURRENT_SOURCE_DIR}/src/myOtherSource.f90 )
target_include_directories( myProject PUBLIC ${fortran_module_directory} )
target_compile_options( njoy PRIVATE
$<$<Fortran_COMPILER_ID:GNU>:
$<$<CONFIG:Debug>:${myProject_GNU_DEBUG_flags}>>
$<$<CONFIG:Release>:${myProject_GNU_RELEASE_flags}>>>
$<$<Fortran_COMPILER_ID:Intel>:
$<$<CONFIG:Debug>:${myProject_Intel_DEBUG_flags}>>
$<$<CONFIG:Release>:${myProject_Intel_RELEASE_flags}>>>
# other compilers
)
答案 0 :(得分:1)
我从HDF5 CMakeLists.txt的灵感中发现了一种可行的方法。 例如:
set (Fortran_COMPILER_ID CMAKE_Fortran_COMPILER_ID)
target_link_libraries(target PUBLIC
$<$<STREQUAL:"${Fortran_COMPILER_ID}","GNU">:"-lgfortran">
$<$<STREQUAL:"${Fortran_COMPILER_ID}","Intel">:"-lifcore">
)