blob: a99430983caae35ece99fedab71e87d650fa82c3 [file] [log] [blame]
Pierre Ossmanf95272d2014-10-17 13:59:35 +02001#
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
10option(BUILD_STATIC
Pierre Ossmanced99a92014-10-28 17:01:08 +010011 "Link statically against most libraries, if possible" OFF)
Pierre Ossmanf95272d2014-10-17 13:59:35 +020012
DRC2a2e2c32015-10-17 18:13:05 -050013option(BUILD_STATIC_GCC
14 "Link statically against only libgcc and libstdc++" OFF)
15
Pierre Ossmanf95272d2014-10-17 13:59:35 +020016if(BUILD_STATIC)
17 message(STATUS "Attempting to link static binaries...")
18
DRC2a2e2c32015-10-17 18:13:05 -050019 set(BUILD_STATIC_GCC 1)
20
Pierre Ossmanf95272d2014-10-17 13:59:35 +020021 set(JPEG_LIBRARIES "-Wl,-Bstatic -ljpeg -Wl,-Bdynamic")
Peter Åstrand (astrand)97eeefd2018-09-19 15:18:25 +020022 set(ZLIB_LIBRARIES "-Wl,-Bstatic -lz -Wl,-Bdynamic")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020023
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. Hinz44759762014-11-04 00:03:24 -050035 FIND_LIBRARY(TASN1_LIBRARY NAMES tasn1 libtasn1
36 HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
Pierre Ossmanf95272d2014-10-17 13:59:35 +020037
Brian P. Hinz44759762014-11-04 00:03:24 -050038 set(GNUTLS_LIBRARIES "-Wl,-Bstatic -lgnutls")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020039
Brian P. Hinz44759762014-11-04 00:03:24 -050040 if(TASN1_LIBRARY)
41 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -ltasn1")
42 endif()
Pierre Ossmanf95272d2014-10-17 13:59:35 +020043 if(NETTLE_LIBRARY)
Pierre Ossman95491392014-10-28 16:58:14 +010044 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lnettle -lhogweed -lgmp")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020045 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 Ossman95491392014-10-28 16:58:14 +010052 if (WIN32)
Pierre Ossman95763a62015-02-13 11:12:42 +010053 # GnuTLS uses various crypto-api stuff
Pierre Ossman95491392014-10-28 16:58:14 +010054 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lcrypt32")
Pierre Ossman95763a62015-02-13 11:12:42 +010055 # And sockets
56 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lws2_32")
Pierre Ossman95491392014-10-28 16:58:14 +010057 endif()
58
Pierre Ossmanf95272d2014-10-17 13:59:35 +020059 if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
Pierre Ossman95763a62015-02-13 11:12:42 +010060 # nanosleep() lives here on Solaris
Pierre Ossman4790e112014-10-28 16:57:05 +010061 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lrt")
Pierre Ossman95763a62015-02-13 11:12:42 +010062 # and socket functions are hidden here
63 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lsocket")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020064 endif()
Pierre Ossman5b9303a2014-10-28 16:59:02 +010065
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 Ossmanf95272d2014-10-17 13:59:35 +020074 endif()
75
76 if(FLTK_FOUND)
77 set(FLTK_LIBRARIES "-Wl,-Bstatic -lfltk_images -lpng -ljpeg -lfltk -Wl,-Bdynamic")
78
79 if(WIN32)
Pierre Ossman4790e112014-10-28 16:57:05 +010080 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lcomctl32")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020081 elseif(APPLE)
Pierre Ossman4790e112014-10-28 16:57:05 +010082 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -framework Cocoa")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020083 else()
Peter Åstrand (astrand)c4bc5a82015-02-10 12:21:05 +010084 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lm -ldl")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020085 endif()
86
87 if(X11_FOUND AND NOT APPLE)
88 if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
Peter Åstrand (astrand)c4bc5a82015-02-10 12:21:05 +010089 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 Ossmanf95272d2014-10-17 13:59:35 +020090 else()
Pierre Ossman82e753d2017-08-24 11:28:33 +020091 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -Wl,-Bstatic -lXcursor -lXfixes -lXft -lfontconfig -lexpat -lfreetype -lpng -lbz2 -lXrender -lXext -lXinerama -Wl,-Bdynamic")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020092 endif()
93
Pierre Ossman4790e112014-10-28 16:57:05 +010094 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lX11")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020095 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 Ossman4790e112014-10-28 16:57:05 +0100102 set(X11_LIBRARIES "-Wl,-Bstatic -lXext -Wl,-Bdynamic -lX11")
Pierre Ossmanf95272d2014-10-17 13:59:35 +0200103 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)242c5b22018-03-07 13:00:47 +0100109 if(X11_Xrandr_LIB)
110 set(X11_Xrandr_LIB "-Wl,-Bstatic -lXrandr -lXrender -Wl,-Bdynamic")
111 endif()
Pierre Ossmanf95272d2014-10-17 13:59:35 +0200112 endif()
DRC2a2e2c32015-10-17 18:13:05 -0500113endif()
Pierre Ossmanf95272d2014-10-17 13:59:35 +0200114
DRC2a2e2c32015-10-17 18:13:05 -0500115if(BUILD_STATIC_GCC)
Pierre Ossmanf95272d2014-10-17 13:59:35 +0200116 # 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 Ossman86640e82015-09-29 09:40:20 +0200119 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 Ossmanb74728f2015-11-13 14:06:35 +0100122 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 Ossmanf95272d2014-10-17 13:59:35 +0200129 if(WIN32)
Pierre Ossmandf3cc422014-11-03 14:36:23 +0100130 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 Ossmanf95272d2014-10-17 13:59:35 +0200135 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 Ossmanf95272d2014-10-17 13:59:35 +0200139endif()