adaptive unity build

This commit is contained in:
2026-08-04 20:53:14 -04:00
parent 5eb8fb7e9d
commit 3065441989
2 changed files with 296 additions and 51 deletions
+223 -34
View File
@@ -301,56 +301,120 @@ static bool GenerateUnityFiles(const UnityConfig* ucfg)
EnsureDirectoryExists(ucfg->OutputDir);
int fileIdx = 0;
int unityCount = 1;
char currentUnityPath[MAX_PATH_LEN];
bool anyChanged = false;
char* memBuf = (char*)ArenaPush(1024 * 1024);
int memOffset = 0;
SYSTEMTIME st;
GetSystemTime(&st);
FILETIME ftNow;
SystemTimeToFileTime(&st, &ftNow);
ULARGE_INTEGER liNow;
liNow.LowPart = ftNow.dwLowDateTime;
liNow.HighPart = ftNow.dwHighDateTime;
int64_t nowTime = liNow.QuadPart;
int64_t oneHour = 3600LL * 10000000LL;
FilePathList* coldFiles = CreateFilePathList();
FilePathList* hotFiles = CreateFilePathList();
for (int i = 0; i < srcFiles->Count; i++)
{
if (StringContains(srcFiles->Paths[i], "_Unity")) continue;
if (fileIdx == 0)
{
memOffset = 0;
int64_t fileTime = GetFileModTimeInt64(srcFiles->Paths[i]);
if (nowTime - fileTime < oneHour) {
FilePathListAdd(hotFiles, srcFiles->Paths[i]);
} else {
FilePathListAdd(coldFiles, srcFiles->Paths[i]);
}
}
int unityCount = 1;
bool anyChanged = false;
char currentUnityPath[MAX_PATH_LEN];
char* memBuf = (char*)ArenaPush(1024 * 1024);
// Process cold files (MaxFiles)
int fileIdx = 0;
int memOffset = 0;
int64_t maxIncludedTime = 0;
for (int i = 0; i < coldFiles->Count; i++)
{
char absPath[MAX_PATH_LEN];
GetFullPathNameA(srcFiles->Paths[i], MAX_PATH_LEN, absPath, NULL);
for (int k = 0; absPath[k]; k++) {
if (absPath[k] == '\\') absPath[k] = '/';
}
GetFullPathNameA(coldFiles->Paths[i], MAX_PATH_LEN, absPath, NULL);
for (int k = 0; absPath[k]; k++) { if (absPath[k] == '\\') absPath[k] = '/'; }
memOffset += sprintf_s(memBuf + memOffset, (1024 * 1024) - memOffset, "#include \"%s\"\n", absPath);
fileIdx++;
int64_t fileTime = GetFileModTimeInt64(coldFiles->Paths[i]);
if (fileTime > maxIncludedTime) maxIncludedTime = fileTime;
if (fileIdx >= ucfg->MaxFiles || i == srcFiles->Count - 1)
if (fileIdx >= ucfg->MaxFiles || i == coldFiles->Count - 1)
{
snprintf(currentUnityPath, sizeof(currentUnityPath), "%s\\%s%d.cpp", ucfg->OutputDir, ucfg->Prefix, unityCount);
FileContent exist = ReadEntireFile(currentUnityPath);
bool write = true;
if (exist.Data && exist.Size == memOffset)
{
if (memcmp(exist.Data, memBuf, memOffset) == 0)
{
write = false;
if (exist.Data && exist.Size == memOffset && memcmp(exist.Data, memBuf, memOffset) == 0) write = false;
if (write) { WriteEntireFile(currentUnityPath, memBuf, memOffset); anyChanged = true; }
// Touch Unity file if included files are newer
if (!write) {
int64_t unityTime = GetFileModTimeInt64(currentUnityPath);
if (maxIncludedTime > unityTime) {
HANDLE hFile = CreateFileA(currentUnityPath, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
FILETIME ft;
ULARGE_INTEGER li;
li.QuadPart = nowTime;
ft.dwLowDateTime = li.LowPart;
ft.dwHighDateTime = li.HighPart;
SetFileTime(hFile, NULL, NULL, &ft);
CloseHandle(hFile);
}
}
}
if (write)
{
WriteEntireFile(currentUnityPath, memBuf, memOffset);
anyChanged = true;
}
unityCount++;
fileIdx = 0;
memOffset = 0;
maxIncludedTime = 0;
}
}
// Process hot files (Standalone)
for (int i = 0; i < hotFiles->Count; i++)
{
char absPath[MAX_PATH_LEN];
GetFullPathNameA(hotFiles->Paths[i], MAX_PATH_LEN, absPath, NULL);
for (int k = 0; absPath[k]; k++) { if (absPath[k] == '\\') absPath[k] = '/'; }
memOffset = sprintf_s(memBuf, (1024 * 1024), "#include \"%s\"\n", absPath);
snprintf(currentUnityPath, sizeof(currentUnityPath), "%s\\%s%d.cpp", ucfg->OutputDir, ucfg->Prefix, unityCount);
FileContent exist = ReadEntireFile(currentUnityPath);
bool write = true;
if (exist.Data && exist.Size == memOffset && memcmp(exist.Data, memBuf, memOffset) == 0) write = false;
if (write) { WriteEntireFile(currentUnityPath, memBuf, memOffset); anyChanged = true; }
if (!write) {
int64_t fileTime = GetFileModTimeInt64(hotFiles->Paths[i]);
int64_t unityTime = GetFileModTimeInt64(currentUnityPath);
if (fileTime > unityTime) {
HANDLE hFile = CreateFileA(currentUnityPath, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
FILETIME ft;
ULARGE_INTEGER li;
li.QuadPart = nowTime;
ft.dwLowDateTime = li.LowPart;
ft.dwHighDateTime = li.HighPart;
SetFileTime(hFile, NULL, NULL, &ft);
CloseHandle(hFile);
}
}
}
unityCount++;
}
// Delete remaining old unity files
while (true)
{
snprintf(currentUnityPath, sizeof(currentUnityPath), "%s\\%s%d.cpp", ucfg->OutputDir, ucfg->Prefix, unityCount);
@@ -360,10 +424,7 @@ static bool GenerateUnityFiles(const UnityConfig* ucfg)
anyChanged = true;
unityCount++;
}
else
{
break;
}
else break;
}
return anyChanged;
@@ -376,9 +437,11 @@ static bool GenerateUnityFiles(const UnityConfig* ucfg)
typedef struct
{
char StepName[256];
char OutputFile[MAX_PATH_LEN];
char OutputFile[256];
char Depends[MAX_PATH_LEN];
char Command[MAX_FILES];
char IncSrc[256];
char IncObj[256];
} BuildStep;
typedef struct
@@ -449,6 +512,99 @@ static void AppendTiming(const char* stepName, double secs)
g_TimingsLen += sprintf_s(g_TimingsBuf + g_TimingsLen, sizeof(g_TimingsBuf) - g_TimingsLen, "%s|%.3fs\n", stepName, secs);
}
static int ExecuteIncrementalCompile(const char* srcDir, const char* objDir, const char* baseCmd)
{
EnsureDirectoryExists(objDir);
FilePathList* cppFiles = CreateFilePathList();
CollectFiles(srcDir, NULL, ".cpp", cppFiles);
CollectFiles(srcDir, NULL, ".c", cppFiles);
// Clean up stale .obj files
char objSearchPath[MAX_PATH_LEN];
snprintf(objSearchPath, sizeof(objSearchPath), "%s\\*.obj", objDir);
WIN32_FIND_DATAA findData;
HANDLE hFind = FindFirstFileA(objSearchPath, &findData);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
char baseName[256];
strcpy_s(baseName, sizeof(baseName), findData.cFileName);
char* ext = strrchr(baseName, '.');
if (ext) *ext = '\0';
bool foundSrc = false;
for (int i = 0; i < cppFiles->Count; i++) {
const char* srcPath = cppFiles->Paths[i];
const char* srcFile = strrchr(srcPath, '\\');
if (!srcFile) srcFile = strrchr(srcPath, '/');
if (!srcFile) srcFile = srcPath; else srcFile++;
char srcBase[256];
strcpy_s(srcBase, sizeof(srcBase), srcFile);
char* srcExt = strrchr(srcBase, '.');
if (srcExt) *srcExt = '\0';
if (_stricmp(baseName, srcBase) == 0) {
foundSrc = true;
break;
}
}
if (!foundSrc) {
char fullObjPath[MAX_PATH_LEN];
snprintf(fullObjPath, sizeof(fullObjPath), "%s\\%s", objDir, findData.cFileName);
DeleteFileA(fullObjPath);
}
} while (FindNextFileA(hFind, &findData));
FindClose(hFind);
}
char* dirtyFiles = (char*)ArenaPush(MAX_FILES);
int dirtyCount = 0;
dirtyFiles[0] = '\0';
for (int i = 0; i < cppFiles->Count; i++)
{
const char* srcPath = cppFiles->Paths[i];
const char* fileName = strrchr(srcPath, '\\');
if (!fileName) fileName = strrchr(srcPath, '/');
if (!fileName) fileName = srcPath; else fileName++;
char objPath[MAX_PATH_LEN];
sprintf_s(objPath, sizeof(objPath), "%s\\%s", objDir, fileName);
char* ext = strrchr(objPath, '.');
if (ext) strcpy_s(ext, sizeof(objPath) - (ext - objPath), ".obj");
bool dirty = false;
if (!FileExists(objPath)) {
dirty = true;
} else {
if (GetFileModTimeInt64(srcPath) > GetFileModTimeInt64(objPath)) {
dirty = true;
}
}
if (dirty) {
if (dirtyCount > 0) strcat_s(dirtyFiles, MAX_FILES, " ");
strcat_s(dirtyFiles, MAX_FILES, srcPath);
dirtyCount++;
}
}
if (dirtyCount == 0) {
printf("[SKIP / UP-TO-DATE] %s [All objects up to date]\n", objDir);
return 0;
}
printf("[REBUILDING] Compiling %d dirty file(s) in %s...\n", dirtyCount, srcDir);
char* fullCmd = (char*)ArenaPush(MAX_FILES * 2);
sprintf_s(fullCmd, MAX_FILES * 2, "%s %s", baseCmd, dirtyFiles);
return ExecuteCmd(fullCmd);
}
static int CommandRunPlan(int argc, char* argv[])
{
if (argc < 3)
@@ -597,6 +753,22 @@ static int CommandRunPlan(int argc, char* argv[])
memcpy(curCfg->Steps[curCfg->StepCount - 1].Command, lineStart + 8, nLen);
curCfg->Steps[curCfg->StepCount - 1].Command[nLen] = '\0';
}
else if (lineLen > 8 && memcmp(lineStart, "INC_SRC ", 8) == 0 && curCfg->StepCount > 0)
{
int nLen = lineLen - 8;
if (nLen >= (int)sizeof(curCfg->Steps[curCfg->StepCount - 1].IncSrc))
nLen = (int)sizeof(curCfg->Steps[curCfg->StepCount - 1].IncSrc) - 1;
memcpy(curCfg->Steps[curCfg->StepCount - 1].IncSrc, lineStart + 8, nLen);
curCfg->Steps[curCfg->StepCount - 1].IncSrc[nLen] = '\0';
}
else if (lineLen > 8 && memcmp(lineStart, "INC_OBJ ", 8) == 0 && curCfg->StepCount > 0)
{
int nLen = lineLen - 8;
if (nLen >= (int)sizeof(curCfg->Steps[curCfg->StepCount - 1].IncObj))
nLen = (int)sizeof(curCfg->Steps[curCfg->StepCount - 1].IncObj) - 1;
memcpy(curCfg->Steps[curCfg->StepCount - 1].IncObj, lineStart + 8, nLen);
curCfg->Steps[curCfg->StepCount - 1].IncObj[nLen] = '\0';
}
}
}
while (*pos == '\n' || *pos == '\r')
@@ -693,6 +865,23 @@ static int CommandRunPlan(int argc, char* argv[])
}
printf("\n--- %s ---\n", step->StepName);
if (step->IncSrc[0] != '\0' && step->IncObj[0] != '\0')
{
LARGE_INTEGER tStart, tEnd;
QueryPerformanceCounter(&tStart);
int res = ExecuteIncrementalCompile(step->IncSrc, step->IncObj, step->Command);
QueryPerformanceCounter(&tEnd);
AppendTiming(step->StepName, (double)(tEnd.QuadPart - tStart.QuadPart) / (double)perfFreq.QuadPart);
if (res != 0) {
printf("[BUILD FAILED] Command failed with exit code %d.\n", res);
return res;
}
anyNeedBuild = true;
anyBuilt = true;
continue;
}
if (!needCompile)
{
printf("[SKIP / UP-TO-DATE] %s [Binary up to date]\n", step->OutputFile);