From 78b64375550eee99d2bab694063f8b1633da5ade Mon Sep 17 00:00:00 2001
From: Vincent Rabaud <vrabaud@google.com>
Date: Thu, 11 Jul 2024 23:46:08 +0200
Subject: [PATCH] Rename Googletest to GTest

This is to get -DAVIF_GTEST=SYSTEM working.
- rename Googletest to GTest which is the proper way to find gtest
- use GTest::GTest and GTest::Main instead of Gtest::gtest and
  Gtest::gtest_main to work with older CMake (CMake 3.20 added the
  new convention)
- add a CI to test it
---
 .github/actions/setup-linux/action.yml         |  8 ++++++++
 .github/actions/setup-macos/action.yml         |  7 +++++++
 .github/workflows/ci-unix-shared-installed.yml |  7 +++----
 ...{LocalGoogletest.cmake => LocalGTest.cmake} | 18 +++++++++---------
 tests/CMakeLists.txt                           | 12 ++++++------
 5 files changed, 33 insertions(+), 19 deletions(-)
 rename cmake/Modules/{LocalGoogletest.cmake => LocalGTest.cmake} (75%)

diff --git a/.github/actions/setup-linux/action.yml b/.github/actions/setup-linux/action.yml
index 11f372273..bd6d414e4 100644
--- a/.github/actions/setup-linux/action.yml
+++ b/.github/actions/setup-linux/action.yml
@@ -16,6 +16,9 @@ inputs:
   gcc-version:
     description: 'Can be empty, in which case CC and CXX are not overriden'
     default: ''
+  gtest:
+    description: 'Can take the values: OFF, LOCAL, SYSTEM'
+    default: 'OFF'
   libxml2:
     description: 'Can take the values: OFF, LOCAL, SYSTEM'
     default: 'OFF'
@@ -66,6 +69,11 @@ runs:
       run:
         sudo apt install meson
       shell: bash
+    - name: Set up gtest
+      if: ${{ inputs.gtest == 'SYSTEM' }}
+      run:
+        sudo apt install libgtest-dev
+      shell: bash
 
     - uses: ./.github/actions/setup-common
       with:
diff --git a/.github/actions/setup-macos/action.yml b/.github/actions/setup-macos/action.yml
index 64ad0e2f1..ad215b381 100644
--- a/.github/actions/setup-macos/action.yml
+++ b/.github/actions/setup-macos/action.yml
@@ -10,6 +10,9 @@ inputs:
   codec-rav1e:
     description: 'Can take the values: OFF, LOCAL, SYSTEM'
     default: 'OFF'
+  gtest:
+    description: 'Can take the values: OFF, LOCAL, SYSTEM'
+    default: 'OFF'
   extra-cache-key:
     description: 'Extra cache key to use in the cache name. Useful when several caches are used in one workflow.'
     default: ''
@@ -46,6 +49,10 @@ runs:
       if: ${{ inputs.codec-dav1d == 'LOCAL' }}
       run: brew install meson
       shell: bash
+    - name: Set up gtest
+      if: ${{ inputs.gtest == 'SYSTEM' }}
+      run: brew install googletest
+      shell: bash
 
     - uses: ./.github/actions/setup-common
       with:
diff --git a/.github/workflows/ci-unix-shared-installed.yml b/.github/workflows/ci-unix-shared-installed.yml
index e065a7aee..961e9972e 100644
--- a/.github/workflows/ci-unix-shared-installed.yml
+++ b/.github/workflows/ci-unix-shared-installed.yml
@@ -32,20 +32,19 @@ jobs:
         codec-aom: 'SYSTEM'
         codec-dav1d: 'SYSTEM'
         gcc-version: ${{ matrix.gcc }}
+        gtest: 'SYSTEM'
         libyuv: 'SYSTEM'
     - uses: ./.github/actions/setup-macos
       if: runner.os == 'macOS'
       with:
         codec-aom: 'SYSTEM'
         codec-dav1d: 'SYSTEM'
+        gtest: 'SYSTEM'
     - name: Disable libyuv on macOS
       # TODO(yguyon): Install libyuv (not available with brew).
       if: runner.os == 'macOS'
       run: echo "CMAKE_AVIF_FLAGS=\"-DAVIF_LIBYUV=OFF\""  >> $GITHUB_ENV
 
-      # `sudo apt-get install googletest libgtest-dev` leads to the following:
-      #   "libgtest.a(gtest-all.cc.o): undefined reference to `std::__throw_bad_array_new_length()'"
-      # so build it locally instead.
     - name: Prepare libavif (cmake)
       run: >
         cmake -G Ninja -S . -B build
@@ -54,7 +53,7 @@ jobs:
         -DAVIF_CODEC_AOM_DECODE=OFF -DAVIF_CODEC_AOM_ENCODE=ON
         -DAVIF_CODEC_DAV1D=SYSTEM
         -DAVIF_BUILD_EXAMPLES=ON -DAVIF_BUILD_APPS=ON
-        -DAVIF_BUILD_TESTS=ON -DAVIF_ENABLE_GTEST=ON -DAVIF_GTEST=LOCAL
+        -DAVIF_BUILD_TESTS=ON -DAVIF_ENABLE_GTEST=ON -DAVIF_GTEST=SYSTEM
         -DAVIF_BUILD_GDK_PIXBUF=ON -DCMAKE_INSTALL_PREFIX=./build/install
         -DAVIF_ENABLE_WERROR=ON ${{ env.CMAKE_AVIF_FLAGS }}
     - name: Build libavif (ninja)
