2
2
3
3
import argparse
4
4
import logging
5
+ import os
5
6
import pathlib
6
7
import shutil
7
8
import subprocess
8
9
import sys
9
10
import tempfile
10
11
import json
11
- from typing import cast
12
+ from typing import cast , Literal
12
13
13
14
ROOT_DIR = pathlib .Path (__file__ ).parent .parent
14
15
MAKE = shutil .which ("gmake" ) or shutil .which ("make" )
19
20
20
21
class Args (argparse .Namespace ):
21
22
dockerfile : pathlib .Path
23
+ platform : Literal ["linux/amd64" , "linux/arm64" , "linux/s390x" , "linux/ppc64le" ]
22
24
remaining : list [str ]
23
25
24
26
25
27
def main () -> int :
26
28
p = argparse .ArgumentParser (allow_abbrev = False )
27
29
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 )
28
33
p .add_argument ('remaining' , nargs = argparse .REMAINDER )
29
34
30
35
args = cast (Args , p .parse_args ())
@@ -38,7 +43,7 @@ def main() -> int:
38
43
print ("must give a `{};` parameter that will be replaced with new build context" )
39
44
return 1
40
45
41
- prereqs = buildinputs (dockerfile = args .dockerfile )
46
+ prereqs = buildinputs (dockerfile = args .dockerfile , platform = args . platform )
42
47
43
48
with tempfile .TemporaryDirectory (delete = True ) as tmpdir :
44
49
setup_sandbox (prereqs , pathlib .Path (tmpdir ))
@@ -52,11 +57,15 @@ def main() -> int:
52
57
return 0
53
58
54
59
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 ]:
56
64
if not (ROOT_DIR / "bin/buildinputs" ).exists ():
57
65
subprocess .check_call ([MAKE , "bin/buildinputs" ], cwd = ROOT_DIR )
58
66
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 })
60
69
prereqs = [pathlib .Path (file ) for file in json .loads (stdout )] if stdout != "\n " else []
61
70
print (f"{ prereqs = } " )
62
71
return prereqs
0 commit comments