summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2017-11-08 10:14:38 -0500
committerYong He <yonghe@outlook.com>2017-11-08 10:14:38 -0500
commit97569db1f4caac84ad03b38647c12fc97e9864c1 (patch)
tree98346197ae6d8c2e7fb901c585fe88bbf1883cf6
parente1710807292544775dc6a0eb338af081fb94493e (diff)
Don't update generated .h file if its not changed.
This changes logic of slang-generate to detect whether the newly generated .h file is different from the existing file, and update the existing file only when the actual content has changed. This helps prevent visual studio from repetitively rebuilding the slang project due to the header file being updated on every build.
-rw-r--r--tools/slang-generate/main.cpp51
1 files changed, 50 insertions, 1 deletions
diff --git a/tools/slang-generate/main.cpp b/tools/slang-generate/main.cpp
index 8208354e4..475849993 100644
--- a/tools/slang-generate/main.cpp
+++ b/tools/slang-generate/main.cpp
@@ -370,6 +370,42 @@ void usage(char const* appName)
fprintf(stderr, "usage: %s <input>\n", appName);
}
+char* readAllText(char const * fileName)
+{
+ FILE * f;
+ fopen_s(&f, fileName, "rb");
+ if (!f)
+ {
+ return "";
+ }
+ else
+ {
+ fseek(f, 0, SEEK_END);
+ auto size = ftell(f);
+ char * buffer = new char[size + 1];
+ memset(buffer, 0, size + 1);
+ fseek(f, 0, SEEK_SET);
+ fread(buffer, sizeof(char), size, f);
+ fclose(f);
+ return buffer;
+ }
+}
+
+void writeAllText(char const *srcFileName, char const* fileName, char* content)
+{
+ FILE * f = nullptr;
+ fopen_s(&f, fileName, "wb");
+ if (!f)
+ {
+ printf("%s(0): error G0001: cannot write file %s\n", srcFileName, fileName);
+ }
+ else
+ {
+ fwrite(content, 1, strlen(content), f);
+ fclose(f);
+ }
+}
+
int main(
int argc,
char** argv)
@@ -416,8 +452,9 @@ int main(
Node* node = readInput(input, inputEnd);
+ // write output to a temporary file first
char outputPath[1024];
- sprintf_s(outputPath, "%s.h", inputPath);
+ sprintf_s(outputPath, "%s.temp.h", inputPath);
FILE* outputStream;
fopen_s(&outputStream, outputPath, "w");
@@ -426,5 +463,17 @@ int main(
fclose(outputStream);
+ // update final output only when content has changed
+ char outputPathFinal[1024];
+ sprintf_s(outputPathFinal, "%s.h", inputPath);
+
+ char * allTextOld = readAllText(outputPathFinal);
+ char * allTextNew = readAllText(outputPath);
+ if (strcmp(allTextNew, allTextOld) != 0)
+ {
+ writeAllText(inputPath, outputPathFinal, allTextNew);
+ }
+ remove(outputPath);
+
return 0;
}