From 12b16e7d7a7cf0065d9688f948f3953ce8ba44d6 Mon Sep 17 00:00:00 2001
From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com>
Date: Thu, 14 Jul 2022 14:27:52 +0800
Subject: [PATCH] libphonenumber: use Homebrew `abseil`

This downloads its own `abseil` during the build, and then gets confused
about where to find `abseil` headers when you try to link with it.

Let's fix that by relying on our version of `abseil` instead.

This should help unblock #105712 and previous attempts to bump this
formula's version (e.g. #105188, #101703, #98994, #96834).
---
 Formula/libphonenumber.rb | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/Formula/libphonenumber.rb b/Formula/libphonenumber.rb
index b6941ea6c7e47..b57d09241c2e6 100644
--- a/Formula/libphonenumber.rb
+++ b/Formula/libphonenumber.rb
@@ -22,16 +22,36 @@ class Libphonenumber < Formula
 
   depends_on "cmake" => :build
   depends_on "googletest" => :build
+  depends_on "abseil"
   depends_on "boost"
   depends_on "icu4c"
   depends_on "protobuf"
   depends_on "re2"
 
+  on_linux do
+    depends_on "gcc"
+  end
+
+  fails_with gcc: "5" # For abseil and C++17
+
+  # Use Homebrew abseil. Patch from Arch. Debian uses a similar patch.
+  # A version is being upstreamed at:
+  #   https://github.com/google/libphonenumber/pull/2772
+  #
+  # We're not using the upstream patch because it doesn't work yet.
+  # https://github.com/google/libphonenumber/pull/2772#issuecomment-1184289820
+  patch do
+    url "https://raw.githubusercontent.com/archlinux/svntogit-packages/864f0cf5874088d76e9e7309fb3da1eeed8b701d/trunk/absl.diff"
+    sha256 "f6bceb2409ff7cba1e6947e6fdce3fe82b511b04fefcd1f597eceb13af67a8a4"
+  end
+
   def install
-    ENV.cxx11
-    system "cmake", "cpp", "-DGTEST_INCLUDE_DIR=#{Formula["googletest"].include}",
-                           *std_cmake_args
-    system "make", "install"
+    system "cmake", "-S", "cpp", "-B", "build",
+                    "-DCMAKE_CXX_STANDARD=17", # keep in sync with C++ standard in abseil.rb
+                    "-DGTEST_INCLUDE_DIR=#{Formula["googletest"].opt_include}",
+                      *std_cmake_args
+    system "cmake", "--build", "build"
+    system "cmake", "--install", "build"
   end
 
   test do
@@ -57,7 +77,7 @@ def install
         }
       }
     EOS
-    system ENV.cxx, "-std=c++11", "test.cpp", "-L#{lib}", "-lphonenumber", "-o", "test"
+    system ENV.cxx, "-std=c++17", "test.cpp", "-L#{lib}", "-lphonenumber", "-o", "test"
     system "./test"
   end
 end