Skip to content

Conversation

@frobtech
Copy link
Contributor

@frobtech frobtech commented Jan 8, 2025

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.

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.
@frobtech frobtech requested review from Caslyn and petrhosek January 8, 2025 01:37
@frobtech frobtech marked this pull request as ready for review January 8, 2025 01:39
@llvmbot llvmbot added the libc label Jan 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 8, 2025

@llvm/pr-subscribers-libc

Author: Roland McGrath (frobtech)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/122037.diff

2 Files Affected:

  • (modified) libc/cmake/modules/LLVMLibCHeaderRules.cmake (+1)
  • (modified) libc/utils/hdrgen/main.py (+13-3)
diff --git a/libc/cmake/modules/LLVMLibCHeaderRules.cmake b/libc/cmake/modules/LLVMLibCHeaderRules.cmake
index a95c13389012f8..288e4dade0b472 100644
--- a/libc/cmake/modules/LLVMLibCHeaderRules.cmake
+++ b/libc/cmake/modules/LLVMLibCHeaderRules.cmake
@@ -113,6 +113,7 @@ function(add_gen_header target_name)
     COMMAND ${Python3_EXECUTABLE} "${LIBC_SOURCE_DIR}/utils/hdrgen/main.py"
             --output ${out_file}
             --depfile ${dep_file}
+            --write-if-changed
             ${entry_points}
             ${yaml_file}
     DEPENDS ${yaml_file} ${fq_data_files}
diff --git a/libc/utils/hdrgen/main.py b/libc/utils/hdrgen/main.py
index 2bba1efa04e42a..5dd392ab6662ee 100755
--- a/libc/utils/hdrgen/main.py
+++ b/libc/utils/hdrgen/main.py
@@ -37,6 +37,12 @@ def main():
         help="Path to write a depfile",
         type=Path,
     )
+    parser.add_argument(
+        "--write-if-changed",
+        help="Write the output file only if its contents have changed",
+        action="store_true",
+        default=False,
+    )
     parser.add_argument(
         "-e",
         "--entry-point",
@@ -72,9 +78,13 @@ def write_depfile():
 
     write_depfile()
 
-    args.output.parent.mkdir(parents=True, exist_ok=True)
-    with open(args.output, "w") as out:
-        out.write(contents)
+    if (
+        not args.write_if_changed
+        or not args.output.exists()
+        or args.output.read_text() != contents
+    ):
+        args.output.parent.mkdir(parents=True, exist_ok=True)
+        args.output.write_text(contents)
 
 
 if __name__ == "__main__":

@frobtech frobtech merged commit cdbba15 into llvm:main Jan 8, 2025
15 checks passed
@frobtech frobtech deleted the p/libc-hdrgen-write-if-changed branch January 8, 2025 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants