Add SkCodec to the CMake build

BUG=skia:4956
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1705503002
CQ_EXTRA_TRYBOTS=client.skia.compile:Build-Ubuntu-GCC-x86_64-Release-CMake-Trybot

Review URL: https://codereview.chromium.org/1705503002
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
index 58c04db..751361a 100644
--- a/cmake/CMakeLists.txt
+++ b/cmake/CMakeLists.txt
@@ -113,7 +113,6 @@
 endif()
 
 remove_srcs(../src/gpu/gl/angle/*)  # TODO
-remove_srcs(../src/codec/* ../src/android/*)  # TODO: Requires Chromium's libjpeg-turbo, and incompatible giflib.
 
 # Certain files must be compiled with support for SSSE3, SSE4.1, AVX, or AVX2 intrinsics.
 file (GLOB_RECURSE ssse3_srcs ../src/*ssse3*.cpp ../src/*SSSE3*.cpp)
@@ -146,6 +145,16 @@
     find_package (PNG)
 endif()
 
+# Decide whether to turn on SkCodec.
+# TODO (skbug.com/4956): We should be able to turn specific codecs on and off rather than
+#                        disabling all of them if one library is missing.
+if (NOT (GIF_FOUND AND JPEG_FOUND AND PNG_FOUND AND WEBP_INCLUDE_DIRS AND WEBP_LIBRARIES))
+    remove_srcs(../src/codec/* ../src/android/*)
+endif()
+
+# Do not compile SkRawCodec.
+remove_srcs(../src/codec/*Raw*.cpp)
+
 # TODO: macro away this if (found) ... else() ... endif() stuff.
 
 if (EXPAT_FOUND)
@@ -179,6 +188,8 @@
 if (PNG_FOUND)
     list (APPEND private_includes ${PNG_INCLUDE_DIRS})
     list (APPEND libs             ${PNG_LIBRARIES})
+    add_definitions(-DPNG_SKIP_SETJMP_CHECK)
+    add_definitions(-DPNG_SKIP_SKIA_OPTS)
 else()
     remove_srcs(../src/images/*png*)
 endif()
diff --git a/cmake/README.md b/cmake/README.md
index 9fd84a7..0cbd2b3 100644
--- a/cmake/README.md
+++ b/cmake/README.md
@@ -21,7 +21,3 @@
 Currently maybe-kinda-working platforms
 ---------------------------------------
   - x86-64 Mac OS X, Ubuntu 15.04
-
-Caveats
--------
-  - SkCodec, Skia's new image decoder library, does not yet build.
diff --git a/src/codec/SkGifCodec.cpp b/src/codec/SkGifCodec.cpp
index 785c17e..6995547 100644
--- a/src/codec/SkGifCodec.cpp
+++ b/src/codec/SkGifCodec.cpp
@@ -49,7 +49,11 @@
  * Open the gif file
  */
 static GifFileType* open_gif(SkStream* stream) {
+#if GIFLIB_MAJOR < 5
+    return DGifOpen(stream, read_bytes_callback);
+#else
     return DGifOpen(stream, read_bytes_callback, nullptr);
+#endif
 }
 
 /*
@@ -117,7 +121,11 @@
  * It is used in a SkAutoTCallIProc template
  */
 void SkGifCodec::CloseGif(GifFileType* gif) {
-    DGifCloseFile(gif, NULL);
+#if GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0)
+    DGifCloseFile(gif);
+#else
+    DGifCloseFile(gif, nullptr);
+#endif
 }
 
 /*
@@ -126,7 +134,11 @@
  */
 void SkGifCodec::FreeExtension(SavedImage* image) {
     if (NULL != image->ExtensionBlocks) {
+#if GIFLIB_MAJOR < 5
+        FreeExtension(image);
+#else
         GifFreeExtensions(&image->ExtensionBlockCount, &image->ExtensionBlocks);
+#endif
     }
 }
 
@@ -311,10 +323,15 @@
                 // Create an extension block with our data
                 while (nullptr != extData) {
                     // Add a single block
+
+#if GIFLIB_MAJOR < 5
+                    if (AddExtensionBlock(&saveExt, extData[0],
+                                          &extData[1]) == GIF_ERROR) {
+#else
                     if (GIF_ERROR == GifAddExtensionBlock(&saveExt.ExtensionBlockCount,
                                                           &saveExt.ExtensionBlocks,
-                                                          extFunction, extData[0], &extData[1]))
-                    {
+                                                          extFunction, extData[0], &extData[1])) {
+#endif
                         return gif_error("Could not add extension block.\n", kIncompleteInput);
                     }
                     // Move to the next block
diff --git a/src/codec/SkPngCodec.cpp b/src/codec/SkPngCodec.cpp
index 54a82c4..974befc 100644
--- a/src/codec/SkPngCodec.cpp
+++ b/src/codec/SkPngCodec.cpp
@@ -19,7 +19,7 @@
 #include "SkTemplates.h"
 
 // png_struct::read_filter[] was added in libpng 1.5.7.
-#if defined(__SSE2__) && PNG_LIBPNG_VER >= 10507
+#if defined(__SSE2__) && PNG_LIBPNG_VER >= 10507 && !defined(PNG_SKIP_SKIA_OPTS)
     #include "pngstruct.h"
 
     extern "C" void sk_png_init_filter_functions_sse2(png_structp png, unsigned int bpp) {