made build system more agnostic to the projet it builds

This commit is contained in:
2026-07-27 15:16:39 -04:00
parent 0d6b196ce6
commit b196641f18
19 changed files with 1717 additions and 1404 deletions
+463
View File
@@ -844,6 +844,467 @@ static int CommandRunPlan(int argc, char* argv[])
return 0;
}
static void GenerateProject(const char* projectName, const char* projectDir, const char* guid, const char* includeSearchPath, const char* extraDefs, const char* debuggerCommandArgs, const char Srcs[64][256], int SrcCount, const char* defCommon, const char* defDebug, const char* defProfile, const char* defRelease)
{
FilePathList* srcFiles = CreateFilePathList();
if (SrcCount > 0) {
for (int i = 0; i < SrcCount; i++) {
FilePathListAdd(srcFiles, Srcs[i]);
}
} else {
CollectFiles(projectDir, NULL, ".cpp", srcFiles);
CollectFiles(projectDir, NULL, ".c", srcFiles);
CollectFiles(projectDir, NULL, ".h", srcFiles);
CollectFiles(projectDir, NULL, ".hpp", srcFiles);
CollectFiles(projectDir, NULL, ".inl", srcFiles);
}
char* buf = (char*)ArenaPush(4 * 1024 * 1024);
int offset = 0;
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<Project DefaultTargets=\"Build\" ToolsVersion=\"17.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n"
" <ItemGroup Label=\"ProjectConfigurations\">\n"
" <ProjectConfiguration Include=\"Debug|x64\">\n"
" <Configuration>Debug</Configuration>\n"
" <Platform>x64</Platform>\n"
" </ProjectConfiguration>\n"
" <ProjectConfiguration Include=\"Profile|x64\">\n"
" <Configuration>Profile</Configuration>\n"
" <Platform>x64</Platform>\n"
" </ProjectConfiguration>\n"
" <ProjectConfiguration Include=\"Release|x64\">\n"
" <Configuration>Release</Configuration>\n"
" <Platform>x64</Platform>\n"
" </ProjectConfiguration>\n"
" </ItemGroup>\n"
" <PropertyGroup Label=\"Globals\">\n"
" <ProjectGuid>{%s}</ProjectGuid>\n"
" <Keyword>MakeFileProj</Keyword>\n"
" </PropertyGroup>\n"
" <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n", guid);
const char* configs[] = { "Debug", "Profile", "Release" };
for (int i = 0; i < 3; i++) {
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
" <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='%s|x64'\" Label=\"Configuration\">\n"
" <ConfigurationType>Makefile</ConfigurationType>\n"
" <UseDebugLibraries>false</UseDebugLibraries>\n"
" <PlatformToolset>v143</PlatformToolset>\n"
" </PropertyGroup>\n", configs[i]);
}
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
" <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n"
" <ImportGroup Label=\"ExtensionSettings\">\n"
" </ImportGroup>\n");
for (int i = 0; i < 3; i++) {
const char* cfg = configs[i];
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
" <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='%s|x64'\">\n"
" <NMakeBuildCommandLine>cd $(SolutionDir) &amp; misc\\build.bat %s $(SolutionName)</NMakeBuildCommandLine>\n"
" <NMakeReBuildCommandLine>cd $(SolutionDir) &amp; misc\\build.bat -clean %s $(SolutionName)</NMakeReBuildCommandLine>\n"
" <NMakeCleanCommandLine>cd $(SolutionDir) &amp; misc\\build.bat -clean %s $(SolutionName)</NMakeCleanCommandLine>\n"
" <NMakeOutput>$(SolutionDir)bin\\x64-%s\\%s.exe</NMakeOutput>\n"
" <NMakeIncludeSearchPath>%s$(NMakeIncludeSearchPath)</NMakeIncludeSearchPath>\n"
" <IncludePath>%s$(IncludePath)</IncludePath>\n",
cfg, cfg, cfg, cfg, cfg, projectName, includeSearchPath, includeSearchPath);
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
" <NMakePreprocessorDefinitions>");
if (defCommon && defCommon[0]) offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, "%s", defCommon);
if (strcmp(cfg, "Debug") == 0 && defDebug && defDebug[0]) offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, "%s", defDebug);
else if (strcmp(cfg, "Profile") == 0 && defProfile && defProfile[0]) offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, "%s", defProfile);
else if (strcmp(cfg, "Release") == 0 && defRelease && defRelease[0]) offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, "%s", defRelease);
if (extraDefs && extraDefs[0]) offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, "%s", extraDefs);
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, "$(NMakePreprocessorDefinitions)</NMakePreprocessorDefinitions>\n"
" <AdditionalOptions>/std:c++20</AdditionalOptions>\n"
" <LanguageStandard>stdcpp20</LanguageStandard>\n");
if (debuggerCommandArgs && debuggerCommandArgs[0] != '\0') {
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
" <LocalDebuggerCommandArguments>%s</LocalDebuggerCommandArguments>\n", debuggerCommandArgs);
}
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
" <LocalDebuggerWorkingDirectory>$(SolutionDir)bin\\x64-%s\\</LocalDebuggerWorkingDirectory>\n"
" <IntDir>$(SolutionDir)Intermediate\\x64-%s\\%s\\</IntDir>\n"
" <OutDir>$(SolutionDir)bin\\x64-%s\\</OutDir>\n"
" </PropertyGroup>\n", cfg, cfg, projectName, cfg);
}
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, " <ItemGroup>\n");
int projectDirLen = (int)strlen(projectDir);
for (int i = 0; i < srcFiles->Count; i++) {
const char* path = srcFiles->Paths[i];
const char* relPath = path;
if (strncmp(path, projectDir, projectDirLen) == 0 && (path[projectDirLen] == '\\' || path[projectDirLen] == '/')) {
relPath = path + projectDirLen + 1;
}
if (EndsWithNoCase(path, ".cpp") || EndsWithNoCase(path, ".c")) {
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, " <ClCompile Include=\"%s\" />\n", relPath);
} else {
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, " <ClInclude Include=\"%s\" />\n", relPath);
}
}
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset, " </ItemGroup>\n");
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
" <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n"
" <ImportGroup Label=\"ExtensionTargets\">\n"
" </ImportGroup>\n"
"</Project>\n");
char outPath[MAX_PATH_LEN];
snprintf(outPath, sizeof(outPath), "%s\\%s.vcxproj", projectDir, projectName);
WriteEntireFile(outPath, buf, offset);
printf("Generated %s\n", outPath);
// --- Generate .vcxproj.filters ---
char filters[512][256];
int filterCount = 0;
for (int i = 0; i < srcFiles->Count; i++) {
const char* path = srcFiles->Paths[i];
const char* relPath = path;
if (strncmp(path, projectDir, projectDirLen) == 0 && (path[projectDirLen] == '\\' || path[projectDirLen] == '/')) {
relPath = path + projectDirLen + 1;
}
const char* lastSlash = strrchr(relPath, '\\');
if (!lastSlash) lastSlash = strrchr(relPath, '/');
if (lastSlash) {
char dir[256];
int len = (int)(lastSlash - relPath);
if (len >= 256) len = 255;
memcpy(dir, relPath, len);
dir[len] = '\0';
char cur[256];
int c = 0;
for (int k = 0; k < len; k++) {
cur[c++] = dir[k];
if (dir[k] == '\\' || dir[k] == '/') {
cur[c - 1] = '\0';
bool found = false;
for (int f = 0; f < filterCount; f++) {
if (_stricmp(filters[f], cur) == 0) { found = true; break; }
}
if (!found && filterCount < 512) {
strcpy_s(filters[filterCount++], 256, cur);
}
cur[c - 1] = '\\';
}
}
bool found = false;
for (int f = 0; f < filterCount; f++) {
if (_stricmp(filters[f], dir) == 0) { found = true; break; }
}
if (!found && filterCount < 512) {
strcpy_s(filters[filterCount++], 256, dir);
}
}
}
char* filterBuf = (char*)ArenaPush(4 * 1024 * 1024);
int filterOffset = 0;
filterOffset += sprintf_s(filterBuf + filterOffset, (4 * 1024 * 1024) - filterOffset,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n"
" <ItemGroup>\n");
for (int i = 0; i < filterCount; i++) {
filterOffset += sprintf_s(filterBuf + filterOffset, (4 * 1024 * 1024) - filterOffset,
" <Filter Include=\"%s\">\n"
" </Filter>\n", filters[i]);
}
filterOffset += sprintf_s(filterBuf + filterOffset, (4 * 1024 * 1024) - filterOffset,
" </ItemGroup>\n"
" <ItemGroup>\n");
for (int i = 0; i < srcFiles->Count; i++) {
const char* path = srcFiles->Paths[i];
const char* relPath = path;
if (strncmp(path, projectDir, projectDirLen) == 0 && (path[projectDirLen] == '\\' || path[projectDirLen] == '/')) {
relPath = path + projectDirLen + 1;
}
const char* lastSlash = strrchr(relPath, '\\');
if (!lastSlash) lastSlash = strrchr(relPath, '/');
const char* type = (EndsWithNoCase(path, ".cpp") || EndsWithNoCase(path, ".c")) ? "ClCompile" : "ClInclude";
if (lastSlash) {
char dir[256];
int len = (int)(lastSlash - relPath);
if (len >= 256) len = 255;
memcpy(dir, relPath, len);
dir[len] = '\0';
filterOffset += sprintf_s(filterBuf + filterOffset, (4 * 1024 * 1024) - filterOffset,
" <%s Include=\"%s\">\n"
" <Filter>%s</Filter>\n"
" </%s>\n", type, relPath, dir, type);
} else {
filterOffset += sprintf_s(filterBuf + filterOffset, (4 * 1024 * 1024) - filterOffset,
" <%s Include=\"%s\" />\n", type, relPath);
}
}
filterOffset += sprintf_s(filterBuf + filterOffset, (4 * 1024 * 1024) - filterOffset,
" </ItemGroup>\n</Project>\n");
char filterOutPath[MAX_PATH_LEN];
snprintf(filterOutPath, sizeof(filterOutPath), "%s\\%s.vcxproj.filters", projectDir, projectName);
WriteEntireFile(filterOutPath, filterBuf, filterOffset);
printf("Generated %s\n", filterOutPath);
}
static void GenerateSolution(const char* slnName, const char** projectNames, const char** projectDirs, const char** projectGuids, int projCount)
{
char* buf = (char*)ArenaPush(4 * 1024 * 1024);
int offset = 0;
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
"Microsoft Visual Studio Solution File, Format Version 12.00\n"
"# Visual Studio Version 17\n"
"VisualStudioVersion = 17.0.31903.59\n"
"MinimumVisualStudioVersion = 10.0.40219.1\n");
for (int i = 0; i < projCount; i++) {
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
"Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"%s\", \"%s\\%s.vcxproj\", \"{%s}\"\n"
"EndProject\n", projectNames[i], projectDirs[i], projectNames[i], projectGuids[i]);
}
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
"Global\n"
" GlobalSection(SolutionConfigurationPlatforms) = preSolution\n"
" Debug|x64 = Debug|x64\n"
" Profile|x64 = Profile|x64\n"
" Release|x64 = Release|x64\n"
" EndGlobalSection\n"
" GlobalSection(ProjectConfigurationPlatforms) = postSolution\n");
for (int i = 0; i < projCount; i++) {
const char* configs[] = { "Debug", "Profile", "Release" };
for (int c = 0; c < 3; c++) {
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
" {%s}.%s|x64.ActiveCfg = %s|x64\n"
" {%s}.%s|x64.Build.0 = %s|x64\n",
projectGuids[i], configs[c], configs[c],
projectGuids[i], configs[c], configs[c]);
}
}
offset += sprintf_s(buf + offset, (4 * 1024 * 1024) - offset,
" EndGlobalSection\n"
" GlobalSection(SolutionProperties) = preSolution\n"
" HideSolutionNode = FALSE\n"
" EndGlobalSection\n"
"EndGlobal\n");
char outPath[MAX_PATH_LEN];
snprintf(outPath, sizeof(outPath), "%s.sln", slnName);
WriteEntireFile(outPath, buf, offset);
printf("Generated %s\n", outPath);
}
typedef struct {
char Name[64];
char Dir[256];
char Guid[64];
char ExtraDef[1024];
char IncludeSearch[1024];
char DebugCmd[1024];
char Srcs[64][256];
int SrcCount;
} VSProject;
typedef struct {
char Name[64];
char Projects[16][64];
int ProjCount;
} VSSolution;
static void GenerateDeterministicGuid(const char* input, char* outputGuid) {
unsigned long long hash1 = 14695981039346656037ULL;
unsigned long long hash2 = 1099511628211ULL;
for (const char* c = input; *c; ++c) {
hash1 ^= (unsigned long long)(*c);
hash1 *= 1099511628211ULL;
hash2 ^= (unsigned long long)(*c);
hash2 *= 137ULL;
}
sprintf_s(outputGuid, 64, "%08x-%04x-%04x-%04x-%012llx",
(unsigned int)(hash1 >> 32),
(unsigned int)((hash1 >> 16) & 0xFFFF),
(unsigned int)(hash1 & 0xFFFF),
(unsigned int)((hash2 >> 48) & 0xFFFF),
hash2 & 0xFFFFFFFFFFFFULL);
}
static int CommandGenVS(int argc, char* argv[])
{
if (argc < 3) {
printf("[ERROR] Usage: build_system gen_vs <plan_file>\n");
return 1;
}
const char* planFile = argv[2];
FileContent planContent = ReadEntireFile(planFile);
if (!planContent.Data) {
printf("[ERROR] Could not read VS plan: %s\n", planFile);
return 1;
}
char globalInclude[1024] = {0};
char globalDefCommon[1024] = {0};
char globalDefDebug[1024] = {0};
char globalDefProfile[1024] = {0};
char globalDefRelease[1024] = {0};
VSProject projects[16];
int projCount = 0;
VSSolution solutions[8];
int slnCount = 0;
const char* pos = planContent.Data;
while (*pos) {
const char* lineStart = pos;
while (*pos && *pos != '\n' && *pos != '\r') pos++;
int lineLen = (int)(pos - lineStart);
if (lineLen > 0) {
if (lineLen > 15 && memcmp(lineStart, "GLOBAL_INCLUDE ", 15) == 0) {
int nLen = lineLen - 15;
if (nLen >= 1024) nLen = 1023;
memcpy(globalInclude, lineStart + 15, nLen);
globalInclude[nLen] = '\0';
}
else if (lineLen > 11 && memcmp(lineStart, "DEF_COMMON ", 11) == 0) {
int nLen = lineLen - 11;
if (nLen >= 1024) nLen = 1023;
memcpy(globalDefCommon, lineStart + 11, nLen);
globalDefCommon[nLen] = '\0';
}
else if (lineLen > 10 && memcmp(lineStart, "DEF_DEBUG ", 10) == 0) {
int nLen = lineLen - 10;
if (nLen >= 1024) nLen = 1023;
memcpy(globalDefDebug, lineStart + 10, nLen);
globalDefDebug[nLen] = '\0';
}
else if (lineLen > 12 && memcmp(lineStart, "DEF_PROFILE ", 12) == 0) {
int nLen = lineLen - 12;
if (nLen >= 1024) nLen = 1023;
memcpy(globalDefProfile, lineStart + 12, nLen);
globalDefProfile[nLen] = '\0';
}
else if (lineLen > 12 && memcmp(lineStart, "DEF_RELEASE ", 12) == 0) {
int nLen = lineLen - 12;
if (nLen >= 1024) nLen = 1023;
memcpy(globalDefRelease, lineStart + 12, nLen);
globalDefRelease[nLen] = '\0';
}
else if (lineLen > 8 && memcmp(lineStart, "PROJECT ", 8) == 0) {
if (projCount < 16) {
VSProject* p = &projects[projCount++];
memset(p, 0, sizeof(VSProject));
char lineBuf[1024];
int nLen = lineLen - 8;
if (nLen >= 1024) nLen = 1023;
memcpy(lineBuf, lineStart + 8, nLen);
lineBuf[nLen] = '\0';
int scanned = sscanf_s(lineBuf, "%s %s %s", p->Name, (unsigned)_countof(p->Name), p->Dir, (unsigned)_countof(p->Dir), p->Guid, (unsigned)_countof(p->Guid));
if (scanned < 2) {
strcpy_s(p->Dir, sizeof(p->Dir), p->Name);
}
if (scanned < 3) {
GenerateDeterministicGuid(p->Name, p->Guid);
}
}
}
else if (lineLen > 12 && memcmp(lineStart, "PROJECT_SRC ", 12) == 0 && projCount > 0) {
VSProject* p = &projects[projCount - 1];
if (p->SrcCount < 64) {
int nLen = lineLen - 12;
if (nLen >= 256) nLen = 255;
memcpy(p->Srcs[p->SrcCount], lineStart + 12, nLen);
p->Srcs[p->SrcCount][nLen] = '\0';
p->SrcCount++;
}
}
else if (lineLen > 10 && memcmp(lineStart, "EXTRA_DEF ", 10) == 0 && projCount > 0) {
VSProject* p = &projects[projCount - 1];
int nLen = lineLen - 10;
if (nLen >= 1024) nLen = 1023;
memcpy(p->ExtraDef, lineStart + 10, nLen);
p->ExtraDef[nLen] = '\0';
}
else if (lineLen > 15 && memcmp(lineStart, "INCLUDE_SEARCH ", 15) == 0 && projCount > 0) {
VSProject* p = &projects[projCount - 1];
int nLen = lineLen - 15;
if (nLen >= 1024) nLen = 1023;
memcpy(p->IncludeSearch, lineStart + 15, nLen);
p->IncludeSearch[nLen] = '\0';
}
else if (lineLen > 10 && memcmp(lineStart, "DEBUG_CMD ", 10) == 0 && projCount > 0) {
VSProject* p = &projects[projCount - 1];
int nLen = lineLen - 10;
if (nLen >= 1024) nLen = 1023;
memcpy(p->DebugCmd, lineStart + 10, nLen);
p->DebugCmd[nLen] = '\0';
}
else if (lineLen > 9 && memcmp(lineStart, "SOLUTION ", 9) == 0) {
if (slnCount < 8) {
VSSolution* s = &solutions[slnCount++];
memset(s, 0, sizeof(VSSolution));
int nLen = lineLen - 9;
if (nLen >= 64) nLen = 63;
memcpy(s->Name, lineStart + 9, nLen);
s->Name[nLen] = '\0';
}
}
else if (lineLen > 9 && memcmp(lineStart, "SLN_PROJ ", 9) == 0 && slnCount > 0) {
VSSolution* s = &solutions[slnCount - 1];
if (s->ProjCount < 16) {
int nLen = lineLen - 9;
if (nLen >= 64) nLen = 63;
memcpy(s->Projects[s->ProjCount], lineStart + 9, nLen);
s->Projects[s->ProjCount][nLen] = '\0';
s->ProjCount++;
}
}
}
while (*pos == '\n' || *pos == '\r') pos++;
}
for (int i = 0; i < projCount; i++) {
VSProject* p = &projects[i];
const char* incs = p->IncludeSearch[0] != '\0' ? p->IncludeSearch : globalInclude;
GenerateProject(p->Name, p->Dir, p->Guid, incs, p->ExtraDef, p->DebugCmd, p->Srcs, p->SrcCount, globalDefCommon, globalDefDebug, globalDefProfile, globalDefRelease);
}
for (int i = 0; i < slnCount; i++) {
VSSolution* s = &solutions[i];
const char* pNames[16];
const char* pDirs[16];
const char* pGuids[16];
for (int j = 0; j < s->ProjCount; j++) {
pNames[j] = s->Projects[j];
for (int k = 0; k < projCount; k++) {
if (strcmp(projects[k].Name, s->Projects[j]) == 0) {
pDirs[j] = projects[k].Dir;
pGuids[j] = projects[k].Guid;
break;
}
}
}
GenerateSolution(s->Name, pNames, pDirs, pGuids, s->ProjCount);
}
return 0;
}
int main(int argc, char* argv[])
{
@@ -855,6 +1316,7 @@ int main(int argc, char* argv[])
printf("Usage: build_system <command> [args...]\n");
printf("Commands:\n");
printf(" run_plan <plan_file> Run the build from a generated plan file\n");
printf(" gen_vs Generate Visual Studio project and solution files\n");
printf(" check_alive Returns 0 immediately to test execution\n");
return 1;
}
@@ -862,6 +1324,7 @@ int main(int argc, char* argv[])
const char* cmd = argv[1];
if (strcmp(cmd, "check_alive") == 0) return 0;
if (strcmp(cmd, "run_plan") == 0) return CommandRunPlan(argc, argv);
if (strcmp(cmd, "gen_vs") == 0) return CommandGenVS(argc, argv);
printf("[ERROR] Unknown command: %s\n", cmd);
return 1;