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

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