Linking.cmake 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- cmake -*-
  2. if (LINKING_CMAKE_INCLUDED)
  3. return()
  4. endif (LINKING_CMAKE_INCLUDED)
  5. set (LINKING_CMAKE_INCLUDED TRUE)
  6. include(00-BuildOptions)
  7. include(mimalloc)
  8. set(ARCH_PREBUILT_DIRS ${LIBS_PREBUILT_DIR}/lib/release)
  9. set(ARCH_PREBUILT_DIRS_RELEASE ${LIBS_PREBUILT_DIR}/lib/release)
  10. set(ARCH_PREBUILT_DIRS_DEBUG ${LIBS_PREBUILT_DIR}/lib/debug)
  11. if (WINDOWS)
  12. # Under Windows, the Debug and Release/RelWithDebInfo libraries use
  13. # different link flags, so we can only use the Debug libaries for the Debug
  14. # release type.
  15. if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG" OR "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
  16. # Note: automatically fall back to non-debug lib when a debug lib is absent
  17. link_directories(${ARCH_PREBUILT_DIRS_DEBUG} ${ARCH_PREBUILT_DIRS})
  18. else ()
  19. link_directories(${ARCH_PREBUILT_DIRS})
  20. endif ()
  21. else (WINDOWS)
  22. # Under Linux or macOS we use the release pre-built libraries for the
  23. # Release build only, i.e. the debug libraries will be used for both Debug
  24. # and RelWithDebInfo builds.
  25. if ("${CMAKE_BUILD_TYPE}" STREQUAL "RELEASE" OR "${CMAKE_BUILD_TYPE}" STREQUAL "Release")
  26. link_directories(${ARCH_PREBUILT_DIRS})
  27. else ()
  28. # Note: automatically fall back to non-debug lib when a debug lib is absent
  29. link_directories(${ARCH_PREBUILT_DIRS_DEBUG} ${ARCH_PREBUILT_DIRS})
  30. endif ()
  31. endif (WINDOWS)
  32. if (LINUX)
  33. # Note: for gcc, cmake specifies -flto=auto, which already takes care to
  34. # set an appropriate amount of jobs (and -plugin-opt,jobs=N is not a valid
  35. # gcc option either).
  36. if (USELTO AND LINK_JOBS AND ${CLANG_VERSION} GREATER 0)
  37. set(CMAKE_EXE_LINKER_FLAGS "-Wl,-plugin-opt,jobs=${LINK_JOBS} ${CMAKE_EXE_LINKER_FLAGS}")
  38. endif ()
  39. if (NOT USESYSTEMLIBS)
  40. # Needed for ld to find the link-time dependency (libffi.so.x) for
  41. # libglib and libgobject
  42. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath-link,${ARCH_PREBUILT_DIRS_RELEASE}")
  43. endif (NOT USESYSTEMLIBS)
  44. if (PROTECTSTACK)
  45. # If the user requested stack protection, then also protect the ELF GOT
  46. # by making it read-only (with shared functions address resolution
  47. # performed at startup time). HB
  48. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,relro -Wl,-z,now -Wl,--as-needed")
  49. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,relro -Wl,-z,now -Wl,--as-needed")
  50. # Forcing the noexecstack, which could actually break the code when the
  51. # trampoline is not optimized out during compilation... HB
  52. #set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,noexecstack")
  53. else (PROTECTSTACK)
  54. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed")
  55. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed")
  56. endif (PROTECTSTACK)
  57. set(DL_LIBRARY dl)
  58. else (LINUX)
  59. set(DL_LIBRARY "")
  60. endif (LINUX)
  61. if (WINDOWS)
  62. # Needed by VS2017 to avoid missing printf/scanf/_iob symbols
  63. set(LEGACY_STDIO_LIBS legacy_stdio_definitions)
  64. if (USE_NETBIOS)
  65. set(NETWORK_LIBS netapi32 iphlpapi)
  66. else (USE_NETBIOS)
  67. set(NETWORK_LIBS iphlpapi)
  68. endif (USE_NETBIOS)
  69. set(WINDOWS_LIBRARIES
  70. # Make sure MIMALLOC_LIBRARY appears first in the list
  71. ${MIMALLOC_LIBRARY}
  72. advapi32
  73. shell32
  74. ws2_32
  75. mswsock
  76. psapi
  77. winmm
  78. ${NETWORK_LIBS}
  79. wldap32
  80. gdi32
  81. user32
  82. dbghelp
  83. wer
  84. rpcrt4
  85. ${LEGACY_STDIO_LIBS}
  86. )
  87. # No libcmt and do not bother us with warnings about missing *.pdb files...
  88. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:LIBCMT /IGNORE:4099")
  89. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NODEFAULTLIB:LIBCMT /IGNORE:4099")
  90. # When using mimalloc, ensure the malloc overrides DLL will be loaded at runtime.
  91. # See: https://microsoft.github.io/mimalloc/overrides.html
  92. if (USE_MIMALLOC)
  93. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /INCLUDE:mi_version")
  94. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /INCLUDE:mi_version")
  95. endif (USE_MIMALLOC)
  96. else (WINDOWS)
  97. set(WINDOWS_LIBRARIES "")
  98. endif (WINDOWS)
  99. mark_as_advanced(DL_LIBRARY WINDOWS_LIBRARIES)