dawn-cmake/generator/extract_json.py
Corentin Wallez c48e487998 Avoid unnecessary recompiles with code generators.
Previously code generators would unconditionally write the generated
files even if they didn't change, causing all many more files to be
rebuilt than necessary.

Make extract_json.py check the file content to skip writing if it can to
fix this.

Bug: None
Change-Id: I22389444179c9b16a7ccc03ea133a973d419fad3
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/45761
Auto-Submit: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
2021-03-23 19:06:02 +00:00

45 lines
1.4 KiB
Python

#!/usr/bin/env python3
# Copyright 2018 The Dawn Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os, sys, json
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: extract_json.py JSON DIR")
sys.exit(1)
with open(sys.argv[1]) as f:
files = json.loads(f.read())
output_dir = sys.argv[2]
for (name, content) in files.items():
output_file = output_dir + os.path.sep + name
# Create the output directory if needed.
directory = os.path.dirname(output_file)
if not os.path.exists(directory):
os.makedirs(directory)
# Skip writing to the file if it already has the correct content.
try:
with open(output_file, 'r') as outfile:
if outfile.read() == content:
continue
except (OSError, EnvironmentError):
pass
with open(output_file, 'w') as outfile:
outfile.write(content)