From 239ac4e516ab50b8527598f5b1be8cf0232f239a Mon Sep 17 00:00:00 2001 From: Simon Wright Date: Mon, 21 Aug 2023 16:14:46 +0100 Subject: [PATCH] MacOS; export PATH variables for includes and libraries. (#1420) * MacOS; export PATH variables for includes and libraries. Homebrew and MacPorts install include files and libraries in places where GCC won't look by default. GCC will use these environment variables if set: C_INCLUDE_PATH for C includes CPLUS_INCLUDE_PATH for C++ includes LIBRARY_PATH for libraries Both of the distribution managers place (symbolic links to) include files in ${top_level}/include and libraries in ${top_level}/lib. For Homebrew on Intel silicon, top_level is normally /usr/local. For Homebrew on Apple silicon, top_level is normally /opt/homebrew. For MacPorts, top_level is normally /opt/local * src/alire/alire-platforms-current.ads (Load_Environment): add note on macOS use. * src/alire/os_macos/alire-platforms-current__macos.adb (context): added Alire.Environment (was limited), Ada.Directories. (Brew_Access): new. (Homebrew_Present): if Brew_Access is not null. (Detected_Distribution): made into an expression function. (Containing_Containing_Dir): new, used in Distribution_Root. (Distribution_Root): reworked. (Load_Environment): if either distribution is present, arrange to export the environment variables to suit. * Update testsuite to match new macOS distribution detection. * testsuite/drivers/helpers.py (distribution): if on macOS, check whether the distribution management tool is on the PATH. We used to check for the environment variable HOMEBREW_PREFIX, but users don't have to arrange for this to be set in order to run Homebrew. First, if 'brew' is found, the distribution is Homebrew. If not and 'port' is found, the distribution is MacPorts. Otherwise, the distribution is unknown. * In the macOS CI workflow, run the test script once only. * .github/workflows/ci-macos.yml (Run test script): remove the second call, which set up HOMEBREW_PREFIX (now no longer used by alr), and remove the note '(without Homebrew)' in the first. --- .github/workflows/ci-macos.yml | 11 +--- src/alire/alire-platforms-current.ads | 3 +- .../alire-platforms-current__macos.adb | 52 +++++++++++++------ testsuite/drivers/helpers.py | 6 ++- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/.github/workflows/ci-macos.yml b/.github/workflows/ci-macos.yml index db919969..5bc89ee2 100644 --- a/.github/workflows/ci-macos.yml +++ b/.github/workflows/ci-macos.yml @@ -33,22 +33,13 @@ jobs: with: python-version: '3.x' - - name: Run test script (without Homebrew) + - name: Run test script run: scripts/ci-github.sh shell: bash env: BRANCH: ${{ github.base_ref }} INDEX: "" - - name: Run test script (with Homebrew) - run: | - eval $(brew shellenv) - scripts/ci-github.sh - shell: bash - env: - BRANCH: ${{ github.base_ref }} - INDEX: "" - - name: Upload binaries uses: actions/upload-artifact@v2 with: diff --git a/src/alire/alire-platforms-current.ads b/src/alire/alire-platforms-current.ads index 8028ad70..5baae4b9 100644 --- a/src/alire/alire-platforms-current.ads +++ b/src/alire/alire-platforms-current.ads @@ -19,7 +19,8 @@ package Alire.Platforms.Current is procedure Load_Environment (Ctx : in out Alire.Environment.Context); -- Set environment variables from the platform. Used by Windows to - -- initialize msys2 environment. + -- initialize msys2 environment, and by macOS to initialize which, + -- if either, of the Homebrew or MacPorts environment. ----------------------- -- Self identification diff --git a/src/alire/os_macos/alire-platforms-current__macos.adb b/src/alire/os_macos/alire-platforms-current__macos.adb index e6799136..faa45aef 100644 --- a/src/alire/os_macos/alire-platforms-current__macos.adb +++ b/src/alire/os_macos/alire-platforms-current__macos.adb @@ -1,19 +1,23 @@ +with Alire.Environment; with Alire.OS_Lib; +with Ada.Directories; with GNAT.OS_Lib; package body Alire.Platforms.Current is -- macOS implementation + use type GNAT.OS_Lib.String_Access; + -- Homebrew - Homebrew_Prefix : constant String - := Alire.OS_Lib.Getenv ("HOMEBREW_PREFIX", ""); - Homebrew_Present : constant Boolean := Homebrew_Prefix /= ""; + + Brew_Access : constant GNAT.OS_Lib.String_Access + := GNAT.OS_Lib.Locate_Exec_On_Path ("brew"); + Homebrew_Present : constant Boolean := Brew_Access /= null; -- MacPorts Port_Access : constant GNAT.OS_Lib.String_Access := GNAT.OS_Lib.Locate_Exec_On_Path ("port"); - use type GNAT.OS_Lib.String_Access; Macports_Present : constant Boolean := Port_Access /= null; ------------------ @@ -21,26 +25,27 @@ package body Alire.Platforms.Current is ------------------ function Detected_Distribution return Platforms.Distributions is - begin - if Homebrew_Present - then - return Homebrew; - elsif Macports_Present then - return Macports; - else - return Distro_Unknown; - end if; - end Detected_Distribution; + (if Homebrew_Present + then Homebrew + elsif Macports_Present + then Macports + else Distro_Unknown); ----------------------- -- Distribution_Root -- ----------------------- + function Containing_Containing_Dir + (Executable : not null GNAT.OS_Lib.String_Access) return String + is (Ada.Directories.Containing_Directory + (Ada.Directories.Containing_Directory + (Executable.all))); + function Distribution_Root return Absolute_Path is (if Homebrew_Present - then Homebrew_Prefix + then Containing_Containing_Dir (Brew_Access) elsif Macports_Present - then "/opt/local" + then Containing_Containing_Dir (Port_Access) else "/"); ---------------------- @@ -48,7 +53,20 @@ package body Alire.Platforms.Current is ---------------------- procedure Load_Environment (Ctx : in out Alire.Environment.Context) - is null; + is + Root : constant Absolute_Path := Distribution_Root; + begin + -- Set up paths if a distribution manager is present + if Homebrew_Present then + Ctx.Append ("C_INCLUDE_PATH", Root & "/include", "homebrew"); + Ctx.Append ("CPLUS_INCLUDE_PATH", Root & "/include", "homebrew"); + Ctx.Append ("LIBRARY_PATH", Root & "/lib", "homebrew"); + elsif Macports_Present then + Ctx.Append ("C_INCLUDE_PATH", Root & "/include", "macports"); + Ctx.Append ("CPLUS_INCLUDE_PATH", Root & "/include", "macports"); + Ctx.Append ("LIBRARY_PATH", Root & "/lib", "macports"); + end if; + end Load_Environment; ---------------------- -- Operating_System -- diff --git a/testsuite/drivers/helpers.py b/testsuite/drivers/helpers.py index c70bd565..8112c09f 100644 --- a/testsuite/drivers/helpers.py +++ b/testsuite/drivers/helpers.py @@ -97,8 +97,10 @@ def distribution(): return 'DISTRO_UNKNOWN' elif on_macos(): - if os.environ.get('HOMEBREW_PREFIX'): + if shutil.which('brew'): return 'HOMEBREW' + elif shutil.which('port'): + return 'MACPORTS' else: return 'DISTRO_UNKNOWN' @@ -270,4 +272,4 @@ class FileLock(): # Release the file lock import fcntl fcntl.flock(self.lock_file.fileno(), fcntl.LOCK_UN) - self.lock_file.close() \ No newline at end of file + self.lock_file.close() -- 2.39.5