native/cross: Add ar wrapper for determinism
PATCH 1/1 native/cross: Add ar wrapper for determinism
From: Jacob Kroon <[email protected]>
Add a wrapper around ar calls for native/cross recipes. This wrapper adds the -D option so that deterministic archives are built for native/cross output. This improves the changes of hash equivalence matches and hence build artefact reuse.
We don't need this in the target case since we compile binutils-cross with an option making this the default. We need a wrapper since we need to remove the "u" option and replace it with "D" but also allow things like "--version" to continue to work too.
--- meta/classes/cross.bbclass | 2 ++ scripts/cross-intercept/ar | 1 + scripts/native-intercept/ar | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 120000 scripts/cross-intercept/ar create mode 100755 scripts/native-intercept/ar
meta/classes/cross.bbclass +2 -0
@@ -93,3 +93,5 @@ python do_addto_recipe_sysroot () { } addtask addto_recipe_sysroot after do_populate_sysroot do_addto_recipe_sysroot[deptask] = "do_populate_sysroot" + +PATH:prepend = "${COREBASE}/scripts/cross-intercept:"
scripts/cross-intercept/ar new file +1 -0
@@ -0,0 +1 @@ +../native-intercept/ar
scripts/native-intercept/ar new file +29 -0
@@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# +# Wrapper around 'ar' that defaults to deterministic archives + +import os +import shutil +import sys + +# calculate path to the real 'ar' +path = os.environ['PATH'] +path = path.replace(os.path.dirname(sys.argv[0]), '') +real_ar = shutil.which('ar', path=path) + +if len(sys.argv) == 1: + os.execl(real_ar, 'ar') + +# modify args to mimic 'ar' configured with --default-deterministic-archives +argv = sys.argv +if argv[1].startswith('--'): + # No modifier given + None +else: + # remove the optional '-' + if argv[1][0] == '-': + argv[1] = argv[1][1:] + argv[1] = argv[1].replace('u', '') + argv[1] = 'D' + argv[1] + +os.execv(real_ar, argv)
Signature · 1 line
2.32.0
Quoted 66 lines · Richard Purdie
From: Jacob Kroon <[email protected]>
Add a wrapper around ar calls for native/cross recipes. This wrapper adds the -D option so that deterministic archives are built for native/cross output. This improves the changes of hash equivalence matches and hence build artefact reuse.
We don't need this in the target case since we compile binutils-cross with an option making this the default. We need a wrapper since we need to remove the "u" option and replace it with "D" but also allow things like "--version" to continue to work too.
Signed-off-by Jacob Kroon <[email protected]>Signed-off-by Richard Purdie <[email protected]>--- meta/classes/cross.bbclass | 2 ++ scripts/cross-intercept/ar | 1 + scripts/native-intercept/ar | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 120000 scripts/cross-intercept/ar create mode 100755 scripts/native-intercept/armeta/classes/cross.bbclass +2
-0@@ -93,3 +93,5 @@ python do_addto_recipe_sysroot () { } addtask addto_recipe_sysroot after do_populate_sysroot do_addto_recipe_sysroot[deptask] = "do_populate_sysroot" + +PATH:prepend = "${COREBASE}/scripts/cross-intercept:"scripts/cross-intercept/ar new file +1
-0@@ -0,0 +1 @@ +../native-intercept/arscripts/native-intercept/ar new file +19
-0@@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# +# Wrapper around 'ar' that defaults to deterministic archives + +import os +import shutil +import sys + +# calculate path to the real 'ar' +path = os.environ['PATH'] +path = path.replace(os.path.dirname(sys.argv[0]), '') +real_ar = shutil.which('ar', path=path) + +if len(sys.argv) == 1: + os.execl(real_ar, 'ar') + +# modify args to mimic 'ar' configured with --default-deterministic-archives +argv = sys.argv +if argv[1].startswith('--'):
scripts/native-intercept/ar line 29
Quoted 10 lines
scripts/native-intercept/ar new file +10
-0+ # No modifier given + None +else: + # remove the optional '-' + if argv[1][0] == '-': + argv[1] = argv[1][1:] + argv[1] = argv[1].replace('u', '') + argv[1] = 'D' + argv[1] + +os.execv(real_ar, argv)
What should we do if the user is explicitly passing U, "Do not operate in deterministic mode." ? Don't replace any 'u' and don't pass D ?
Jacob
Quoted 73 lines · Jacob Kroon
Quoted 71 lines · Richard Purdie
From: Jacob Kroon <[email protected]>
Add a wrapper around ar calls for native/cross recipes. This wrapper adds the -D option so that deterministic archives are built for native/cross output. This improves the changes of hash equivalence matches and hence build artefact reuse.
We don't need this in the target case since we compile binutils-cross with an option making this the default. We need a wrapper since we need to remove the "u" option and replace it with "D" but also allow things like "--version" to continue to work too.
Signed-off-by Jacob Kroon <[email protected]>Signed-off-by Richard Purdie <[email protected]>--- meta/classes/cross.bbclass | 2 ++ scripts/cross-intercept/ar | 1 + scripts/native-intercept/ar | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 120000 scripts/cross-intercept/ar create mode 100755 scripts/native-intercept/armeta/classes/cross.bbclass +2
-0@@ -93,3 +93,5 @@ python do_addto_recipe_sysroot () { } addtask addto_recipe_sysroot after do_populate_sysroot do_addto_recipe_sysroot[deptask] = "do_populate_sysroot" + +PATH:prepend = "${COREBASE}/scripts/cross-intercept:"scripts/cross-intercept/ar new file +1
-0@@ -0,0 +1 @@ +../native-intercept/arscripts/native-intercept/ar new file +25
-0@@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# +# Wrapper around 'ar' that defaults to deterministic archives + +import os +import shutil +import sys + +# calculate path to the real 'ar' +path = os.environ['PATH'] +path = path.replace(os.path.dirname(sys.argv[0]), '') +real_ar = shutil.which('ar', path=path) + +if len(sys.argv) == 1: + os.execl(real_ar, 'ar') + +# modify args to mimic 'ar' configured with --default-deterministic-archives +argv = sys.argv +if argv[1].startswith('--'): + # No modifier given + None +else: + # remove the optional '-' + if argv[1][0] == '-': + argv[1] = argv[1][1:]
Quoted 8 lines
scripts/native-intercept/ar line 29
Quoted 5 lines
scripts/native-intercept/ar new file +4
-0+ argv[1] = argv[1].replace('u', '') + argv[1] = 'D' + argv[1] + +os.execv(real_ar, argv)What should we do if the user is explicitly passing U, "Do not operate in deterministic mode." ? Don't replace any 'u' and don't pass D ?
That would seem to make sense to me...
Cheers,
Richard
Quoted 83 lines · Richard Purdie
Quoted 73 lines · Jacob Kroon
Quoted 71 lines · Richard Purdie
From: Jacob Kroon <[email protected]>
Add a wrapper around ar calls for native/cross recipes. This wrapper adds the -D option so that deterministic archives are built for native/cross output. This improves the changes of hash equivalence matches and hence build artefact reuse.
We don't need this in the target case since we compile binutils-cross with an option making this the default. We need a wrapper since we need to remove the "u" option and replace it with "D" but also allow things like "--version" to continue to work too.
Signed-off-by Jacob Kroon <[email protected]>Signed-off-by Richard Purdie <[email protected]>--- meta/classes/cross.bbclass | 2 ++ scripts/cross-intercept/ar | 1 + scripts/native-intercept/ar | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 120000 scripts/cross-intercept/ar create mode 100755 scripts/native-intercept/armeta/classes/cross.bbclass +2
-0@@ -93,3 +93,5 @@ python do_addto_recipe_sysroot () { } addtask addto_recipe_sysroot after do_populate_sysroot do_addto_recipe_sysroot[deptask] = "do_populate_sysroot" + +PATH:prepend = "${COREBASE}/scripts/cross-intercept:"scripts/cross-intercept/ar new file +1
-0@@ -0,0 +1 @@ +../native-intercept/arscripts/native-intercept/ar new file +25
-0@@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# +# Wrapper around 'ar' that defaults to deterministic archives + +import os +import shutil +import sys + +# calculate path to the real 'ar' +path = os.environ['PATH'] +path = path.replace(os.path.dirname(sys.argv[0]), '') +real_ar = shutil.which('ar', path=path) + +if len(sys.argv) == 1: + os.execl(real_ar, 'ar') + +# modify args to mimic 'ar' configured with --default-deterministic-archives +argv = sys.argv +if argv[1].startswith('--'): + # No modifier given + None +else: + # remove the optional '-' + if argv[1][0] == '-': + argv[1] = argv[1][1:]Quoted 8 lines
scripts/native-intercept/ar line 29
Quoted 5 lines
scripts/native-intercept/ar new file +4
-0+ argv[1] = argv[1].replace('u', '') + argv[1] = 'D' + argv[1] + +os.execv(real_ar, argv)What should we do if the user is explicitly passing U, "Do not operate in deterministic mode." ? Don't replace any 'u' and don't pass D ?
Quoted 2 lines
That would seem to make sense to me...
Could we log a warning somewhere when this is done ? I can't write to stdout here, but stderr perhaps ?
Jacob