]> source.dussan.org Git - tigervnc.git/commitdiff
Extend "static" build option 49/head
authorPierre Ossman <ossman@cendio.se>
Fri, 17 Oct 2014 11:59:35 +0000 (13:59 +0200)
committerPierre Ossman <ossman@cendio.se>
Fri, 17 Oct 2014 11:59:35 +0000 (13:59 +0200)
Try to link as much as possible statically in an effort to produce
binaries that can be run everywhere.

BUILDING.txt
CMakeLists.txt
cmake/StaticBuild.cmake [new file with mode: 0644]

index 71d26b65e595ff3f0e92d2a861a5ee48bce72b8d..8f686cc390f7e70610376ef4efb6826285d7fc4f 100644 (file)
@@ -252,16 +252,21 @@ Debug Build
 Add "-DCMAKE_BUILD_TYPE=Debug" to the CMake command line.
 
 
-Self-Contained GCC Build
-------------------------
+Portable (semi-static) Build
+----------------------------
 
-If TigerVNC is built using GCC (including MinGW), then it may depend on the
-libgcc or libstdc++ dynamic libraries.  To eliminate this dependency, add
+TigerVNC can under favourble circumstances be built in a way that allows
+the resulting binaries to run on any system without having to also install
+all the dynamic libraries it depends on. Enable this mode by adding:
 
   -DBUILD_STATIC=1
 
 to the CMake command line.
 
+Note that the method used to achieve this is very fragile and it may be
+necessary to tweak cmake/StaticBuild.cmake to make things work on your
+specific system.
+
 
 =====================
 Building Java Support
index 5e600a2671cb24202ec4f86caa9924daa5ba2f80..1dcfd9edf3c01de7686fd0aceba0323d5b179355 100644 (file)
@@ -89,49 +89,6 @@ else()
   message(STATUS "32-bit build")
 endif()
 
-# This ensures that we don't depend on libstdc++ or libgcc
-if(CMAKE_COMPILER_IS_GNUCXX AND NOT APPLE AND NOT CYGWIN)
-  option(BUILD_STATIC
-    "Link statically against libgcc and libstdc++, if possible" OFF)
-  if(BUILD_STATIC)
-    # For some reason, simply passing ${CMAKE_CXX_FLAGS} to the compiler in
-    # execute_process() doesn't work.  Grrr...
-    if(CMAKE_SIZEOF_VOID_P MATCHES 8)
-      execute_process(COMMAND ${CMAKE_CXX_COMPILER} -m64
-        --print-file-name=libstdc++.a OUTPUT_VARIABLE LIBSTDCPLUSPLUS
-        RESULT_VARIABLE RESULT)
-    else()
-      execute_process(COMMAND ${CMAKE_CXX_COMPILER} -m32
-        --print-file-name=libstdc++.a OUTPUT_VARIABLE LIBSTDCPLUSPLUS
-        RESULT_VARIABLE RESULT)
-    endif()
-    string(REGEX REPLACE "\n" "" LIBSTDCPLUSPLUS ${LIBSTDCPLUSPLUS})
-    if(RESULT MATCHES 0 AND LIBSTDCPLUSPLUS)
-      message(STATUS "Linking with static libstdc++:\n   ${LIBSTDCPLUSPLUS}")
-      file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/staticlib)
-      execute_process(COMMAND ${CMAKE_COMMAND} -E remove
-        ${CMAKE_BINARY_DIR}/staticlib/libstdc++.a)
-      if(MINGW)
-        execute_process(COMMAND ${CMAKE_COMMAND} -E copy
-          ${LIBSTDCPLUSPLUS} ${CMAKE_BINARY_DIR}/staticlib/libstdc++.a)
-      else()
-        execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
-          ${LIBSTDCPLUSPLUS} ${CMAKE_BINARY_DIR}/staticlib/libstdc++.a)
-      endif()
-      set(CMAKE_EXE_LINKER_FLAGS
-        "${CMAKE_EXE_LINKER_FLAGS} -L${CMAKE_BINARY_DIR}/staticlib")
-      set(CMAKE_SHARED_LINKER_FLAGS
-        "${CMAKE_SHARED_LINKER_FLAGS} -L${CMAKE_BINARY_DIR}/staticlib")
-    else()
-      message(WARNING Cannot find static libstdc++.  TigerVNC will depend on dynamic libstdc++.)
-    endif()
-    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc")
-    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc")
-    set(CMAKE_SHARED_LINKER_FLAGS
-      "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc")
-  endif()
-endif()
-
 # CMake doesn't properly support resource compilation with MinGW.  Boo!
 if(MINGW)
   if(NOT DEFINED RC)
@@ -373,6 +330,8 @@ configure_file(config.h.in config.h)
 add_definitions(-DHAVE_CONFIG_H)
 include_directories(${CMAKE_BINARY_DIR})
 
+include(cmake/StaticBuild.cmake)
+
 add_subdirectory(common)
 
 if(WIN32)
