From 602cb1f2e8800a82f2cf9531b687fc92449b897e Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Sun, 24 Oct 2021 00:04:44 -0400
Subject: [PATCH 01/13] Use pkg-config to find x11 and xt

---
 configure.ac | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/configure.ac b/configure.ac
index 801d24d610..600bfd23d4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -73,6 +73,35 @@ dnl   automatically (see the automake manual's FAQ for justification).
 drivers="exdriv.lo pgdriv.lo cadriv.lo hgdriv.lo lxdriv.lo  vtdriv.lo hidriv.lo psdriv.lo ttdriv.lo cwdriv.lo gldriv.lo nedriv.lo hpdriv.lo lsdriv.lo nudriv.lo qmdriv.lo vadriv.lo cgdriv.lo nexsup.lo rvdriv.lo xmdriv.lo tkdriv.lo pkdriv.lo xadriv.lo"
 
 dnl   Not all platforms have X
+have_x11=no
+have_xt=no
+AS_IF([ test "x$no_x" = "x"],
+      [
+       PKG_CHECK_MODULES(
+          [X11],
+          [x11],
+          [have_x11=yes
+           PKG_CONFIG_REQUIRES="${PKG_CONFIG_REQUIRES}[,]x11"
+           X_LIBS="$X_LIBS $X11_LIBS"
+           LIBS="$LIBS $X11_LIBS"
+           X_CFLAGS="$X_CFLAGS $X11_CFLAGS"
+           CPPFLAGS="$CPPFLAGS $X11_CFLAGS"
+           PKG_CHECK_MODULES(
+              [XT],
+              [xt],
+              [have_xt=yes
+               PKG_CONFIG_REQUIRES="${PKG_CONFIG_REQUIRES}[,]xt"
+               X_LIBS="$X_LIBS $XT_LIBS"
+               LIBS="$LIBS $XT_LIBS"
+               X_CFLAGS="$X_CFLAGS $XT_CFLAGS"
+               CPPFLAGS="$CPPFLAGS $XT_CFLAGS"
+              ]
+           )
+          ]
+       )
+      ]
+)
+
 COMPILE_PGDISP=no
 AS_IF([ test "x$no_x" = "x"],
       [

From 1f0fb2b5bce590b68ff7e522d720e2e5b206e40c Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Sun, 24 Oct 2021 00:07:47 -0400
Subject: [PATCH 02/13] Add *.o to .gitignore

---
 .gitignore | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.gitignore b/.gitignore
index 935b0466c6..622a38c859 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@ pgplot_link_adam
 .libs
 *.la
 *.lo
+*.o
 Makefile
 Makefile.in
 acinclude.m4

From 4c985aba60d20a4e59297859f77169a32bb6e39a Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Sun, 24 Oct 2021 00:08:07 -0400
Subject: [PATCH 03/13] GHA: Use same make step for all platforms

---
 .github/workflows/ci.yml | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 09c8d9b3b5..7834be0f88 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -33,10 +33,5 @@ jobs:
         run: |
           sh ./configure
       - name: Make
-        if: runner.os == 'Linux'
         run: |
           make
-      - name: Make
-        if: runner.os == 'macOS'
-        run: |
-          make LDFLAGS="$(pkg-config --libs --static libpng xt)"

From 54830264dac7a0de0e64a3147b5bb4468c8e5a73 Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Sun, 24 Oct 2021 00:55:07 -0400
Subject: [PATCH 04/13] Look for modules to support -lSM -lICE

Because these are listed in `$X_PRE_LIBS`.
---
 configure.ac | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 600bfd23d4..ef0a625272 100644
--- a/configure.ac
+++ b/configure.ac
@@ -88,7 +88,9 @@ AS_IF([ test "x$no_x" = "x"],
            CPPFLAGS="$CPPFLAGS $X11_CFLAGS"
            PKG_CHECK_MODULES(
               [XT],
-              [xt],
+              dnl have sm and ice listed here so that contents of $X_PRE_LIBS
+              dnl can be found
+              [sm ice xt],
               [have_xt=yes
                PKG_CONFIG_REQUIRES="${PKG_CONFIG_REQUIRES}[,]xt"
                X_LIBS="$X_LIBS $XT_LIBS"

From 55e39f8de461fc84faea2db3bce75f7804e185ce Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Sun, 24 Oct 2021 01:10:48 -0400
Subject: [PATCH 05/13] pkg-config: xt already depends on x11, so add once

---
 configure.ac | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/configure.ac b/configure.ac
index ef0a625272..20c03858cf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -82,10 +82,6 @@ AS_IF([ test "x$no_x" = "x"],
           [x11],
           [have_x11=yes
            PKG_CONFIG_REQUIRES="${PKG_CONFIG_REQUIRES}[,]x11"
-           X_LIBS="$X_LIBS $X11_LIBS"
-           LIBS="$LIBS $X11_LIBS"
-           X_CFLAGS="$X_CFLAGS $X11_CFLAGS"
-           CPPFLAGS="$CPPFLAGS $X11_CFLAGS"
            PKG_CHECK_MODULES(
               [XT],
               dnl have sm and ice listed here so that contents of $X_PRE_LIBS
@@ -93,10 +89,18 @@ AS_IF([ test "x$no_x" = "x"],
               [sm ice xt],
               [have_xt=yes
                PKG_CONFIG_REQUIRES="${PKG_CONFIG_REQUIRES}[,]xt"
+               dnl XT_* has xt + x11 because xt depends on x11
                X_LIBS="$X_LIBS $XT_LIBS"
                LIBS="$LIBS $XT_LIBS"
                X_CFLAGS="$X_CFLAGS $XT_CFLAGS"
                CPPFLAGS="$CPPFLAGS $XT_CFLAGS"
+              ],
+              [
+               dnl only have x11
+               X_LIBS="$X_LIBS $X11_LIBS"
+               LIBS="$LIBS $X11_LIBS"
+               X_CFLAGS="$X_CFLAGS $X11_CFLAGS"
+               CPPFLAGS="$CPPFLAGS $X11_CFLAGS"
               ]
            )
           ]

From f2929d2ae85f7beee7a325f86aee324e74c91dc7 Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Sun, 24 Oct 2021 01:17:13 -0400
Subject: [PATCH 06/13] GHA: Add debugging of pkg-config files

---
 .github/workflows/ci.yml | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 7834be0f88..980059ae15 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -35,3 +35,6 @@ jobs:
       - name: Make
         run: |
           make
+      - name: Show pkg-config files
+        run: |
+          head -n100 *.pc

From 82d5f202d50a4f74fd9b2b36256a712af5fc6b42 Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Sat, 30 Oct 2021 00:20:34 -0400
Subject: [PATCH 07/13] GHA: add builds to check disabling X11

---
 .github/workflows/ci.yml | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 980059ae15..13e5ba95de 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -12,6 +12,10 @@ jobs:
       fail-fast: false
       matrix:
         os: [ubuntu-latest,macos-latest]
+        # without-x:
+        # - false: automatically configure X11
+        # - true: disable building with X11 (use --without-x option)
+        without-x: [false,true]
     steps:
       - uses: actions/checkout@v2
       - name: Pre-reqs (apt)
@@ -30,8 +34,9 @@ jobs:
         run: |
           sh ./bootstrap
       - name: Configure
-        run: |
+        run:
           sh ./configure
+            ${{ fromJSON('[ "", "--without-x" ]')[ matrix.without-x ] }}
       - name: Make
         run: |
           make

From cb12d209307bca7d71d2e1a605f489bdc0e85ccc Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Fri, 5 Nov 2021 12:12:59 -0400
Subject: [PATCH 08/13] Use pkg-config for x11 in addition to AC_PATH_XTRA

---
 configure.ac | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 20c03858cf..4efa3f4b80 100644
--- a/configure.ac
+++ b/configure.ac
@@ -75,12 +75,13 @@ drivers="exdriv.lo pgdriv.lo cadriv.lo hgdriv.lo lxdriv.lo  vtdriv.lo hidriv.lo
 dnl   Not all platforms have X
 have_x11=no
 have_xt=no
-AS_IF([ test "x$no_x" = "x"],
+AS_IF([ test "$with_x" != no ],
       [
        PKG_CHECK_MODULES(
           [X11],
           [x11],
           [have_x11=yes
+           no_x=""
            PKG_CONFIG_REQUIRES="${PKG_CONFIG_REQUIRES}[,]x11"
            PKG_CHECK_MODULES(
               [XT],

From bb4bc010232bad211c641384b2d1239364012353 Mon Sep 17 00:00:00 2001
From: "Zaki Mughal [sivoais]" <zaki.mughal@gmail.com>
Date: Sun, 2 Jan 2022 19:06:45 -0500
Subject: [PATCH 09/13] Test $no_x as well to detect AC_PATH_XTRA failure

Co-authored-by: djerius <djerius@cfa.harvard.edu>
---
 configure.ac | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 4efa3f4b80..14bc1fe785 100644
--- a/configure.ac
+++ b/configure.ac
@@ -75,7 +75,22 @@ drivers="exdriv.lo pgdriv.lo cadriv.lo hgdriv.lo lxdriv.lo  vtdriv.lo hidriv.lo
 dnl   Not all platforms have X
 have_x11=no
 have_xt=no
-AS_IF([ test "$with_x" != no ],
+dnl	+--------+------------------------+----------+----------+--------+-----+
+dnl	|Variable|      AC_PATH_XTRA      |-without-x|-with-x=no|-with-x | --x |
+dnl	+--------+------------+-----------+----------+----------+--------+-----+
+dnl	|        |  success   |  failure  |          |          |        |     |
+dnl	+--------+------------+-----------+----------+----------+--------+-----+
+dnl	|  no_x  |     ''     |   'yes'   |  'yes'   |          |        |     |
+dnl	+--------+------------+-----------+----------+----------+--------+-----+
+dnl	| with_x |            |           |   'no'   |   'no'   | 'yes'  |'yes'|
+dnl	+--------+------------+-----------+----------+----------+--------+-----+
+dnl	Notes:
+dnl     1) AC_PATH_XTRA runs first and modifies no_x in the presence of -without-x
+dnl     2) If --with-x is not specified, AC_PATH_XTRA assumes with_x='yes'
+dnl
+dnl     Check pkg-config if the user did not forbid X and AC_PATH_XTRA failed to find it
+AS_IF([ test "x$with_x" != xno -a "x$no_x" != x ],
+
       [
        PKG_CHECK_MODULES(
           [X11],

From 98f172312bad64906735d245094e8bbe6bb940ff Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Wed, 5 Jan 2022 01:49:49 -0500
Subject: [PATCH 10/13] Set macOS LIBRARY_PATH for Homebrew

Per the macOS ld(1) man page <https://www.unix.com/man-page/OSX/1/ld/>,

> The default library search path is /usr/lib then /usr/local/lib.

When `HOMEBREW_PREFIX=/usr/local`, this is where the `.dylib` files for
X11 are symlinked by Homebrew.

When `AC_PATH_XTRA` is called, it uses `/usr/bin/gcc` (Apple clang) and
since the libraries are found in `/usr/local/lib`, it does not need to
specify any `-L` paths.

However when `libtool` is used to call Homebrew `gfortran`, it sets
the flag `-syslibroot` which per the man page for ld(1):

> The -syslibroot option will prepend a prefix to all search paths.

and this means that it no longer looks under `/usr/local/lib` by
default. To fix this, add the default path back by setting the
`LIBRARY_PATH` environment variable.
---
 .github/workflows/ci.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 13e5ba95de..13772ff3ab 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -30,6 +30,7 @@ jobs:
           brew install automake \
             libpng \
             libxt
+          echo "LIBRARY_PATH=$(brew --prefix)/lib${LIBRARY_PATH:+:${LIBRARY_PATH}}" >> $GITHUB_ENV
       - name: Bootstrap
         run: |
           sh ./bootstrap

From e8a55e2287231062a0b3816d1215f8ec2b56451b Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Wed, 12 Jan 2022 20:25:10 -0600
Subject: [PATCH 11/13] GHA: Test Homebrew linking via pkg-config only

This is to test that if libraries are not symlinked into the Homebrew
prefix (here by using `brew unlink`), then `autotool` will use
`pkg-config` to find the libraries.
---
 .github/workflows/ci.yml | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 13772ff3ab..48818730eb 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -16,6 +16,12 @@ jobs:
         # - false: automatically configure X11
         # - true: disable building with X11 (use --without-x option)
         without-x: [false,true]
+        # used for macos runner
+        homebrew-unlink: [ false ]
+        include:
+          - os: macos-latest
+            without-x: false
+            homebrew-unlink: true
     steps:
       - uses: actions/checkout@v2
       - name: Pre-reqs (apt)
@@ -30,7 +36,18 @@ jobs:
           brew install automake \
             libpng \
             libxt
-          echo "LIBRARY_PATH=$(brew --prefix)/lib${LIBRARY_PATH:+:${LIBRARY_PATH}}" >> $GITHUB_ENV
+      - name: Prepare for linking with Homebrew
+        if: runner.os == 'macOS'
+        run: |
+          if ! ${{ matrix.homebrew-unlink }}; then
+            echo "Using Homebrew prefix /usr/local/lib for linking via LIBRARY_PATH in order to keep the default search path."
+            echo "LIBRARY_PATH=$(brew --prefix)/lib${LIBRARY_PATH:+:${LIBRARY_PATH}}" >> $GITHUB_ENV
+          else
+            echo "Unlinking libraries from Homebrew prefix so that pkg-config must be used."
+            brew unlink \
+              libpng \
+              libxt libice libsm libx11
+          fi
       - name: Bootstrap
         run: |
           sh ./bootstrap

From 76f12beba69500c18ede49a33acab27f400636bf Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Sat, 15 Jan 2022 01:52:10 -0500
Subject: [PATCH 12/13] Mark when X11 is not found via pkg-config

---
 configure.ac | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/configure.ac b/configure.ac
index 14bc1fe785..aefab6b9f3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -119,6 +119,9 @@ AS_IF([ test "x$with_x" != xno -a "x$no_x" != x ],
                CPPFLAGS="$CPPFLAGS $X11_CFLAGS"
               ]
            )
+          ],
+          [
+           dnl no x11 via pkg-config either
           ]
        )
       ]

From 70ce353d6810541ffda6e1f75857c2c82ce9b6fe Mon Sep 17 00:00:00 2001
From: Zakariyya Mughal <zaki.mughal@gmail.com>
Date: Sat, 15 Jan 2022 02:12:29 -0500
Subject: [PATCH 13/13] Add Homebrew formulae to PKG_CONFIG_PATH

---
 .github/workflows/ci.yml | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 48818730eb..64d92faa76 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -44,9 +44,12 @@ jobs:
             echo "LIBRARY_PATH=$(brew --prefix)/lib${LIBRARY_PATH:+:${LIBRARY_PATH}}" >> $GITHUB_ENV
           else
             echo "Unlinking libraries from Homebrew prefix so that pkg-config must be used."
-            brew unlink \
-              libpng \
-              libxt libice libsm libx11
+            FORMULAE_TO_UNLINK="libpng libxt libice libsm libx11"
+            brew unlink $FORMULAE_TO_UNLINK
+            PKG_CONFIG_PATH_FOR_FORMULAE=$( brew ls $FORMULAE_TO_UNLINK \
+              | grep /pkgconfig/ | xargs -n1 dirname \
+              | sort -u | tr '\n' ':' | sed 's/:$//' )
+            echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH_FOR_FORMULAE${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}" >> $GITHUB_ENV
           fi
       - name: Bootstrap
         run: |