Handle dependencies

* Only build CompressingStream and DecompressingStream when the
  AEMU_BASE_USE_LZ4 build option is enabled, and use the lz4 dependency
  provided by the caller.
* Do not guard lz4 code in both CompressingStream and
  DecompressingStream.

Bug: b/256177310
Change-Id: I87491c347a3270186a2ab15101157cd17eb0ae0c
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2b303a7..fe65aed 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -28,6 +28,7 @@
        "Enable clang thread-safety checks (-Wthread-safety)."
        OFF)
 option(AEMU_COMMON_USE_PERFETTO "Use perfotto for tracing." OFF)
+option(AEMU_BASE_USE_LZ4 "The lz4 dependency is provided, and compile the compressing stream." OFF)
 
 if (WIN32)
     set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
diff --git a/base/Android.bp b/base/Android.bp
index 4ffebe7..40bb18b 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -42,7 +42,8 @@
         "Tracing.cpp",
         "Thread_pthread.cpp",
     ],
-    export_header_lib_headers: ["gfxstream_renderdoc_headers"]
+    export_header_lib_headers: ["gfxstream_renderdoc_headers"],
+    static_libs: ["liblz4"],
 }
 
 // Run with `atest --host gfxstream_base_tests`
diff --git a/base/CMakeLists.txt b/base/CMakeLists.txt
index dc789b9..4aa49e7 100644
--- a/base/CMakeLists.txt
+++ b/base/CMakeLists.txt
@@ -21,9 +21,7 @@
         set(aemu-base-srcs
             AlignedBuf.cpp
             CLog.cpp
-            CompressingStream.cpp
             CpuTime.cpp
-            DecompressingStream.cpp
             FileUtils.cpp
             FunctorThread.cpp
             GLObjectCounter.cpp
@@ -50,6 +48,9 @@
             SharedMemory_win32.cpp
             Thread_win32.cpp
             Win32UnicodeString.cpp)
