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.8KB

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