diff --git a/cmake/Modules/LocalGoogletest.cmake b/cmake/Modules/LocalGTest.cmake
similarity index 75%
rename from cmake/Modules/LocalGoogletest.cmake
rename to cmake/Modules/LocalGTest.cmake
index d687237f5..b072f392f 100644
--- a/cmake/Modules/LocalGoogletest.cmake
+++ b/cmake/Modules/LocalGTest.cmake
@@ -11,17 +11,17 @@ set(GTEST_MAIN_LIB_FILENAME
 if(EXISTS ${GTEST_INCLUDE_DIRS}/gtest/gtest.h AND EXISTS ${GTEST_LIB_FILENAME} AND EXISTS ${GTEST_MAIN_LIB_FILENAME})
     message(STATUS "libavif(AVIF_GTEST=LOCAL): compiled library found in ext/googletest")
 
-    add_library(GTest::gtest STATIC IMPORTED)
-    set_target_properties(GTest::gtest PROPERTIES IMPORTED_LOCATION "${GTEST_LIB_FILENAME}" AVIF_LOCAL ON)
+    add_library(GTest::GTest STATIC IMPORTED)
+    set_target_properties(GTest::GTest PROPERTIES IMPORTED_LOCATION "${GTEST_LIB_FILENAME}" AVIF_LOCAL ON)
 
     if(TARGET Threads::Threads)
-        target_link_libraries(GTest::gtest INTERFACE Threads::Threads)
+        target_link_libraries(GTest::GTest INTERFACE Threads::Threads)
     endif()
-    target_include_directories(GTest::gtest INTERFACE "${GTEST_INCLUDE_DIRS}")
+    target_include_directories(GTest::GTest INTERFACE "${GTEST_INCLUDE_DIRS}")
 
-    add_library(GTest::gtest_main STATIC IMPORTED)
-    target_link_libraries(GTest::gtest_main INTERFACE GTest::gtest)
-    set_target_properties(GTest::gtest_main PROPERTIES IMPORTED_LOCATION "${GTEST_MAIN_LIB_FILENAME}" AVIF_LOCAL ON)
+    add_library(GTest::Main STATIC IMPORTED)
+    target_link_libraries(GTest::Main INTERFACE GTest::GTest)
+    set_target_properties(GTest::Main PROPERTIES IMPORTED_LOCATION "${GTEST_MAIN_LIB_FILENAME}" AVIF_LOCAL ON)
 else()
     message(STATUS "libavif(AVIF_GTEST=LOCAL): compiled library not found in ext/googletest; using FetchContent")
     if(EXISTS "${AVIF_SOURCE_DIR}/ext/googletest")
@@ -45,8 +45,8 @@ else()
 
     set_target_properties(gtest gtest_main PROPERTIES AVIF_LOCAL ON)
 
-    add_library(GTest::gtest ALIAS gtest)
-    add_library(GTest::gtest_main ALIAS gtest_main)
+    add_library(GTest::GTest ALIAS gtest)
+    add_library(GTest::Main ALIAS gtest_main)
 
     message(CHECK_PASS "complete")
 endif()
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 5067a32d1..dadd1c0e2 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -63,33 +63,33 @@ add_test(NAME repro_avif_decode_fuzzer COMMAND repro_avif_decode_fuzzer
 # are considered as extra linked libraries.
 macro(add_avif_gtest TEST_NAME)
     add_executable(${TEST_NAME} gtest/${TEST_NAME}.cc)
-    target_link_libraries(${TEST_NAME} PRIVATE aviftest_helpers GTest::gtest GTest::gtest_main ${ARGN})
+    target_link_libraries(${TEST_NAME} PRIVATE aviftest_helpers GTest::GTest GTest::Main ${ARGN})
     add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
     register_test_for_coverage(${TEST_NAME})
 endmacro()
 macro(add_avif_internal_gtest TEST_NAME)
     add_executable(${TEST_NAME} gtest/${TEST_NAME}.cc)
-    target_link_libraries(${TEST_NAME} PRIVATE aviftest_helpers_internal GTest::gtest GTest::gtest_main ${ARGN})
+    target_link_libraries(${TEST_NAME} PRIVATE aviftest_helpers_internal GTest::GTest GTest::Main ${ARGN})
     add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
     register_test_for_coverage(${TEST_NAME})
 endmacro()
 macro(add_avif_gtest_with_data TEST_NAME)
     add_executable(${TEST_NAME} gtest/${TEST_NAME}.cc)
-    target_link_libraries(${TEST_NAME} PRIVATE aviftest_helpers GTest::gtest ${ARGN})
+    target_link_libraries(${TEST_NAME} PRIVATE aviftest_helpers GTest::GTest ${ARGN})
     add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/data/)
     register_test_for_coverage(${TEST_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/data/)
 endmacro()
 macro(add_avif_internal_gtest_with_data TEST_NAME)
     add_executable(${TEST_NAME} gtest/${TEST_NAME}.cc)
-    target_link_libraries(${TEST_NAME} PRIVATE aviftest_helpers_internal GTest::gtest ${ARGN})
+    target_link_libraries(${TEST_NAME} PRIVATE aviftest_helpers_internal GTest::GTest ${ARGN})
     add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/data/)
     register_test_for_coverage(${TEST_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/data/)
 endmacro()
 
 if(AVIF_ENABLE_GTEST)
-    check_avif_option(AVIF_GTEST TARGET GTest::gtest PKG_NAME Googletest)
+    check_avif_option(AVIF_GTEST TARGET GTest::GTest PKG_NAME GTest)
     add_library(avifincrtest_helpers OBJECT gtest/avifincrtest_helpers.cc)
-    target_link_libraries(avifincrtest_helpers avif_internal GTest::gtest)
+    target_link_libraries(avifincrtest_helpers avif_internal GTest::GTest)
 endif()
 
 if(AVIF_ENABLE_GTEST)
