Merge branch 'build' of https://github.com/CendioOssman/tigervnc
diff --git a/BUILDING.txt b/BUILDING.txt
index 897a51a..8f686cc 100644
--- a/BUILDING.txt
+++ b/BUILDING.txt
@@ -14,16 +14,15 @@
    * See "Building FLTK" below.
 
 -- If building TLS support:
-   * GnuTLS and its dependencies (libgcrypt, libtasn1, libgpg-error)
+   * GnuTLS
 
 -- If building native language support (NLS):
    * Gnu gettext 0.14.4 or later
    * See "Building Native Language Support" below.
 
--- libjpeg-turbo SDK
-   * If your system does not include libjpeg-turbo, then you can download the
-     SDK from http://sourceforge.net/projects/libjpeg-turbo/files/
-   * See "Building High-Performance JPEG Support" below.
+-- libjpeg-turbo
+   * "Normal" libjpegv6 is also supported, although it is not
+     recommended as it is much slower.
 
 
 =========================
@@ -253,62 +252,20 @@
 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.
 
-
-======================================
-Building High-Performance JPEG Support
-======================================
-
-In order to achieve its high levels of performance, TigerVNC relies on
-libjpeg-turbo (http://www.libjpeg-turbo.org), a derivative of libjpeg which
-uses SIMD instructions to accelerate baseline JPEG compression and
-decompression.  If you are building TigerVNC on an operating system that
-includes libjpeg-turbo as a system library (for instance, Fedora 14 and later),
-then the TigerVNC build system should detect the system version of
-libjpeg-turbo automatically and link against it.  However, this produces a
-version of TigerVNC that depends on the libjpeg-turbo dynamic libraries, and
-thus the TigerVNC binaries are not portable.
-
-To build a fully portable, cross-compatible version of TigerVNC with
-high-performance JPEG support, it is necessary to link against the
-libjpeg-turbo static library.  This is also necessary when building the
-Windows or OS X versions of TigerVNC.  To link against the libjpeg-turbo static
-library, first install the libjpeg-turbo SDK, which is available from
-https://sourceforge.net/projects/libjpeg-turbo/files/.  Next, use the
-JPEG_INCLUDE_DIR and JPEG_LIBRARY CMake variables to specify the location of
-libjpeg-turbo.  For example, adding
-
-  -DJPEG_INCLUDE_DIR=/opt/libjpeg-turbo/include \
-    -DJPEG_LIBRARY=/opt/libjpeg-turbo/lib/libjpeg.a
-
-to the CMake command line will link TigerVNC against a static version of
-libjpeg-turbo installed under /opt/TigerVNC (which is the normal install
-location for the libjpeg-turbo SDK on Unix and Linux platforms.)  Replace "lib"
-with "lib32" or "lib64" to use the 32-bit or 64-bit version of the library on
-64-bit Linux platforms.
-
-When building on Windows systems, adding
-
-  -DJPEG_INCLUDE_DIR=/c/libjpeg-turbo-gcc[64]/include \
-    -DJPEG_LIBRARY=/c/libjpeg-turbo-gcc[64]/lib/libjpeg.a
-
-to the CMake command line will link TigerVNC against the static version of
-libjpeg-turbo provided by the libjpeg-turbo SDK for GCC (MinGW.)
-
-CMake will report:
-
-  Performing Test FOUND_LIBJPEG_TURBO - Success
-
-if it successfully finds libjpeg-turbo.
+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.
 
 
 =====================
@@ -390,87 +347,6 @@
 libiconv and libintl included in the MinGW Developer Toolkit.
 
 
-===============================================
-Building Transport Layer Security (TLS) support
-===============================================
-
-VeNCrypt (the TigerVNC security and authentication extensions) can be built
-with TLS support, which provides built-in encryption for VNC sessions.  This
-requires GnuTLS, which is readily available in many Linux distributions but not
-as readily available in binary form on other types of systems.  This section
-describes the issues associated with building a version of TigerVNC with TLS
-support and how to work around those issues.
-
-
-Unix/Mac
---------
-
-In general, if you are building on a Unix-ish platform that has the GnuTLS
-libraries and include files installed in the standard system locations, then
-the TigerVNC build system should detect the system version of GnuTLS
-automatically and link against it.  However, this produces a version of
-TigerVNC that depends on the GnuTLS dynamic libraries, and thus the TigerVNC
-binaries are not portable.
-
-To build a fully portable, cross-compatible version of TigerVNC with TLS
-support, it is necessary to link against the GnuTLS static library (as well
-as the static libraries of its dependencies.)  If you are lucky enough, then
-your O/S distribution may include pre-packaged versions of these static
-libraries.  Otherwise, it will be necessary to build GnuTLS, libgcrypt,
-libtasn1, and libgpg-error from source.
-
-You can manipulate the GNUTLS_INCLUDE_DIR and GNUTLS_LIBRARY CMake variables to
-build TigerVNC against a custom build of GnuTLS that is installed in a
-non-system directory.  For instance, adding
-
-  -DGNUTLS_INCLUDE_DIR=/opt/gnutls/include \
-    -DGNUTLS_LIBRARY='/opt/gnutls/lib/libgnutls.a;/opt/gnutls/lib/libgcrypt.a;/opt/gnutls/lib/libgpg-error.a;/opt/gnutls/lib/libtasn1.a' \
-    -DUSE_INCLUDED_ZLIB=1
-
-to the CMake or 'build-xorg build' command line will cause TigerVNC to be
-statically linked against a custom installation of GnuTLS that resides under
-/opt/gnutls.  GnuTLS depends on zlib, so specifying -DUSE_INCLUDED_ZLIB=1 will
-satisfy that dependency using TigerVNC's in-tree version of zlib, which
-prevents TigerVNC from depending on the libz dynamic library.
-
-
-MinGW
------
-
-An installer containing the GnuTLS header files, as well as static and dynamic
-link libraries for 32-bit MinGW, can be downloaded from the following site:
-
-http://josefsson.org/gnutls4win/
-
-As of this writing, GnuTLS cannot be built cleanly with MinGW64 due to the fact
-that portions of the code assume an LP64 data model (Windows uses LLP64.)
-Thus, it is not possible at this time to produce a Win64 version of TigerVNC
-with TLS support.
-
-Whether you use the above installer or build GnuTLS from source, make sure that
-you install the libraries and headers into a pathname that doesn't contain
-spaces (the installer will try to install under c:\Program Files unless you
-tell it otherwise.)  If the GnuTLS include path contains spaces, then the MinGW
-resource compiler will barf when you try to build TigerVNC.
-
-You can manipulate the GNUTLS_INCLUDE_DIR and GNUTLS_LIBRARY CMake variables to
-specify the directory under which you installed GnuTLS.  For instance, adding
-
-  -DGNUTLS_INCLUDE_DIR=/c/gnutls/include \
-    -DGNUTLS_LIBRARY=/c/gnutls/lib/libgnutls.dll.a
-
-to the CMake command line when using MinGW will cause TigerVNC to be linked
-against GnuTLS DLLs that are installed under c:\gnutls.
-
-Adding
-
-  -DGNUTLS_INCLUDE_DIR=/c/gnutls/include \
-    -DGNUTLS_LIBRARY='/c/gnutls/lib/libgnutls.a;/c/gnutls/lib/libgcrypt.a;/c/gnutls/lib/libtasn1.a;/c/gnutls/lib/libgpg-error.a'
-
-to the CMake command line will cause TigerVNC to be statically linked against
-GnuTLS libraries that are installed under c:\gnutls.
-
-
 ===================
 Installing TigerVNC
 ===================
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 24e923e..1dcfd9e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -89,49 +89,6 @@
   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)
