blob: 8f06a148b53f8b39794d55b7bc436fb4bb5fedf6 [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
11 "Link statically against most libraries, if possible" ON)
12
13if(BUILD_STATIC)
14 message(STATUS "Attempting to link static binaries...")
15
16 set(JPEG_LIBRARIES "-Wl,-Bstatic -ljpeg -Wl,-Bdynamic")
17
18 if(WIN32 AND NOT USE_INCLUDED_ZLIB)
19 set(ZLIB_LIBRARIES "-Wl,-Bstatic -lz -Wl,-Bdynamic")
20 endif()
21
22 # gettext is included in libc on many unix systems
23 if(NOT LIBC_HAS_DGETTEXT)
24 set(GETTEXT_LIBRARIES "-Wl,-Bstatic -lintl -liconv -Wl,-Bdynamic")
25 endif()
26
27 if(GNUTLS_FOUND)
28 # GnuTLS has historically had different crypto backends
29 FIND_LIBRARY(GCRYPT_LIBRARY NAMES gcrypt libgcrypt
30 HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
31 FIND_LIBRARY(NETTLE_LIBRARY NAMES nettle libnettle
32 HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
33
34 set(GNUTLS_LIBRARIES "-Wl,-Bstatic -lgnutls -ltasn1")
35
36 if(NETTLE_LIBRARY)
Pierre Ossman95491392014-10-28 16:58:14 +010037 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lnettle -lhogweed -lgmp")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020038 endif()
39 if(GCRYPT_LIBRARY)
40 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lgcrypt -lgpg-error")
41 endif()
42
43 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -Wl,-Bdynamic")
44
Pierre Ossman95491392014-10-28 16:58:14 +010045 # GnuTLS uses various crypto-api stuff
46 if (WIN32)
47 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lcrypt32")
48 endif()
49
Pierre Ossmanf95272d2014-10-17 13:59:35 +020050 # nanosleep() lives here on Solaris
51 if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
Pierre Ossman4790e112014-10-28 16:57:05 +010052 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lrt")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020053 endif()
Pierre Ossman5b9303a2014-10-28 16:59:02 +010054
55 # GnuTLS uses gettext and zlib, so make sure those are always
56 # included and in the proper order
57 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${ZLIB_LIBRARIES}")
58 set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} ${GETTEXT_LIBRARIES}")
59
60 # The last variables might introduce whitespace, which CMake
61 # throws a hissy fit about
62 string(STRIP ${GNUTLS_LIBRARIES} GNUTLS_LIBRARIES)
Pierre Ossmanf95272d2014-10-17 13:59:35 +020063 endif()
64
65 if(FLTK_FOUND)
66 set(FLTK_LIBRARIES "-Wl,-Bstatic -lfltk_images -lpng -ljpeg -lfltk -Wl,-Bdynamic")
67
68 if(WIN32)
Pierre Ossman4790e112014-10-28 16:57:05 +010069 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lcomctl32")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020070 elseif(APPLE)
Pierre Ossman4790e112014-10-28 16:57:05 +010071 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -framework Cocoa")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020072 else()
Pierre Ossman4790e112014-10-28 16:57:05 +010073 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lm")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020074 endif()
75
76 if(X11_FOUND AND NOT APPLE)
77 if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
Pierre Ossman4790e112014-10-28 16:57:05 +010078 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} ${X11_Xcursor_LIB} ${X11_Xfixes_LIB} -Wl,-Bstatic -lXft -Wl,-Bdynamic -lfontconfig -lXext -R/usr/sfw/lib")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020079 else()
Pierre Ossman4790e112014-10-28 16:57:05 +010080 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -Wl,-Bstatic -lXcursor -lXfixes -lXft -lfontconfig -lexpat -lfreetype -lbz2 -lXrender -lXext -lXinerama -Wl,-Bdynamic")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020081 endif()
82
Pierre Ossman4790e112014-10-28 16:57:05 +010083 set(FLTK_LIBRARIES "${FLTK_LIBRARIES} -lX11")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020084 endif()
85 endif()
86
87 # X11 libraries change constantly on Linux systems so we have to link
88 # them statically, even libXext. libX11 is somewhat stable, although
89 # even it has had an ABI change once or twice.
90 if(X11_FOUND AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
Pierre Ossman4790e112014-10-28 16:57:05 +010091 set(X11_LIBRARIES "-Wl,-Bstatic -lXext -Wl,-Bdynamic -lX11")
Pierre Ossmanf95272d2014-10-17 13:59:35 +020092 if(X11_XTest_LIB)
93 set(X11_XTest_LIB "-Wl,-Bstatic -lXtst -Wl,-Bdynamic")
94 endif()
95 if(X11_Xdamage_LIB)
96 set(X11_Xdamage_LIB "-Wl,-Bstatic -lXdamage -Wl,-Bdynamic")
97 endif()
98 endif()
99
100 # This ensures that we don't depend on libstdc++ or libgcc_s
101 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nodefaultlibs")
102 set(STATIC_BASE_LIBRARIES "-Wl,-Bstatic -lstdc++ -Wl,-Bdynamic")
103 if(WIN32)
104 set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lmoldname -lmingwex -lgcc -lgcc_eh -lmsvcrt -lkernel32")
105 else()
106 set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lgcc -lgcc_eh -lc")
107 endif()
108 set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} ${STATIC_BASE_LIBRARIES}")
109
110endif()