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") |
Peter Åstrand (astrand) | 97eeefd | 2018-09-19 15:18:25 +0200 | [diff] [blame] | 22 | set(ZLIB_LIBRARIES "-Wl,-Bstatic -lz -Wl,-Bdynamic") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 23 | |
| 24 | # gettext is included in libc on many unix systems |
| 25 | if(NOT LIBC_HAS_DGETTEXT) |
| 26 | set(GETTEXT_LIBRARIES "-Wl,-Bstatic -lintl -liconv -Wl,-Bdynamic") |
| 27 | endif() |
| 28 | |
| 29 | if(GNUTLS_FOUND) |
| 30 | # GnuTLS has historically had different crypto backends |
| 31 | FIND_LIBRARY(GCRYPT_LIBRARY NAMES gcrypt libgcrypt |
| 32 | HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS}) |
| 33 | FIND_LIBRARY(NETTLE_LIBRARY NAMES nettle libnettle |
| 34 | HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS}) |
Brian P. Hinz | 4475976 | 2014-11-04 00:03:24 -0500 | [diff] [blame] | 35 | FIND_LIBRARY(TASN1_LIBRARY NAMES tasn1 libtasn1 |
| 36 | HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS}) |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 37 | |
Brian P. Hinz | 4475976 | 2014-11-04 00:03:24 -0500 | [diff] [blame] | 38 | set(GNUTLS_LIBRARIES "-Wl,-Bstatic -lgnutls") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 39 | |
Brian P. Hinz | 4475976 | 2014-11-04 00:03:24 -0500 | [diff] [blame] | 40 | if(TASN1_LIBRARY) |
| 41 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -ltasn1") |
| 42 | endif() |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 43 | if(NETTLE_LIBRARY) |
Pierre Ossman | 9549139 | 2014-10-28 16:58:14 +0100 | [diff] [blame] | 44 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lnettle -lhogweed -lgmp") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 45 | endif() |
| 46 | if(GCRYPT_LIBRARY) |
| 47 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lgcrypt -lgpg-error") |
| 48 | endif() |
| 49 | |
| 50 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -Wl,-Bdynamic") |
| 51 | |
Pierre Ossman | 9549139 | 2014-10-28 16:58:14 +0100 | [diff] [blame] | 52 | if (WIN32) |
Pierre Ossman | 95763a6 | 2015-02-13 11:12:42 +0100 | [diff] [blame] | 53 | # GnuTLS uses various crypto-api stuff |
Pierre Ossman | 9549139 | 2014-10-28 16:58:14 +0100 | [diff] [blame] | 54 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lcrypt32") |
Pierre Ossman | 95763a6 | 2015-02-13 11:12:42 +0100 | [diff] [blame] | 55 | # And sockets |
| 56 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lws2_32") |
Pierre Ossman | 9549139 | 2014-10-28 16:58:14 +0100 | [diff] [blame] | 57 | endif() |
| 58 | |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 59 | if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") |
Pierre Ossman | 95763a6 | 2015-02-13 11:12:42 +0100 | [diff] [blame] | 60 | # nanosleep() lives here on Solaris |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 61 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lrt") |
Pierre Ossman | 95763a6 | 2015-02-13 11:12:42 +0100 | [diff] [blame] | 62 | # and socket functions are hidden here |
| 63 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lsocket") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 64 | endif() |
Pierre Ossman | 5b9303a | 2014-10-28 16:59:02 +0100 | [diff] [blame] | 65 | |
| 66 | # GnuTLS uses gettext and zlib, so make sure those are always |
| 67 | # included and in the proper order |
| 68 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${ZLIB_LIBRARIES}") |
| 69 | set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${GETTEXT_LIBRARIES}") |
| 70 | |
| 71 | # The last variables might introduce whitespace, which CMake |
| 72 | # throws a hissy fit about |
| 73 | string(STRIP ${GNUTLS_LIBRARIES} GNUTLS_LIBRARIES) |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 74 | endif() |
| 75 | |
| 76 | if(FLTK_FOUND) |
| 77 | set(FLTK_LIBRARIES "-Wl,-Bstatic -lfltk_images -lpng -ljpeg -lfltk -Wl,-Bdynamic") |
| 78 | |
| 79 | if(WIN32) |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 80 | set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lcomctl32") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 81 | elseif(APPLE) |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 82 | set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -framework Cocoa") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 83 | else() |
Peter Åstrand (astrand) | c4bc5a8 | 2015-02-10 12:21:05 +0100 | [diff] [blame] | 84 | set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lm -ldl") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 85 | endif() |
| 86 | |
| 87 | if(X11_FOUND AND NOT APPLE) |
| 88 | if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") |
Peter Åstrand (astrand) | c4bc5a8 | 2015-02-10 12:21:05 +0100 | [diff] [blame] | 89 | 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] | 90 | else() |
Pierre Ossman | 18ba39c | 2019-01-09 10:30:26 +0100 | [diff] [blame^] | 91 | set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -Wl,-Bstatic -lXcursor -lXfixes -lXft -lfontconfig -lexpat -lfreetype -lpng -lbz2 -luuid -lXrender -lXext -lXinerama -Wl,-Bdynamic") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 92 | endif() |
| 93 | |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 94 | set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lX11") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 95 | endif() |
| 96 | endif() |
| 97 | |
| 98 | # X11 libraries change constantly on Linux systems so we have to link |
| 99 | # them statically, even libXext. libX11 is somewhat stable, although |
| 100 | # even it has had an ABI change once or twice. |
| 101 | if(X11_FOUND AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS") |
Pierre Ossman | 4790e11 | 2014-10-28 16:57:05 +0100 | [diff] [blame] | 102 | set(X11_LIBRARIES "-Wl,-Bstatic -lXext -Wl,-Bdynamic -lX11") |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 103 | if(X11_XTest_LIB) |
| 104 | set(X11_XTest_LIB "-Wl,-Bstatic -lXtst -Wl,-Bdynamic") |
| 105 | endif() |
| 106 | if(X11_Xdamage_LIB) |
| 107 | set(X11_Xdamage_LIB "-Wl,-Bstatic -lXdamage -Wl,-Bdynamic") |
| 108 | endif() |
Peter Åstrand (astrand) | 242c5b2 | 2018-03-07 13:00:47 +0100 | [diff] [blame] | 109 | if(X11_Xrandr_LIB) |
| 110 | set(X11_Xrandr_LIB "-Wl,-Bstatic -lXrandr -lXrender -Wl,-Bdynamic") |
| 111 | endif() |
Pierre Ossman | f95272d | 2014-10-17 13:59:35 +0200 | [diff] [blame] | 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() |