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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. 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(DEFINED FLTK_LIBRARIES)
  100. set(FLTK_LIBRARIES "-Wl,-Bstatic -lfltk_images -lpng -ljpeg -lfltk -Wl,-Bdynamic")
  101. if(WIN32)
  102. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lcomctl32")
  103. elseif(APPLE)
  104. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -framework Cocoa")
  105. else()
  106. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lm -ldl")
  107. endif()
  108. if(X11_FOUND AND NOT APPLE)
  109. if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  110. 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")
  111. else()
  112. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -Wl,-Bstatic -lXcursor -lXfixes -lXft -lfontconfig -lexpat -lfreetype -lpng -lbz2 -luuid -lXrender -lXext -lXinerama -Wl,-Bdynamic")
  113. endif()
  114. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lX11")
  115. endif()
  116. endif()
  117. # X11 libraries change constantly on Linux systems so we have to link
  118. # them statically, even libXext. libX11 is somewhat stable, although
  119. # even it has had an ABI change once or twice.
  120. if(X11_FOUND AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  121. set(X11_LIBRARIES "-Wl,-Bstatic -lXext -Wl,-Bdynamic -lX11")
  122. if(X11_XTest_LIB)
  123. set(X11_XTest_LIB "-Wl,-Bstatic -lXtst -Wl,-Bdynamic")
  124. endif()
  125. if(X11_Xdamage_LIB)
  126. set(X11_Xdamage_LIB "-Wl,-Bstatic -lXdamage -Wl,-Bdynamic")
  127. endif()
  128. if(X11_Xrandr_LIB)
  129. set(X11_Xrandr_LIB "-Wl,-Bstatic -lXrandr -lXrender -Wl,-Bdynamic")
  130. endif()
  131. if(X11_Xi_LIB)
  132. set(X11_Xi_LIB "-Wl,-Bstatic -lXi -Wl,-Bdynamic")
  133. endif()
  134. endif()
  135. endif()
  136. if(BUILD_STATIC_GCC)
  137. # This ensures that we don't depend on libstdc++ or libgcc_s
  138. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nodefaultlibs")
  139. set(STATIC_BASE_LIBRARIES "")
  140. if(ENABLE_ASAN AND NOT WIN32 AND NOT APPLE)
  141. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -Wl,-Bstatic -lasan -Wl,-Bdynamic -ldl -lpthread")
  142. endif()
  143. if(ENABLE_TSAN AND NOT WIN32 AND NOT APPLE AND CMAKE_SIZEOF_VOID_P MATCHES 8)
  144. # libtsan redefines some C++ symbols which then conflict with a
  145. # statically linked libstdc++. Work around this by allowing multiple
  146. # definitions. The linker will pick the first one (i.e. the one
  147. # from libtsan).
  148. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -Wl,-z -Wl,muldefs -Wl,-Bstatic -ltsan -Wl,-Bdynamic -ldl -lm")
  149. endif()
  150. if(WIN32)
  151. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt")
  152. find_package(Threads)
  153. if(CMAKE_USE_PTHREADS_INIT)
  154. # pthread has to be statically linked after libraries above and before kernel32
  155. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -Wl,-Bstatic -lpthread -Wl,-Bdynamic")
  156. endif()
  157. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -luser32 -lkernel32 -ladvapi32 -lshell32")
  158. # mingw has some fun circular dependencies that requires us to link
  159. # these things again
  160. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt")
  161. else()
  162. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lm -lgcc -lgcc_eh -lc")
  163. endif()
  164. set(CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE} ${STATIC_BASE_LIBRARIES}")
  165. set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic ${STATIC_BASE_LIBRARIES}")
  166. endif()