Make building with X11 libraries optional.

Desktop capturing on Linux will be disabled in this case, but everything
can be built without any X11 development libraries installed.

BUG=webrtc:5716,webrtc:8319

Change-Id: I01bd6a4b02816b407be19476e22ff073d264b496
Reviewed-on: https://webrtc-review.googlesource.com/32360
Reviewed-by: Henrik Andreassson (OOO until Jan 2) <henrika@webrtc.org>
Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
Commit-Queue: Joachim Bauch <jbauch@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21462}
diff --git a/modules/BUILD.gn b/modules/BUILD.gn
index 33394f4..1f93e99 100644
--- a/modules/BUILD.gn
+++ b/modules/BUILD.gn
@@ -17,7 +17,6 @@
     "audio_processing",
     "bitrate_controller",
     "congestion_controller",
-    "desktop_capture",
     "media_file",
     "pacing",
     "remote_bitrate_estimator",
@@ -26,6 +25,10 @@
     "video_coding",
     "video_processing",
   ]
+
+  if (rtc_desktop_capture_supported) {
+    deps += [ "desktop_capture" ]
+  }
 }
 
 rtc_source_set("module_api_public") {
@@ -83,13 +86,16 @@
       "../test:test_main",
       "../test:video_test_common",
       "audio_coding:audio_coding_modules_tests",
-      "desktop_capture:desktop_capture_modules_tests",
       "rtp_rtcp:rtp_rtcp_modules_tests",
       "video_coding:video_coding_modules_tests",
       "//testing/gmock",
       "//testing/gtest",
     ]
 
+    if (rtc_desktop_capture_supported) {
+      deps += [ "desktop_capture:desktop_capture_modules_tests" ]
+    }
+
     data = modules_tests_resources
 
     if (is_android) {
@@ -253,7 +259,6 @@
       "audio_processing:audio_processing_unittests",
       "bitrate_controller:bitrate_controller_unittests",
       "congestion_controller:congestion_controller_unittests",
-      "desktop_capture:desktop_capture_unittests",
       "media_file:media_file_unittests",
       "pacing:pacing_unittests",
       "remote_bitrate_estimator:remote_bitrate_estimator_unittests",
@@ -264,6 +269,10 @@
       "video_processing:video_processing_unittests",
     ]
 
+    if (rtc_desktop_capture_supported) {
+      deps += [ "desktop_capture:desktop_capture_unittests" ]
+    }
+
     data = modules_unittests_resources
 
     if (is_android) {
diff --git a/modules/audio_device/BUILD.gn b/modules/audio_device/BUILD.gn
index ef48180..46cac11 100644
--- a/modules/audio_device/BUILD.gn
+++ b/modules/audio_device/BUILD.gn
@@ -195,8 +195,9 @@
         ]
         defines += [ "LINUX_ALSA" ]
         libs = [ "dl" ]
