From 906095fcb3f018331c46adef6ab2df391acffbcf Mon Sep 17 00:00:00 2001 From: Fabien Chouteau Date: Wed, 4 Nov 2020 21:32:52 +0100 Subject: [PATCH] Alire.Environment: load env context from the platform (#608) * Alire.Environment: sort env list for predictable output * Alire.Environment: load env context from the platform For the moment this is only used on Windows for msys2. Before this patch the msys2 env was not included in `alr print` which means that the context was not complete. * Alr.Platforms.Windows: do not install msys2 if pacman already in PATH This makes alr usable when the user is already in an msys2 context. And it alse makes possible to have an alire package in msys2. --- src/alire/alire-environment.adb | 7 +++++ src/alire/alire-environment.ads | 9 ++++++ src/alire/alire-platform.ads | 4 +++ src/alire/os_linux/alire-platform.adb | 7 +++++ src/alire/os_macos/alire-platform.adb | 7 +++++ src/alire/os_windows/alire-platform.adb | 30 +++++++++++++++++++ src/alr/os_windows/alr-platforms-windows.adb | 23 +++++++++----- testsuite/tests/printenv/basic/test.py | 7 +++-- testsuite/tests/printenv/linked-paths/test.py | 7 +++-- .../tests/printenv/with-external/test.py | 5 +++- 10 files changed, 93 insertions(+), 13 deletions(-) diff --git a/src/alire/alire-environment.adb b/src/alire/alire-environment.adb index 068f6707..cc901769 100644 --- a/src/alire/alire-environment.adb +++ b/src/alire/alire-environment.adb @@ -13,6 +13,7 @@ with Alire.Solutions; with Alire.Utils; with Alire.Environment.Formatting; with Alire.Utils.TTY; +with Alire.Platform; with GNAT.IO; @@ -87,6 +88,9 @@ package body Alire.Environment is Solution : constant Solutions.Solution := Root.Solution; begin + -- Load platform environment + Alire.Platform.Load_Environment (This); + -- Warnings when setting up an incomplete environment if not Solution.Is_Complete then @@ -336,6 +340,9 @@ package body Alire.Environment is Index := Index + 1; end loop; + -- Sort results for predictable output + Sort (Result); + return Result; end Compile; diff --git a/src/alire/alire-environment.ads b/src/alire/alire-environment.ads index 43c5ae2b..66d54a24 100644 --- a/src/alire/alire-environment.ads +++ b/src/alire/alire-environment.ads @@ -9,6 +9,7 @@ private with Ada.Strings.Unbounded.Hash; private with Ada.Containers.Vectors; private with Ada.Containers.Hashed_Maps; private with Alire.Properties.Environment; +private with Ada.Containers.Generic_Array_Sort; package Alire.Environment is @@ -54,8 +55,16 @@ private Value : Ada.Strings.Unbounded.Unbounded_String; end record; + function "<" (Left, Right : Var) return Boolean + is (Ada.Strings.Unbounded."<" (Left.Key, Right.Key)); + type Var_Array is array (Natural range <>) of Var; + procedure Sort is new Ada.Containers.Generic_Array_Sort + (Index_Type => Natural, + Element_Type => Var, + Array_Type => Var_Array); + function Compile (This : Context) return Var_Array; -- Return an array of environment variable key/value built from the -- variables previously loaded and defined in the context. diff --git a/src/alire/alire-platform.ads b/src/alire/alire-platform.ads index cb66c6ae..ab6c2f57 100644 --- a/src/alire/alire-platform.ads +++ b/src/alire/alire-platform.ads @@ -1,4 +1,5 @@ with Alire.Platforms; +with Alire.Environment; package Alire.Platform is @@ -15,6 +16,9 @@ package Alire.Platform is function Distribution_Root return Absolute_Path; -- Root directory of the distribution + procedure Load_Environment (Ctx : in out Alire.Environment.Context); + -- Set environment variables from the platform + -------------------------------- -- Portable derived utilities -- -------------------------------- diff --git a/src/alire/os_linux/alire-platform.adb b/src/alire/os_linux/alire-platform.adb index 80e2a048..2d76936f 100644 --- a/src/alire/os_linux/alire-platform.adb +++ b/src/alire/os_linux/alire-platform.adb @@ -74,4 +74,11 @@ package body Alire.Platform is function Distribution_Root return Absolute_Path is ("/"); + ---------------------- + -- Load_Environment -- + ---------------------- + + procedure Load_Environment (Ctx : in out Alire.Environment.Context) + is null; + end Alire.Platform; diff --git a/src/alire/os_macos/alire-platform.adb b/src/alire/os_macos/alire-platform.adb index 40a2c4b4..259b0e12 100644 --- a/src/alire/os_macos/alire-platform.adb +++ b/src/alire/os_macos/alire-platform.adb @@ -29,4 +29,11 @@ package body Alire.Platform is function Distribution_Root return Absolute_Path is ("/"); + ---------------------- + -- Load_Environment -- + ---------------------- + + procedure Load_Environment (Ctx : in out Alire.Environment.Context) + is null; + end Alire.Platform; diff --git a/src/alire/os_windows/alire-platform.adb b/src/alire/os_windows/alire-platform.adb index a9536296..abbc4ee3 100644 --- a/src/alire/os_windows/alire-platform.adb +++ b/src/alire/os_windows/alire-platform.adb @@ -106,4 +106,34 @@ package body Alire.Platform is end case; end Distribution_Root; + ---------------------- + -- Load_Environment -- + ---------------------- + + procedure Load_Environment (Ctx : in out Alire.Environment.Context) is + begin + case Distribution is + + when Platforms.Msys2 => + declare + Root : constant Absolute_Path := Detect_Msys2_Root; + begin + + Ctx.Append ("PATH", Root / "mingw64" / "bin", "msys2"); + Ctx.Append ("PATH", Root / "usr" / "bin", "msys2"); + Ctx.Append ("PATH", Root / "usr" / "local" / "bin", "msys2"); + + Ctx.Append ("LIBRARY_PATH", Root / "mingw64" / "lib", "msys2"); + + Ctx.Append ("C_INCLUDE_PATH", Root / "mingw64" / "include", + "msys2"); + end; + + when others => + null; + + end case; + + end Load_Environment; + end Alire.Platform; diff --git a/src/alr/os_windows/alr-platforms-windows.adb b/src/alr/os_windows/alr-platforms-windows.adb index ad876ccb..168d82a7 100644 --- a/src/alr/os_windows/alr-platforms-windows.adb +++ b/src/alr/os_windows/alr-platforms-windows.adb @@ -1,5 +1,7 @@ with Ada.Directories; +with GNAT.OS_Lib; + with Alire; with Alire.Origins.Deployers; with Alire.Platform; @@ -33,16 +35,10 @@ package body Alr.Platforms.Windows is procedure Set_Msys2_Env (Install_Dir : Alire.Absolute_Path) is begin - Setenv ("PATH", Install_Dir / "mingw64" / "bin" & - ";" & Install_Dir / "usr" / "bin" & + -- Change PATH to have msys2 binaries available (unzip, curl, git, etc.) + Setenv ("PATH", Install_Dir / "usr" / "bin" & ";" & Install_Dir / "usr" / "local" / "bin" & ";" & Getenv ("PATH")); - - Setenv ("LIBRARY_PATH", Install_Dir / "mingw64" / "lib" & - ";" & Getenv ("LIBRARY_PATH")); - - Setenv ("C_INCLUDE_PATH", Install_Dir / "mingw64" / "include" & - ";" & Getenv ("C_INCLUDE_PATH")); end Set_Msys2_Env; ---------------------------------- @@ -170,7 +166,18 @@ package body Alr.Platforms.Windows is Cfg_Install_Dir : constant String := Cfg.Get ("msys2.install_dir", Default_Install_Dir); + + Pacman : constant String := + Alire.OS_Lib.Subprocess.Locate_In_Path ("pacman"); + begin + if Pacman /= "" then + -- pacman already in PATH, no need to install msys2 + Set_Msys2_Env (GNAT.OS_Lib.Normalize_Pathname + (Ada.Directories.Containing_Directory + (Pacman) / ".." / "..")); + return; + end if; if not Alire.Check_Absolute_Path (Cfg_Install_Dir) then -- This error is recoverable as msys2 is not required for alr to diff --git a/testsuite/tests/printenv/basic/test.py b/testsuite/tests/printenv/basic/test.py index 3173ee8b..1dfd8cb9 100644 --- a/testsuite/tests/printenv/basic/test.py +++ b/testsuite/tests/printenv/basic/test.py @@ -33,9 +33,12 @@ for i, path in enumerate(expected_gpr_path): expected_gpr_path = os.pathsep.join(expected_gpr_path) -assert_match('export TEST_GPR_EXTERNAL="gpr_ext_B"\n' +assert_match('export ALIRE="True"\n' + '.*' 'export GPR_PROJECT_PATH="' + expected_gpr_path + '"\n' - 'export ALIRE="True"\n', + '.*' + 'export TEST_GPR_EXTERNAL="gpr_ext_B"\n' + '.*', p.out, flags=re.S) diff --git a/testsuite/tests/printenv/linked-paths/test.py b/testsuite/tests/printenv/linked-paths/test.py index 0e80d262..405004ef 100644 --- a/testsuite/tests/printenv/linked-paths/test.py +++ b/testsuite/tests/printenv/linked-paths/test.py @@ -33,8 +33,11 @@ expected_gpr_path = os.pathsep.join(expected_gpr_path) # Check paths are proper (base and one extra nested) p = run_alr("printenv") -assert_match(('export GPR_PROJECT_PATH="' + expected_gpr_path + '"\n' + - 'export ALIRE="True"\n').replace('/', re.escape(dir_separator())), +assert_match(('.*' + 'export ALIRE="True"\n' + '.*' + 'export GPR_PROJECT_PATH="' + expected_gpr_path + '"\n' + '.*').replace('/', re.escape(dir_separator())), p.out) diff --git a/testsuite/tests/printenv/with-external/test.py b/testsuite/tests/printenv/with-external/test.py index 4b4cdf04..ab8072ea 100644 --- a/testsuite/tests/printenv/with-external/test.py +++ b/testsuite/tests/printenv/with-external/test.py @@ -30,8 +30,11 @@ else: # Check the printenv output assert_match('warn: Generating incomplete environment' # Note: this warning is ' because of missing dependencies\n' # via stderr so it's OK + '.*' + 'export ALIRE="True"\n' + '.*' 'export GPR_PROJECT_PATH="' + expected_gpr_path + '"\n' - 'export ALIRE="True"\n', + '.*', p.out) print('SUCCESS') -- 2.39.5