Files
Juliet/tools/build_system.cpp
T

1059 lines
33 KiB
C++

// tools/build.cpp - Fast native C++ build orchestrator for Juliet project
// Replaces all PowerShell scripts and orchestrates the full build process
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <windows.h>
// ============================================================================
// Minimal string / path helpers (no STL, no std::filesystem — pure speed)
// ============================================================================
static constexpr int MaxPath = 1024;
static constexpr int MaxFiles = 4096;
static constexpr int MaxHashEntries = 512;
struct FixedString
{
char Data[MaxPath];
int Length;
FixedString()
: Data{}
, Length(0)
{
}
void Set(const char* str)
{
assert(str != nullptr);
Length = 0;
while (str[Length] != '\0' && Length < MaxPath - 1)
{
Data[Length] = str[Length];
Length++;
}
Data[Length] = '\0';
}
void Set(const char* str, int len)
{
assert(str != nullptr);
assert(len >= 0 && len < MaxPath);
for (int i = 0; i < len; i++)
{
Data[i] = str[i];
}
Data[len] = '\0';
Length = len;
}
void Append(const char* str)
{
assert(str != nullptr);
int i = 0;
while (str[i] != '\0' && Length < MaxPath - 1)
{
Data[Length++] = str[i++];
}
Data[Length] = '\0';
}
void AppendChar(char c)
{
if (Length < MaxPath - 1)
{
Data[Length++] = c;
Data[Length] = '\0';
}
}
[[nodiscard]] bool Equals(const char* other) const
{
assert(other != nullptr);
return strcmp(Data, other) == 0;
}
[[nodiscard]] bool EqualsNoCase(const char* other) const
{
assert(other != nullptr);
return _stricmp(Data, other) == 0;
}
};
[[nodiscard]] static bool StringContains(const char* haystack, const char* needle)
{
assert(haystack != nullptr);
assert(needle != nullptr);
return strstr(haystack, needle) != nullptr;
}
[[nodiscard]] static bool EndsWithNoCase(const char* str, const char* suffix)
{
assert(str != nullptr);
assert(suffix != nullptr);
int strLen = static_cast<int>(strlen(str));
int sufLen = static_cast<int>(strlen(suffix));
if (sufLen > strLen)
{
return false;
}
return _stricmp(str + strLen - sufLen, suffix) == 0;
}
// ============================================================================
// File I/O helpers using Win32
// ============================================================================
struct FileContent
{
char* Data;
DWORD Size;
};
[[nodiscard]] static FileContent ReadEntireFile(const char* path)
{
assert(path != nullptr);
FileContent result = { nullptr, 0 };
HANDLE file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, nullptr);
if (file == INVALID_HANDLE_VALUE)
{
return result;
}
DWORD size = GetFileSize(file, nullptr);
if (size == INVALID_FILE_SIZE || size == 0)
{
CloseHandle(file);
return result;
}
char* buffer = static_cast<char*>(HeapAlloc(GetProcessHeap(), 0, size + 1));
if (buffer == nullptr)
{
CloseHandle(file);
return result;
}
DWORD bytesRead = 0;
ReadFile(file, buffer, size, &bytesRead, nullptr);
CloseHandle(file);
buffer[bytesRead] = '\0';
result.Data = buffer;
result.Size = bytesRead;
return result;
}
static void FreeFileContent(FileContent& content)
{
if (content.Data != nullptr)
{
HeapFree(GetProcessHeap(), 0, content.Data);
content.Data = nullptr;
content.Size = 0;
}
}
static bool WriteEntireFile(const char* path, const char* data, DWORD size)
{
assert(path != nullptr);
assert(data != nullptr);
HANDLE file = CreateFileA(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, nullptr);
if (file == INVALID_HANDLE_VALUE)
{
return false;
}
DWORD written = 0;
WriteFile(file, data, size, &written, nullptr);
CloseHandle(file);
return written == size;
}
[[nodiscard]] static bool FileExists(const char* path)
{
assert(path != nullptr);
DWORD attribs = GetFileAttributesA(path);
return (attribs != INVALID_FILE_ATTRIBUTES) && !(attribs & FILE_ATTRIBUTE_DIRECTORY);
}
[[nodiscard]] static bool DirectoryExists(const char* path)
{
assert(path != nullptr);
DWORD attribs = GetFileAttributesA(path);
return (attribs != INVALID_FILE_ATTRIBUTES) && (attribs & FILE_ATTRIBUTE_DIRECTORY);
}
static void EnsureDirectoryExists(const char* path)
{
assert(path != nullptr);
CreateDirectoryA(path, nullptr);
}
[[nodiscard]] static int64_t GetFileModTimeInt64(const char* path)
{
assert(path != nullptr);
WIN32_FILE_ATTRIBUTE_DATA data;
if (GetFileAttributesExA(path, GetFileExInfoStandard, &data))
{
ULARGE_INTEGER li;
li.LowPart = data.ftLastWriteTime.dwLowDateTime;
li.HighPart = data.ftLastWriteTime.dwHighDateTime;
return static_cast<int64_t>(li.QuadPart);
}
return 0;
}
// ============================================================================
// FNV-1a hash
// ============================================================================
[[nodiscard]] static uint64_t ComputeFnv1aHash(const char* data, DWORD size)
{
assert(data != nullptr);
uint64_t hash = 14695981039346656037ULL;
for (DWORD i = 0; i < size; i++)
{
hash ^= static_cast<uint64_t>(static_cast<unsigned char>(data[i]));
hash *= 1099511628211ULL;
}
return hash;
}
// ============================================================================
// Hash cache for unity files
// ============================================================================
struct HashEntry
{
FixedString Path;
char HashStr[32];
};
struct HashCache
{
HashEntry* Entries;
int Count;
HashCache()
{
Entries = static_cast<HashEntry*>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HashEntry) * MaxHashEntries));
assert(Entries != nullptr);
Count = 0;
}
~HashCache()
{
if (Entries != nullptr)
{
HeapFree(GetProcessHeap(), 0, Entries);
Entries = nullptr;
}
}
HashCache(const HashCache&) = delete;
HashCache& operator=(const HashCache&) = delete;
[[nodiscard]] const char* Find(const char* path) const
{
assert(path != nullptr);
for (int i = 0; i < Count; i++)
{
if (Entries[i].Path.Equals(path))
{
return Entries[i].HashStr;
}
}
return nullptr;
}
void Add(const char* path, const char* hash)
{
assert(path != nullptr);
assert(hash != nullptr);
if (Count < MaxHashEntries && Entries != nullptr)
{
Entries[Count].Path.Set(path);
strncpy_s(Entries[Count].HashStr, hash, sizeof(Entries[Count].HashStr) - 1);
Count++;
}
}
};
static void LoadHashCache(const char* cachePath, HashCache& cache)
{
assert(cachePath != nullptr);
FileContent content = ReadEntireFile(cachePath);
if (content.Data == nullptr)
{
return;
}
const char* pos = content.Data;
while (*pos != '\0')
{
const char* lineStart = pos;
while (*pos != '\0' && *pos != '\n' && *pos != '\r')
{
pos++;
}
int lineLen = static_cast<int>(pos - lineStart);
if (lineLen > 0)
{
const char* sep = nullptr;
for (const char* c = lineStart; c < lineStart + lineLen; c++)
{
if (*c == '=')
{
sep = c;
break;
}
}
if (sep != nullptr)
{
FixedString key;
key.Set(lineStart, static_cast<int>(sep - lineStart));
int valLen = static_cast<int>((lineStart + lineLen) - (sep + 1));
char val[32] = {};
if (valLen > 0 && valLen < 32)
{
memcpy(val, sep + 1, static_cast<size_t>(valLen));
val[valLen] = '\0';
}
cache.Add(key.Data, val);
}
}
while (*pos == '\n' || *pos == '\r')
{
pos++;
}
}
FreeFileContent(content);
}
static void SaveHashCache(const char* cachePath, const HashCache& cache)
{
assert(cachePath != nullptr);
size_t bufSize = static_cast<size_t>(MaxHashEntries) * (MaxPath + 32 + 2);
char* buffer = static_cast<char*>(HeapAlloc(GetProcessHeap(), 0, bufSize));
if (buffer == nullptr)
{
return;
}
int offset = 0;
for (int i = 0; i < cache.Count; i++)
{
int written = sprintf_s(buffer + offset, bufSize - static_cast<size_t>(offset), "%s=%s\n",
cache.Entries[i].Path.Data, cache.Entries[i].HashStr);
if (written > 0)
{
offset += written;
}
}
WriteEntireFile(cachePath, buffer, static_cast<DWORD>(offset));
HeapFree(GetProcessHeap(), 0, buffer);
}
// ============================================================================
// Clean #pragma message lines and compute hash for a unity file
// ============================================================================
struct CleanResult
{
char HashStr[32];
bool WasCleaned;
};
[[nodiscard]] static CleanResult CleanAndHashUnityFile(const char* filePath)
{
assert(filePath != nullptr);
CleanResult result = { {}, false };
FileContent content = ReadEntireFile(filePath);
if (content.Data == nullptr)
{
result.HashStr[0] = '\0';
return result;
}
bool hasPragma = StringContains(content.Data, "#pragma message");
if (hasPragma)
{
// Filter out #pragma message lines
char* filtered = static_cast<char*>(HeapAlloc(GetProcessHeap(), 0, content.Size + 1));
assert(filtered != nullptr);
DWORD filteredSize = 0;
const char* pos = content.Data;
while (*pos != '\0')
{
const char* lineStart = pos;
while (*pos != '\0' && *pos != '\n')
{
pos++;
}
bool skipLine = false;
for (const char* c = lineStart; c < pos; c++)
{
if (c + 15 <= content.Data + content.Size && memcmp(c, "#pragma message", 15) == 0)
{
skipLine = true;
break;
}
}
if (!skipLine)
{
DWORD lineLen = static_cast<DWORD>(pos - lineStart);
memcpy(filtered + filteredSize, lineStart, lineLen);
filteredSize += lineLen;
if (*pos == '\n')
{
filtered[filteredSize++] = '\n';
}
}
if (*pos == '\n')
{
pos++;
}
}
filtered[filteredSize] = '\0';
WriteEntireFile(filePath, filtered, filteredSize);
uint64_t hashVal = ComputeFnv1aHash(filtered, filteredSize);
sprintf_s(result.HashStr, "%llu", hashVal);
result.WasCleaned = true;
HeapFree(GetProcessHeap(), 0, filtered);
}
else
{
uint64_t hashVal = ComputeFnv1aHash(content.Data, content.Size);
sprintf_s(result.HashStr, "%llu", hashVal);
}
FreeFileContent(content);
return result;
}
// ============================================================================
// Recursive directory scan for newest source file time
// ============================================================================
[[nodiscard]] static int64_t GetNewestSourceTimeRecursive(const char* dirPath)
{
assert(dirPath != nullptr);
int64_t newest = 0;
if (!DirectoryExists(dirPath))
{
return newest;
}
FixedString searchPath;
searchPath.Set(dirPath);
searchPath.Append("\\*");
WIN32_FIND_DATAA findData;
HANDLE hFind = FindFirstFileA(searchPath.Data, &findData);
if (hFind == INVALID_HANDLE_VALUE)
{
return newest;
}
do
{
if (findData.cFileName[0] == '.' &&
(findData.cFileName[1] == '\0' || (findData.cFileName[1] == '.' && findData.cFileName[2] == '\0')))
{
continue;
}
FixedString fullPath;
fullPath.Set(dirPath);
fullPath.AppendChar('\\');
fullPath.Append(findData.cFileName);
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
int64_t subNewest = GetNewestSourceTimeRecursive(fullPath.Data);
if (subNewest > newest)
{
newest = subNewest;
}
}
else
{
if (EndsWithNoCase(findData.cFileName, ".cpp") || EndsWithNoCase(findData.cFileName, ".h") ||
EndsWithNoCase(findData.cFileName, ".hpp") || EndsWithNoCase(findData.cFileName, ".inl") ||
EndsWithNoCase(findData.cFileName, ".c"))
{
ULARGE_INTEGER li;
li.LowPart = findData.ftLastWriteTime.dwLowDateTime;
li.HighPart = findData.ftLastWriteTime.dwHighDateTime;
int64_t ft = static_cast<int64_t>(li.QuadPart);
if (ft > newest)
{
newest = ft;
}
}
}
}
while (FindNextFileA(hFind, &findData));
FindClose(hFind);
return newest;
}
// ============================================================================
// Collect unity files recursively from Intermediate/
// ============================================================================
struct FilePathList
{
FixedString* Paths;
int Count;
FilePathList()
{
Paths = static_cast<FixedString*>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FixedString) * MaxFiles));
assert(Paths != nullptr);
Count = 0;
}
~FilePathList()
{
if (Paths != nullptr)
{
HeapFree(GetProcessHeap(), 0, Paths);
Paths = nullptr;
}
}
FilePathList(const FilePathList&) = delete;
FilePathList& operator=(const FilePathList&) = delete;
void Add(const char* path)
{
assert(path != nullptr);
if (Count < MaxFiles && Paths != nullptr)
{
Paths[Count].Set(path);
Count++;
}
}
};
static void CollectUnityFiles(const char* dirPath, FilePathList& list)
{
assert(dirPath != nullptr);
FixedString searchPath;
searchPath.Set(dirPath);
searchPath.Append("\\*");
WIN32_FIND_DATAA findData;
HANDLE hFind = FindFirstFileA(searchPath.Data, &findData);
if (hFind == INVALID_HANDLE_VALUE)
{
return;
}
do
{
if (findData.cFileName[0] == '.' &&
(findData.cFileName[1] == '\0' || (findData.cFileName[1] == '.' && findData.cFileName[2] == '\0')))
{
continue;
}
FixedString fullPath;
fullPath.Set(dirPath);
fullPath.AppendChar('\\');
fullPath.Append(findData.cFileName);
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
CollectUnityFiles(fullPath.Data, list);
}
else
{
if (StringContains(findData.cFileName, "_Unity") && EndsWithNoCase(findData.cFileName, ".cpp"))
{
list.Add(fullPath.Data);
}
}
}
while (FindNextFileA(hFind, &findData));
FindClose(hFind);
}
// ============================================================================
// Commands
// ============================================================================
struct BuildStep {
char StepName[256];
char OutputFile[MaxPath];
char Depends[MaxPath];
char Command[MaxFiles];
};
struct BuildConfig {
char Name[64];
char FastBuildTargets[512];
BuildStep Steps[32];
int StepCount;
};
static int CommandRunPlan(int argc, char* argv[])
{
if (argc < 3)
{
printf("[ERROR] Usage: build_system run_plan <plan_file.txt>\n");
return 1;
}
const char* planFile = argv[2];
FileContent planContent = ReadEntireFile(planFile);
if (!planContent.Data)
{
printf("[ERROR] Could not read build plan: %s\n", planFile);
return 1;
}
EnsureDirectoryExists("Intermediate");
DeleteFileA("Intermediate\\step_timings.tmp");
DeleteFileA("Intermediate\\built_outputs.tmp");
LARGE_INTEGER perfStart, perfEnd, perfFreq;
QueryPerformanceCounter(&perfStart);
QueryPerformanceFrequency(&perfFreq);
// Parse plan file
BuildConfig configs[4] = {};
int configCount = 0;
const char* pos = planContent.Data;
while (*pos != '\0')
{
const char* lineStart = pos;
while (*pos != '\0' && *pos != '\n' && *pos != '\r') pos++;
int lineLen = static_cast<int>(pos - lineStart);
if (lineLen > 0)
{
if (lineLen > 7 && memcmp(lineStart, "CONFIG ", 7) == 0)
{
if (configCount < 4)
{
int nLen = lineLen - 7;
if (nLen >= static_cast<int>(sizeof(configs[configCount].Name))) nLen = static_cast<int>(sizeof(configs[configCount].Name)) - 1;
memcpy(configs[configCount].Name, lineStart + 7, nLen);
configs[configCount].Name[nLen] = '\0';
configCount++;
}
}
else if (configCount > 0)
{
BuildConfig& curCfg = configs[configCount - 1];
if (lineLen > 10 && memcmp(lineStart, "FASTBUILD ", 10) == 0)
{
int nLen = lineLen - 10;
if (nLen >= static_cast<int>(sizeof(curCfg.FastBuildTargets))) nLen = static_cast<int>(sizeof(curCfg.FastBuildTargets)) - 1;
memcpy(curCfg.FastBuildTargets, lineStart + 10, nLen);
curCfg.FastBuildTargets[nLen] = '\0';
}
else if (lineLen > 5 && memcmp(lineStart, "STEP ", 5) == 0)
{
if (curCfg.StepCount < 32)
{
int nLen = lineLen - 5;
if (nLen >= static_cast<int>(sizeof(curCfg.Steps[curCfg.StepCount].StepName))) nLen = static_cast<int>(sizeof(curCfg.Steps[curCfg.StepCount].StepName)) - 1;
memcpy(curCfg.Steps[curCfg.StepCount].StepName, lineStart + 5, nLen);
curCfg.Steps[curCfg.StepCount].StepName[nLen] = '\0';
curCfg.StepCount++;
}
}
else if (lineLen > 7 && memcmp(lineStart, "OUTPUT ", 7) == 0 && curCfg.StepCount > 0)
{
int nLen = lineLen - 7;
if (nLen >= static_cast<int>(sizeof(curCfg.Steps[curCfg.StepCount - 1].OutputFile))) nLen = static_cast<int>(sizeof(curCfg.Steps[curCfg.StepCount - 1].OutputFile)) - 1;
memcpy(curCfg.Steps[curCfg.StepCount - 1].OutputFile, lineStart + 7, nLen);
curCfg.Steps[curCfg.StepCount - 1].OutputFile[nLen] = '\0';
}
else if (lineLen > 8 && memcmp(lineStart, "DEPENDS ", 8) == 0 && curCfg.StepCount > 0)
{
int nLen = lineLen - 8;
if (nLen >= static_cast<int>(sizeof(curCfg.Steps[curCfg.StepCount - 1].Depends))) nLen = static_cast<int>(sizeof(curCfg.Steps[curCfg.StepCount - 1].Depends)) - 1;
memcpy(curCfg.Steps[curCfg.StepCount - 1].Depends, lineStart + 8, nLen);
curCfg.Steps[curCfg.StepCount - 1].Depends[nLen] = '\0';
}
else if (lineLen > 8 && memcmp(lineStart, "COMMAND ", 8) == 0 && curCfg.StepCount > 0)
{
int nLen = lineLen - 8;
if (nLen >= static_cast<int>(sizeof(curCfg.Steps[curCfg.StepCount - 1].Command))) nLen = static_cast<int>(sizeof(curCfg.Steps[curCfg.StepCount - 1].Command)) - 1;
memcpy(curCfg.Steps[curCfg.StepCount - 1].Command, lineStart + 8, nLen);
curCfg.Steps[curCfg.StepCount - 1].Command[nLen] = '\0';
}
}
}
while (*pos == '\n' || *pos == '\r') pos++;
}
FreeFileContent(planContent);
bool anyBuilt = false;
char timingsBuf[MaxFiles * 2] = {};
int timingsLen = 0;
auto AppendTiming = [&](const char* stepName, double secs)
{
timingsLen += sprintf_s(timingsBuf + timingsLen, sizeof(timingsBuf) - timingsLen, "%s|%.3fs\n", stepName, secs);
};
auto ExecuteCmd = [](const char* cmdLine) -> int
{
STARTUPINFOA si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi = {};
DWORD exitCode = 1;
char* cmdBuf = static_cast<char*>(HeapAlloc(GetProcessHeap(), 0, MaxFiles));
if (!cmdBuf) return 1;
strcpy_s(cmdBuf, MaxFiles, cmdLine);
if (CreateProcessA(nullptr, cmdBuf, nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, &exitCode);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
printf("[ERROR] Failed to execute command.\n");
HeapFree(GetProcessHeap(), 0, cmdBuf);
return 1;
}
HeapFree(GetProcessHeap(), 0, cmdBuf);
return static_cast<int>(exitCode);
};
for (int i = 0; i < configCount; i++)
{
BuildConfig& cfg = configs[i];
printf("\n==============================================================================\n");
printf("Generating Unity Files into Intermediate/ via FASTBuild [%s]...\n", cfg.Name);
printf("==============================================================================\n");
if (cfg.FastBuildTargets[0] != '\0')
{
char fbuildCmd[MaxPath];
sprintf_s(fbuildCmd, sizeof(fbuildCmd), "misc\\fbuild.exe %s", cfg.FastBuildTargets);
LARGE_INTEGER tStart, tEnd;
QueryPerformanceCounter(&tStart);
int res = ExecuteCmd(fbuildCmd);
QueryPerformanceCounter(&tEnd);
char stepName[MaxPath];
sprintf_s(stepName, sizeof(stepName), "FASTBuild Unity Gen [%s]", cfg.Name);
AppendTiming(stepName, static_cast<double>(tEnd.QuadPart - tStart.QuadPart) / static_cast<double>(perfFreq.QuadPart));
if (res != 0)
{
printf("[BUILD FAILED] FASTBuild failed for %s.\n", cfg.Name);
return res;
}
}
HashCache oldCache;
LoadHashCache("Intermediate\\.unity_hashes_cache", oldCache);
HashCache newCache;
bool unityChanged = false;
FilePathList unityFiles;
CollectUnityFiles("Intermediate", unityFiles);
if (unityFiles.Count == 0)
{
unityChanged = true;
}
else
{
for (int k = 0; k < unityFiles.Count; k++)
{
CleanResult cr = CleanAndHashUnityFile(unityFiles.Paths[k].Data);
newCache.Add(unityFiles.Paths[k].Data, cr.HashStr);
const char* oldHash = oldCache.Find(unityFiles.Paths[k].Data);
if (oldHash == nullptr || strcmp(oldHash, cr.HashStr) != 0)
{
unityChanged = true;
}
}
}
SaveHashCache("Intermediate\\.unity_hashes_cache", newCache);
if (unityChanged)
{
printf("[FASTBuild] Unity structure or source file list changed.\n");
}
else
{
printf("[FASTBuild] No Unity file structure changes detected [up-to-date].\n");
}
printf("\n==============================================================================\n");
printf("Compiling Targets [%s]...\n", cfg.Name);
printf("==============================================================================\n");
struct DirCacheEntry { FixedString Path; int64_t Time; };
DirCacheEntry dirCache[64];
int dirCacheCount = 0;
auto GetDirTime = [&](const char* dir) -> int64_t
{
for (int k = 0; k < dirCacheCount; k++)
{
if (dirCache[k].Path.EqualsNoCase(dir)) return dirCache[k].Time;
}
int64_t t = GetNewestSourceTimeRecursive(dir);
if (dirCacheCount < 64)
{
dirCache[dirCacheCount].Path.Set(dir);
dirCache[dirCacheCount].Time = t;
dirCacheCount++;
}
return t;
};
bool anyNeedBuild = false;
for (int j = 0; j < cfg.StepCount; j++)
{
BuildStep& step = cfg.Steps[j];
int64_t maxTime = 0;
const char* cur = step.Depends;
while (*cur)
{
const char* nextPipe = strchr(cur, '|');
char dirPath[MaxPath] = {};
if (nextPipe)
{
int dLen = static_cast<int>(nextPipe - cur);
if (dLen >= static_cast<int>(sizeof(dirPath))) dLen = static_cast<int>(sizeof(dirPath)) - 1;
memcpy(dirPath, cur, dLen);
dirPath[dLen] = '\0';
cur = nextPipe + 1;
}
else
{
strcpy_s(dirPath, sizeof(dirPath), cur);
cur += strlen(cur);
}
if (dirPath[0] != '\0')
{
int64_t t = GetDirTime(dirPath);
if (t > maxTime) maxTime = t;
}
}
bool needCompile = unityChanged;
if (!needCompile)
{
if (!FileExists(step.OutputFile))
{
needCompile = true;
}
else
{
int64_t outTime = GetFileModTimeInt64(step.OutputFile);
if (outTime < maxTime) needCompile = true;
}
}
printf("\n--- %s ---\n", step.StepName);
if (!needCompile)
{
printf("[SKIP / UP-TO-DATE] %s [Binary up to date]\n", step.OutputFile);
}
else
{
printf("[REBUILDING] Compiling %s...\n", step.OutputFile);
anyNeedBuild = true;
LARGE_INTEGER tStart, tEnd;
QueryPerformanceCounter(&tStart);
int res = ExecuteCmd(step.Command);
QueryPerformanceCounter(&tEnd);
AppendTiming(step.StepName, static_cast<double>(tEnd.QuadPart - tStart.QuadPart) / static_cast<double>(perfFreq.QuadPart));
if (res != 0)
{
printf("[BUILD FAILED] Command failed with exit code %d.\n", res);
return res;
}
char builtOut[MaxPath + 2];
sprintf_s(builtOut, "%s\n", step.OutputFile);
HANDLE hFile = CreateFileA("Intermediate\\built_outputs.tmp", FILE_APPEND_DATA, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD written = 0;
WriteFile(hFile, builtOut, static_cast<DWORD>(strlen(builtOut)), &written, nullptr);
CloseHandle(hFile);
}
anyBuilt = true;
}
}
if (!anyNeedBuild)
{
printf("\n[SKIP / UP-TO-DATE] All requested targets in [%s] are up-to-date.\n", cfg.Name);
}
}
if (timingsLen > 0)
{
WriteEntireFile("Intermediate\\step_timings.tmp", timingsBuf, timingsLen);
}
QueryPerformanceCounter(&perfEnd);
double elapsedSeconds = static_cast<double>(perfEnd.QuadPart - perfStart.QuadPart) / static_cast<double>(perfFreq.QuadPart);
char elapsedBuf[64];
if (elapsedSeconds < 60.0)
{
sprintf_s(elapsedBuf, "%.3fs", elapsedSeconds);
}
else
{
int totalMs = static_cast<int>(elapsedSeconds * 1000.0);
int hours = totalMs / 3600000;
int minutes = (totalMs % 3600000) / 60000;
int seconds = (totalMs % 60000) / 1000;
int ms = totalMs % 1000;
sprintf_s(elapsedBuf, "%02d:%02d:%02d.%03d", hours, minutes, seconds, ms);
}
printf("\n==============================================================================\n");
printf("[BUILD SUMMARY]\n");
printf("==============================================================================\n");
if (timingsLen > 0)
{
printf("Step Timings:\n");
const char* pos = timingsBuf;
while (*pos)
{
const char* lineStart = pos;
while (*pos && *pos != '\n') pos++;
int len = static_cast<int>(pos - lineStart);
if (len > 0)
{
const char* pipe = strchr(lineStart, '|');
if (pipe && pipe < pos)
{
char stepName[MaxPath] = {};
int nameLen = static_cast<int>(pipe - lineStart);
memcpy(stepName, lineStart, nameLen);
char durStr[64] = {};
int durLen = static_cast<int>(pos - (pipe + 1));
memcpy(durStr, pipe + 1, durLen);
printf(" - %-42s : %s\n", stepName, durStr);
}
}
if (*pos == '\n') pos++;
}
printf("------------------------------------------------------------------------------\n");
}
printf("Total Duration : %s\n", elapsedBuf);
printf("Output Binaries:\n");
if (anyBuilt)
{
FileContent outputsContent = ReadEntireFile("Intermediate\\built_outputs.tmp");
if (outputsContent.Data)
{
const char* pos = outputsContent.Data;
while (*pos)
{
const char* lineStart = pos;
while (*pos && *pos != '\n' && *pos != '\r') pos++;
int len = static_cast<int>(pos - lineStart);
if (len > 0)
{
char filePath[MaxPath] = {};
memcpy(filePath, lineStart, len);
WIN32_FILE_ATTRIBUTE_DATA fileData;
if (GetFileAttributesExA(filePath, GetFileExInfoStandard, &fileData))
{
ULARGE_INTEGER fileSize;
fileSize.LowPart = fileData.nFileSizeLow;
fileSize.HighPart = fileData.nFileSizeHigh;
double sizeMB = static_cast<double>(fileSize.QuadPart) / (1024.0 * 1024.0);
printf(" - %s (%.2f MB)\n", filePath, sizeMB);
}
}
while (*pos == '\n' || *pos == '\r') pos++;
}
FreeFileContent(outputsContent);
}
DeleteFileA("Intermediate\\built_outputs.tmp");
}
else
{
printf(" - None built in this session (all requested targets up-to-date)\n");
}
printf("==============================================================================\n");
printf("[ALL BUILDS SUCCEEDED] All requested targets processed successfully.\n");
printf("==============================================================================\n");
fflush(stdout);
return 0;
}
// ============================================================================
// Main entry point
// ============================================================================
int main(int argc, char* argv[])
{
if (argc < 2)
{
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(" check_alive Returns 0 immediately to test execution\n");
return 1;
}
const char* cmd = argv[1];
if (strcmp(cmd, "check_alive") == 0) return 0;
if (strcmp(cmd, "run_plan") == 0) return CommandRunPlan(argc, argv);
printf("[ERROR] Unknown command: %s\n", cmd);
return 1;
}