22
33import argparse
44import logging
5+ import os
56import pathlib
67import shutil
78import subprocess
89import sys
910import tempfile
1011import json
11- from typing import cast
12+ from typing import cast , Literal
1213
1314ROOT_DIR = pathlib .Path (__file__ ).parent .parent
1415MAKE = shutil .which ("gmake" ) or shutil .which ("make" )
1920
2021class Args (argparse .Namespace ):
2122 dockerfile : pathlib .Path
23+ platform : Literal ["linux/amd64" , "linux/arm64" , "linux/s390x" , "linux/ppc64le" ]
2224 remaining : list [str ]
2325
2426
2527def main () -> int :
2628 p = argparse .ArgumentParser (allow_abbrev = False )
2729 p .add_argument ("--dockerfile" , type = pathlib .Path , required = True )
30+ p .add_argument ("--platform" , type = str ,
31+ choices = ["linux/amd64" , "linux/arm64" , "linux/s390x" , "linux/ppc64le" ],
32+ required = True )
2833 p .add_argument ('remaining' , nargs = argparse .REMAINDER )
2934
3035 args = cast (Args , p .parse_args ())
@@ -38,7 +43,7 @@ def main() -> int:
3843 print ("must give a `{};` parameter that will be replaced with new build context" )
3944 return 1
4045
41- prereqs = buildinputs (dockerfile = args .dockerfile )
46+ prereqs = buildinputs (dockerfile = args .dockerfile , platform = args . platform )
4247
4348 with tempfile .TemporaryDirectory (delete = True ) as tmpdir :
4449 setup_sandbox (prereqs , pathlib .Path (tmpdir ))
@@ -52,11 +57,15 @@ def main() -> int:
5257 return 0
5358
5459
55- def buildinputs (dockerfile : pathlib .Path | str ) -> list [pathlib .Path ]:
60+ def buildinputs (
61+ dockerfile : pathlib .Path | str ,
62+ platform : Literal ["linux/amd64" , "linux/arm64" , "linux/s390x" , "linux/ppc64le" ] = "linux/amd64"
63+ ) -> list [pathlib .Path ]:
5664 if not (ROOT_DIR / "bin/buildinputs" ).exists ():
5765 subprocess .check_call ([MAKE , "bin/buildinputs" ], cwd = ROOT_DIR )
5866 stdout = subprocess .check_output ([ROOT_DIR / "bin/buildinputs" , str (dockerfile )],
59- text = True , cwd = ROOT_DIR )
67+ text = True , cwd = ROOT_DIR ,
68+ env = {"TARGETPLATFORM" : platform , ** os .environ })
6069 prereqs = [pathlib .Path (file ) for file in json .loads (stdout )] if stdout != "\n " else []
6170 print (f"{ prereqs = } " )
6271 return prereqs
0 commit comments