diff --git a/cmake/StaticBuild.cmake b/cmake/StaticBuild.cmake
new file mode 100644 (file)
index 0000000..4ef2905
--- /dev/null
@@ -0,0 +1,96 @@
+#
+# Best-effort magic that tries to produce semi-static binaries
+# (i.e. only depends on "safe" libraries like libc and libX11)
+#
+# Note that this often fails as there is no way to automatically
+# determine the dependencies of the libraries we depend on, and
+# a lot of details change with each different build environment.
+#
+
+option(BUILD_STATIC
+    "Link statically against most libraries, if possible" ON)
+
+if(BUILD_STATIC)
+  message(STATUS "Attempting to link static binaries...")
+
+  set(JPEG_LIBRARIES "-Wl,-Bstatic -ljpeg -Wl,-Bdynamic")
+
+  if(WIN32 AND NOT USE_INCLUDED_ZLIB)
+    set(ZLIB_LIBRARIES "-Wl,-Bstatic -lz -Wl,-Bdynamic")
+  endif()
+
+  # gettext is included in libc on many unix systems
+  if(NOT LIBC_HAS_DGETTEXT)
+    set(GETTEXT_LIBRARIES "-Wl,-Bstatic -lintl -liconv -Wl,-Bdynamic")
+  endif()
+
+  if(GNUTLS_FOUND)
+    # GnuTLS has historically had different crypto backends
+    FIND_LIBRARY(GCRYPT_LIBRARY NAMES gcrypt libgcrypt
+      HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
+    FIND_LIBRARY(NETTLE_LIBRARY NAMES nettle libnettle
+      HINTS ${PC_GNUTLS_LIBDIR} ${PC_GNUTLS_LIBRARY_DIRS})
+
+    set(GNUTLS_LIBRARIES "-Wl,-Bstatic -lgnutls -ltasn1")
+
+    if(NETTLE_LIBRARY)
+      set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lnettle -lhogweed -lgmp")
+    endif()
+    if(GCRYPT_LIBRARY)
+      set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -lgcrypt -lgpg-error")
+    endif()
+
+    set(GNUTLS_LIBRARIES "${GNUTLS_LIBRARIES} -Wl,-Bdynamic")
+
+    # nanosleep() lives here on Solaris
+    if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
+      set(GNUTLS_LIBRARIES ${GNUTLS_LIBRARIES} -lrt)
+    endif()
+  endif()
+
+  if(FLTK_FOUND)
+    set(FLTK_LIBRARIES "-Wl,-Bstatic -lfltk_images -lpng -ljpeg -lfltk -Wl,-Bdynamic")
+
+    if(WIN32)
+      set(FLTK_LIBRARIES ${FLTK_LIBRARIES} comctl32)
+    elseif(APPLE)
+      set(FLTK_LIBRARIES ${FLTK_LIBRARIES} "-framework Cocoa")
+    else()
+      set(FLTK_LIBRARIES ${FLTK_LIBRARIES} m)
+    endif()
+
+    if(X11_FOUND AND NOT APPLE)
+      if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
+        set(FLTK_LIBRARIES ${FLTK_LIBRARIES} ${X11_Xcursor_LIB} ${X11_Xfixes_LIB} "-Wl,-Bstatic -lXft -Wl,-Bdynamic" fontconfig Xext -R/usr/sfw/lib)
+      else()
+        set(FLTK_LIBRARIES ${FLTK_LIBRARIES} "-Wl,-Bstatic -lXcursor -lXfixes -lXft -lfontconfig -lexpat -lfreetype -lbz2 -lXrender -lXext -lXinerama -Wl,-Bdynamic")
+      endif()
+
+      set(FLTK_LIBRARIES ${FLTK_LIBRARIES} X11)
+    endif()
+  endif()
+
+  # X11 libraries change constantly on Linux systems so we have to link
+  # them statically, even libXext. libX11 is somewhat stable, although
+  # even it has had an ABI change once or twice.
+  if(X11_FOUND AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
+    set(X11_LIBRARIES "-Wl,-Bstatic -lXext -Wl,-Bdynamic" X11)
+    if(X11_XTest_LIB)
+      set(X11_XTest_LIB "-Wl,-Bstatic -lXtst -Wl,-Bdynamic")
+    endif()
+    if(X11_Xdamage_LIB)
+      set(X11_Xdamage_LIB "-Wl,-Bstatic -lXdamage -Wl,-Bdynamic")
+    endif()
+  endif()
+
+  # This ensures that we don't depend on libstdc++ or libgcc_s
+  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nodefaultlibs")
+  set(STATIC_BASE_LIBRARIES "-Wl,-Bstatic -lstdc++ -Wl,-Bdynamic")
+  if(WIN32)
+    set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lmingw32 -lmoldname -lmingwex -lgcc -lgcc_eh -lmsvcrt -lkernel32")
+  else()
+    set(STATIC_BASE_LIBRARIES "${STATIC_BASE_LIBRARIES} -lgcc -lgcc_eh -lc")
+  endif()
+  set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} ${STATIC_BASE_LIBRARIES}")
+
+endif()