@@ -310,20 +267,8 @@
 option(ENABLE_GNUTLS "Enable protocol encryption and advanced authentication" ON)
 if(ENABLE_GNUTLS)
   find_package(GnuTLS)
-  find_package(Gcrypt)
-  find_package(Gpg_Error)
   if (GNUTLS_FOUND)
     include_directories(${GNUTLS_INCLUDE_DIR})
-    if (GCRYPT_FOUND)
-      include_directories(${GCRYPT_INCLUDE_DIR})
-      set(GNUTLS_LIBRARIES ${GNUTLS_LIBRARIES};${GCRYPT_LIBRARIES})
-      set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${GCRYPT_LIBRARIES})
-      if (GPG_ERROR_FOUND)
-        include_directories(${GPG_ERROR_INCLUDE_DIR})
-        set(GNUTLS_LIBRARIES ${GNUTLS_LIBRARIES};${GPG_ERROR_LIBRARIES})
-        set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${GPG_ERROR_LIBRARIES})
-      endif()
-    endif()
     add_definitions("-DHAVE_GNUTLS")
     add_definitions(${GNUTLS_DEFINITIONS})
     
@@ -332,7 +277,6 @@
     set(CMAKE_EXTRA_INCLUDE_FILES gnutls/gnutls.h)
     set(CMAKE_REQUIRED_LIBRARIES ${GNUTLS_LIBRARIES})
     if(WIN32)
