Skip to content

Commit

Permalink
Misc CMake file fixes for linking in Windows
Browse files Browse the repository at this point in the history
- Set the target linker language for the flang libraries to CXX on windows.

  This makes sure link.exe from Visual Studio is used to link the libraries,
  even though the libraries include fortran code. CMake is not able to detect this
  since the flang fortran compiler is not working at CMake configure time and thus
  the CMAKE_Fortran_COMPILER_ID is unknown.

- Add WINDOWS_EXPORT_ALL_SYMBOLS for shared flang library

- Use "lib" prefix instead of "_static" suffix for static runtime libraries.

  This is more in sync with windows default names
  (e.g. ucrt.lib as import library for ucrt.dll and libucrt.lib for the static runtime).

- Set WINDOWS_EXPORT_ALL_SYMBOLS property for for shared flangrti and ompstub targets.

  This works around the issue with missing __declspec(dllexport/dllimport) declerations
  in the source files.
  • Loading branch information
Albert Ziegenhagel authored and kiranchandramohan committed Aug 5, 2021
1 parent 1375882 commit 1729d1a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
56 changes: 34 additions & 22 deletions runtime/flang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ SET(FTN_INTRINSICS_DESC_DEP
red_any.c
red_count.c
red_findloc.c
red_iany.c
red_iany.c
red_maxloc.c
red_minloc.c
red_maxval.c
Expand Down Expand Up @@ -418,7 +418,7 @@ separate_arguments(SEPARATED_CMAKE_Fortran_FLAGS NATIVE_COMMAND ${CMAKE_Fortran_
add_custom_command(
OUTPUT "${I8_FILES_DIR}/ieee_arithmetic.F95"
COMMAND "${CMAKE_Fortran_COMPILER}" -E -cpp ${SEPARATED_CMAKE_Fortran_FLAGS}
"${CMAKE_CURRENT_SOURCE_DIR}/ieee_arithmetic.F95" -DDESC_I8
"${CMAKE_CURRENT_SOURCE_DIR}/ieee_arithmetic.F95" -DDESC_I8
> "${I8_FILES_DIR}/ieee_arithmetic.F95"
COMMENT "Preprocessing ieee_arithmetic.F95"
VERBATIM
Expand All @@ -428,7 +428,7 @@ add_custom_command(
add_custom_command(
OUTPUT "${I8_FILES_DIR}/ieee_exceptions.F95"
COMMAND "${CMAKE_Fortran_COMPILER}" -E -cpp ${SEPARATED_CMAKE_Fortran_FLAGS}
"${CMAKE_CURRENT_SOURCE_DIR}/ieee_exceptions.F95" -DDESC_I8
"${CMAKE_CURRENT_SOURCE_DIR}/ieee_exceptions.F95" -DDESC_I8
> "${I8_FILES_DIR}/ieee_exceptions.F95"
COMMENT "Preprocessing ieee_exceptions.F95"
VERBATIM
Expand Down Expand Up @@ -460,7 +460,7 @@ FOREACH(file ${FTN_SUPPORT_DESC_DEP})
ENDFOREACH(file)

LIST(APPEND FTN_SUPPORT_DESC_DEP ieee_arithmetic.F95 ieee_exceptions.F95 norm2_1.F95)
LIST(APPEND FTN_SUPPORT_I8
LIST(APPEND FTN_SUPPORT_I8
"${I8_FILES_DIR}/ieee_arithmetic.F95"
"${I8_FILES_DIR}/ieee_exceptions.F95"
"${I8_FILES_DIR}/norm2_1.F95"
Expand All @@ -475,7 +475,11 @@ add_flang_library(flang_static
${FTN_SUPPORT_I8}
${SHARED_SOURCES}
)
if (MSVC)
set_property(TARGET flang_static PROPERTY OUTPUT_NAME libflang)
else()
set_property(TARGET flang_static PROPERTY OUTPUT_NAME flang)
endif()

set(SHARED_LIBRARY TRUE)
add_flang_library(flang_shared
Expand All @@ -501,13 +505,17 @@ add_dependencies(flang_shared flang_static)

target_link_libraries(flang_shared ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib/libflangrti.so)
# Resolve symbols against libm and librt
target_link_libraries(flang_shared m rt)
if (NOT MSVC)
target_link_libraries(flang_shared m rt)
else()
set_target_properties(flang_shared PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()

set(SHARED_LIBRARY FALSE)

set_property(
SOURCE ${FTN_INTRINSICS_DESC_INDEP} ${FTN_INTRINSICS_DESC_DEP} ${SHARED_SOURCES}
PROPERTY COMPILE_DEFINITIONS
PROPERTY COMPILE_DEFINITIONS
PGF90
KANJI
INT32PTR64
Expand All @@ -517,7 +525,7 @@ set_property(

set_property(
SOURCE ${FTN_INTRINSICS_I8}
PROPERTY COMPILE_DEFINITIONS
PROPERTY COMPILE_DEFINITIONS
PGF90
KANJI
INT32PTR64
Expand All @@ -527,16 +535,16 @@ set_property(
)

set_property(
SOURCE ${FTN_SUPPORT_DESC_INDEP} ${FTN_SUPPORT_DESC_DEP}
PROPERTY COMPILE_DEFINITIONS
SOURCE ${FTN_SUPPORT_DESC_INDEP} ${FTN_SUPPORT_DESC_DEP}
PROPERTY COMPILE_DEFINITIONS
INT32PTR64
TM_I8
HAVE_LONG_LONG_INT
)

set_property(
SOURCE ${FTN_SUPPORT_I8}
PROPERTY COMPILE_DEFINITIONS
SOURCE ${FTN_SUPPORT_I8}
PROPERTY COMPILE_DEFINITIONS
INT32PTR64
TM_I8
DESC_I8
Expand Down Expand Up @@ -585,30 +593,30 @@ if(NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
# State a dependency on the module
set_source_files_properties(
${I8_FILES_DIR}/ieee_arithmetic.F95
PROPERTIES
PROPERTIES
COMPILE_DEFINITIONS DESC_I8
OBJECT_DEPENDS ${CMAKE_Fortran_MODULE_DIRECTORY}/iso_c_binding.mod
OBJECT_OUTPUTS ${CMAKE_Fortran_MODULE_DIRECTORY}/ieee_arithmetic_la.mod
)

set_source_files_properties(
${I8_FILES_DIR}/ieee_exceptions.F95
PROPERTIES
PROPERTIES
COMPILE_DEFINITIONS DESC_I8
OBJECT_DEPENDS ${CMAKE_Fortran_MODULE_DIRECTORY}/iso_c_binding.mod
OBJECT_OUTPUTS ${CMAKE_Fortran_MODULE_DIRECTORY}/ieee_exceptions_la.mod
)

set_source_files_properties(
norm2_1.F95
PROPERTIES
PROPERTIES
OBJECT_DEPENDS ${CMAKE_Fortran_MODULE_DIRECTORY}/ieee_arithmetic.mod
OBJECT_DEPENDS ${CMAKE_Fortran_MODULE_DIRECTORY}/ieee_exceptions.mod
)

set_source_files_properties(
${I8_FILES_DIR}/norm2_1.F95
PROPERTIES
PROPERTIES
OBJECT_DEPENDS ${CMAKE_Fortran_MODULE_DIRECTORY}/ieee_arithmetic_la.mod
OBJECT_DEPENDS ${CMAKE_Fortran_MODULE_DIRECTORY}/ieee_exceptions_la.mod
)
Expand All @@ -618,8 +626,8 @@ set_target_properties(flang_static flang_shared
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${FLANG_RTE_LIB_DIR}
)
target_include_directories(flang_static

target_include_directories(flang_static
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
Expand All @@ -632,20 +640,24 @@ target_include_directories(flang_shared
)

# Make sure the compiler is built before we bootstrap
add_dependencies(flang_static
add_dependencies(flang_static
flang1
flang2
)

# Make sure the compiler is built before we bootstrap
add_dependencies(flang_shared
add_dependencies(flang_shared
flang1
flang2
)

target_compile_options(flang_static PRIVATE -fPIC)

target_compile_options(flang_shared PRIVATE -fPIC)
if (NOT MSVC)
target_compile_options(flang_static PRIVATE -fPIC)
target_compile_options(flang_shared PRIVATE -fPIC)
else()
set_target_properties(flang_static PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(flang_shared PROPERTIES LINKER_LANGUAGE CXX)
endif()

target_compile_options(flang_static PUBLIC $<$<COMPILE_LANGUAGE:Fortran>:-Mreentrant>)

Expand Down
21 changes: 14 additions & 7 deletions runtime/flangrti/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SET(ASM_OPTIONS "-DLINUX_ELF")
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS}" )

if( ${TARGET_ARCHITECTURE} STREQUAL "x86_64" )
set(ARCH_DEP_FILES
set(ARCH_DEP_FILES
x86_64-Linux/x86_daz.c
x86_64-Linux/x86_flushz.c
x86_64-Linux/dumpregs.c
Expand Down Expand Up @@ -61,7 +61,11 @@ add_flang_library(flangrti_static
${PGC_SRC_FILES}
${SHARED_SOURCES}
)
set_property(TARGET flangrti_static PROPERTY OUTPUT_NAME flangrti)
if (MSVC)
set_property(TARGET flangrti_static PROPERTY OUTPUT_NAME libflangrti)
else()
set_property(TARGET flangrti_static PROPERTY OUTPUT_NAME flangrti)
endif()


set(SHARED_LIBRARY TRUE)
Expand All @@ -81,14 +85,14 @@ endif()

# Import OpenMP
if (NOT DEFINED LIBOMP_EXPORT_DIR)
find_library(
find_library(
FLANG_LIBOMP
libomp.so
HINTS ${CMAKE_BINARY_DIR}/lib)
target_link_libraries(flangrti_shared ${FLANG_LIBOMP})
endif()

find_library(
find_library(
LIBPGMATH
libpgmath.so
HINTS ${CMAKE_BINARY_DIR}/lib)
Expand Down Expand Up @@ -118,9 +122,12 @@ target_include_directories(flangrti_shared
set_target_properties(flangrti_shared flangrti_static
PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${FLANG_RTE_LIB_DIR})

target_compile_options(flangrti_static PRIVATE -fPIC)

target_compile_options(flangrti_shared PRIVATE -fPIC)
if (NOT MSVC)
target_compile_options(flangrti_static PRIVATE -fPIC)
target_compile_options(flangrti_shared PRIVATE -fPIC)
else()
set_target_properties(flangrti_shared PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()

target_compile_options(flangrti_static PUBLIC $<$<COMPILE_LANGUAGE:Fortran>:-Mreentrant>)

Expand Down
9 changes: 8 additions & 1 deletion runtime/ompstub/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
set(OMPSTUB_SRC init_nomp.c ompstubs.c)

add_flang_library(ompstub_static ${OMPSTUB_SRC})
set_property(TARGET ompstub_static PROPERTY OUTPUT_NAME ompstub)
if (MSVC)
set_property(TARGET ompstub_static PROPERTY OUTPUT_NAME libompstub)
else()
set_property(TARGET ompstub_static PROPERTY OUTPUT_NAME ompstub)
endif()

set(SHARED_LIBRARY TRUE)
add_flang_library(ompstub_shared ${OMPSTUB_SRC})
set_property(TARGET ompstub_shared PROPERTY OUTPUT_NAME ompstub)
if (MSVC)
set_target_properties(ompstub_shared PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()
set(SHARED_LIBRARY FALSE)

set_target_properties(ompstub_static ompstub_shared
Expand Down

0 comments on commit 1729d1a

Please sign in to comment.