You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StaticBuild.cmake 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #
  2. # Best-effort magic that tries to produce semi-static binaries
  3. # (i.e. only depends on "safe" libraries like libc and libX11)
  4. #
  5. # Note that this often fails as there is no way to automatically
  6. # determine the dependencies of the libraries we depend on, and
  7. # a lot of details change with each different build environment.
  8. #
  9. option(BUILD_STATIC
  10. "Link statically against most libraries, if possible" OFF)
  11. option(BUILD_STATIC_GCC
  12. "Link statically against only libgcc and libstdc++" OFF)
  13. if(BUILD_STATIC)
  14. message(STATUS "Attempting to link static binaries...")
  15. set(BUILD_STATIC_GCC 1)
  16. set(JPEG_LIBRARIES "-Wl,-Bstatic -ljpeg -Wl,-Bdynamic")
  17. set(ZLIB_LIBRARIES "-Wl,-Bstatic -lz -Wl,-Bdynamic")
  18. # gettext is included in libc on many unix systems
  19. if(NOT LIBC_HAS_DGETTEXT)
  20. set(GETTEXT_LIBRARIES "-Wl,-Bstatic -lintl -liconv -Wl,-Bdynamic")
  21. if(APPLE)
  22. set(GETTEXT_LIBRARIES "${GETTEXT_LIBRARIES} -framework Carbon")
  23. endif()
  24. endif()
  25. if(GNUTLS_FOUND)
  26. # GnuTLS has historically had different crypto backends
  27. FIND_LIBRARY(GCRYPT_LIBRARY NAMES gcrypt libgcrypt
  28. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  29. FIND_LIBRARY(NETTLE_LIBRARY NAMES nettle libnettle
  30. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  31. FIND_LIBRARY(TASN1_LIBRARY NAMES tasn1 libtasn1
  32. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  33. set(GNUTLS_LIBRARIES "-Wl,-Bstatic -lgnutls")
  34. if(TASN1_LIBRARY)
  35. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -ltasn1")
  36. endif()
  37. if(NETTLE_LIBRARY)
  38. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lhogweed -lnettle -lgmp")
  39. endif()
  40. if(GCRYPT_LIBRARY)
  41. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lgcrypt -lgpg-error")
  42. endif()
  43. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -Wl,-Bdynamic")
  44. if (WIN32)
  45. # GnuTLS uses various crypto-api stuff
  46. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lcrypt32")
  47. # And sockets
  48. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lws2_32")
  49. endif()
  50. if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  51. # nanosleep() lives here on Solaris
  52. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lrt")
  53. # and socket functions are hidden here
  54. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lsocket")
  55. endif()
  56. # GnuTLS uses gettext and zlib, so make sure those are always
  57. # included and in the proper order
  58. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${ZLIB_LIBRARIES}")
  59. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${GETTEXT_LIBRARIES}")
  60. # The last variables might introduce whitespace, which CMake
  61. # throws a hissy fit about
  62. string(STRIP ${GNUTLS_LIBRARIES} GNUTLS_LIBRARIES)
  63. endif()
  64. if(FLTK_FOUND)
  65. set(FLTK_LIBRARIES "-Wl,-Bstatic -lfltk_images -lpng -ljpeg -lfltk -Wl,-Bdynamic")
  66. if(WIN32)
  67. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lcomctl32")
  68. elseif(APPLE)
  69. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -framework Cocoa")
  70. else()
  71. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lm -ldl")
  72. endif()
  73. if(X11_FOUND AND NOT APPLE)
  74. if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  75. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} ${X11_Xcursor_LIB} ${X11_Xfixes_LIB} -Wl,-Bstatic -lXft -Wl,-Bdynamic -lfontconfig -lXrender -lXext -R/usr/sfw/lib -L=/usr/sfw/lib -lfreetype -lsocket -lnsl")
  76. else()
  77. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -Wl,-Bstatic -lXcursor -lXfixes -lXft -lfontconfig -lexpat -lfreetype -lpng -lbz2 -luuid -lXrender -lXext -lXinerama -Wl,-Bdynamic")
  78. endif()
  79. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lX11")
  80. endif()
  81. endif()
  82. # X11 libraries change constantly on Linux systems so we have to link
  83. # them statically, even libXext. libX11 is somewhat stable, although
  84. # even it has had an ABI change once or twice.
  85. if(X11_FOUND AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  86. set(X11_LIBRARIES "-Wl,-Bstatic -lXext -Wl,-Bdynamic -lX11")
  87. if(X11_XTest_LIB)
  88. set(X11_XTest_LIB "-Wl,-Bstatic -lXtst -Wl,-Bdynamic")
  89. endif()
  90. if(X11_Xdamage_LIB)
  91. set(X11_Xdamage_LIB "-Wl,-Bstatic -lXdamage -Wl,-Bdynamic")
  92. endif()
  93. if(X11_Xrandr_LIB)
  94. set(X11_Xrandr_LIB "-Wl,-Bstatic -lXrandr -lXrender -Wl,-Bdynamic")
  95. endif()
  96. if(X11_Xi_LIB)
  97. set(X11_Xi_LIB "-Wl,-Bstatic -lXi -Wl,-Bdynamic")
  98. endif()
  99. endif()
  100. endif()
  101. if(BUILD_STATIC_GCC)
  102. # This ensures that we don't depend on libstdc++ or libgcc_s
  103. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nodefaultlibs")
  104. set(STATIC_BASE_LIBRARIES "")
  105. if(ENABLE_ASAN AND NOT WIN32 AND NOT APPLE)
  106. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -Wl,-Bstatic -lasan -Wl,-Bdynamic -ldl -lm -lpthread")
  107. endif()
  108. if(ENABLE_TSAN AND NOT WIN32 AND NOT APPLE AND CMAKE_SIZEOF_VOID_P MATCHES 8)
  109. # libtsan redefines some C++ symbols which then conflict with a
  110. # statically linked libstdc++. Work around this by allowing multiple
  111. # definitions. The linker will pick the first one (i.e. the one
  112. # from libtsan).
  113. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -Wl,-z -Wl,muldefs -Wl,-Bstatic -ltsan -Wl,-Bdynamic -ldl -lm")
  114. endif()
  115. if(WIN32)
  116. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt")
  117. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -luser32 -lkernel32 -ladvapi32 -lshell32")
  118. # mingw has some fun circular dependencies that requires us to link
  119. # these things again
  120. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt")
  121. else()
  122. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lgcc -lgcc_eh -lc")
  123. endif()
  124. set(CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE} ${STATIC_BASE_LIBRARIES}")
  125. set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic ${STATIC_BASE_LIBRARIES}")
  126. endif()