diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt --- a/flang/CMakeLists.txt +++ b/flang/CMakeLists.txt @@ -6,6 +6,17 @@ include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake NO_POLICY_SCOPE) +# Cases where "all" lib types are built will cause libs with "_static" +# appended to the lib name to be built. +if ((BUILD_SHARED_LIBS AND BUILD_STATIC_LIBS) OR + (LLVM_BUILD_USER_FORTRAN_LIBS STREQUAL "all") OR + (BUILD_SHARED_LIBS AND LLVM_BUILD_USER_FORTRAN_LIBS STREQUAL "static") OR + (BUILD_STATIC_LIBS AND LLVM_BUILD_USER_FORTRAN_LIBS STREQUAL "shared")) + set_property(GLOBAL PROPERTY FORTRAN_STATIC_LIB_NAME TRUE) +elseif() + set_property(GLOBAL PROPERTY FORTRAN_STATIC_LIB_NAME FALSE) +endif() + set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON) # Flang requires C++17. diff --git a/flang/cmake/modules/AddFlang.cmake b/flang/cmake/modules/AddFlang.cmake --- a/flang/cmake/modules/AddFlang.cmake +++ b/flang/cmake/modules/AddFlang.cmake @@ -64,31 +64,72 @@ set_property(GLOBAL APPEND PROPERTY FLANG_STATIC_LIBS ${name}) endif() + # Append additional linkage build types for fortran libraries used by users. + if (LLVM_BUILD_USER_FORTRAN_LIBS) + set(USER_FORTRAN_LIBS "FortranDecimal" "FortranRuntime") + if (${name} IN_LIST USER_FORTRAN_LIBS) + if (LLVM_BUILD_USER_FORTRAN_LIBS STREQUAL "all") + list(APPEND LIBTYPE SHARED STATIC) + elseif (LLVM_BUILD_USER_FORTRAN_LIBS STREQUAL "shared") + list(APPEND LIBTYPE SHARED) + elseif (LLVM_BUILD_USER_FORTRAN_LIBS STREQUAL "static") + list(APPEND LIBTYPE STATIC) + else() + message(FATAL_ERROR + "LLVM_BUILD_USER_FORTRAN_LIBS must be one of [static|shared|all].") + endif() + endif() + endif() + llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs}) clang_target_link_libraries(${name} PRIVATE ${ARG_CLANG_LIBS}) - if (TARGET ${name}) - - if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libflang" - OR ARG_INSTALL_WITH_TOOLCHAIN) - get_target_export_arg(${name} Flang export_to_flangtargets UMBRELLA flang-libraries) - install(TARGETS ${name} - COMPONENT ${name} - ${export_to_flangtargets} - LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} - ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") - - if (NOT LLVM_ENABLE_IDE) - add_llvm_install_targets(install-${name} - DEPENDS ${name} - COMPONENT ${name}) + set(name_static "${name}_static") + # Both name and name_static can exist at the same time. + if (TARGET ${name} OR TARGET ${name_static}) + if (TARGET ${name}) + if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libflang" + OR ARG_INSTALL_WITH_TOOLCHAIN) + get_target_export_arg(${name} Flang export_to_flangtargets UMBRELLA flang-libraries) + install(TARGETS ${name} + COMPONENT ${name} + ${export_to_flangtargets} + LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} + ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") + + if (NOT LLVM_ENABLE_IDE) + add_llvm_install_targets(install-${name} + DEPENDS ${name} + COMPONENT ${name}) + endif() + + set_property(GLOBAL APPEND PROPERTY FLANG_LIBS ${name}) endif() - - set_property(GLOBAL APPEND PROPERTY FLANG_LIBS ${name}) + set_property(GLOBAL APPEND PROPERTY FLANG_EXPORTS ${name}) + endif() + if (TARGET ${name_static}) + if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name_static} STREQUAL "libflang" + OR ARG_INSTALL_WITH_TOOLCHAIN) + get_target_export_arg(${name_static} Flang export_to_flangtargets UMBRELLA flang-libraries) + install(TARGETS ${name_static} + COMPONENT ${name_static} + ${export_to_flangtargets} + LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} + ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") + + if (NOT LLVM_ENABLE_IDE) + add_llvm_install_targets(install-${name_static} + DEPENDS ${name_static} + COMPONENT ${name_static}) + endif() + + set_property(GLOBAL APPEND PROPERTY FLANG_LIBS ${name_static}) + endif() + set_property(GLOBAL APPEND PROPERTY FLANG_EXPORTS ${name_static}) endif() - set_property(GLOBAL APPEND PROPERTY FLANG_EXPORTS ${name}) else() # Add empty "phony" target add_custom_target(${name}) diff --git a/flang/lib/Evaluate/CMakeLists.txt b/flang/lib/Evaluate/CMakeLists.txt --- a/flang/lib/Evaluate/CMakeLists.txt +++ b/flang/lib/Evaluate/CMakeLists.txt @@ -19,6 +19,16 @@ endif() endif() +# If the user fortran libraries are built both shared and static, +# FortranDecimal becomes FortranDecimal_static. If we want to build flang-new +# statically, we must check if FortranDecimal_static exists. +get_property(STATIC_NAME GLOBAL PROPERTY FORTRAN_STATIC_LIB_NAME) +if(STATIC_NAME) + list(APPEND USER_FORTRAN_LIBS FortranDecimal_static) +else() + list(APPEND USER_FORTRAN_LIBS FortranDecimal) +endif() + add_flang_library(FortranEvaluate call.cpp characteristics.cpp @@ -52,7 +62,7 @@ LINK_LIBS FortranCommon - FortranDecimal + ${USER_FORTRAN_LIBS} FortranParser ${LIBPGMATH} @@ -62,4 +72,5 @@ DEPENDS acc_gen omp_gen + ${USER_FORTRAN_LIBS} ) diff --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt --- a/flang/runtime/CMakeLists.txt +++ b/flang/runtime/CMakeLists.txt @@ -84,6 +84,16 @@ add_subdirectory(FortranMain) +# If the user fortran libraries are built both shared and static, +# FortranDecimal becomes FortranDecimal_static. If we want to build flang-new +# statically, we must check if FortranDecimal_static exists. +get_property(STATIC_NAME GLOBAL PROPERTY FORTRAN_STATIC_LIB_NAME) +if(STATIC_NAME) + list(APPEND USER_FORTRAN_LIBS FortranDecimal_static) +else() + list(APPEND USER_FORTRAN_LIBS FortranDecimal) +endif() + add_flang_library(FortranRuntime ISO_Fortran_binding.cpp allocatable.cpp @@ -144,7 +154,7 @@ utf.cpp LINK_LIBS - FortranDecimal + ${USER_FORTRAN_LIBS} INSTALL_WITH_TOOLCHAIN ) diff --git a/flang/tools/flang-driver/CMakeLists.txt b/flang/tools/flang-driver/CMakeLists.txt --- a/flang/tools/flang-driver/CMakeLists.txt +++ b/flang/tools/flang-driver/CMakeLists.txt @@ -11,6 +11,16 @@ TargetParser ) +# If the user fortran libraries are built both shared and static, the static +# libs get "_static" appended to the name. If we want to build flang-new +# statically, we must check if the "_static" libraries exist. +get_property(STATIC_NAME GLOBAL PROPERTY FORTRAN_STATIC_LIB_NAME) +if(STATIC_NAME) + list(APPEND USER_FORTRAN_LIBS FortranDecimal_static FortranRuntime_static) +else() + list(APPEND USER_FORTRAN_LIBS FortranDecimal FortranRuntime) +endif() + add_flang_tool(flang-new driver.cpp fc1_main.cpp @@ -19,8 +29,7 @@ # These libraries are used in the linker invocation generated by the driver # (i.e. when constructing the linker job). Without them the driver would be # unable to generate executables. - FortranRuntime - FortranDecimal + ${USER_FORTRAN_LIBS} Fortran_main )