228 lines
6.0 KiB
C
228 lines
6.0 KiB
C
|
#include <windows.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#define BUILD_DIR ".\\build"
|
||
|
#define GITREV_FILE ".\\build\\gitrev.txt"
|
||
|
#define GITHASH_FILE ".\\build\\githash.txt"
|
||
|
#define GITSTATUS_FILE ".\\build\\gitstatus.txt"
|
||
|
#define REVISION_H_FILE "..\\applications\\revision.h"
|
||
|
|
||
|
void createBuildDir()
|
||
|
{
|
||
|
if (!CreateDirectory(BUILD_DIR, NULL))
|
||
|
{
|
||
|
if (GetLastError() != ERROR_ALREADY_EXISTS)
|
||
|
{
|
||
|
printf("Failed to create build directory.\n");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void deleteRevisionH()
|
||
|
{
|
||
|
if (!DeleteFile(REVISION_H_FILE))
|
||
|
{
|
||
|
printf("Failed to delete revision.h.\n");
|
||
|
// exit(EXIT_FAILURE);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int find_git_revision_number()
|
||
|
{
|
||
|
FILE *fp = popen("git rev-list HEAD", "r");
|
||
|
if (fp == NULL)
|
||
|
{
|
||
|
perror("popen failed");
|
||
|
return EXIT_FAILURE;
|
||
|
}
|
||
|
int cnt = 0;
|
||
|
char buffer[1024];
|
||
|
while (fgets(buffer, sizeof(buffer), fp) != NULL)
|
||
|
{
|
||
|
// printf("%s", buffer);
|
||
|
cnt++;
|
||
|
}
|
||
|
|
||
|
pclose(fp);
|
||
|
return cnt;
|
||
|
}
|
||
|
|
||
|
int get_latest_version_hash(char *buffer, int len)
|
||
|
{
|
||
|
FILE *fp = popen("git rev-list HEAD", "r");
|
||
|
if (fp == NULL)
|
||
|
{
|
||
|
perror("popen failed");
|
||
|
return EXIT_FAILURE;
|
||
|
}
|
||
|
int cnt = 0;
|
||
|
while (fgets(buffer, len, fp) != NULL)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
pclose(fp);
|
||
|
return cnt;
|
||
|
}
|
||
|
/**
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*/
|
||
|
int get_git_state()
|
||
|
{
|
||
|
FILE *fp = popen("git status", "r");
|
||
|
if (fp == NULL)
|
||
|
{
|
||
|
perror("popen failed");
|
||
|
return EXIT_FAILURE;
|
||
|
}
|
||
|
char buffer[1024];
|
||
|
while (fgets(buffer, sizeof(buffer), fp) != NULL)
|
||
|
{
|
||
|
|
||
|
if (strstr(buffer, "modified:") != NULL)
|
||
|
{
|
||
|
return 'M';
|
||
|
}
|
||
|
if (strstr(buffer, "new file") != NULL)
|
||
|
{
|
||
|
return 'N';
|
||
|
}
|
||
|
if (strstr(buffer, "deleted:") != NULL)
|
||
|
{
|
||
|
return 'D';
|
||
|
}
|
||
|
if (strstr(buffer, "renamed:") != NULL)
|
||
|
{
|
||
|
return 'R';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pclose(fp);
|
||
|
return '\0';
|
||
|
}
|
||
|
|
||
|
int write_notargeted_file(int version_number, char *version_hash, char sta)
|
||
|
{
|
||
|
FILE *fp = fopen(REVISION_H_FILE, "w+");
|
||
|
if (fp == NULL)
|
||
|
{
|
||
|
perror("fopen failed");
|
||
|
return EXIT_FAILURE;
|
||
|
}
|
||
|
fprintf(fp, "#ifndef REVISION_H_\n");
|
||
|
fprintf(fp, "#define REVISION_H_\n");
|
||
|
fprintf(fp, "#define FIRMWARE_VER_REV %d\n", version_number);
|
||
|
fprintf(fp, "#define FIRMWARE_VER_REV_STR \"%d\"\n", version_number);
|
||
|
fprintf(fp, "#define FIRMWARE_VER_HASH_STR ");
|
||
|
fprintf(fp, "\"");
|
||
|
for (int i = 0; i < 8; i++)
|
||
|
{
|
||
|
fprintf(fp, "%c", version_hash[i]);
|
||
|
}
|
||
|
fprintf(fp, "\"");
|
||
|
fprintf(fp, "\n");
|
||
|
fprintf(fp, "#define FIRMWARE_VER_MULTI \"%c\"\n", sta);
|
||
|
fprintf(fp, "#define FIRMWARE_BUILD_INFO \"r\"FIRMWARE_VER_REV_STR\" built on \"__DATE__\" \"__TIME__\n");
|
||
|
fprintf(fp, "#endif // REVISION_H_\n");
|
||
|
fclose(fp);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int print_git_submodule_info(FILE *fp_write)
|
||
|
{
|
||
|
FILE *fp = popen("git submodule status --recursive", "r");
|
||
|
if (fp == NULL)
|
||
|
{
|
||
|
perror("popen failed");
|
||
|
return EXIT_FAILURE;
|
||
|
}
|
||
|
char buffer[1024];
|
||
|
fprintf(fp_write, "\"\\\n");
|
||
|
while (fgets(buffer, 1024, fp) != NULL)
|
||
|
{
|
||
|
char *pos = buffer;
|
||
|
while (*pos != '\0' && *pos != '\r' && *pos != '\n')
|
||
|
{
|
||
|
fprintf(fp_write, "%c", *pos);
|
||
|
pos++;
|
||
|
}
|
||
|
fprintf(fp_write, " \\r\\n\\\n");
|
||
|
}
|
||
|
fprintf(fp_write, "\"\n");
|
||
|
|
||
|
pclose(fp);
|
||
|
}
|
||
|
int write_targeted_file(int version_number, char *version_hash, char sta)
|
||
|
{
|
||
|
FILE *fp = fopen(REVISION_H_FILE, "w+");
|
||
|
if (fp == NULL)
|
||
|
{
|
||
|
perror("fopen failed");
|
||
|
return EXIT_FAILURE;
|
||
|
}
|
||
|
fprintf(fp, "#ifndef REVISION_H_\n");
|
||
|
fprintf(fp, "#define REVISION_H_\n");
|
||
|
fprintf(fp, "#if (defined DEBUG)\n");
|
||
|
fprintf(fp, "#define FIRMWARE_TARGET_STR \"debug\"\n");
|
||
|
fprintf(fp, "#elif (defined _DEBUG)\n");
|
||
|
fprintf(fp, "#define FIRMWARE_TARGET_STR \"debug\"\n");
|
||
|
fprintf(fp, "#elif (defined __DEBUG)\n");
|
||
|
fprintf(fp, "#define FIRMWARE_TARGET_STR \"debug\"\n");
|
||
|
fprintf(fp, "#elif (defined DISTRIBUTION)\n");
|
||
|
fprintf(fp, "#define FIRMWARE_TARGET_STR \"distribution\"\n");
|
||
|
fprintf(fp, "#elif (defined _DISTRIBUTION)\n");
|
||
|
fprintf(fp, "#define FIRMWARE_TARGET_STR \"distribution\"\n");
|
||
|
fprintf(fp, "#elif (defined __DISTRIBUTION)\n");
|
||
|
fprintf(fp, "#define FIRMWARE_TARGET_STR \"distribution\"\n");
|
||
|
fprintf(fp, "#else\n");
|
||
|
fprintf(fp, "#define FIRMWARE_TARGET_STR \"release\"\n");
|
||
|
fprintf(fp, "#endif\n");
|
||
|
fprintf(fp, "#define FIRMWARE_VER_REV %d\n", version_number);
|
||
|
fprintf(fp, "#define FIRMWARE_VER_REV_STR \"%d\"\n", version_number);
|
||
|
fprintf(fp, "#define FIRMWARE_VER_HASH_STR ");
|
||
|
fprintf(fp, "\"");
|
||
|
for (int i = 0; i < 8; i++)
|
||
|
{
|
||
|
fprintf(fp, "%c", version_hash[i]);
|
||
|
}
|
||
|
fprintf(fp, "\"");
|
||
|
fprintf(fp, "\n");
|
||
|
fprintf(fp, "#define FIRMWARE_VER_MULTI \"%c\"\n", sta);
|
||
|
if (sta == '\0')
|
||
|
{
|
||
|
fprintf(fp, "#define FIRMWARE_BUILD_INFO \"r\"FIRMWARE_VER_REV_STR\"-\"FIRMWARE_VER_HASH_STR\" \"FIRMWARE_TARGET_STR\" \"\" built on \"__DATE__\" \"__TIME__\n");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
fprintf(fp, "#define FIRMWARE_BUILD_INFO \"r\"FIRMWARE_VER_REV_STR\"-\"FIRMWARE_VER_HASH_STR\"-\"FIRMWARE_VER_MULTI\" \"FIRMWARE_TARGET_STR\" \"\" built on \"__DATE__\" \"__TIME__\n");
|
||
|
}
|
||
|
fprintf(fp, "#define FIRMWARE_SUBMODULE_REV \\\n");
|
||
|
print_git_submodule_info(fp);
|
||
|
fprintf(fp, "#endif\n");
|
||
|
return 0;
|
||
|
}
|
||
|
int main(int argc, char **argv)
|
||
|
{
|
||
|
createBuildDir();
|
||
|
// deleteRevisionH();
|
||
|
int version_number = find_git_revision_number();
|
||
|
char version_hash[256];
|
||
|
get_latest_version_hash(version_hash, sizeof(version_hash));
|
||
|
printf("%s\n", version_hash);
|
||
|
char sta = get_git_state();
|
||
|
printf("git state is : %c\n", sta);
|
||
|
if (argv[2][0] != 's')
|
||
|
{
|
||
|
write_notargeted_file(version_number, version_hash, sta);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
write_targeted_file(version_number, version_hash, sta);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|