-        if (use_x11) {
+        if (rtc_use_x11) {
           libs += [ "X11" ]
+          defines += [ "WEBRTC_USE_X11" ]
         }
         if (rtc_include_pulse_audio) {
           sources += [
diff --git a/modules/audio_device/linux/audio_device_alsa_linux.cc b/modules/audio_device/linux/audio_device_alsa_linux.cc
index 68d3311..afa54b7 100644
--- a/modules/audio_device/linux/audio_device_alsa_linux.cc
+++ b/modules/audio_device/linux/audio_device_alsa_linux.cc
@@ -149,7 +149,7 @@
   if (_initialized) {
     return InitStatus::OK;
   }
-#if defined(USE_X11)
+#if defined(WEBRTC_USE_X11)
   // Get X display handle for typing detection
   _XDisplay = XOpenDisplay(NULL);
   if (!_XDisplay) {
@@ -193,7 +193,7 @@
 
     _critSect.Enter();
   }
-#if defined(USE_X11)
+#if defined(WEBRTC_USE_X11)
   if (_XDisplay) {
     XCloseDisplay(_XDisplay);
     _XDisplay = NULL;
@@ -1624,7 +1624,7 @@
 }
 
 bool AudioDeviceLinuxALSA::KeyPressed() const {
-#if defined(USE_X11)
+#if defined(WEBRTC_USE_X11)
   char szKey[32];
   unsigned int i = 0;
   char state = 0;
diff --git a/modules/audio_device/linux/audio_device_alsa_linux.h b/modules/audio_device/linux/audio_device_alsa_linux.h
index 602da01..60e5743 100644
--- a/modules/audio_device/linux/audio_device_alsa_linux.h
+++ b/modules/audio_device/linux/audio_device_alsa_linux.h
@@ -18,7 +18,7 @@
 #include "rtc_base/criticalsection.h"
 #include "rtc_base/platform_thread.h"
 
-#if defined(USE_X11)
+#if defined(WEBRTC_USE_X11)
 #include <X11/Xlib.h>
 #endif
 #include <alsa/asoundlib.h>
@@ -192,7 +192,7 @@
     snd_pcm_sframes_t _playoutDelay;
 
     char _oldKeyState[32];
-#if defined(USE_X11)
+#if defined(WEBRTC_USE_X11)
     Display* _XDisplay;
 #endif
 };
diff --git a/modules/desktop_capture/BUILD.gn b/modules/desktop_capture/BUILD.gn
index 8e8d92d..fa414b7 100644
--- a/modules/desktop_capture/BUILD.gn
+++ b/modules/desktop_capture/BUILD.gn
@@ -296,7 +296,7 @@
     "window_finder_win.h",
   ]
 
-  if (use_x11) {
+  if (rtc_use_x11) {
     sources += [
       "mouse_cursor_monitor_x11.cc",
       "screen_capturer_x11.cc",
@@ -317,7 +317,7 @@
     configs += [ "//build/config/linux:x11" ]
   }
 
-  if (!is_win && !is_mac && !use_x11) {
+  if (!is_win && !is_mac && !rtc_use_x11) {
     sources += [
       "mouse_cursor_monitor_null.cc",
       "screen_capturer_null.cc",
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index 7349f17..cc475f7 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -717,7 +717,7 @@
     sources += [ "macifaddrs_converter.cc" ]
   }
 
-  if (use_x11) {
+  if (rtc_use_x11) {
     libs += [
       "dl",
       "rt",
diff --git a/test/BUILD.gn b/test/BUILD.gn
index ab31e99..7e929e8 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -685,17 +685,17 @@
     "../rtc_base:rtc_base_approved",
     "//testing/gtest",
   ]
-  if (!(is_linux && use_x11) && !is_mac && !is_win) {
+  if (!(is_linux && rtc_use_x11) && !is_mac && !is_win) {
     sources += [ "null_platform_renderer.cc" ]
   }
-  if ((is_linux && use_x11) || is_mac) {
+  if ((is_linux && rtc_use_x11) || is_mac) {
     sources += [
       "gl/gl_renderer.cc",
       "gl/gl_renderer.h",
     ]
   }
 
-  if (is_linux && use_x11) {
+  if (is_linux && rtc_use_x11) {
     sources += [
       "linux/glx_renderer.cc",
       "linux/glx_renderer.h",
diff --git a/webrtc.gni b/webrtc.gni
index 986598b..b8ceb8e 100644
--- a/webrtc.gni
+++ b/webrtc.gni
@@ -83,6 +83,9 @@
   # Set this to false to skip building tools.
   rtc_build_tools = true
 
+  # Set this to false to skip building code that requires X11.
+  rtc_use_x11 = use_x11
+
   # Disable these to not build components which can be externally provided.
   rtc_build_json = true
   rtc_build_libsrtp = true
@@ -201,7 +204,7 @@
 rtc_opus_dir = "//third_party/opus"
 
 # Desktop capturer is supported only on Windows, OSX and Linux.
-rtc_desktop_capture_supported = is_win || is_mac || (is_linux && use_x11)
+rtc_desktop_capture_supported = is_win || is_mac || (is_linux && rtc_use_x11)
 
 ###############################################################################
 # Templates