@@ -38,10 +38,11 @@ def find_path(cmd: str) -> Path | None:
3838
3939
4040def modify_cross_file (file : Path , link_script : str , output_map : str , arm_path : str | None ) -> None :
41- if str (file ).endswith ("gcc-arm-none-eabi.ini" ):
42- arm_cmd = "arm-none-eabi-gcc"
43- else :
41+ print (green ("Modifying:" ), file .as_posix (), flush = True )
42+ if str (file ).endswith ("armclang.ini" ):
4443 arm_cmd = "armclang"
44+ else :
45+ arm_cmd = "arm-none-eabi-gcc"
4546
4647 if arm_path :
4748 path = Path (arm_path )
@@ -92,51 +93,72 @@ def modify_cross_file(file: Path, link_script: str, output_map: str, arm_path: s
9293 file .write_text (text )
9394
9495
95- def download_files (cross_files : list [str ]) -> list [str ]:
96- path = Path ("cross_files" )
97- if not path .exists ():
98- os .mkdir (path )
96+ def download_file (url : str , file : Path ) -> None :
97+ print (green ("Downloading:" ), f"{ url } -> { file .as_posix ()} " , flush = True )
98+ urllib .request .urlretrieve (url , file )
99+
100+
101+ def prepare_cross_files (build_dir : Path , cross_files : list [str ]) -> list [str ]:
102+ cross_dir = build_dir .joinpath ("cross_files" )
103+ os .makedirs (cross_dir , exist_ok = True )
104+
105+ repo = "https://raw.githubusercontent.com/JalonWong/mcu_meson"
99106
100107 rst_list = []
101108 for f in cross_files :
102- if f .startswith ("http" ):
103- filename = path .joinpath (f .rsplit ("/" , maxsplit = 1 )[1 ])
104- urllib .request .urlretrieve (f , filename )
105- rst_list .append (str (filename ))
109+ if f .startswith ("main:" ):
110+ filename = cross_dir .joinpath (f .rsplit (":" , maxsplit = 1 )[1 ])
111+ download_file (
112+ f .replace ("main:" , f"{ repo } /refs/heads/main/" ),
113+ filename ,
114+ )
115+ elif f .startswith ("tag:" ):
116+ tag_list = f .split (":" )
117+ filename = cross_dir .joinpath (tag_list [2 ])
118+ download_file (
119+ f"{ repo } /{ tag_list [1 ]} /{ tag_list [2 ]} " ,
120+ filename ,
121+ )
122+ elif f .startswith ("http" ):
123+ filename = cross_dir .joinpath (f .rsplit ("/" , maxsplit = 1 )[1 ])
124+ download_file (f , filename )
106125 else :
107- rst_list .append (f )
126+ filename = cross_dir .joinpath (Path (f ).name )
127+ print (green ("Copying:" ), f"{ f } -> { filename .as_posix ()} " , flush = True )
128+ shutil .copy (f , filename )
129+
130+ rst_list .append (filename .as_posix ())
108131
109132 return rst_list
110133
111134
112135def setup (
113136 build_dir : str ,
114- cross_files : list [str ],
137+ cross_files : list [str ] = [] ,
115138 link_script : str = "" ,
116139 output_map : str = "" ,
117- msvc : bool = False ,
140+ reconfigure : bool = True ,
141+ wipe : bool = False ,
142+ vsenv : bool = False ,
143+ args : list [str ] = [],
118144) -> None :
119145 parser = argparse .ArgumentParser ()
120- parser .add_argument ("--rm" , help = "Remove builddir before setup" , action = "store_true" )
121- parser .add_argument ("--native" , help = "Setup for native at the same time" , action = "store_true" )
122146 parser .add_argument ("--arm_path" , help = "Path of arm toolchain" , type = str )
123- opts = parser .parse_args ()
124-
125- # Native
126- if opts .native :
127- if opts .rm and os .path .exists (f"{ build_dir } -native" ):
128- shutil .rmtree (f"{ build_dir } -native" )
129- cmd = f"meson setup { build_dir } -native" .split ()
130- if msvc and platform .system () == "Windows" :
131- cmd += ["--vsenv" ]
132- print (green ("Run:" ), " " .join (cmd ), flush = True )
133- subprocess .run (cmd )
134-
135- # Cross
136- if opts .rm and os .path .exists (build_dir ):
147+ opts , extra = parser .parse_known_args ()
148+
149+ if wipe and os .path .exists (build_dir ):
150+ print (green ("Wipe:" ), f"Removing { build_dir } " , flush = True )
137151 shutil .rmtree (build_dir )
138- cross_files = download_files (cross_files )
139- modify_cross_file (Path (cross_files [0 ]), link_script , output_map , opts .arm_path )
152+
153+ cross_files = prepare_cross_files (Path (build_dir ), cross_files )
154+ if len (cross_files ) > 0 :
155+ modify_cross_file (Path (cross_files [0 ]), link_script , output_map , opts .arm_path )
156+
140157 cmd = f"meson setup { build_dir } " .split () + [f"--cross-file={ f } " for f in cross_files ]
141- print (green ("Run:" ), " " .join (cmd ), flush = True )
158+ if reconfigure :
159+ cmd += ["--reconfigure" ]
160+ if vsenv and platform .system () == "Windows" :
161+ cmd += ["--vsenv" ]
162+
163+ print (green ("Running:" ), " " .join (cmd + args + extra ), flush = True )
142164 subprocess .run (cmd )
0 commit comments