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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if(BUILD_STATIC)
  12. message(STATUS "Attempting to link static binaries...")
  13. set(JPEG_LIBRARIES "-Wl,-Bstatic -ljpeg -Wl,-Bdynamic")
  14. if(WIN32 AND NOT USE_INCLUDED_ZLIB)
  15. set(ZLIB_LIBRARIES "-Wl,-Bstatic -lz -Wl,-Bdynamic")
  16. endif()
  17. # gettext is included in libc on many unix systems
  18. if(NOT LIBC_HAS_DGETTEXT)
  19. set(GETTEXT_LIBRARIES "-Wl,-Bstatic -lintl -liconv -Wl,-Bdynamic")
  20. endif()
  21. if(GNUTLS_FOUND)
  22. # GnuTLS has historically had different crypto backends
  23. FIND_LIBRARY(GCRYPT_LIBRARY NAMES gcrypt libgcrypt
  24. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  25. FIND_LIBRARY(NETTLE_LIBRARY NAMES nettle libnettle
  26. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  27. FIND_LIBRARY(TASN1_LIBRARY NAMES tasn1 libtasn1
  28. HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
  29. set(GNUTLS_LIBRARIES "-Wl,-Bstatic -lgnutls")
  30. if(TASN1_LIBRARY)
  31. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -ltasn1")
  32. endif()
  33. if(NETTLE_LIBRARY)
  34. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lnettle -lhogweed -lgmp")
  35. endif()
  36. if(GCRYPT_LIBRARY)
  37. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lgcrypt -lgpg-error")
  38. endif()
  39. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -Wl,-Bdynamic")
  40. if (WIN32)
  41. # GnuTLS uses various crypto-api stuff
  42. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lcrypt32")
  43. # And sockets
  44. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lws2_32")
  45. endif()
  46. if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  47. # nanosleep() lives here on Solaris
  48. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lrt")
  49. # and socket functions are hidden here
  50. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lsocket")
  51. endif()
  52. # GnuTLS uses gettext and zlib, so make sure those are always
  53. # included and in the proper order
  54. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${ZLIB_LIBRARIES}")
  55. set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${GETTEXT_LIBRARIES}")
  56. # The last variables might introduce whitespace, which CMake
  57. # throws a hissy fit about
  58. string(STRIP ${GNUTLS_LIBRARIES} GNUTLS_LIBRARIES)
  59. endif()
  60. if(FLTK_FOUND)
  61. set(FLTK_LIBRARIES "-Wl,-Bstatic -lfltk_images -lpng -ljpeg -lfltk -Wl,-Bdynamic")
  62. if(WIN32)
  63. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lcomctl32")
  64. elseif(APPLE)
  65. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -framework Cocoa")
  66. else()
  67. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lm -ldl")
  68. endif()
  69. if(X11_FOUND AND NOT APPLE)
  70. if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  71. 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")
  72. else()
  73. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -Wl,-Bstatic -lXcursor -lXfixes -lXft -lfontconfig -lexpat -lfreetype -lbz2 -lXrender -lXext -lXinerama -Wl,-Bdynamic")
  74. endif()
  75. set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lX11")
  76. endif()
  77. endif()
  78. # X11 libraries change constantly on Linux systems so we have to link
  79. # them statically, even libXext. libX11 is somewhat stable, although
  80. # even it has had an ABI change once or twice.
  81. if(X11_FOUND AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
  82. set(X11_LIBRARIES "-Wl,-Bstatic -lXext -Wl,-Bdynamic -lX11")
  83. if(X11_XTest_LIB)
  84. set(X11_XTest_LIB "-Wl,-Bstatic -lXtst -Wl,-Bdynamic")
  85. endif()
  86. if(X11_Xdamage_LIB)
  87. set(X11_Xdamage_LIB "-Wl,-Bstatic -lXdamage -Wl,-Bdynamic")
  88. endif()
  89. endif()
  90. # This ensures that we don't depend on libstdc++ or libgcc_s
  91. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nodefaultlibs")
  92. set(STATIC_BASE_LIBRARIES "-Wl,-Bstatic -lstdc++ -Wl,-Bdynamic")
  93. if(WIN32)
  94. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt")
  95. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -luser32 -lkernel32 -ladvapi32 -lshell32")
  96. # mingw has some fun circular dependencies that requires us to link
  97. # these things again
  98. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt")
  99. else()
  100. set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lgcc -lgcc_eh -lc")
  101. endif()
  102. set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} ${STATIC_BASE_LIBRARIES}")
  103. endif()