Add support for Mac OS X
diff --git a/buildenv/BUILD b/buildenv/BUILD
new file mode 100644
index 0000000..c746ed1
--- /dev/null
+++ b/buildenv/BUILD
@@ -0,0 +1,25 @@
+# Copyright 2018 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+config_setting(
+    name = "osx",
+    values = {"cpu": "darwin_x86_64"},
+    visibility = ["//visibility:public"],
+)
+
+config_setting(
+    name = "linux",
+    values = {"cpu": "k8"},
+    visibility = ["//visibility:public"],
+)
diff --git a/external/freetype.BUILD b/external/freetype.BUILD
index f7b0135..655c542 100644
--- a/external/freetype.BUILD
+++ b/external/freetype.BUILD
@@ -104,7 +104,7 @@
         "src/**/*.h",
         "include/**/*.h",
     ]),
-    copts = ["-DFT2_BUILD_LIBRARY"],
+    copts = ["-DFT2_BUILD_LIBRARY", "-UDEBUG"],
     includes = ["include"],
     textual_hdrs = glob(["src/**/*.c"]),
     visibility = ["//visibility:public"],
diff --git a/external/sdl2.BUILD b/external/sdl2.BUILD
index 886cbc1..cfb690e 100644
--- a/external/sdl2.BUILD
+++ b/external/sdl2.BUILD
@@ -14,52 +14,73 @@
 
 licenses(["notice"])  # zlib, portions BSD, MIT, PostgreSQL
 
-# We are currently using the system SDL2.  Building this on Linux is somewhat
-# complicated because it actually expects us to run --configure (i.e. patching
-# that in would require to make it work).
-cc_library(
-    name = "sdl2",
-    hdrs = glob([
+# Common include paths
+sdl_includes = [
+    "include",
+    "src/video/khronos",
+]
+
+objc_library(
+    name = "sdl2_objc",
+    srcs = glob([
+        "src/**/*.h",
         "include/*.h",
     ]),
-    includes = ["include"],
-    linkopts = ["-lSDL2"],
-    visibility = ["//visibility:public"],
+    includes = sdl_includes,
+    non_arc_srcs = glob([
+        "src/audio/coreaudio/*.m",
+        "src/file/cocoa/*.m",
+        "src/filesystem/cocoa/*.m",
+        "src/render/metal/*.m",
+        "src/video/cocoa/*.m",
+    ]),
+    sdk_frameworks = [
+        "AudioToolbox",
+        "Carbon",
+        "CoreAudio",
+        "CoreVideo",
+        "Cocoa",
+        "ForceFeedback",
+        "IOKit",
+        "OpenGL",
+        "Metal",
+    ],
     alwayslink = 1,
 )
 
-# Old attempt at building SDL directly.  May be useful for Windows and OS X
-# builds, when those exist.
-# cc_library(
-#     name = "sdl2",
-#     srcs = glob(
-#         include = [
-#             "src/**/*.c",
-#             "src/**/*.h",
-#         ],
-#         exclude = [
-#             "src/video/qnx/**",
-#             "src/haptic/windows/**",
-#             "src/test/*.c",
-#             #"src/thread/**/*.c",
-#         ],
-#     ),
-#     hdrs = glob([
-#         "include/*.h",
-#     ]),
-#     includes = ["include"],
-#     copts = [
-#         "-DSDL_VIDEO_DRIVER_X11",
-#         "-DSDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS",
-#         "-DSDL_VIDEO_DRIVER_X11_XDBE",
-#         "-DSDL_VIDEO_OPENGL",
-#         "-DSDL_VIDEO_OPENGL_GLX",
-#         "-DSDL_VIDEO_RENDER_OGL",
-#     ],
-#     linkopts = [
-#         "-ldl",
-#         "-lX11",
-#         "-lXext",
-#     ],
-#     visibility = ["//visibility:public"],
-# )
+sdl_srcs = glob(
+    include = [
+        "src/**/*.c",
+        "src/**/*.h",
+    ],
+    exclude = [
+        "src/video/qnx/**",
+        "src/haptic/windows/**",
+        "src/test/*.c",
+        "src/thread/generic/*.c",
+        "src/core/linux/*.c",
+    ],
+)
+
+# In general, we bundle SDL2.  However, on Linux that would require a header
+# generated by --configure, and a lot of buildflags and installing dev headers
+# is in general less painful, so we just link it dynamically.
+cc_library(
+    name = "sdl2",
+    srcs = select({
+        "@com_google_quic_trace//buildenv:linux": [],
+        "@com_google_quic_trace//buildenv:osx": sdl_srcs,
+    }),
+    hdrs = glob(["include/*.h"]),
+    includes = sdl_includes,
+    linkopts = select({
+        "@com_google_quic_trace//buildenv:linux": ["-lSDL2"],
+        "@com_google_quic_trace//buildenv:osx": [],
+    }),
+    textual_hdrs = glob(["src/thread/generic/*.c"]),
+    visibility = ["//visibility:public"],
+    deps = select({
+        "@com_google_quic_trace//buildenv:linux": [],
+        "@com_google_quic_trace//buildenv:osx": [":sdl2_objc"],
+    }),
+)
diff --git a/third_party/glew/BUILD b/third_party/glew/BUILD
index 913c6de..53087dc 100644
--- a/third_party/glew/BUILD
+++ b/third_party/glew/BUILD
@@ -26,6 +26,9 @@
     srcs = ["src/glew.c"],
     hdrs = glob(["include/GL/*.h"]),
     includes = ["include"],
-    linkopts = ["-lGL"],
+    linkopts = select({
+        "//buildenv:linux": ["-lGL"],
+        "//buildenv:osx": ["-framework OpenGL"],
+    }),
     visibility = ["//visibility:public"],
 )