From de1d655b94b5466183bfcf0977aa01fb0376eb87 Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Thu, 23 Jun 2022 17:45:51 +0200 Subject: [PATCH] Keep `last_build_profile` in sync with profile requested in manifest (#1049) * Sync internal last_build_profile with manifest * Test for the new behavior * Replace field in Root with a pointer and manual management * Fix warnings in gnatcoll-slim * Fix new warnings unearthed by GNAT 12.1 Co-authored-by: GHA --- src/alire/alire-crate_configuration.adb | 17 +++++++++ src/alire/alire-crate_configuration.ads | 14 ++++++- src/alire/alire-roots.adb | 34 ++++++++++++++++- src/alire/alire-roots.ads | 25 ++++++++++++ src/alr/alr-commands-build.adb | 19 +++++++++- .../local-index/my_index/index/index.toml | 2 +- .../tests/build_profile/last_profile/test.py | 38 +++++++++++++++++++ .../build_profile/last_profile/test.yaml | 1 + 8 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 testsuite/tests/build_profile/last_profile/test.py create mode 100644 testsuite/tests/build_profile/last_profile/test.yaml diff --git a/src/alire/alire-crate_configuration.adb b/src/alire/alire-crate_configuration.adb index 5dda9b90..e3169a88 100644 --- a/src/alire/alire-crate_configuration.adb +++ b/src/alire/alire-crate_configuration.adb @@ -64,6 +64,15 @@ package body Alire.Crate_Configuration is package Crate_Name_Vect is new Ada.Containers.Indefinite_Vectors (Natural, Crate_Name); + ------------------- + -- Build_Profile -- + ------------------- + + function Build_Profile (This : Global_Config; + Crate : Crate_Name) + return Utils.Switches.Profile_Kind + is (This.Profile_Map (Crate)); + ----------------------- -- Make_Release_Vect -- ----------------------- @@ -417,6 +426,14 @@ package body Alire.Crate_Configuration is TIO.Close (File); end Generate_Ada_Config; + -------------- + -- Is_Valid -- + -------------- + + function Is_Valid (This : Global_Config) return Boolean + is (not This.Profile_Map.Is_Empty); + -- Because at a minimum it must contain the root crate profile + --------------------------- -- Pretty_Print_Switches -- --------------------------- diff --git a/src/alire/alire-crate_configuration.ads b/src/alire/alire-crate_configuration.ads index 0302ddf0..0cc562c8 100644 --- a/src/alire/alire-crate_configuration.ads +++ b/src/alire/alire-crate_configuration.ads @@ -12,6 +12,8 @@ private with Ada.Containers.Indefinite_Ordered_Maps; package Alire.Crate_Configuration is + -- Types used to store build profiles/switches and declared variables + Default_Root_Build_Profile : constant Utils.Switches.Profile_Kind := Utils.Switches.Development; @@ -21,7 +23,15 @@ package Alire.Crate_Configuration is Root_Build_Profile : Utils.Switches.Profile_Kind := Default_Root_Build_Profile; - type Global_Config is tagged limited private; + type Global_Config is tagged private; + + function Is_Valid (This : Global_Config) return Boolean; + -- False until Load is called + + function Build_Profile (This : Global_Config; + Crate : Crate_Name) + return Utils.Switches.Profile_Kind + with Pre => This.Is_Valid; procedure Load (This : in out Global_Config; Root : in out Alire.Roots.Root); @@ -65,7 +75,7 @@ private is new Ada.Containers.Indefinite_Ordered_Maps (Crate_Name, Alire.Utils.Switches.Switch_List); - type Global_Config is tagged limited record + type Global_Config is tagged record Map : Config_Maps.Map; Profile_Map : Profile_Maps.Map; diff --git a/src/alire/alire-roots.adb b/src/alire/alire-roots.adb index 5e0b4c0d..d9cc44b3 100644 --- a/src/alire/alire-roots.adb +++ b/src/alire/alire-roots.adb @@ -1,5 +1,6 @@ +with Ada.Unchecked_Deallocation; + with Alire.Conditional; -with Alire.Crate_Configuration; with Alire.Dependencies.Containers; with Alire.Directories; with Alire.Environment; @@ -246,6 +247,21 @@ package body Alire.Roots is end return; end Direct_Withs; + ------------------- + -- Configuration -- + ------------------- + + function Configuration (This : in out Root) + return Crate_Configuration.Global_Config + is + begin + if not This.Configuration.Is_Valid then + Crate_Configuration.Load (This.Configuration.all, This); + end if; + + return This.Configuration.all; + end Configuration; + ---------------------------- -- Generate_Configuration -- ---------------------------- @@ -817,6 +833,7 @@ package body Alire.Roots is Path => +Path, Release => Releases.Containers.To_Release_H (R), Cached_Solution => <>, + Configuration => <>, Pins => <>, Lockfile => <>, Manifest => <>); @@ -1370,4 +1387,19 @@ package body Alire.Roots is Root => Releases.Containers.Optional_Releases.Unit (Release (This))); end Traverse; + overriding + procedure Adjust (This : in out Root) is + begin + This.Configuration := + new Crate_Configuration.Global_Config'(This.Configuration.all); + end Adjust; + + overriding + procedure Finalize (This : in out Root) is + procedure Free is new Ada.Unchecked_Deallocation + (Crate_Configuration.Global_Config, Global_Config_Access); + begin + Free (This.Configuration); + end Finalize; + end Alire.Roots; diff --git a/src/alire/alire-roots.ads b/src/alire/alire-roots.ads index 15bea361..af70b4e3 100644 --- a/src/alire/alire-roots.ads +++ b/src/alire/alire-roots.ads @@ -5,6 +5,7 @@ private with Ada.Finalization; with AAA.Strings; with Alire.Containers; +with Alire.Crate_Configuration; with Alire.Dependencies.States; limited with Alire.Environment; private with Alire.Lockfiles; @@ -217,6 +218,11 @@ package Alire.Roots is -- all releases in the solution (even those not built). Returns True on -- successful build. + function Configuration (This : in out Root) + return Crate_Configuration.Global_Config; + -- Returns the global configuration for the root and dependencies. This + -- configuration is computed the first time it is requested. + procedure Generate_Configuration (This : in out Root); -- Generate or re-generate the crate configuration files @@ -251,12 +257,25 @@ private Load => Load_Solution, Write => Write_Solution); + type Global_Config_Access is access Crate_Configuration.Global_Config; + type Root is new Ada.Finalization.Controlled with record Environment : Properties.Vector; Path : UString; Release : Releases.Containers.Release_H; Cached_Solution : Cached_Solutions.Cache; + Configuration : Global_Config_Access := + new Crate_Configuration.Global_Config; + -- Variables and Build profiles configuration + -- This pointer and the consequently necessary Adjust and Finalize are + -- here just because with some compiler versions, the direct use of the + -- pointee type results in exceptions during finalization of the main + -- program (starting in the guts at b__ generated procedures, so hard to + -- debug). It may be worthwhile to try to remove this with future GNAT + -- versions. As a data point, with the stock Ubuntu 20.04 GNAT (9.3), + -- there is no problem. + Pins : Solutions.Solution; -- Closure of all pins that are recursively found @@ -265,6 +284,12 @@ private Lockfile : Unbounded_Absolute_Path; end record; + overriding + procedure Adjust (This : in out Root); + + overriding + procedure Finalize (This : in out Root); + -- Support for editable roots begins here. These are not expected to be -- directly useful to clients, so better kept them under wraps. diff --git a/src/alr/alr-commands-build.adb b/src/alr/alr-commands-build.adb index ced8434e..eef13aca 100644 --- a/src/alr/alr-commands-build.adb +++ b/src/alr/alr-commands-build.adb @@ -15,7 +15,6 @@ package body Alr.Commands.Build is is use Alire.Utils.Switches; begin - if Alire.Utils.Count_True ((Cmd.Release_Mode, Cmd.Validation_Mode, Cmd.Dev_Mode)) > 1 @@ -23,6 +22,14 @@ package body Alr.Commands.Build is Reportaise_Wrong_Arguments ("Only one build mode can be selected"); end if; + -- If a build profile has been set in the manifest we apply it now. + -- There will be a default otherwise. + + Alire.Crate_Configuration.Root_Build_Profile := + Cmd.Root.Configuration.Build_Profile (Cmd.Root.Name); + + -- Build profile in the command line takes precedence + if Cmd.Release_Mode then Alire.Crate_Configuration.Root_Build_Profile := Release; elsif Cmd.Validation_Mode then @@ -48,10 +55,20 @@ package body Alr.Commands.Build is return Boolean is begin + Cmd.Requires_Full_Index; Cmd.Requires_Valid_Session; + -- If we were invoked from another command (e.g. run) we apply the + -- profile found in the manifest as no override in the command line can + -- appear: + + if Cmd not in Command'Class then + Alire.Crate_Configuration.Root_Build_Profile := + Cmd.Root.Configuration.Build_Profile (Cmd.Root.Name); + end if; + declare Timer : Stopwatch.Instance; begin diff --git a/testsuite/skels/local-index/my_index/index/index.toml b/testsuite/skels/local-index/my_index/index/index.toml index bad265e4..c2a2c7db 100644 --- a/testsuite/skels/local-index/my_index/index/index.toml +++ b/testsuite/skels/local-index/my_index/index/index.toml @@ -1 +1 @@ -version = "1.1" +version = "1.2" diff --git a/testsuite/tests/build_profile/last_profile/test.py b/testsuite/tests/build_profile/last_profile/test.py new file mode 100644 index 00000000..b2c58a02 --- /dev/null +++ b/testsuite/tests/build_profile/last_profile/test.py @@ -0,0 +1,38 @@ +""" +Check that the last build profile is stored properly, no matter its procedence +""" + +from drivers.alr import run_alr, init_local_crate, alr_manifest +from drivers.asserts import assert_eq, assert_match + +init_local_crate("xxx") + +# Check profile is the default one (development) if unspecified + +run_alr("build") +assert_match(".*last_build_profile=DEVELOPMENT.*", + run_alr("config").out) + +# Check explicit profile in command line + +run_alr("build", "--release") +assert_match(".*last_build_profile=RELEASE.*", + run_alr("config").out) + +# Check implicit profile when build is indirect + +run_alr("run") # Causes a default build +assert_match(".*last_build_profile=DEVELOPMENT.*", + run_alr("config").out) + +# Check explicit profile requested in the manifest + +with open(alr_manifest(), "at") as manifest: + manifest.writelines(["[build-profiles]\n", + "xxx = 'validation'\n"]) + +run_alr("build") +assert_match(".*last_build_profile=VALIDATION.*", + run_alr("config").out) + +print('SUCCESS') diff --git a/testsuite/tests/build_profile/last_profile/test.yaml b/testsuite/tests/build_profile/last_profile/test.yaml new file mode 100644 index 00000000..32c747b3 --- /dev/null +++ b/testsuite/tests/build_profile/last_profile/test.yaml @@ -0,0 +1 @@ +driver: python-script -- 2.39.5