11import os
22from pathlib import Path
3+ import shlex
34import stat
45from collections .abc import Mapping
56from typing import Any , Generator
7879C2RUST_TRANSFORMS
7980"""
8081
82+ POSTPROCESS_SH : str = r"""#!/usr/bin/env bash
83+ # this file was autogenerated by templates.py
84+ set -e; set -o pipefail
85+
86+ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
87+ LOG_FILE="$SCRIPT_DIR/$(basename "$0")".log
88+
89+ cd "${SCRIPT_DIR}"
90+
91+ c2rust-postprocess --update-rust {{args}} repo/lib.rs > "$LOG_FILE"
92+ """
93+
8194CARGO_SH : str = r"""#!/usr/bin/env bash
8295# this file was autogenerated by templates.py
8396set -e; set -o pipefail
92105"""
93106
94107
95- def render_script (template : str , out_path : str , params : dict ):
108+ def render_script (template : str , out_path : str | Path , params : dict ):
96109 out = Template (template ).render (** params )
97110
98111 with open (out_path , "w" ) as fh :
@@ -108,11 +121,11 @@ def autogen_transpile(conf: str, yaml: dict[str, Any]) -> Generator[Path, None,
108121 transpile = yaml .get ("transpile" )
109122 if not (transpile and isinstance (transpile , dict )):
110123 return
111-
124+
112125 ag = transpile .get ("autogen" )
113126 if not (ag and isinstance (ag , bool )):
114127 return
115-
128+
116129 params = {"binary" : "--emit-build-files" , "cflags" : "" }
117130
118131 binary = transpile .get ("binary" )
@@ -144,11 +157,11 @@ def autogen_refactor(conf: str, yaml: dict[str, Any]) -> Generator[Path, None, N
144157 refactor = yaml .get ("refactor" )
145158 if not (refactor and isinstance (refactor , dict )):
146159 return
147-
160+
148161 ag = refactor .get ("autogen" )
149162 if not (ag and isinstance (ag , bool )):
150163 return
151-
164+
152165 params = {"transform_lines" : "" }
153166
154167 # Get list of transformations from config
@@ -171,6 +184,33 @@ def autogen_refactor(conf: str, yaml: dict[str, Any]) -> Generator[Path, None, N
171184 yield Path (out_path )
172185
173186
187+ def autogen_postprocess (conf : str , yaml : dict [str , Any ]) -> Generator [Path , None , None ]:
188+ """
189+ Yield generated paths.
190+ """
191+
192+ conf = Path (conf )
193+
194+ postprocess = yaml .get ("postprocess" )
195+ if not (postprocess and isinstance (postprocess , dict )):
196+ return
197+
198+ ag = postprocess .get ("autogen" )
199+ if not (ag and isinstance (ag , bool )):
200+ return
201+
202+ params = {"args" : "" }
203+
204+ exclude_file = conf .with_name ("postprocess-exclude.yml" )
205+ if exclude_file .exists ():
206+ # args are relative to script dir
207+ params ["args" ] = shlex .join (["--exclude-file" , str (exclude_file .relative_to (conf .parent ))])
208+
209+ out_path = conf .with_name ("postprocess.gen.sh" )
210+ render_script (POSTPROCESS_SH , out_path , params )
211+ yield out_path
212+
213+
174214def autogen_cargo (conf : str , yaml : dict [str , Any ]) -> Generator [Path , None , None ]:
175215 """
176216 Yield generated paths.
@@ -217,4 +257,5 @@ def autogen(conf: Config) -> Generator[Path, None, None]:
217257 for cf , yaml in conf .project_conf .items ():
218258 yield from autogen_transpile (cf , yaml )
219259 yield from autogen_refactor (cf , yaml )
260+ yield from autogen_postprocess (cf , yaml )
220261 yield from autogen_cargo (cf , yaml )
0 commit comments