-      set(CMAKE_EXTRA_INCLUDE_FILES gcrypt.h ${CMAKE_EXTRA_INCLUDE_FILES})
       set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ws2_32 user32)
     endif()
     if(ZLIB_FOUND)
@@ -386,6 +330,8 @@
 add_definitions(-DHAVE_CONFIG_H)
 include_directories(${CMAKE_BINARY_DIR})
 
+include(cmake/StaticBuild.cmake)
+
 add_subdirectory(common)
 
 if(WIN32)
diff --git a/cmake/Modules/FindGcrypt.cmake b/cmake/Modules/FindGcrypt.cmake
deleted file mode 100644
index 2158081..0000000
--- a/cmake/Modules/FindGcrypt.cmake
+++ /dev/null
@@ -1,36 +0,0 @@
-# - Find gcrypt
-# Find the native GCRYPT includes and library
-#
-#  GCRYPT_FOUND - True if gcrypt found.
-#  GCRYPT_INCLUDE_DIR - where to find gcrypt.h, etc.
-#  GCRYPT_LIBRARIES - List of libraries when using gcrypt.
-
-if (GCRYPT_INCLUDE_DIR AND GCRYPT_LIBRARIES)
-  set(GCRYPT_FIND_QUIETLY TRUE)
-endif (GCRYPT_INCLUDE_DIR AND GCRYPT_LIBRARIES)
-
-# Include dir
-find_path(GCRYPT_INCLUDE_DIR
-	NAMES
-	  gcrypt.h
-)
-
-# Library
-find_library(GCRYPT_LIBRARY 
-  NAMES gcrypt
-)
-
-# handle the QUIETLY and REQUIRED arguments and set GCRYPT_FOUND to TRUE if 
-# all listed variables are TRUE
-INCLUDE(FindPackageHandleStandardArgs)
-FIND_PACKAGE_HANDLE_STANDARD_ARGS(GCRYPT DEFAULT_MSG GCRYPT_LIBRARY GCRYPT_INCLUDE_DIR)
-
-IF(GCRYPT_FOUND)
-  SET( GCRYPT_LIBRARIES ${GCRYPT_LIBRARY} )
-ELSE(GCRYPT_FOUND)
-  SET( GCRYPT_LIBRARIES )
-ENDIF(GCRYPT_FOUND)
-
-# Lastly make it so that the GCRYPT_LIBRARY and GCRYPT_INCLUDE_DIR variables
-# only show up under the advanced options in the gui cmake applications.
-MARK_AS_ADVANCED( GCRYPT_LIBRARY GCRYPT_INCLUDE_DIR )
diff --git a/cmake/Modules/FindGpg_Error.cmake b/cmake/Modules/FindGpg_Error.cmake
deleted file mode 100644
index 2f1d372..0000000
--- a/cmake/Modules/FindGpg_Error.cmake
+++ /dev/null
@@ -1,36 +0,0 @@
-# - Find gpg-error
-# Find the native GPG_ERROR includes and library
-#
-#  GPG_ERROR_FOUND - True if gpg-error found.
-#  GPG_ERROR_INCLUDE_DIR - where to find gpg-error.h, etc.
-#  GPG_ERROR_LIBRARIES - List of libraries when using gpg-error.
-
-if (GPG_ERROR_INCLUDE_DIR AND GPG_ERROR_LIBRARIES)
-  set(GPG_ERROR_FIND_QUIETLY TRUE)
-endif (GPG_ERROR_INCLUDE_DIR AND GPG_ERROR_LIBRARIES)
-
-# Include dir
-find_path(GPG_ERROR_INCLUDE_DIR
-	NAMES
-	  gpg-error.h
-)
-
-# Library
-find_library(GPG_ERROR_LIBRARY 
-  NAMES gpg-error
-)
-
-# handle the QUIETLY and REQUIRED arguments and set GPG_ERROR_FOUND to TRUE if 
-# all listed variables are TRUE
-INCLUDE(FindPackageHandleStandardArgs)
-FIND_PACKAGE_HANDLE_STANDARD_ARGS(GPG_ERROR DEFAULT_MSG GPG_ERROR_LIBRARY GPG_ERROR_INCLUDE_DIR)
-
-IF(GPG_ERROR_FOUND)
-  SET( GPG_ERROR_LIBRARIES ${GPG_ERROR_LIBRARY} )
-ELSE(GPG_ERROR_FOUND)
-  SET( GPG_ERROR_LIBRARIES )
-ENDIF(GPG_ERROR_FOUND)
-
-# Lastly make it so that the GPG_ERROR_LIBRARY and GPG_ERROR_INCLUDE_DIR variables
-# only show up under the advanced options in the gui cmake applications.
-MARK_AS_ADVANCED( GPG_ERROR_LIBRARY GPG_ERROR_INCLUDE_DIR )
diff --git a/cmake/StaticBuild.cmake b/cmake/StaticBuild.cmake
new file mode 100644
index 0000000..4ef2905
--- /dev/null
+++ b/cmake/StaticBuild.cmake
@@ -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()
diff --git a/common/network/CMakeLists.txt b/common/network/CMakeLists.txt
index ddde574..b624c8e 100644
--- a/common/network/CMakeLists.txt
+++ b/common/network/CMakeLists.txt
@@ -3,6 +3,10 @@
 add_library(network STATIC
   TcpSocket.cxx)
 
