diff options
author | Logan Chien <tzuhsiang.chien@gmail.com> | 2011-03-15 09:13:32 +0800 |
---|---|---|
committer | Logan Chien <tzuhsiang.chien@gmail.com> | 2011-03-15 09:22:20 +0800 |
commit | c0a87ab7907e88efa4a5d7cba27e876f0dab1f5b (patch) | |
tree | 0cc3e3002d1691088dcf392c719955ae4cc59c1e | |
parent | 95b62c22d4e04993d6c3004fe06c372565392253 (diff) | |
download | external_llvm-c0a87ab7907e88efa4a5d7cba27e876f0dab1f5b.zip external_llvm-c0a87ab7907e88efa4a5d7cba27e876f0dab1f5b.tar.gz external_llvm-c0a87ab7907e88efa4a5d7cba27e876f0dab1f5b.tar.bz2 |
Implement copy-when-different build rules for tblgen.
Before this commit, tblgen rules will always generate new file,
change the timestamp, and then a lot of c++ files have to be
recompiled.
This commit modifies transform-td-to-out macro. Now, tblgen will
generate its output to an intermediate file. The timestamp of the
inc files will only be changed when the newly generated intermediate
file is different from the old one.
-rwxr-xr-x | copy-diff.py | 72 | ||||
-rw-r--r-- | llvm-device-build.mk | 4 | ||||
-rw-r--r-- | llvm-host-build.mk | 4 |
3 files changed, 78 insertions, 2 deletions
diff --git a/copy-diff.py b/copy-diff.py new file mode 100755 index 0000000..e66ec2c --- /dev/null +++ b/copy-diff.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python + +from __future__ import print_function +from __future__ import with_statement + +import os +import shutil +import sys + +BUFFER_SIZE = 1024 + +def is_file_different(a, b): + if os.path.getsize(a) != os.path.getsize(b): + # If the file size is different, the content must be different. + return True + + # Read the content of the files, and compare them. + with open(a, 'rb') as fa: + with open(b, 'rb') as fb: + while True: + buff_a = fa.read(BUFFER_SIZE) + buff_b = fb.read(BUFFER_SIZE) + + if buff_a != buff_b: + # File is different in this block. + return True + + if len(buff_a) < BUFFER_SIZE: + # Finished + break + + # File is the same. + return False + +def copyfile(src, dest): + if not os.path.exists(src): + raise ValueError('Source file not found') + + # Make parent directory (if necessary) + destdir = os.path.dirname(dest) + if not os.path.exists(destdir): + try: + os.makedirs(destdir) + except os.error as e: + raise ValueError('Unable to create directory ' + destdir) + elif not os.path.isdir(destdir): + raise ValueError(destdir + ' is not a directory') + + if not os.path.exists(dest) or is_file_different(src, dest): + # If the destination file does not exist or the source file is + # different from the destination file, then we copy the file. + shutil.copyfile(src, dest) + +def main(): + if len(sys.argv) < 3: + print('USAGE:', sys.argv[0], '<srcfile> <destfile>', file=sys.stderr) + sys.exit(1) + + srcfile = os.path.abspath(sys.argv[1]) + destfile = os.path.abspath(sys.argv[2]) + + if srcfile == destfile: + print('WARNING: <srcfile> is equal to <destfile>', file=sys.stderr) + else: + try: + copyfile(srcfile, destfile) + except ValueError as e: + print('ERROR:', e, file=sys.stderr) + sys.exit(1) + +if __name__ == '__main__': + main() diff --git a/llvm-device-build.mk b/llvm-device-build.mk index 83fc5ef..00f089d 100644 --- a/llvm-device-build.mk +++ b/llvm-device-build.mk @@ -73,5 +73,7 @@ $(hide) $(TBLGEN) \ -I $(LLVM_ROOT_PATH)/device/include \ -I $(LLVM_ROOT_PATH)/lib/Target \ -gen-$(strip $(1)) \ - -o $@ $< + -o $@.tmp $< +$(hide) $(LLVM_ROOT_PATH)/copy-diff.py $@.tmp $@ +$(hide) $(RM) $@.tmp endef diff --git a/llvm-host-build.mk b/llvm-host-build.mk index d7ef682..fe68934 100644 --- a/llvm-host-build.mk +++ b/llvm-host-build.mk @@ -63,5 +63,7 @@ $(hide) $(TBLGEN) \ -I $(LLVM_ROOT_PATH)/lib/Target \ $(if $(strip $(CLANG_ROOT_PATH)),-I $(CLANG_ROOT_PATH)/include,) \ -gen-$(strip $(1)) \ - -o $@ $< + -o $@.tmp $< +$(hide) $(LLVM_ROOT_PATH)/copy-diff.py $@.tmp $@ +$(hide) $(RM) $@.tmp endef |