From b76aa2721f3b64f1e14e7f723627b0640606b998 Mon Sep 17 00:00:00 2001 From: Sven Schwyn Date: Sun, 25 Feb 2024 22:54:00 +0100 Subject: [PATCH] Add support for PostgreSQL 16 --- Formula/postgis@16.rb | 156 +++++++++++++++++++++++++++++++++++++++ Formula/postgresql@16.rb | 139 ++++++++++++++++++++++++++++++++++ README.md | 4 +- 3 files changed, 297 insertions(+), 2 deletions(-) create mode 100644 Formula/postgis@16.rb create mode 100644 Formula/postgresql@16.rb diff --git a/Formula/postgis@16.rb b/Formula/postgis@16.rb new file mode 100644 index 0000000..7577540 --- /dev/null +++ b/Formula/postgis@16.rb @@ -0,0 +1,156 @@ +class PostgisAT16 < Formula + desc "Adds support for geographic objects to PostgreSQL" + homepage "https://postgis.net/" + url "https://download.osgeo.org/postgis/source/postgis-3.3.6.tar.gz" + sha256 "7e898063a9f50f21f42277bfb687bbb9bf5cb6f61ce160031c825403a66911b5" + license "GPL-2.0-or-later" + revision 1 + + livecheck do + url "https://download.osgeo.org/postgis/source/" + regex(/href=.*?postgis[._-]v?(\d+(?:\.\d+)+)\.t/i) + end + + head do + url "https://git.osgeo.org/gitea/postgis/postgis.git", branch: "master" + + depends_on "autoconf" => :build + depends_on "automake" => :build + depends_on "libtool" => :build + end + + depends_on "gpp" => :build + depends_on "pkg-config" => :build + depends_on "gdal" # for GeoJSON and raster handling + depends_on "geos" + depends_on "icu4c" + depends_on "json-c" # for GeoJSON and raster handling + depends_on "ozeias/postgresql/postgresql@16" + depends_on "pcre2" + depends_on "proj" + depends_on "protobuf-c" # for MVT (map vector tiles) support + depends_on "sfcgal" # for advanced 2D/3D functions + + fails_with gcc: "5" # C++17 + + def postgresql + Formula["ozeias/postgresql/postgresql@16"] + end + + def install + ENV.deparallelize + + # C++17 is required. + ENV.append "CXXFLAGS", "-std=c++17" + + ENV["PG_CONFIG"] = postgresql.opt_bin/"pg_config" + + args = [ + "--with-projdir=#{Formula["proj"].opt_prefix}", + "--with-jsondir=#{Formula["json-c"].opt_prefix}", + "--with-pgconfig=#{postgresql.opt_bin}/pg_config", + "--with-protobufdir=#{Formula["protobuf-c"].opt_bin}", + # Unfortunately, NLS support causes all kinds of headaches because + # PostGIS gets all of its compiler flags from the PGXS makefiles. This + # makes it nigh impossible to tell the buildsystem where our keg-only + # gettext installations are. + "--disable-nls", + ] + + system "./autogen.sh" if build.head? + # Fixes config/install-sh: No such file or directory + # This is caused by a misalignment between ./configure in postgresql@16 and postgis + mv "build-aux", "config" + inreplace %w[configure utils/Makefile.in] do |s| + s.gsub! "build-aux", "config" + end + system "./configure", *args, *std_configure_args + system "make" + + # Install to a staging directory to circumvent the hardcoded install paths + # set by the PGXS makefiles. + mkdir "stage" + system "make", "install", "DESTDIR=#{buildpath}/stage" + + # Some files are stored in the stage directory with the cellar prefix of + # the version of postgresql used to build postgis. Since we copy these + # files into the postgis keg and symlink them to HOMEBREW_PREFIX, postgis + # only needs to be rebuilt when there is a new major version of postgresql. + postgresql_stage_path = File.join("stage", postgresql.prefix.realpath) + bin.install (buildpath/postgresql_stage_path/"bin").children + doc.install (buildpath/postgresql_stage_path/"share/doc").children + + stage_path = File.join("stage", HOMEBREW_PREFIX) + lib.install (buildpath/stage_path/"lib").children + share.install (buildpath/stage_path/"share").children + + # Extension scripts + bin.install %w[ + utils/create_undef.pl + utils/create_upgrade.pl + utils/postgis_restore.pl + utils/profile_intersects.pl + utils/test_estimation.pl + utils/test_geography_estimation.pl + utils/test_geography_joinestimation.pl + utils/test_joinestimation.pl + ] + end + + test do + pg_version = postgresql.version.major + expected = /'PostGIS built for PostgreSQL % cannot be loaded in PostgreSQL %',\s+#{pg_version}\.\d,/ + postgis_version = Formula["postgis"].version.major_minor + assert_match expected, (share/postgresql.name/"contrib/postgis-#{postgis_version}/postgis.sql").read + + require "base64" + (testpath/"brew.shp").write ::Base64.decode64 <<~EOS + AAAnCgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAoOgDAAALAAAAAAAAAAAAAAAA + AAAAAADwPwAAAAAAABBAAAAAAAAAFEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAEAAAASCwAAAAAAAAAAAPA/AAAAAAAA8D8AAAAAAAAA + AAAAAAAAAAAAAAAAAgAAABILAAAAAAAAAAAACEAAAAAAAADwPwAAAAAAAAAA + AAAAAAAAAAAAAAADAAAAEgsAAAAAAAAAAAAQQAAAAAAAAAhAAAAAAAAAAAAA + AAAAAAAAAAAAAAQAAAASCwAAAAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAAAAA + AAAAAAAAAAAABQAAABILAAAAAAAAAAAAAAAAAAAAAAAUQAAAAAAAACJAAAAA + AAAAAEA= + EOS + (testpath/"brew.dbf").write ::Base64.decode64 <<~EOS + A3IJGgUAAABhAFsAAAAAAAAAAAAAAAAAAAAAAAAAAABGSVJTVF9GTEQAAEMA + AAAAMgAAAAAAAAAAAAAAAAAAAFNFQ09ORF9GTEQAQwAAAAAoAAAAAAAAAAAA + AAAAAAAADSBGaXJzdCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg + ICAgICAgICAgICAgIFBvaW50ICAgICAgICAgICAgICAgICAgICAgICAgICAg + ICAgICAgICAgU2Vjb25kICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg + ICAgICAgICAgICAgICBQb2ludCAgICAgICAgICAgICAgICAgICAgICAgICAg + ICAgICAgICAgIFRoaXJkICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg + ICAgICAgICAgICAgICAgUG9pbnQgICAgICAgICAgICAgICAgICAgICAgICAg + ICAgICAgICAgICBGb3VydGggICAgICAgICAgICAgICAgICAgICAgICAgICAg + ICAgICAgICAgICAgICAgIFBvaW50ICAgICAgICAgICAgICAgICAgICAgICAg + ICAgICAgICAgICAgQXBwZW5kZWQgICAgICAgICAgICAgICAgICAgICAgICAg + ICAgICAgICAgICAgICAgICBQb2ludCAgICAgICAgICAgICAgICAgICAgICAg + ICAgICAgICAgICAg + EOS + (testpath/"brew.shx").write ::Base64.decode64 <<~EOS + AAAnCgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARugDAAALAAAAAAAAAAAAAAAA + AAAAAADwPwAAAAAAABBAAAAAAAAAFEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAADIAAAASAAAASAAAABIAAABeAAAAEgAAAHQAAAASAAAA + igAAABI= + EOS + result = shell_output("#{bin}/shp2pgsql #{testpath}/brew.shp") + assert_match "Point", result + assert_match "AddGeometryColumn", result + + pg_ctl = postgresql.opt_bin/"pg_ctl" + psql = postgresql.opt_bin/"psql" + port = free_port + + system pg_ctl, "initdb", "-D", testpath/"test" + (testpath/"test/postgresql.conf").write <<~EOS, mode: "a+" + + shared_preload_libraries = 'postgis-3' + port = #{port} + EOS + system pg_ctl, "start", "-D", testpath/"test", "-l", testpath/"log" + system psql, "-p", port.to_s, "-c", "CREATE EXTENSION \"postgis\";", "postgres" + system pg_ctl, "stop", "-D", testpath/"test" + end +end diff --git a/Formula/postgresql@16.rb b/Formula/postgresql@16.rb new file mode 100644 index 0000000..3c09cdc --- /dev/null +++ b/Formula/postgresql@16.rb @@ -0,0 +1,139 @@ +class PostgresqlAT16 < Formula + desc "Object-relational database system" + homepage "https://www.postgresql.org/" + url "https://ftp.postgresql.org/pub/source/v16.2/postgresql-16.2.tar.bz2" + sha256 "446e88294dbc2c9085ab4b7061a646fa604b4bec03521d5ea671c2e5ad9b2952" + license "PostgreSQL" + + livecheck do + url "https://ftp.postgresql.org/pub/source/" + regex(%r{href=["']?v?(16(?:\.\d+)+)/?["' >]}i) + end + + # https://www.postgresql.org/support/versioning/ + deprecate! date: "2028-11-09", because: :unsupported + + depends_on "pkg-config" => :build + depends_on "gettext" + depends_on "icu4c" + + # GSSAPI provided by Kerberos.framework crashes when forked. + # See https://github.com/Homebrew/homebrew-core/issues/47494. + depends_on "krb5" + + depends_on "lz4" + depends_on "openssl@3" + depends_on "readline" + depends_on "zstd" + + uses_from_macos "libxml2" + uses_from_macos "libxslt" + uses_from_macos "openldap" + uses_from_macos "perl" + + on_linux do + depends_on "linux-pam" + depends_on "util-linux" + end + + def install + ENV.prepend "LDFLAGS", "-L#{Formula["openssl@3"].opt_lib} -L#{Formula["readline"].opt_lib}" + ENV.prepend "CPPFLAGS", "-I#{Formula["openssl@3"].opt_include} -I#{Formula["readline"].opt_include}" + + args = %W[ + --disable-debug + --prefix=#{prefix} + --datadir=#{HOMEBREW_PREFIX}/share/#{name} + --libdir=#{HOMEBREW_PREFIX}/lib/#{name} + --includedir=#{HOMEBREW_PREFIX}/include/#{name} + --enable-nls + --enable-thread-safety + --with-gssapi + --with-icu + --with-ldap + --with-libxml + --with-libxslt + --with-lz4 + --with-zstd + --with-openssl + --with-pam + --with-perl + --with-uuid=e2fs + --with-extra-version=\ (#{tap.user}) + ] + if OS.mac? + args += %w[ + --with-bonjour + --with-tcl + ] + end + + # PostgreSQL by default uses xcodebuild internally to determine this, + # which does not work on CLT-only installs. + args << "PG_SYSROOT=#{MacOS.sdk_path}" if OS.mac? && MacOS.sdk_root_needed? + + system "./configure", *args + system "make" + system "make", "install-world", "datadir=#{pkgshare}", + "libdir=#{lib}/#{name}", + "pkglibdir=#{lib}/#{name}", + "includedir=#{include}/#{name}", + "pkgincludedir=#{include}/#{name}", + "includedir_server=#{include}/#{name}/server", + "includedir_internal=#{include}/#{name}/internal" + + if OS.linux? + inreplace lib/name/"pgxs/src/Makefile.global", + "LD = #{HOMEBREW_PREFIX}/Homebrew/Library/Homebrew/shims/linux/super/ld", + "LD = #{HOMEBREW_PREFIX}/bin/ld" + end + end + + def post_install + (var/"log").mkpath + postgresql_datadir.mkpath + + # Don't initialize database, it clashes when testing other PostgreSQL versions. + return if ENV["HOMEBREW_GITHUB_ACTIONS"] + + system "#{bin}/initdb", "--locale=C", "-E", "UTF-8", postgresql_datadir unless pg_version_exists? + end + + def postgresql_datadir + var/name + end + + def postgresql_log_path + var/"log/#{name}.log" + end + + def pg_version_exists? + (postgresql_datadir/"PG_VERSION").exist? + end + + def caveats + <<~EOS + This formula has created a default database cluster with: + initdb --locale=C -E UTF-8 #{postgresql_datadir} + For more details, read: + https://www.postgresql.org/docs/#{version.major}/app-initdb.html + EOS + end + + service do + run [opt_bin/"postgres", "-D", f.postgresql_datadir] + environment_variables LC_ALL: "C" + keep_alive true + log_path f.postgresql_log_path + error_log_path f.postgresql_log_path + working_dir HOMEBREW_PREFIX + end + + test do + system bin/"initdb", testpath/"test" unless ENV["HOMEBREW_GITHUB_ACTIONS"] + assert_equal "#{HOMEBREW_PREFIX}/share/#{name}", shell_output("#{bin}/pg_config --sharedir").chomp + assert_equal "#{HOMEBREW_PREFIX}/lib/#{name}", shell_output("#{bin}/pg_config --libdir").chomp + assert_equal "#{HOMEBREW_PREFIX}/lib/#{name}", shell_output("#{bin}/pg_config --pkglibdir").chomp + assert_equal "#{HOMEBREW_PREFIX}/include/#{name}", shell_output("#{bin}/pg_config --includedir").chomp + end +end diff --git a/README.md b/README.md index 363f993..8c45979 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # PostgreSQL and PostGIS This is a brew tap for installing PostgreSQL and PostGIS. Currently this tap has the following formulas: -* `postgis@15` -* `postgresql@15` +* `postgis@15` and `postgis@16` +* `postgresql@15` and `postgresql@16` ## How do I install these formulae?