Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 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 | |
| 10 | option(BUILD_STATIC |
Pierre Ossman | ced99a9 | 2014-10-28 17:01:08 +0100 | [diff] [blame] | 11 | "Link statically against most libraries, if possible" OFF) |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 12 | |
DRC | 2a2e2c3 | 2015-10-17 18:13:05 -0500 | [diff] [blame] | 13 | option(BUILD_STATIC_GCC |
| 14 | "Link statically against only libgcc and libstdc++" OFF) |
| 15 | |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 16 | if(BUILD_STATIC) |
| 17 | message(STATUS "Attempting to link static binaries...") |
| 18 | |
DRC | 2a2e2c3 | 2015-10-17 18:13:05 -0500 | [diff] [blame] | 19 | set(BUILD_STATIC_GCC 1) |
| 20 | |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 21 | set(JPEG_LIBRARIES "-Wl,-Bstatic -ljpeg -Wl,-Bdynamic") |
| 22 | |
Pierre Ossman | cc8c6a2 | 2015-02-03 09:54:49 +0100 | [diff] [blame] | 23 | if(WIN32) |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 24 | set(ZLIB_LIBRARIES "-Wl,-Bstatic -lz -Wl,-Bdynamic") |
| 25 | endif() |
| 26 | |
| 27 | # gettext is included in libc on many unix systems |
| 28 | if(NOT LIBC_HAS_DGETTEXT) |
| 29 | set(GETTEXT_LIBRARIES "-Wl,-Bstatic -lintl -liconv -Wl,-Bdynamic") |
| 30 | endif() |
| 31 | |
| 32 | if(GNUTLS_FOUND) |
| 33 | # GnuTLS has historically had different crypto backends |
| 34 | FIND_LIBRARY(GCRYPT_LIBRARY NAMES gcrypt libgcrypt |
| 35 | HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS}) |
| 36 | FIND_LIBRARY(NETTLE_LIBRARY NAMES nettle libnettle |
| 37 | HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS}) |
Brian P. Hinz | 4475976 | 2014-11-04 00:03:24 -0500 | [diff] [blame] | 38 | FIND_LIBRARY(TASN1_LIBRARY NAMES tasn1 libtasn1 |
| 39 | HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS}) |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 40 | |
Brian P. Hinz | 4475976 | 2014-11-04 00:03:24 -0500 | [diff] [blame] | 41 | set(GNUTLS_LIBRARIES "-Wl,-Bstatic -lgnutls") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 42 | |
Brian P. Hinz | 4475976 | 2014-11-04 00:03:24 -0500 | [diff] [blame] | 43 | if(TASN1_LIBRARY) |
| 44 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -ltasn1") |
| 45 | endif() |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 46 | if(NETTLE_LIBRARY) |
Pierre Ossman | 9549139 | 2014-10-28 16:58:14 +0100 | [diff] [blame] | 47 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lnettle -lhogweed -lgmp") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 48 | endif() |
| 49 | if(GCRYPT_LIBRARY) |
| 50 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lgcrypt -lgpg-error") |
| 51 | endif() |
| 52 | |
| 53 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -Wl,-Bdynamic") |
| 54 | |
Pierre Ossman | 9549139 | 2014-10-28 16:58:14 +0100 | [diff] [blame] | 55 | if (WIN32) |
Pierre Ossman | 95763a6 | 2015-02-13 11:12:42 +0100 | [diff] [blame] | 56 | # GnuTLS uses various crypto-api stuff |
Pierre Ossman | 9549139 | 2014-10-28 16:58:14 +0100 | [diff] [blame] | 57 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lcrypt32") |
Pierre Ossman | 95763a6 | 2015-02-13 11:12:42 +0100 | [diff] [blame] | 58 | # And sockets |
| 59 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lws2_32") |
Pierre Ossman | 9549139 | 2014-10-28 16:58:14 +0100 | [diff] [blame] | 60 | endif() |
| 61 | |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 62 | if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") |
Pierre Ossman | 95763a6 | 2015-02-13 11:12:42 +0100 | [diff] [blame] | 63 | # nanosleep() lives here on Solaris |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 64 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lrt") |
Pierre Ossman | 95763a6 | 2015-02-13 11:12:42 +0100 | [diff] [blame] | 65 | # and socket functions are hidden here |
| 66 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lsocket") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 67 | endif() |
Pierre Ossman | 5b9303a | 2014-10-28 16:59:02 +0100 | [diff] [blame] | 68 | |
| 69 | # GnuTLS uses gettext and zlib, so make sure those are always |
| 70 | # included and in the proper order |
| 71 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${ZLIB_LIBRARIES}") |
| 72 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${GETTEXT_LIBRARIES}") |
| 73 | |
| 74 | # The last variables might introduce whitespace, which CMake |
| 75 | # throws a hissy fit about |
| 76 | string(STRIP ${GNUTLS_LIBRARIES} GNUTLS_LIBRARIES) |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 77 | endif() |
| 78 | |
| 79 | if(FLTK_FOUND) |
| 80 | set(FLTK_LIBRARIES "-Wl,-Bstatic -lfltk_images -lpng -ljpeg -lfltk -Wl,-Bdynamic") |
| 81 | |
| 82 | if(WIN32) |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 83 | set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lcomctl32") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 84 | elseif(APPLE) |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 85 | set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -framework Cocoa") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 86 | else() |
Peter Åstrand (astrand) | c4bc5a8 | 2015-02-10 12:21:05 +0100 | [diff] [blame] | 87 | set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lm -ldl") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 88 | endif() |
| 89 | |
| 90 | if(X11_FOUND AND NOT APPLE) |
| 91 | if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") |
Peter Åstrand (astrand) | c4bc5a8 | 2015-02-10 12:21:05 +0100 | [diff] [blame] | 92 | 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") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 93 | else() |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 94 | set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -Wl,-Bstatic -lXcursor -lXfixes -lXft -lfontconfig -lexpat -lfreetype -lbz2 -lXrender -lXext -lXinerama -Wl,-Bdynamic") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 95 | endif() |
| 96 | |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 97 | set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lX11") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 98 | endif() |
| 99 | endif() |
| 100 | |
| 101 | # X11 libraries change constantly on Linux systems so we have to link |
| 102 | # them statically, even libXext. libX11 is somewhat stable, although |
| 103 | # even it has had an ABI change once or twice. |
| 104 | if(X11_FOUND AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS") |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 105 | set(X11_LIBRARIES "-Wl,-Bstatic -lXext -Wl,-Bdynamic -lX11") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 106 | if(X11_XTest_LIB) |
| 107 | set(X11_XTest_LIB "-Wl,-Bstatic -lXtst -Wl,-Bdynamic") |
| 108 | endif() |
| 109 | if(X11_Xdamage_LIB) |
| 110 | set(X11_Xdamage_LIB "-Wl,-Bstatic -lXdamage -Wl,-Bdynamic") |
| 111 | endif() |
| 112 | endif() |
DRC | 2a2e2c3 | 2015-10-17 18:13:05 -0500 | [diff] [blame] | 113 | endif() |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 114 | |
DRC | 2a2e2c3 | 2015-10-17 18:13:05 -0500 | [diff] [blame] | 115 | if(BUILD_STATIC_GCC) |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 116 | # This ensures that we don't depend on libstdc++ or libgcc_s |
| 117 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nodefaultlibs") |
| 118 | set(STATIC_BASE_LIBRARIES "-Wl,-Bstatic -lstdc++ -Wl,-Bdynamic") |
Pierre Ossman | 86640e8 | 2015-09-29 09:40:20 +0200 | [diff] [blame] | 119 | if(ENABLE_ASAN AND NOT WIN32 AND NOT APPLE) |
| 120 | set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -Wl,-Bstatic -lasan -Wl,-Bdynamic -ldl -lm -lpthread") |
| 121 | endif() |
Pierre Ossman | b74728f | 2015-11-13 14:06:35 +0100 | [diff] [blame] | 122 | if(ENABLE_TSAN AND NOT WIN32 AND NOT APPLE AND CMAKE_SIZEOF_VOID_P MATCHES 8) |
| 123 | # libtsan redefines some C++ symbols which then conflict with a |
| 124 | # statically linked libstdc++. Work around this by allowing multiple |
| 125 | # definitions. The linker will pick the first one (i.e. the one |
| 126 | # from libtsan). |
| 127 | set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -Wl,-z -Wl,muldefs -Wl,-Bstatic -ltsan -Wl,-Bdynamic -ldl -lm") |
| 128 | endif() |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 129 | if(WIN32) |
Pierre Ossman | df3cc42 | 2014-11-03 14:36:23 +0100 | [diff] [blame] | 130 | set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt") |
| 131 | set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -luser32 -lkernel32 -ladvapi32 -lshell32") |
| 132 | # mingw has some fun circular dependencies that requires us to link |
| 133 | # these things again |
| 134 | set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 135 | else() |
| 136 | set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lgcc -lgcc_eh -lc") |
| 137 | endif() |
| 138 | set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} ${STATIC_BASE_LIBRARIES}") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 139 | endif() |