Skip to content

Commit

Permalink
Add crate configuration values to build hash inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Aug 28, 2023
1 parent 94f0ff7 commit 19cdc8f
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 10 deletions.
27 changes: 20 additions & 7 deletions src/alire/alire-builds-hashes.adb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
with Alire.Crate_Configuration.Hashes;
with Alire.Directories;
with Alire.Environment;
with Alire.GPR;
Expand Down Expand Up @@ -172,17 +173,27 @@ package body Alire.Builds.Hashes is
end loop;
end Add_Environment;

-----------------------
-- Add_Configuration --
-----------------------

procedure Add_Configuration is
begin
Crate_Configuration.Hashes.Add_From
(Config => Root.Configuration,
Rel => Rel,
Add => Add'Access);
end Add_Configuration;

begin
Trace.Debug (" build hashing: " & Rel.Milestone.TTY_Image);

-- Add individual contributors to the hash input
Add_Profile;
Add_Externals;
Add_Environment;
Add_Compiler;

-- Configuration variables
-- TBD
Add_Profile; -- Build profile
Add_Externals; -- GPR externals
Add_Environment; -- Environment variables
Add_Compiler; -- Compiler version
Add_Configuration; -- Crate configuration variables

-- Dependencies recursive hash? Since a crate can use a dependency
-- config spec, it is possible in the worst case for a crate to
Expand Down Expand Up @@ -213,6 +224,8 @@ package body Alire.Builds.Hashes is
Environment.Load (Context, Root, For_Hashing => True);
Env := Context.Get_All;

Root.Configuration.Ensure_Complete;

for Rel of Root.Solution.Releases loop
if Root.Requires_Build_Sync (Rel) then
Compute (Rel);
Expand Down
42 changes: 42 additions & 0 deletions src/alire/alire-crate_configuration-hashes.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
with Alire.Errors;

package body Alire.Crate_Configuration.Hashes is

procedure Add_From (Config : Global_Config;
Rel : Releases.Release;
Add : access procedure (Kind, Key, Value : String))
is
begin
-- We could iterate over Config contents, but by relying on the release
-- reported variables we ensure nothing is amiss during hashing.

for Untyped of Rel.Config_Variables loop
declare
Def : constant Properties.Configurations.Config_Type_Definition :=
Properties.Configurations.Config_Type_Definition (Untyped);
Key : constant String :=
Crate_Configuration.Key (Rel.Name, Def.Name);
begin
if not Config.Var_Map.Contains (+Key) then
raise Program_Error
with Errors.Set
("Incomplete configuration during hashing, missing value "
& "for: " & Key);
end if;

if not Def.Valid (Config.Var_Map (+Key).Value) then
raise Program_Error
with Errors.Set ("Invalid config value during hashing: "
& Def.Image);
end if;

Add (Kind => "config",
Key => Key,
Value =>
Properties.Configurations.Image
(Config.Var_Map (+Key).Value));
end;
end loop;
end Add_From;

end Alire.Crate_Configuration.Hashes;
10 changes: 10 additions & 0 deletions src/alire/alire-crate_configuration-hashes.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
with Alire.Releases;

package Alire.Crate_Configuration.Hashes is

procedure Add_From (Config : Global_Config;
Rel : Releases.Release;
Add : access procedure (Kind, Key, Value : String));
-- Add configuration values of the given crate to the build hash input

end Alire.Crate_Configuration.Hashes;
9 changes: 8 additions & 1 deletion src/alire/alire-crate_configuration.adb
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,13 @@ package body Alire.Crate_Configuration is
TIO.Close (File);
end Generate_C_Config;

---------
-- Key --
---------

function Key (Crate : Crate_Name; Var_Name : String) return String
is (To_Lower_Case (Crate.As_String & "." & Var_Name));

--------------------
-- Add_Definition --
--------------------
Expand All @@ -744,7 +751,7 @@ package body Alire.Crate_Configuration is
is
Type_Name_Lower : constant String := To_Lower_Case (Type_Def.Name);

Name : constant Unbounded_String := +(+Crate & "." & Type_Name_Lower);
Name : constant Unbounded_String := +Key (Crate, Type_Def.Name);
begin

if Is_Reserved_Name (Type_Name_Lower) then
Expand Down
3 changes: 3 additions & 0 deletions src/alire/alire-crate_configuration.ads
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,7 @@ private
Filepath : Absolute_Path;
Version : String);

function Key (Crate : Crate_Name; Var_Name : String) return String;
-- Keys in the var map are normalized as "crate.var" lowercased

end Alire.Crate_Configuration;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "1.0.0"
maintainers = ["[email protected]"]
maintainers-logins = ["mylogin"]

# Externals and environment used to track build hash generation immutability
# Externals, environment, config used to track build hash generation immutability

[gpr-externals]
foo = ""
Expand All @@ -15,6 +15,9 @@ bar = "foo"
[environment]
BAZ.set = "foo"

[configuration.variables]
Var1={type="Boolean", default=true}

[origin]
url = "file:../../../crates/libhello_1.0.0.tgz"
hashes = ["sha512:99fa3a55540d0655c87605b54af732f76a8a363015f183b06e98aa91e54c0e69397872718c5c16f436dd6de0fba506dc50c66d34a0e5c61fb63cb01fa22f35ac"]
1 change: 1 addition & 0 deletions testsuite/tests/build/hashes/hashing-inputs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# profile, compiler version.

assert_eq(
'config:libhello.var1=true\n' # crate config var
'environment:TEST_ENV=myenv\n' # plain env var set
'external:TEST_FREEFORM_UNSET=default\n' # declared unset GPR external
'external:TEST_GPR_EXTERNAL=gpr_ext_B\n' # declared set GPR external
Expand Down
2 changes: 1 addition & 1 deletion testsuite/tests/dockerized/misc/default-cache/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# We hardcode this hash so we detect unwilling changes to our hashing scheme.
# Every time this hash changes we must know the reason (changes in the hashing
# procedures)
hash = "1a29f0454348e767e78ac6912c7409b374b7bf650e81396c8a8750797ae073eb"
hash = "d56fceceba3a7deaa9df1a9b43afa4fb9c643bf5245b335cc12ba0ae4df15682"
assert \
os.path.isdir(f"{base}/builds/crate_real_1.0.0_filesystem_{hash}"), \
f"Shared build not found at the expected location: f{contents(base)}"
Expand Down

0 comments on commit 19cdc8f

Please sign in to comment.