Update genversion tool to avoid recreating file

This commit is contained in:
Tim Felgentreff 2020-07-01 09:12:03 +02:00
parent adffb0139e
commit f38702ee81

View file

@ -36,8 +36,9 @@
int main(int argc, char * argv[]) {
FILE * file;
int old_ver[5] = { -1, -1, -1, -1, -1 };
int new_ver[5] = { -1, -1, -1, -1, -1 };
int old_ver[4] = { -1, -1, -1, -1 };
int new_ver[4] = { -1, -1, -1, -1 };
char* old_rev = (char*)calloc(sizeof(char), 1024);
if ( argc != 3 )
return 1;
@ -50,12 +51,11 @@ int main(int argc, char * argv[]) {
file = fopen(argv[1], "r");
if ( file ) {
if ( fscanf(file, "/* %d %d %d %d %d */", &old_ver[0], &old_ver[1], &old_ver[2], &old_ver[3], &old_ver[4]) != 5 ) {
if ( fscanf(file, "/* %d %d %d %d %s */", &old_ver[0], &old_ver[1], &old_ver[2], &old_ver[3], old_rev) != 5 ) {
old_ver[0] = -1;
old_ver[1] = -1;
old_ver[2] = -1;
old_ver[3] = -1;
old_ver[4] = -1;
}
fclose(file);
}
@ -63,35 +63,49 @@ int main(int argc, char * argv[]) {
file = fopen(".git/HEAD", "r");
char *git_rev, *gitrevfile;
if ( file ) {
new_ver[4] = 1;
git_rev = (char*)calloc(sizeof(char), 1024);
if (fscanf(file, "ref: %s", git_rev) != 1 ) {
if (fscanf(file, "%s", git_rev) != 1 ) {
fscanf(file, "%s", git_rev);
}
fclose(file);
gitrevfile = (char*)calloc(sizeof(char), strlen(git_rev) + 6);
sprintf(gitrevfile, ".git/%s", git_rev);
file = fopen(gitrevfile, "r");
free(gitrevfile);
if (file) {
git_rev = (char*)calloc(sizeof(char), 1024);
if (fscanf(file, "%s", git_rev) != 1) {
new_ver[4] = -1;
}
fclose(file);
} else {
fclose(file);
gitrevfile = (char*)calloc(sizeof(char), strlen(git_rev) + 6);
sprintf(gitrevfile, ".git/%s", git_rev);
free(git_rev);
file = fopen(gitrevfile, "r");
free(gitrevfile);
if (file) {
git_rev = (char*)calloc(sizeof(char), 1024);
if (fscanf(file, "%s", git_rev) != 1) {
new_ver[4] = -1;
}
fclose(file);
}
}
} else {
fprintf(stderr, "Cannot get git revision\n");
}
bool needs_update = false;
for (int i = 0; i < 4; i++) {
if (old_ver[i] != new_ver[i]) {
printf("%d %d %d %d - %d %d %d %d\n", old_ver[0], old_ver[1], old_ver[2], old_ver[3], new_ver[0], new_ver[1], new_ver[2], new_ver[3]);
puts("ver update");
needs_update = true;
break;
}
}
if (!needs_update) {
needs_update = strcmp(old_rev, git_rev);
}
if (!needs_update) {
puts("[genversion] no update needed");
return 0;
}
file = fopen(argv[1], "w");
if ( ! file )
return 1;
fprintf(file, "/* %d %d %d %d %d */\n", new_ver[0], new_ver[1], new_ver[2], new_ver[3], new_ver[4]);
fprintf(file, "/* %d %d %d %d %s */\n", new_ver[0], new_ver[1], new_ver[2], new_ver[3], git_rev);
fprintf(file, "/* This file is autogenerated, do not modify it! */\n");
fprintf(file, "#define StratagusMajorVersion %d\n", new_ver[0]);
fprintf(file, "#define StratagusMinorVersion %d\n", new_ver[1]);