1# 2#//===----------------------------------------------------------------------===// 3#// 4#// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5#// See https://llvm.org/LICENSE.txt for license information. 6#// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7#// 8#//===----------------------------------------------------------------------===// 9# 10 11# Configure omp.h, kmp_config.h and omp-tools.h if necessary 12configure_file(${LIBOMP_INC_DIR}/omp.h.var omp.h @ONLY) 13configure_file(kmp_config.h.cmake kmp_config.h @ONLY) 14if(${LIBOMP_OMPT_SUPPORT}) 15 configure_file(${LIBOMP_INC_DIR}/omp-tools.h.var omp-tools.h @ONLY) 16endif() 17 18# Generate message catalog files: kmp_i18n_id.inc and kmp_i18n_default.inc 19add_custom_command( 20 OUTPUT kmp_i18n_id.inc 21 COMMAND ${PERL_EXECUTABLE} ${LIBOMP_TOOLS_DIR}/message-converter.pl --os=${LIBOMP_PERL_SCRIPT_OS} 22 --prefix=kmp_i18n --enum=kmp_i18n_id.inc ${LIBOMP_SRC_DIR}/i18n/en_US.txt 23 DEPENDS ${LIBOMP_SRC_DIR}/i18n/en_US.txt ${LIBOMP_TOOLS_DIR}/message-converter.pl 24) 25add_custom_command( 26 OUTPUT kmp_i18n_default.inc 27 COMMAND ${PERL_EXECUTABLE} ${LIBOMP_TOOLS_DIR}/message-converter.pl --os=${LIBOMP_PERL_SCRIPT_OS} 28 --prefix=kmp_i18n --default=kmp_i18n_default.inc ${LIBOMP_SRC_DIR}/i18n/en_US.txt 29 DEPENDS ${LIBOMP_SRC_DIR}/i18n/en_US.txt ${LIBOMP_TOOLS_DIR}/message-converter.pl 30) 31 32# Set the -D definitions for all sources 33# UNICODE and _UNICODE are set in LLVM's CMake system. They affect the 34# ittnotify code and should only be set when compiling ittnotify_static.cpp 35# on Windows (done below). 36# TODO: Fix the UNICODE usage in ittnotify code for Windows. 37remove_definitions(-DUNICODE -D_UNICODE) 38libomp_get_definitions_flags(LIBOMP_CONFIGURED_DEFINITIONS_FLAGS) 39add_definitions(${LIBOMP_CONFIGURED_DEFINITIONS_FLAGS}) 40 41# Set the -I includes for all sources 42include_directories( 43 ${CMAKE_CURRENT_BINARY_DIR} 44 ${LIBOMP_SRC_DIR} 45 ${LIBOMP_SRC_DIR}/i18n 46 ${LIBOMP_INC_DIR} 47 ${LIBOMP_SRC_DIR}/thirdparty/ittnotify 48) 49if(${LIBOMP_USE_HWLOC}) 50 include_directories(${LIBOMP_HWLOC_INSTALL_DIR}/include) 51endif() 52 53# Getting correct source files to build library 54set(LIBOMP_CXXFILES) 55set(LIBOMP_ASMFILES) 56if(STUBS_LIBRARY) 57 set(LIBOMP_CXXFILES kmp_stub.cpp) 58else() 59 # Get C++ files 60 set(LIBOMP_CXXFILES 61 kmp_alloc.cpp 62 kmp_atomic.cpp 63 kmp_csupport.cpp 64 kmp_debug.cpp 65 kmp_itt.cpp 66 kmp_environment.cpp 67 kmp_error.cpp 68 kmp_global.cpp 69 kmp_i18n.cpp 70 kmp_io.cpp 71 kmp_runtime.cpp 72 kmp_settings.cpp 73 kmp_str.cpp 74 kmp_tasking.cpp 75 kmp_threadprivate.cpp 76 kmp_utility.cpp 77 kmp_barrier.cpp 78 kmp_wait_release.cpp 79 kmp_affinity.cpp 80 kmp_dispatch.cpp 81 kmp_lock.cpp 82 kmp_sched.cpp 83 ) 84 if(WIN32) 85 # Windows specific files 86 libomp_append(LIBOMP_CXXFILES z_Windows_NT_util.cpp) 87 libomp_append(LIBOMP_CXXFILES z_Windows_NT-586_util.cpp) 88 libomp_append(LIBOMP_ASMFILES z_Windows_NT-586_asm.asm) # Windows assembly file 89 else() 90 # Unix specific files 91 libomp_append(LIBOMP_CXXFILES z_Linux_util.cpp) 92 libomp_append(LIBOMP_CXXFILES kmp_gsupport.cpp) 93 libomp_append(LIBOMP_ASMFILES z_Linux_asm.S) # Unix assembly file 94 endif() 95 libomp_append(LIBOMP_CXXFILES thirdparty/ittnotify/ittnotify_static.cpp LIBOMP_USE_ITT_NOTIFY) 96 libomp_append(LIBOMP_CXXFILES kmp_debugger.cpp LIBOMP_USE_DEBUGGER) 97 libomp_append(LIBOMP_CXXFILES kmp_stats.cpp LIBOMP_STATS) 98 libomp_append(LIBOMP_CXXFILES kmp_stats_timing.cpp LIBOMP_STATS) 99 libomp_append(LIBOMP_CXXFILES kmp_taskdeps.cpp) 100 libomp_append(LIBOMP_CXXFILES kmp_cancel.cpp) 101endif() 102# Files common to stubs and normal library 103libomp_append(LIBOMP_CXXFILES kmp_ftn_cdecl.cpp) 104libomp_append(LIBOMP_CXXFILES kmp_ftn_extra.cpp) 105libomp_append(LIBOMP_CXXFILES kmp_version.cpp) 106libomp_append(LIBOMP_CXXFILES ompt-general.cpp IF_TRUE LIBOMP_OMPT_SUPPORT) 107libomp_append(LIBOMP_CXXFILES tsan_annotations.cpp IF_TRUE LIBOMP_TSAN_SUPPORT) 108 109set(LIBOMP_SOURCE_FILES ${LIBOMP_CXXFILES} ${LIBOMP_ASMFILES}) 110# For Windows, there is a resource file (.rc -> .res) that is also compiled 111libomp_append(LIBOMP_SOURCE_FILES libomp.rc WIN32) 112 113# Get compiler and assembler flags 114libomp_get_cxxflags(LIBOMP_CONFIGURED_CXXFLAGS) 115libomp_get_asmflags(LIBOMP_CONFIGURED_ASMFLAGS) 116# Set the compiler flags for each type of source 117set_source_files_properties(${LIBOMP_CXXFILES} PROPERTIES COMPILE_FLAGS "${LIBOMP_CONFIGURED_CXXFLAGS}") 118set_source_files_properties(${LIBOMP_ASMFILES} PROPERTIES COMPILE_FLAGS "${LIBOMP_CONFIGURED_ASMFLAGS}") 119# Let the compiler handle the assembly files on Unix-like systems 120if(NOT WIN32) 121 set_source_files_properties(${LIBOMP_ASMFILES} PROPERTIES LANGUAGE C) 122endif() 123 124# Remove any cmake-automatic linking of the standard C++ library. 125# We neither need (nor want) the standard C++ library dependency even though we compile c++ files. 126if(NOT ${LIBOMP_USE_STDCPPLIB}) 127 set(LIBOMP_LINKER_LANGUAGE C) 128 set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES) 129else() 130 set(LIBOMP_LINKER_LANGUAGE CXX) 131endif() 132 133# Add the OpenMP library 134libomp_get_ldflags(LIBOMP_CONFIGURED_LDFLAGS) 135 136add_library(omp ${LIBOMP_LIBRARY_KIND} ${LIBOMP_SOURCE_FILES}) 137 138set_target_properties(omp PROPERTIES 139 PREFIX "" SUFFIX "" OUTPUT_NAME "${LIBOMP_LIB_FILE}" 140 LINK_FLAGS "${LIBOMP_CONFIGURED_LDFLAGS}" 141 LINKER_LANGUAGE ${LIBOMP_LINKER_LANGUAGE} 142) 143 144# Get the library's location within the build tree for the unit tester 145if(NOT WIN32) 146 get_target_property(LIBOMP_LIBRARY_DIR omp LIBRARY_OUTPUT_DIRECTORY) 147else() 148 get_target_property(LIBOMP_LIBRARY_DIR omp RUNTIME_OUTPUT_DIRECTORY) 149endif() 150if(NOT LIBOMP_LIBRARY_DIR) 151 set(LIBOMP_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) 152 set(LIBOMP_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE) 153else() 154 set(LIBOMP_LIBRARY_DIR ${LIBOMP_LIBRARY_DIR} PARENT_SCOPE) 155endif() 156set(LIBOMP_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE) 157 158# Add symbolic links to libomp 159if(NOT WIN32) 160 add_custom_command(TARGET omp POST_BUILD 161 COMMAND ${CMAKE_COMMAND} -E create_symlink ${LIBOMP_LIB_FILE} 162 libgomp${LIBOMP_LIBRARY_SUFFIX} 163 COMMAND ${CMAKE_COMMAND} -E create_symlink ${LIBOMP_LIB_FILE} 164 libiomp5${LIBOMP_LIBRARY_SUFFIX} 165 WORKING_DIRECTORY ${LIBOMP_LIBRARY_DIR} 166 ) 167endif() 168 169# Linking command will include libraries in LIBOMP_CONFIGURED_LIBFLAGS 170libomp_get_libflags(LIBOMP_CONFIGURED_LIBFLAGS) 171target_link_libraries(omp ${LIBOMP_CONFIGURED_LIBFLAGS} ${CMAKE_DL_LIBS}) 172 173# Create *.inc before compiling any sources 174# objects depend on : .inc files 175add_custom_target(libomp-needed-headers DEPENDS kmp_i18n_id.inc kmp_i18n_default.inc) 176add_dependencies(omp libomp-needed-headers) 177 178# Windows specific build rules 179if(WIN32) 180 configure_file(libomp.rc.var libomp.rc @ONLY) 181 182 # Create .def and .rc file before compiling any sources 183 add_custom_target(libomp-needed-windows-files DEPENDS ${LIBOMP_LIB_NAME}.def) 184 add_dependencies(omp libomp-needed-windows-files) 185 # z_Windows_NT-586_asm.asm requires definitions to be sent via command line 186 # It only needs the architecture macro and OMPT_SUPPORT=0|1 187 libomp_append(LIBOMP_MASM_DEFINITIONS "-D_M_IA32" IF_TRUE IA32) 188 libomp_append(LIBOMP_MASM_DEFINITIONS "-D_M_AMD64" IF_TRUE INTEL64) 189 libomp_append(LIBOMP_MASM_DEFINITIONS "-DOMPT_SUPPORT" IF_TRUE_1_0 LIBOMP_OMPT_SUPPORT) 190 libomp_list_to_string("${LIBOMP_MASM_DEFINITIONS}" LIBOMP_MASM_DEFINITIONS) 191 set_property(SOURCE z_Windows_NT-586_asm.asm APPEND_STRING PROPERTY COMPILE_FLAGS " ${LIBOMP_MASM_DEFINITIONS}") 192 set_source_files_properties(thirdparty/ittnotify/ittnotify_static.cpp PROPERTIES COMPILE_DEFINITIONS "UNICODE") 193 194 # Create Windows import library 195 # the import library is "re-linked" to include kmp_import.cpp which prevents 196 # linking of both Visual Studio OpenMP and newly built OpenMP 197 set_source_files_properties(kmp_import.cpp PROPERTIES COMPILE_FLAGS "${LIBOMP_CONFIGURED_CXXFLAGS}") 198 set(LIBOMP_IMP_LIB_FILE ${LIBOMP_LIB_NAME}${CMAKE_IMPORT_LIBRARY_SUFFIX}) 199 set(LIBOMP_GENERATED_IMP_LIB_FILENAME ${LIBOMP_LIB_FILE}${CMAKE_STATIC_LIBRARY_SUFFIX}) 200 set_target_properties(omp PROPERTIES 201 VERSION ${LIBOMP_VERSION_MAJOR}.${LIBOMP_VERSION_MINOR} # uses /version flag 202 IMPORT_PREFIX "" IMPORT_SUFFIX "" # control generated import library name when building omp 203 ARCHIVE_OUTPUT_NAME ${LIBOMP_GENERATED_IMP_LIB_FILENAME} 204 ) 205 206 if(MSVC) 207 # Get generated import library from creating omp 208 get_target_property(LIBOMP_IMPORT_LIB_DIRECTORY omp ARCHIVE_OUTPUT_DIRECTORY) 209 if(LIBOMP_IMPORT_LIB_DIRECTORY) 210 set(LIBOMP_GENERATED_IMP_LIB ${LIBOMP_IMPORT_LIB_DIRECTORY}/${LIBOMP_GENERATED_IMP_LIB_FILENAME}) 211 else() 212 set(LIBOMP_GENERATED_IMP_LIB ${CMAKE_CURRENT_BINARY_DIR}/${LIBOMP_GENERATED_IMP_LIB_FILENAME}) 213 endif() 214 set_source_files_properties(${LIBOMP_GENERATED_IMP_LIB} PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE) 215 # Create new import library that is just the previously created one + kmp_import.cpp 216 add_library(ompimp STATIC ${LIBOMP_GENERATED_IMP_LIB} kmp_import.cpp) 217 set_target_properties(ompimp PROPERTIES 218 PREFIX "" SUFFIX "" OUTPUT_NAME "${LIBOMP_IMP_LIB_FILE}" 219 LINKER_LANGUAGE C 220 ) 221 add_dependencies(ompimp omp) # ensure generated import library is created first 222 set(LIBOMP_IMP_LIB_TARGET ompimp) 223 else() 224 set(LIBOMP_IMP_LIB_TARGET omp) 225 endif() 226 227 # Create def file to designate exported functions 228 libomp_get_gdflags(LIBOMP_GDFLAGS) # generate-def.pl flags (Windows only) 229 libomp_string_to_list("${LIBOMP_GDFLAGS}" LIBOMP_GDFLAGS) 230 add_custom_command( 231 OUTPUT ${LIBOMP_LIB_NAME}.def 232 COMMAND ${PERL_EXECUTABLE} ${LIBOMP_TOOLS_DIR}/generate-def.pl ${LIBOMP_GDFLAGS} 233 -o ${LIBOMP_LIB_NAME}.def ${CMAKE_CURRENT_SOURCE_DIR}/dllexports 234 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/dllexports ${LIBOMP_TOOLS_DIR}/generate-def.pl 235 ) 236endif() 237 238# Building the Fortran module files 239# One compilation step creates both omp_lib.mod and omp_lib_kinds.mod 240if(${LIBOMP_FORTRAN_MODULES}) 241 configure_file(${LIBOMP_INC_DIR}/omp_lib.h.var omp_lib.h @ONLY) 242 configure_file(${LIBOMP_INC_DIR}/omp_lib.f90.var omp_lib.f90 @ONLY) 243 # Workaround for gfortran to build modules with the 244 # omp_sched_monotonic integer parameter 245 if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") 246 set(ADDITIONAL_Fortran_FLAGS "-fno-range-check") 247 endif() 248 add_custom_target(libomp-mod ALL DEPENDS omp_lib.mod omp_lib_kinds.mod) 249 libomp_get_fflags(LIBOMP_CONFIGURED_FFLAGS) 250 if(CMAKE_Fortran_COMPILER_SUPPORTS_F90) 251 set(LIBOMP_FORTRAN_SOURCE_FILE omp_lib.f90) 252 else() 253 message(FATAL_ERROR "Fortran module build requires Fortran 90 compiler") 254 endif() 255 add_custom_command( 256 OUTPUT omp_lib.mod omp_lib_kinds.mod 257 COMMAND ${CMAKE_Fortran_COMPILER} -c ${ADDITIONAL_Fortran_FLAGS} 258 ${LIBOMP_CONFIGURED_FFLAGS} ${LIBOMP_FORTRAN_SOURCE_FILE} 259 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${LIBOMP_FORTRAN_SOURCE_FILE} 260 ${CMAKE_CURRENT_BINARY_DIR}/omp_lib.h 261 ) 262 set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES omp_lib${CMAKE_C_OUTPUT_EXTENSION}) 263endif() 264 265# Move files to exports/ directory if requested 266if(${LIBOMP_COPY_EXPORTS}) 267 include(LibompExports) 268endif() 269 270# Micro test rules for after library has been built (cmake/LibompMicroTests.cmake) 271include(LibompMicroTests) 272add_custom_target(libomp-micro-tests) 273if(NOT ${MIC} AND NOT CMAKE_CROSSCOMPILING) 274 add_dependencies(libomp-micro-tests libomp-test-touch) 275endif() 276if(NOT WIN32 AND NOT APPLE) 277 add_dependencies(libomp-micro-tests libomp-test-relo) 278endif() 279if(NOT WIN32 AND NOT APPLE) 280 add_dependencies(libomp-micro-tests libomp-test-execstack) 281endif() 282if(${MIC}) 283 add_dependencies(libomp-micro-tests libomp-test-instr) 284endif() 285add_dependencies(libomp-micro-tests libomp-test-deps) 286 287# Install rules 288# We want to install libomp in DESTDIR/CMAKE_INSTALL_PREFIX/lib 289# We want to install headers in DESTDIR/CMAKE_INSTALL_PREFIX/include 290if(${OPENMP_STANDALONE_BUILD}) 291 set(LIBOMP_HEADERS_INSTALL_PATH include) 292else() 293 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION ${PACKAGE_VERSION}) 294 set(LIBOMP_HEADERS_INSTALL_PATH "${OPENMP_INSTALL_LIBDIR}/clang/${CLANG_VERSION}/include") 295endif() 296if(WIN32) 297 install(TARGETS omp RUNTIME DESTINATION bin) 298 install(TARGETS ${LIBOMP_IMP_LIB_TARGET} ARCHIVE DESTINATION "${OPENMP_INSTALL_LIBDIR}") 299 # Create aliases (regular copies) of the library for backwards compatibility 300 set(LIBOMP_ALIASES "libiomp5md") 301 foreach(alias IN LISTS LIBOMP_ALIASES) 302 install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E copy \"${LIBOMP_LIB_FILE}\" 303 \"${alias}${LIBOMP_LIBRARY_SUFFIX}\" WORKING_DIRECTORY \${CMAKE_INSTALL_PREFIX}/bin)") 304 install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E copy \"${LIBOMP_IMP_LIB_FILE}\" 305 \"${alias}${CMAKE_STATIC_LIBRARY_SUFFIX}\" WORKING_DIRECTORY \${CMAKE_INSTALL_PREFIX}/${OPENMP_INSTALL_LIBDIR})") 306 endforeach() 307else() 308 309 install(TARGETS omp ${LIBOMP_INSTALL_KIND} DESTINATION "${OPENMP_INSTALL_LIBDIR}") 310 311 if(${LIBOMP_INSTALL_ALIASES}) 312 # Create aliases (symlinks) of the library for backwards compatibility 313 set(LIBOMP_ALIASES "libgomp;libiomp5") 314 foreach(alias IN LISTS LIBOMP_ALIASES) 315 install(CODE "execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E create_symlink \"${LIBOMP_LIB_FILE}\" 316 \"${alias}${LIBOMP_LIBRARY_SUFFIX}\" WORKING_DIRECTORY 317 \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${OPENMP_INSTALL_LIBDIR})") 318 endforeach() 319 endif() 320endif() 321install( 322 FILES 323 ${CMAKE_CURRENT_BINARY_DIR}/omp.h 324 DESTINATION ${LIBOMP_HEADERS_INSTALL_PATH} 325) 326if(${LIBOMP_OMPT_SUPPORT}) 327 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/omp-tools.h DESTINATION ${LIBOMP_HEADERS_INSTALL_PATH}) 328 # install under legacy name ompt.h 329 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/omp-tools.h DESTINATION ${LIBOMP_HEADERS_INSTALL_PATH} RENAME ompt.h) 330endif() 331if(${LIBOMP_FORTRAN_MODULES}) 332 install(FILES 333 ${CMAKE_CURRENT_BINARY_DIR}/omp_lib.h 334 ${CMAKE_CURRENT_BINARY_DIR}/omp_lib.mod 335 ${CMAKE_CURRENT_BINARY_DIR}/omp_lib_kinds.mod 336 DESTINATION ${LIBOMP_HEADERS_INSTALL_PATH} 337 ) 338endif() 339