Skip to content

Commit cdbba15

Browse files
authored
[libc] Add --write-if-changed switch to hdrgen/main.py (#122037)
This avoids touching the output file when it hasn't changed. The cmake build integration now uses this so that touching a .yaml or .h.def file in ways that don't affect the generated header output won't cause unnecessary recompilations.
1 parent 8e65940 commit cdbba15

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

libc/cmake/modules/LLVMLibCHeaderRules.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ function(add_gen_header target_name)
113113
COMMAND ${Python3_EXECUTABLE} "${LIBC_SOURCE_DIR}/utils/hdrgen/main.py"
114114
--output ${out_file}
115115
--depfile ${dep_file}
116+
--write-if-changed
116117
${entry_points}
117118
${yaml_file}
118119
DEPENDS ${yaml_file} ${fq_data_files}

libc/utils/hdrgen/main.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def main():
3737
help="Path to write a depfile",
3838
type=Path,
3939
)
40+
parser.add_argument(
41+
"--write-if-changed",
42+
help="Write the output file only if its contents have changed",
43+
action="store_true",
44+
default=False,
45+
)
4046
parser.add_argument(
4147
"-e",
4248
"--entry-point",
@@ -72,9 +78,13 @@ def write_depfile():
7278

7379
write_depfile()
7480

75-
args.output.parent.mkdir(parents=True, exist_ok=True)
76-
with open(args.output, "w") as out:
77-
out.write(contents)
81+
if (
82+
not args.write_if_changed
83+
or not args.output.exists()
84+
or args.output.read_text() != contents
85+
):
86+
args.output.parent.mkdir(parents=True, exist_ok=True)
87+
args.output.write_text(contents)
7888

7989

8090
if __name__ == "__main__":

0 commit comments

Comments
 (0)