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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_LIBRARIES "-Wl,-Bstatic -lpixman-1 -Wl,-Bdynamic")
  19. # gettext is included in libc on many unix systems
  20. if(NOT LIBC_HAS_DGETTEXT)
  21. FIND_LIBRARY(UNISTRING_LIBRARY NAMES unistring libunistring
  22. HINTS ${PC_GETTEXT_LIBDIR} ${PC_GETTEXT_LIBRARY_DIRS})
  23. FIND_LIBRARY(INTL_LIBRARY NAMES intl libintl
  24. HINTS ${PC_GETTEXT_LIBDIR} ${PC_GETTEXT_LIBRARY_DIRS})
  25. FIND_LIBRARY(ICONV_LIBRARY NAMES iconv libiconv
  26. HINTS ${PC_GETTEXT_LIBDIR} ${PC_GETTEXT_LIBRARY_DIRS})
  27. set(GETTEXT_LIBRARIES "-Wl,-Bstatic")
  28. if(INTL_LIBRARY)
  29. set(GETTEXT_LIBRARIES "${GETTEXT_LIBRARIES} -lintl")
  30. endif()
  31. if(ICONV_LIBRARY)
  32. set(GETTEXT_LIBRARIES "${GETTEXT_LIBRARIES} -liconv")
  33. endif()
  34. set(GETTEXT_LIBRARIES "${GETTEXT_LIBRARIES} -Wl,-Bdynamic")
  35. # FIXME: MSYS2 doesn't include a static version of this library, so
  36. # we'll have to link it dynamically for now
  37. if(UNISTRING_LIBRARY)
  38. set(GETTEXT_LIBRARIES "${GETTEXT_LIBRARIES} -lunistring")
  39. endif()
  40. if(APPLE)
  41. set(GETTEXT_LIBRARIES "${GETTEXT_LIBRARIES} -framework Carbon")
  42. endif()
  43. endif()
  44. if(GNUTLS_FOUND)
  45. # GnuTLS has historically had different crypto backends
  46. FIND_LIBRARY(GCRYPT_LIBRARY NAMES gcrypt libgcrypt
  47. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  48. FIND_LIBRARY(NETTLE_LIBRARY NAMES nettle libnettle
  49. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  50. FIND_LIBRARY(TASN1_LIBRARY NAMES tasn1 libtasn1
  51. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  52. FIND_LIBRARY(IDN2_LIBRARY NAMES idn2 libidn2
  53. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  54. set(GNUTLS_LIBRARIES "-Wl,-Bstatic -lgnutls")
  55. if(TASN1_LIBRARY)
  56. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -ltasn1")
  57. endif()
  58. if(NETTLE_LIBRARY)
  59. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lhogweed -lnettle -lgmp")
  60. endif()
  61. if(GCRYPT_LIBRARY)
  62. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lgcrypt -lgpg-error")
  63. endif()
  64. if(IDN2_LIBRARY)
  65. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lidn2")
  66. endif()
  67. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -Wl,-Bdynamic")
  68. if (WIN32)
  69. FIND_LIBRARY(P11KIT_LIBRARY NAMES p11-kit libp11-kit
  70. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  71. FIND_LIBRARY(UNISTRING_LIBRARY NAMES unistring libunistring
  72. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  73. # GnuTLS uses various crypto-api stuff
  74. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lcrypt32 -lncrypt")
  75. # And sockets
  76. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lws2_32")
  77. # p11-kit only available as dynamic library for MSYS2 on Windows and dynamic linking of unistring is required
  78. if(P11KIT_LIBRARY)
  79. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lp11-kit")
  80. endif()
  81. if(UNISTRING_LIBRARY)
  82. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lunistring")
  83. endif()
  84. endif()
  85. if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  86. # nanosleep() lives here on Solaris
  87. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lrt")
  88. # and socket functions are hidden here
  89. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lsocket")
  90. endif()
  91. # GnuTLS uses gettext and zlib, so make sure those are always
  92. # included and in the proper order
  93. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${ZLIB_LIBRARIES}")
  94. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${GETTEXT_LIBRARIES}")
  95. # The last variables might introduce whitespace, which CMake
  96. # throws a hissy fit about
  97. string(STRIP ${GNUTLS_LIBRARIES} GNUTLS_LIBRARIES)
  98. endif()
  99. if(NETTLE_FOUND)
  100. set(NETTLE_LIBRARIES "-Wl,-Bstatic -lnettle -Wl,-Bdynamic")
  101. set(HOGWEED_LIBRARIES "-Wl,-Bstatic -lhogweed -Wl,-Bdynamic")
  102. set(GMP_LIBRARIES "-Wl,-Bstatic -lgmp -Wl,-Bdynamic")
  103. endif()
  104. if(DEFINED FLTK_LIBRARIES)
  105. set(FLTK_LIBRARIES "-Wl,-Bstatic -lfltk_images -lpng -ljpeg -lfltk -Wl,-Bdynamic")
  106. if(WIN32)
  107. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lcomctl32")
  108. elseif(APPLE)
  109. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -framework Cocoa")
  110. else()
  111. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lm -ldl")
  112. endif()
  113. if(X11_FOUND AND NOT APPLE)
  114. if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  115. 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")
  116. else()
  117. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -Wl,-Bstatic -lXcursor -lXfixes -lXft -lfontconfig -lexpat -lfreetype -lpng -lbz2 -luuid -lXrender -lXext -lXinerama -Wl,-Bdynamic")
  118. endif()
  119. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lX11")
  120. endif()
  121. endif()
  122. # X11 libraries change constantly on Linux systems so we have to link
  123. # them statically, even libXext. libX11 is somewhat stable, although
  124. # even it has had an ABI change once or twice.
  125. if(X11_FOUND AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  126. set(X11_LIBRARIES "-Wl,-Bstatic -lXext -Wl,-Bdynamic -lX11")
  127. if(X11_XTest_LIB)
  128. set(X11_XTest_LIB "-Wl,-Bstatic -lXtst -Wl,-Bdynamic")
  129. endif()
  130. if(X11_Xdamage_LIB)
  131. set(X11_Xdamage_LIB "-Wl,-Bstatic -lXdamage -Wl,-Bdynamic")
  132. endif()
  133. if(X11_Xrandr_LIB)
  134. set(X11_Xrandr_LIB "-Wl,-Bstatic -lXrandr -lXrender -Wl,-Bdynamic")
  135. endif()
  136. if(X11_Xi_LIB)
  137. set(X11_Xi_LIB "-Wl,-Bstatic -lXi -Wl,-Bdynamic")
  138. endif()
  139. endif()
  140. endif()
  141. if(BUILD_STATIC_GCC)
  142. # This ensures that we don't depend on libstdc++ or libgcc_s
  143. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nodefaultlibs")
  144. set(STATIC_BASE_LIBRARIES "")
  145. if(ENABLE_ASAN AND NOT WIN32 AND NOT APPLE)
  146. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -Wl,-Bstatic -lasan -Wl,-Bdynamic -ldl -lpthread")
  147. endif()
  148. if(ENABLE_TSAN AND NOT WIN32 AND NOT APPLE AND CMAKE_SIZEOF_VOID_P MATCHES 8)
  149. # libtsan redefines some C++ symbols which then conflict with a
  150. # statically linked libstdc++. Work around this by allowing multiple
  151. # definitions. The linker will pick the first one (i.e. the one
  152. # from libtsan).
  153. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -Wl,-z -Wl,muldefs -Wl,-Bstatic -ltsan -Wl,-Bdynamic -ldl -lm")
  154. endif()
  155. if(WIN32)
  156. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt")
  157. find_package(Threads)
  158. if(CMAKE_USE_PTHREADS_INIT)
  159. # pthread has to be statically linked after libraries above and before kernel32
  160. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -Wl,-Bstatic -lpthread -Wl,-Bdynamic")
  161. endif()
  162. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -luser32 -lkernel32 -ladvapi32 -lshell32")
  163. # mingw has some fun circular dependencies that requires us to link
  164. # these things again
  165. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt")
  166. else()
  167. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lm -lgcc -lgcc_eh -lc")
  168. endif()
  169. set(CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE} ${STATIC_BASE_LIBRARIES}")
  170. set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic ${STATIC_BASE_LIBRARIES}")
  171. endif()