+if(WIN32)
+	target_link_libraries(network ws2_32)
+endif()
+
 if(UNIX)
   libtool_create_control_file(network)
 endif()
diff --git a/common/rdr/CMakeLists.txt b/common/rdr/CMakeLists.txt
index ee71a9b..cc45f91 100644
--- a/common/rdr/CMakeLists.txt
+++ b/common/rdr/CMakeLists.txt
@@ -17,9 +17,9 @@
 set(RDR_LIBRARIES ${ZLIB_LIBRARIES} os)
 if(GNUTLS_FOUND)
   set(RDR_LIBRARIES ${RDR_LIBRARIES} ${GNUTLS_LIBRARIES})
-  if(WIN32)
+endif()
+if(WIN32)
 	set(RDR_LIBRARIES ${RDR_LIBRARIES} ws2_32)
-  endif()
 endif()
 
 target_link_libraries(rdr ${RDR_LIBRARIES})
diff --git a/unix/x0vncserver/CMakeLists.txt b/unix/x0vncserver/CMakeLists.txt
index 64c9e77..82f0d2a 100644
--- a/unix/x0vncserver/CMakeLists.txt
+++ b/unix/x0vncserver/CMakeLists.txt
@@ -15,7 +15,7 @@
   ../vncconfig/QueryConnectDialog.cxx
 )
 
-target_link_libraries(x0vncserver tx rfb network rdr ${X11_LIBRARIES})
+target_link_libraries(x0vncserver tx rfb network rdr)
 
 if(X11_FOUND AND X11_XTest_LIB)
   add_definitions(-DHAVE_XTEST)
@@ -31,5 +31,7 @@
   message(WARNING "No DAMAGE extension.  x0vncserver will have to use the slower polling method.")
 endif()
 
+target_link_libraries(x0vncserver ${X11_LIBRARIES})
+
 install(TARGETS x0vncserver DESTINATION ${BIN_DIR})
 install(FILES x0vncserver.man DESTINATION ${MAN_DIR}/man1 RENAME x0vncserver.1)
diff --git a/vncviewer/CMakeLists.txt b/vncviewer/CMakeLists.txt
index beca98d..de9da8f 100644
--- a/vncviewer/CMakeLists.txt
+++ b/vncviewer/CMakeLists.txt
@@ -48,11 +48,8 @@
 
 target_link_libraries(vncviewer rfb network rdr os Xregion ${FLTK_LIBRARIES} ${GETTEXT_LIBRARIES})
 
-# When building with GnuTLS, librdr depends on ws2_32, so in order to make
-# MinGW happy, we need to put ws2_32 in librdr's target_link_libraries string,
-# not here.
-if(NOT GNUTLS_FOUND AND WIN32)
-  target_link_libraries(vncviewer ws2_32)
+if(APPLE)
+  target_link_libraries(vncviewer "-framework Cocoa" "-framework Carbon")
 endif()
 
 install(TARGETS vncviewer DESTINATION ${BIN_DIR})