+        if(AEMU_BASE_USE_LZ4)
+            list(APPEND aemu-base-srcs CompressingStream.cpp DecompressingStream.cpp)
+        endif()
 
         if (APPLE)
             set(aemu-platform-srcs
@@ -102,6 +103,9 @@
         target_compile_definitions(aemu-base PRIVATE "USE_PERFETTO_TRACING")
         target_link_libraries(aemu-base PRIVATE perfetto-tracing-only)
     endif()
+    if(AEMU_BASE_USE_LZ4)
+        target_link_libraries(aemu-base PRIVATE lz4_static)
+    endif()
 endif()
 
 if (APPLE)
diff --git a/base/CompressingStream.cpp b/base/CompressingStream.cpp
index 39684ad..bb748fd 100644
--- a/base/CompressingStream.cpp
+++ b/base/CompressingStream.cpp
@@ -16,9 +16,7 @@
 
 #include "aemu/base/files/StreamSerializing.h"
 
-#if LZ4_VERSION_RELEASE
 #include "lz4.h"
-#endif
 
 #include <errno.h>
 
@@ -27,16 +25,12 @@
 
 CompressingStream::CompressingStream(Stream& output)
     : mOutput(output) {
-#if LZ4_VERSION_RELEASE
     mLzStream = reinterpret_cast<void *>(LZ4_createStream());
-#endif
 }
 
 CompressingStream::~CompressingStream() {
     saveBuffer(&mOutput, mBuffer);
-#if LZ4_VERSION_RELEASE
     LZ4_freeStream((LZ4_stream_t*)mLzStream);
-#endif
 }
 
 ssize_t CompressingStream::read(void*, size_t) {
@@ -49,18 +43,14 @@
     }
 
     size_t outSize = 0;
-#ifdef LZ4_VERSION_RELEASE
     outSize = LZ4_compressBound(size);
-#endif
     auto oldSize = mBuffer.size();
     mBuffer.resize_noinit(mBuffer.size() + outSize);
     const auto outBuffer = mBuffer.data() + oldSize;
     int written = 0;
-#ifdef LZ4_VERSION_RELEASE
     written = LZ4_compress_fast_continue((LZ4_stream_t*)mLzStream,
                                          (const char*)buffer,
                                          outBuffer, size, outSize, 1);
-#endif
 
     if (!written) {
         return -EIO;
diff --git a/base/DecompressingStream.cpp b/base/DecompressingStream.cpp
index 8bff7b7..8729dcd 100644
--- a/base/DecompressingStream.cpp
+++ b/base/DecompressingStream.cpp
@@ -16,9 +16,7 @@
 
 #include "aemu/base/files/StreamSerializing.h"
 
-#if LZ4_VERSION_RELEASE
 #include "lz4.h"
-#endif
 
 #include <errno.h>
 #include <cassert>
@@ -27,16 +25,12 @@
 namespace base {
 
 DecompressingStream::DecompressingStream(Stream& input) {
-#if LZ4_VERSION_RELEASE
     mLzStream = reinterpret_cast<void *>(LZ4_createStreamDecode());
-#endif
     loadBuffer(&input, &mBuffer);
 }
 
 DecompressingStream::~DecompressingStream() {
-#if LZ4_VERSION_RELEASE
     LZ4_freeStreamDecode((LZ4_streamDecode_t*)mLzStream);
-#endif
 }
 
 ssize_t DecompressingStream::read(void* buffer, size_t size) {
@@ -46,11 +40,9 @@
         return 0;
     }
     int read = 0;
-#ifdef LZ4_VERSION_RELEASE
     read = LZ4_decompress_fast_continue(
            (LZ4_streamDecode_t*)mLzStream, mBuffer.data() + mBufferPos,
            (char*)buffer, size);
-#endif
     if (!read) {
         return -EIO;
     }
diff --git a/build-config/gfxstream/CMakeLists.txt b/build-config/gfxstream/CMakeLists.txt
index 84621fd..ea9ae8a 100644
--- a/build-config/gfxstream/CMakeLists.txt
+++ b/build-config/gfxstream/CMakeLists.txt
@@ -8,9 +8,7 @@
 set(aemu-base-srcs
     AlignedBuf.cpp
     CLog.cpp
-    CompressingStream.cpp
     CpuTime.cpp
-    DecompressingStream.cpp
     FileUtils.cpp
     FunctorThread.cpp
     GLObjectCounter.cpp
@@ -29,6 +27,10 @@
     SubAllocator.cpp
     System.cpp
     Tracing.cpp)
+if(AEMU_BASE_USE_LZ4)
+    list(APPEND aemu-base-srcs CompressingStream.cpp DecompressingStream.cpp)
+endif()
+
 set(aemu-base-posix-srcs
     SharedMemory_posix.cpp
     Thread_pthread.cpp)
diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt
index 3e3dd90..55e9233 100644
--- a/third-party/CMakeLists.txt
+++ b/third-party/CMakeLists.txt
@@ -1,18 +1,14 @@
 # Build only support in Android
 
-if(NOT TARGET lz4)
-    #TODO(joshuaduong) -- add lz4 for those who need it
-    message(STATUS "This message does nothing")
+if(AEMU_BASE_USE_LZ4 AND NOT TARGET lz4_static)
+    message(FATAL_ERROR "lz4 is not provided.")
 endif()
 
-if(NOT TARGET perfetto-tracing-only)
-    #TODO(joshuaduong) -- add perfetto-tracing-only for those who need it
-    message(STATUS "This message is a placeholder")
+if(AEMU_COMMON_USE_PERFETTO AND NOT TARGET perfetto-tracing-only)
+    message(FATAL_ERROR "perfetto-tracing-only is not provided.")
 endif()
 
-if(NOT TARGET gtest)
-    SET(INSTALL_GTEST OFF)
-    #TODO(joshuaduong) -- add ENABLE_TESTS option and use Googletest for those
-    # need it.
-    message(STATUS "This message is just a stand-in")
+if(ENABLE_VKCEREAL_TESTS AND NOT TARGET gtest)
+    set(INSTALL_GTEST OFF)
+    message(FATAL_ERROR "googletest is not provided.")
 endif()