1009 lines
43 KiB
C
1009 lines
43 KiB
C
#include "meta.h"
|
|
|
|
BuildData build = ZI;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Helpers
|
|
|
|
i32 GetBuildStatus(void)
|
|
{
|
|
return Atomic32Fetch(&build.status);
|
|
}
|
|
|
|
void SetBuildStatus(i32 code)
|
|
{
|
|
Atomic32FetchTestSet(&build.status, 0, code);
|
|
}
|
|
|
|
void EchoLine(String msg)
|
|
{
|
|
TempArena scratch = BeginScratchNoConflict();
|
|
{
|
|
String msg_w_newline = StringF(scratch.arena, "%F\n", FmtString(msg));
|
|
Echo(msg_w_newline);
|
|
}
|
|
EndScratch(scratch);
|
|
}
|
|
|
|
void EchoLineOrNothing(String msg)
|
|
{
|
|
String trimmed_msg = TrimWhitespace(msg);
|
|
if (trimmed_msg.len > 0)
|
|
{
|
|
EchoLine(trimmed_msg);
|
|
}
|
|
}
|
|
|
|
LineCol LineColFromPos(String data, i64 pos)
|
|
{
|
|
TempArena scratch = BeginScratchNoConflict();
|
|
LineCol result = ZI;
|
|
for (u64 cur = 0; cur < data.len && cur <= pos; ++cur)
|
|
{
|
|
u8 c = data.text[cur];
|
|
if (c == '\n')
|
|
{
|
|
++result.line;
|
|
result.col = 0;
|
|
}
|
|
else if (c != '\r')
|
|
{
|
|
++result.col;
|
|
}
|
|
}
|
|
result.line += 1;
|
|
EndScratch(scratch);
|
|
return result;
|
|
}
|
|
|
|
String StringFromMetaErrors(Arena *arena, M_ErrorList errors)
|
|
{
|
|
String result = ZI;
|
|
for (M_Error *e = errors.first; e; e = e->next)
|
|
{
|
|
M_Token *token = e->token;
|
|
String token_file = token->file->name;
|
|
String token_file_data = token->file->data;
|
|
if (token_file.len > 0)
|
|
{
|
|
i64 token_pos = -1;
|
|
if (token->s.len > 0
|
|
&& token_file_data.len > 0
|
|
&& token->s.text > token_file_data.text
|
|
&& token->s.text < (token_file_data.text + token_file_data.len))
|
|
{
|
|
token_pos = token->s.text - token_file_data.text;
|
|
}
|
|
LineCol line_col = ZI;
|
|
if (token_pos >= 0)
|
|
{
|
|
line_col = LineColFromPos(token_file_data, token_pos);
|
|
}
|
|
result = StringF(arena,
|
|
"%F:%F:%F: error: %F",
|
|
FmtString(token_file),
|
|
FmtSint(line_col.line),
|
|
FmtSint(line_col.col),
|
|
FmtString(e->msg));
|
|
}
|
|
else
|
|
{
|
|
result = StringF(arena, "error: %F", FmtString(e->msg));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
EmbedObj Embed(String store_name, String dir_path)
|
|
{
|
|
Arena *perm = PermArena();
|
|
EmbedObj result = ZI;
|
|
|
|
/* Generate resource archive contents */
|
|
String arc_contents = ZI;
|
|
{
|
|
StringList files = ZI;
|
|
F_FilesFromDir(perm, &files, dir_path, F_IterFlag_Recurse);
|
|
|
|
Struct(EntryNode)
|
|
{
|
|
EntryNode *next;
|
|
String entry_name;
|
|
String file_name;
|
|
};
|
|
EntryNode *first_entry = 0;
|
|
EntryNode *last_entry = 0;
|
|
u64 entries_count = 0;
|
|
for (StringListNode *file_node = files.first; file_node; file_node = file_node->next)
|
|
{
|
|
String file_name = file_node->s;
|
|
if (F_IsFile(file_name))
|
|
{
|
|
String entry_name = file_name;
|
|
if (entry_name.len > (dir_path.len + 1))
|
|
{
|
|
entry_name.len -= dir_path.len + 1;
|
|
entry_name.text += dir_path.len + 1;
|
|
}
|
|
entry_name = StringF(perm, "%F/%F", FmtString(store_name), FmtString(entry_name));
|
|
for (u64 i = 0; i < entry_name.len; ++i)
|
|
{
|
|
if (entry_name.text[i] == '\\')
|
|
{
|
|
entry_name.text[i] = '/';
|
|
}
|
|
}
|
|
|
|
EntryNode *en = PushStruct(perm, EntryNode);
|
|
en->entry_name = entry_name;
|
|
en->file_name = file_name;
|
|
SllQueuePush(first_entry, last_entry, en);
|
|
++entries_count;
|
|
}
|
|
}
|
|
|
|
BB_Buff bb = BB_AcquireBuff(Gibi(2));
|
|
BB_Writer bw = BB_WriterFromBuff(&bb);
|
|
|
|
/* Write magic */
|
|
BB_WriteUBits(&bw, ResourceEmbeddedMagic, 64);
|
|
|
|
/* Write header */
|
|
BB_WriteUBits(&bw, entries_count, 64);
|
|
|
|
/* Reserve entries space */
|
|
u64 entry_size = 8 /* Name start */
|
|
+ 8 /* Name end */
|
|
+ 8 /* Data start */
|
|
+ 8; /* Data end */
|
|
u8 *entries_start = BB_GetWrittenRaw(&bw) + BB_GetNumBytesWritten(&bw);
|
|
u64 entries_size = entry_size * entries_count;
|
|
String entries_str = STRING(entries_size, entries_start);
|
|
BB_WriteSeekBytes(&bw, entries_size);
|
|
BB_Buff entries_bb = BB_BuffFromString(entries_str);
|
|
BB_Writer entries_bw = BB_WriterFromBuff(&entries_bb);
|
|
|
|
/* Write entries */
|
|
for (EntryNode *en = first_entry; en; en = en->next)
|
|
{
|
|
/* TODO: Copy file data directly into archive file */
|
|
String file_data = F_DataFromFile(perm, en->file_name);
|
|
|
|
/* Write name */
|
|
BB_WriteAlignBytes(&bw, 64);
|
|
u64 name_start = BB_GetNumBytesWritten(&bw) + 1;
|
|
BB_WriteString(&bw, en->entry_name);
|
|
u64 name_len = BB_GetNumBytesWritten(&bw) - name_start;
|
|
|
|
/* Write data */
|
|
BB_WriteAlignBytes(&bw, 64);
|
|
/* FIXME: Why no +1 here? */
|
|
u64 data_start = BB_GetNumBytesWritten(&bw);
|
|
BB_WriteBytes(&bw, file_data);
|
|
u64 data_len = BB_GetNumBytesWritten(&bw) - data_start;
|
|
|
|
/* Write entry */
|
|
BB_WriteUBits(&entries_bw, name_start, 64);
|
|
BB_WriteUBits(&entries_bw, name_len, 64);
|
|
BB_WriteUBits(&entries_bw, data_start, 64);
|
|
BB_WriteUBits(&entries_bw, data_len, 64);
|
|
}
|
|
|
|
arc_contents.len = BB_GetNumBytesWritten(&bw);
|
|
arc_contents.text = BB_GetWrittenRaw(&bw);
|
|
}
|
|
|
|
/* Write archive to file */
|
|
String arc_path = StringF(perm, "%F.arc", FmtString(store_name));
|
|
F_ClearWrite(arc_path, arc_contents);
|
|
|
|
/* Generate object file */
|
|
if (IsPlatformWindows)
|
|
{
|
|
/* Generate RC file */
|
|
String rc_out_file = StringF(perm, "%F.rc", FmtString(store_name));
|
|
{
|
|
RandState rs = ZI;
|
|
StringList rc_out_lines = ZI;
|
|
String arc_file_cp = F_GetFullCrossPlatform(perm, arc_path);
|
|
String line = StringF(perm, "%F_%F RCDATA \"%F\"", FmtString(Lit(Stringize(W32_EmbeddedDataPrefix))), FmtHex(RandU64FromState(&rs)), FmtString(arc_file_cp));
|
|
PushStringToList(perm, &rc_out_lines, line);
|
|
/* Write to file */
|
|
String rc_out = StringFromList(perm, rc_out_lines, Lit("\n"));
|
|
F_ClearWrite(rc_out_file, rc_out);
|
|
}
|
|
|
|
/* Compile RC file */
|
|
result.obj_file = StringF(perm, "%F.res", FmtString(store_name));
|
|
{
|
|
result.obj_file, FmtString(F_GetFull(perm, rc_out_file));
|
|
String cmd = StringF(perm, "rc.exe -nologo -fo %F %F", FmtString(result.obj_file), FmtString(F_GetFull(perm, rc_out_file)));
|
|
OS_CommandResult cmd_result = OS_RunCommand(perm, cmd);
|
|
String cmd_output = TrimWhitespace(cmd_result.output);
|
|
result.output = cmd_output;
|
|
result.return_code = cmd_result.code;
|
|
}
|
|
|
|
SetBuildStatus(result.return_code);
|
|
}
|
|
else
|
|
{
|
|
/* TODO: Generate object files using .incbin on non-windows platforms */
|
|
Panic(Lit("Resource embedding not implemented for this platform"));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Build
|
|
|
|
void BuildEntryPoint(WaveLaneCtx *lane)
|
|
{
|
|
Arena *perm = PermArena();
|
|
|
|
//////////////////////////////
|
|
//- Dirty check
|
|
|
|
/* Return rebuild code if metaprogram is dirty */
|
|
if (lane->idx == 0)
|
|
{
|
|
/* Read old metahash */
|
|
u64 old_metahash = 0;
|
|
if (F_IsFile(Lit("metahash.dat")))
|
|
{
|
|
String hashes_str = F_DataFromFile(perm, Lit("metahash.dat"));
|
|
if (hashes_str.len == sizeof(old_metahash))
|
|
{
|
|
CopyBytes(&old_metahash, hashes_str.text, sizeof(old_metahash));
|
|
}
|
|
OS_Rm(Lit("metahash.dat"));
|
|
}
|
|
|
|
/* Calculate new metahash */
|
|
u64 new_metahash = 0;
|
|
{
|
|
StringList check_files = ZI;
|
|
F_FilesFromDir(perm, &check_files, Lit("../src/base"), F_IterFlag_Recurse);
|
|
F_FilesFromDir(perm, &check_files, Lit("../src/meta"), F_IterFlag_Recurse);
|
|
PushStringToList(perm, &check_files, Lit("../src/config.h"));
|
|
for (StringListNode *n = check_files.first; n; n = n->next)
|
|
{
|
|
String file = n->s;
|
|
new_metahash = RandU64FromSeeds(HashFnv64(new_metahash, file), OS_LastWriteTimestampFromPath(file));
|
|
}
|
|
}
|
|
|
|
/* Exit if metaprogram needs recompilation */
|
|
if (old_metahash == 0 || old_metahash == new_metahash)
|
|
{
|
|
F_ClearWrite(Lit("metahash.dat"), StringFromStruct(&new_metahash));
|
|
}
|
|
else
|
|
{
|
|
EchoLine(Lit("Metaprogram is dirty"));
|
|
ExitNow(MetaRebuildCode);
|
|
}
|
|
}
|
|
|
|
//////////////////////////////
|
|
//- Command line
|
|
|
|
Struct(CmdLineArgs)
|
|
{
|
|
String leaf_layer_name;
|
|
};
|
|
CmdLineArgs cmdline = ZI;
|
|
{
|
|
CommandlineArg layer_arg = CommandlineArgFromName(Lit("layer"));
|
|
String layer_name = ZI;
|
|
if (layer_arg.name.len != 0)
|
|
{
|
|
layer_name = layer_arg.value;
|
|
}
|
|
else
|
|
{
|
|
layer_name = StringFromCommandlineIdx(1);
|
|
}
|
|
if (layer_name.len == 0)
|
|
{
|
|
layer_name = Lit("pp");
|
|
if (lane->idx == 0)
|
|
{
|
|
EchoLine(Lit("No layer supplied, assuming \"pp\" build"));
|
|
}
|
|
}
|
|
cmdline.leaf_layer_name = layer_name;
|
|
}
|
|
|
|
if (lane->idx == 0)
|
|
{
|
|
EchoLine(StringF(perm, "Building layer \"%F\"", FmtString(cmdline.leaf_layer_name)));
|
|
}
|
|
|
|
//////////////////////////////
|
|
//- Generate compiler params
|
|
|
|
CompilerParams cp = ZI;
|
|
{
|
|
//- Common
|
|
{
|
|
PushStringToList(perm, &cp.defs, Lit("-DIsConsoleApp=0"));
|
|
PushStringToList(perm, &cp.defs, Lit("-DIsRtcEnabled=1"));
|
|
PushStringToList(perm, &cp.defs, Lit("-DIsAsanEnabled=0"));
|
|
PushStringToList(perm, &cp.defs, Lit("-DIsCrtlibEnabled=1"));
|
|
PushStringToList(perm, &cp.defs, Lit("-DIsDebinfoEnabled=1"));
|
|
PushStringToList(perm, &cp.defs, Lit("-DIsDeveloperModeEnabled=1"));
|
|
PushStringToList(perm, &cp.defs, Lit("-DIsUnoptimized=1"));
|
|
PushStringToList(perm, &cp.defs, Lit("-DIsTestingEnabled=0"));
|
|
PushStringToList(perm, &cp.defs, Lit("-DIsHotSwappingEnabled=1"));
|
|
}
|
|
|
|
//- Msvc
|
|
{
|
|
PushStringToList(perm, &cp.compiler_only_flags_msvc, Lit("-diagnostics:column"));
|
|
PushStringToList(perm, &cp.flags_msvc, Lit("-INCREMENTAL:NO"));
|
|
PushStringToList(perm, &cp.flags_msvc, Lit("-nologo"));
|
|
|
|
/* Optimization */
|
|
PushStringToList(perm, &cp.compiler_only_flags_msvc, Lit("-Od"));
|
|
|
|
/* Debug info */
|
|
PushStringToList(perm, &cp.flags_msvc, Lit("-DEBUG:FULL"));
|
|
PushStringToList(perm, &cp.compiler_only_flags_msvc, Lit("-Z7"));
|
|
|
|
/* Enable warnings */
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-W4"));
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-WX"));
|
|
// PushStringToList(perm, &cp.warnings_msvc, Lit("-we4013")); /* function undefined; assuming extern returning int */
|
|
|
|
/* Disable warnings */
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4244")); /* 'function': conversion from 'int' to 'f32', possible loss of data */
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4201")); /* nonstandard extension used: nameless struct/union */
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4324")); /* structure was padded due to alignment specifier */
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4100")); /* unreferenced parameter */
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4101")); /* unreferenced local variable */
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4189")); /* local variable is initialized but not referenced */
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4200")); /* nonstandard extension used: zero-sized array in struct/union */
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4702")); /* unreachable code */
|
|
PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4305")); /* 'initializing': truncation from 'double' to 'f32' */
|
|
|
|
// PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4127")); /* conditional expression is constant */
|
|
// PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4820")); /* bytes padding added after data member */
|
|
// PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4464")); /* relative include path contains '..' */
|
|
// PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4061")); /* enumerator is not explicitly handled by a case label */
|
|
// PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4242")); /* conversion from X to Y, possible loss of data */
|
|
// PushStringToList(perm, &cp.warnings_msvc, Lit("-wd4388")); /* signed/unsigned mismatch */
|
|
// PushStringToList(perm, &cp.warnings_msvc, Lit("-wd5045")); /* Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified */
|
|
}
|
|
|
|
//- Clang
|
|
{
|
|
PushStringToList(perm, &cp.flags_clang, Lit("-std=c99"));
|
|
PushStringToList(perm, &cp.flags_clang, Lit("-fno-finite-loops"));
|
|
PushStringToList(perm, &cp.flags_clang, Lit("-fno-strict-aliasing"));
|
|
PushStringToList(perm, &cp.flags_clang, Lit("-g -gcodeview"));
|
|
PushStringToList(perm, &cp.flags_clang, Lit("-O0"));
|
|
PushStringToList(perm, &cp.flags_clang, Lit("-msse4.2"));
|
|
|
|
/* Enable warnings */
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wall"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Werror"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wframe-larger-than=65536"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wmissing-prototypes"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wmissing-declarations"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wunused-variable"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wunused-but-set-variable"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wunused-parameter"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wimplicit-fallthrough"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wswitch"));
|
|
|
|
/* Disable warnings */
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wno-initializer-overrides"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wno-microsoft-enum-forward-reference"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wno-unused-variable"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wno-unused-parameter"));
|
|
PushStringToList(perm, &cp.warnings_clang, Lit("-Wno-incompatible-function-pointer-types"));
|
|
}
|
|
|
|
//- Dxc
|
|
{
|
|
PushStringToList(perm, &cp.flags_dxc, Lit("-Od"));
|
|
PushStringToList(perm, &cp.flags_dxc, Lit("-Zi -Qembed_debug"));
|
|
}
|
|
}
|
|
|
|
//////////////////////////////
|
|
//- Phase 1/3: Prep
|
|
|
|
/*
|
|
* Phase 1/3: Prep (narrow)
|
|
* - Parse layers
|
|
* - Generate final C file
|
|
* - Generate final HLSL file
|
|
* - Generate resource dirs info
|
|
*
|
|
* Phase 2/3: Compile (wide)
|
|
* - Compile C
|
|
* - Compile & embed shaders
|
|
* - Embed resource dirs
|
|
*
|
|
* Phase 3/3: Link (narrow)
|
|
* - Link
|
|
*/
|
|
|
|
/* TODO: Dispatch OS commands asynchronously */
|
|
|
|
String shader_store_name = Lit("ShadersStore");
|
|
String c_out_file = F_GetFull(perm, StringF(perm, "%F_gen_c.c", FmtString(cmdline.leaf_layer_name)));
|
|
String gpu_out_file = F_GetFull(perm, StringF(perm, "%F_gen_gpu.hlsl", FmtString(cmdline.leaf_layer_name)));
|
|
|
|
if (lane->idx == 0 && GetBuildStatus() == 0)
|
|
{
|
|
//- Parse layers
|
|
{
|
|
/* Lex */
|
|
StringList src_dirs = ZI;
|
|
PushStringToList(perm, &src_dirs, Lit("../src"));
|
|
M_TokenFileList lexed = M_TokensFromSrcDirs(perm, src_dirs);
|
|
|
|
/* Parse */
|
|
M_LayerList parsed = M_LayersFromTokenFiles(perm, lexed);;
|
|
|
|
/* Flatten */
|
|
StringList starting_layer_names = ZI;
|
|
PushStringToList(perm, &starting_layer_names, cmdline.leaf_layer_name);
|
|
build.layers_parse = M_FlattenEntries(perm, parsed, starting_layer_names);
|
|
|
|
SetBuildStatus(build.layers_parse.errors.count > 0);
|
|
}
|
|
|
|
//- Generate C file
|
|
{
|
|
String c_out_file = F_GetFull(perm, StringF(perm, "%F_gen_c.c", FmtString(cmdline.leaf_layer_name)));
|
|
StringList c_store_lines = ZI;
|
|
StringList c_shader_lines = ZI;
|
|
StringList c_include_lines = ZI;
|
|
StringList c_startup_lines = ZI;
|
|
{
|
|
for (M_Entry *entry = build.layers_parse.first; entry->valid; entry = entry->next)
|
|
{
|
|
M_EntryKind kind = entry->kind;
|
|
M_Token *entry_tok = entry->name_token;
|
|
M_Token *arg0_tok = entry->arg_tokens[0];
|
|
M_Token *arg1_tok = entry->arg_tokens[1];
|
|
switch (kind)
|
|
{
|
|
default: break;
|
|
case M_EntryKind_EmbedDir:
|
|
{
|
|
if (arg0_tok->valid && arg1_tok->valid)
|
|
{
|
|
String store_name = arg0_tok->s;
|
|
String token_file = arg1_tok->file->name;
|
|
String token_parent_dir = F_GetParentDir(token_file);
|
|
String arg_dir = arg1_tok->s;
|
|
String full = F_GetFullCrossPlatform(perm, StringF(perm, "%F/%F", FmtString(token_parent_dir), FmtString(arg_dir)));
|
|
if (F_IsDir(full))
|
|
{
|
|
u64 hash = HashFnv64(Fnv64Basis, StringF(perm, "%F/", FmtString(store_name)));
|
|
String line = StringF(perm, "ResourceStore %F = { 0x%F };", FmtString(store_name), FmtHex(hash));
|
|
PushStringToList(perm, &c_store_lines, line);
|
|
}
|
|
else
|
|
{
|
|
String err = StringF(perm, "Directory '%F' not found", FmtString(full));
|
|
M_PushError(perm, &build.c_parse.errors, arg1_tok, err);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
M_PushError(perm, &build.c_parse.errors, entry_tok, Lit("Expected resource store & directory name"));
|
|
}
|
|
} break;
|
|
case M_EntryKind_VertexShader:
|
|
case M_EntryKind_PixelShader:
|
|
case M_EntryKind_ComputeShader:
|
|
{
|
|
if (arg0_tok->valid)
|
|
{
|
|
String shader_type = kind == M_EntryKind_VertexShader ? Lit("VertexShader")
|
|
: kind == M_EntryKind_PixelShader ? Lit("PixelShader")
|
|
: kind == M_EntryKind_ComputeShader ? Lit("ComputeShader")
|
|
: Lit("");
|
|
String shader_name = arg0_tok->s;
|
|
u64 hash = HashFnv64(Fnv64Basis, StringF(perm, "%F/%F", FmtString(shader_store_name), FmtString(shader_name)));
|
|
String line = StringF(perm, "%F %F = { 0x%F };", FmtString(shader_type), FmtString(shader_name), FmtHex(hash));
|
|
PushStringToList(perm, &c_shader_lines, line);
|
|
}
|
|
else
|
|
{
|
|
M_PushError(perm, &build.c_parse.errors, entry_tok, Lit("Expected shader name"));
|
|
}
|
|
} break;
|
|
case M_EntryKind_IncludeC:
|
|
{
|
|
if (arg0_tok->valid)
|
|
{
|
|
String token_file = arg0_tok->file->name;
|
|
String token_parent_dir = F_GetParentDir(token_file);
|
|
String arg_file = arg0_tok->s;
|
|
String full = F_GetFull(perm, StringF(perm, "%F/%F", FmtString(token_parent_dir), FmtString(arg_file)));
|
|
if (F_IsFile(full))
|
|
{
|
|
String line = StringF(perm, "#include \"%F\"", FmtString(full));
|
|
PushStringToList(perm, &c_include_lines, line);
|
|
}
|
|
else
|
|
{
|
|
String err = StringF(perm, "File '%F' not found", FmtString(full));
|
|
M_PushError(perm, &build.c_parse.errors, arg0_tok, err);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
M_PushError(perm, &build.c_parse.errors, entry_tok, Lit("Expected file name"));
|
|
}
|
|
} break;
|
|
case M_EntryKind_Startup:
|
|
{
|
|
if (arg0_tok->valid)
|
|
{
|
|
String startup = arg0_tok->s;
|
|
String line = StringF(perm, " %F();", FmtString(startup));
|
|
PushStringToList(perm, &c_startup_lines, line);
|
|
}
|
|
else
|
|
{
|
|
M_PushError(perm, &build.c_parse.errors, entry_tok, Lit("Expected startup function name"));
|
|
}
|
|
} break;
|
|
}
|
|
}
|
|
}
|
|
if (build.c_parse.errors.count == 0)
|
|
{
|
|
StringList c_out_lines = ZI;
|
|
PushStringToList(perm, &c_out_lines, Lit("// Auto generated file"));
|
|
/* Include base layer */
|
|
{
|
|
String base_inc_path = F_GetFull(perm, Lit("../src/base/base_inc.h"));
|
|
PushStringToList(perm, &c_out_lines, Lit(""));
|
|
PushStringToList(perm, &c_out_lines, Lit("//- Base layer includes"));
|
|
PushStringToList(perm, &c_out_lines, StringF(perm, "#include \"%F\"", FmtString(base_inc_path)));
|
|
}
|
|
/* Define resource stores */
|
|
if (c_store_lines.count > 0)
|
|
{
|
|
PushStringToList(perm, &c_out_lines, Lit(""));
|
|
PushStringToList(perm, &c_out_lines, Lit("//- Resource stores"));
|
|
for (StringListNode *n = c_store_lines.first; n; n = n->next)
|
|
{
|
|
PushStringToList(perm, &c_out_lines, n->s);
|
|
}
|
|
}
|
|
/* Define shaders */
|
|
if (c_shader_lines.count > 0)
|
|
{
|
|
PushStringToList(perm, &c_out_lines, Lit(""));
|
|
PushStringToList(perm, &c_out_lines, Lit("//- Shaders"));
|
|
for (StringListNode *n = c_shader_lines.first; n; n = n->next)
|
|
{
|
|
PushStringToList(perm, &c_out_lines, n->s);
|
|
}
|
|
}
|
|
/* Include dependency layers */
|
|
if (c_include_lines.count > 0)
|
|
{
|
|
PushStringToList(perm, &c_out_lines, Lit(""));
|
|
PushStringToList(perm, &c_out_lines, Lit("//- Dependency graph includes"));
|
|
for (StringListNode *n = c_include_lines.first; n; n = n->next)
|
|
{
|
|
PushStringToList(perm, &c_out_lines, n->s);
|
|
}
|
|
}
|
|
/* Define StartupLayers */
|
|
{
|
|
PushStringToList(perm, &c_out_lines, Lit(""));
|
|
PushStringToList(perm, &c_out_lines, Lit("//- Startup"));
|
|
PushStringToList(perm, &c_out_lines, Lit("void StartupLayers(void)"));
|
|
PushStringToList(perm, &c_out_lines, Lit("{"));
|
|
for (StringListNode *n = c_startup_lines.first; n; n = n->next)
|
|
{
|
|
PushStringToList(perm, &c_out_lines, n->s);
|
|
}
|
|
PushStringToList(perm, &c_out_lines, Lit("}"));
|
|
}
|
|
/* Write to file */
|
|
PushStringToList(perm, &c_out_lines, Lit(""));
|
|
String c_out = StringFromList(perm, c_out_lines, Lit("\n"));
|
|
F_ClearWrite(c_out_file, c_out);
|
|
}
|
|
|
|
SetBuildStatus(build.c_parse.errors.count > 0);
|
|
}
|
|
|
|
//- Generate HLSL file
|
|
{
|
|
/* Clear shader store */
|
|
/* TODO: Move to separate artifacts dir that gets cleared, including archive files */
|
|
OS_Mkdir(shader_store_name);
|
|
{
|
|
/* Remove all old shaders */
|
|
StringList files = ZI;
|
|
F_FilesFromDir(perm, &files, shader_store_name, F_IterFlag_None);
|
|
for (StringListNode *n = files.first; n; n = n->next)
|
|
{
|
|
String file = n->s;
|
|
/* Safety check to prevent non-shader files from being removed */
|
|
if (StringEndsWith(file, Lit("VS")) || StringEndsWith(file, Lit("CS")) || StringEndsWith(file, Lit("PS")))
|
|
{
|
|
OS_Rm(n->s);
|
|
}
|
|
else
|
|
{
|
|
/* Unexpected file in shader store */
|
|
Assert(0);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Generate GPU file & shader entries */
|
|
{
|
|
StringList gpu_include_lines = ZI;
|
|
{
|
|
for (M_Entry *entry = build.layers_parse.first; entry->valid; entry = entry->next)
|
|
{
|
|
M_EntryKind kind = entry->kind;
|
|
M_Token *entry_tok = entry->name_token;
|
|
M_Token *arg0_tok = entry->arg_tokens[0];
|
|
M_Token *arg1_tok = entry->arg_tokens[1];
|
|
switch (kind)
|
|
{
|
|
default: break;
|
|
case M_EntryKind_IncludeGpu:
|
|
{
|
|
if (arg0_tok->valid)
|
|
{
|
|
String token_file = arg0_tok->file->name;
|
|
String token_parent_dir = F_GetParentDir(token_file);
|
|
String arg_file = arg0_tok->s;
|
|
String full = F_GetFull(perm, StringF(perm, "%F/%F", FmtString(token_parent_dir), FmtString(arg_file)));
|
|
if (F_IsFile(full))
|
|
{
|
|
String line = StringF(perm, "#include \"%F\"", FmtString(full));
|
|
PushStringToList(perm, &gpu_include_lines, line);
|
|
}
|
|
else
|
|
{
|
|
String err = StringF(perm, "File '%F' not found", FmtString(full));
|
|
M_PushError(perm, &build.gpu_parse.errors, arg0_tok, err);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
M_PushError(perm, &build.gpu_parse.errors, entry_tok, Lit("Expected file name"));
|
|
}
|
|
} break;
|
|
case M_EntryKind_VertexShader:
|
|
case M_EntryKind_PixelShader:
|
|
case M_EntryKind_ComputeShader:
|
|
{
|
|
if (arg0_tok->valid)
|
|
{
|
|
ShaderEntryKind shader_kind = kind == M_EntryKind_VertexShader ? ShaderEntryKind_VS
|
|
: kind == M_EntryKind_PixelShader ? ShaderEntryKind_PS
|
|
: kind == M_EntryKind_ComputeShader ? ShaderEntryKind_CS
|
|
: ShaderEntryKind_VS;
|
|
String shader_name = arg0_tok->s;
|
|
ShaderEntry *e = PushStruct(perm, ShaderEntry);
|
|
e->kind = shader_kind;
|
|
e->name = shader_name;
|
|
SllQueuePush(build.gpu_parse.first_shader_entry, build.gpu_parse.last_shader_entry, e);
|
|
++build.gpu_parse.shader_entries_count;
|
|
}
|
|
else
|
|
{
|
|
M_PushError(perm, &build.gpu_parse.errors, entry_tok, Lit("Expected shader name"));
|
|
}
|
|
} break;
|
|
}
|
|
}
|
|
}
|
|
if (build.gpu_parse.errors.count == 0)
|
|
{
|
|
StringList gpu_out_lines = ZI;
|
|
PushStringToList(perm, &gpu_out_lines, Lit("// Auto generated file"));
|
|
/* Include base layer */
|
|
{
|
|
String base_inc_path = F_GetFull(perm, Lit("../src/base/base_inc.h"));
|
|
PushStringToList(perm, &gpu_out_lines, Lit(""));
|
|
PushStringToList(perm, &gpu_out_lines, Lit("//- Base layer includes"));
|
|
PushStringToList(perm, &gpu_out_lines, StringF(perm, "#include \"%F\"", FmtString(base_inc_path)));
|
|
}
|
|
/* Include dependency layers */
|
|
if (gpu_out_lines.count > 0)
|
|
{
|
|
PushStringToList(perm, &gpu_out_lines, Lit(""));
|
|
PushStringToList(perm, &gpu_out_lines, Lit("//- Dependency graph includes"));
|
|
for (StringListNode *n = gpu_include_lines.first; n; n = n->next)
|
|
{
|
|
PushStringToList(perm, &gpu_out_lines, n->s);
|
|
}
|
|
}
|
|
/* Write to file */
|
|
PushStringToList(perm, &gpu_out_lines, Lit(""));
|
|
String c_out = StringFromList(perm, gpu_out_lines, Lit("\n"));
|
|
F_ClearWrite(gpu_out_file, c_out);
|
|
}
|
|
}
|
|
|
|
SetBuildStatus(build.gpu_parse.errors.count > 0);
|
|
}
|
|
|
|
//- Generate archive info
|
|
{
|
|
/* Push embedded archive dirs */
|
|
for (M_Entry *entry = build.layers_parse.first; entry->valid; entry = entry->next)
|
|
{
|
|
M_EntryKind kind = entry->kind;
|
|
M_Token *entry_tok = entry->name_token;
|
|
M_Token *arg0_tok = entry->arg_tokens[0];
|
|
M_Token *arg1_tok = entry->arg_tokens[1];
|
|
switch (kind)
|
|
{
|
|
default: break;
|
|
case M_EntryKind_EmbedDir:
|
|
{
|
|
if (arg0_tok->valid && arg1_tok->valid)
|
|
{
|
|
String store_name = arg0_tok->s;
|
|
String token_file = arg1_tok->file->name;
|
|
String token_parent_dir = F_GetParentDir(token_file);
|
|
String arg_dir = arg1_tok->s;
|
|
String full = F_GetFullCrossPlatform(perm, StringF(perm, "%F/%F", FmtString(token_parent_dir), FmtString(arg_dir)));
|
|
if (F_IsDir(full))
|
|
{
|
|
ResDir *rd = PushStruct(perm, ResDir);
|
|
rd->store_name = store_name;
|
|
rd->dir_path = full;
|
|
SllQueuePush(build.res_dir_parse.first_res_dir, build.res_dir_parse.last_res_dir, rd);
|
|
++build.res_dir_parse.res_dirs_count;
|
|
}
|
|
else
|
|
{
|
|
String err = StringF(perm, "Directory '%F' not found", FmtString(full));
|
|
M_PushError(perm, &build.res_dir_parse.errors, arg1_tok, err);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
M_PushError(perm, &build.res_dir_parse.errors, entry_tok, Lit("Expected resource store & directory name"));
|
|
}
|
|
} break;
|
|
}
|
|
}
|
|
SetBuildStatus(build.res_dir_parse.errors.count > 0);
|
|
}
|
|
|
|
//- Prep obj arrays
|
|
{
|
|
/* Gpu objs */
|
|
build.gpu_objs.count = build.gpu_parse.shader_entries_count;
|
|
build.gpu_objs.array = PushStructs(perm, GpuObj, build.gpu_objs.count);
|
|
|
|
/* Embed objs */
|
|
build.embed_objs.count += build.res_dir_parse.res_dirs_count;
|
|
if (build.gpu_parse.shader_entries_count > 0)
|
|
{
|
|
build.embed_objs.count += 1;
|
|
}
|
|
build.embed_objs.array = PushStructs(perm, EmbedObj, build.embed_objs.count);
|
|
}
|
|
}
|
|
|
|
WaveSync(lane);
|
|
|
|
//////////////////////////////
|
|
//- Phase 2/3: Compile
|
|
|
|
{
|
|
u64 task_idx = 0;
|
|
u32 embed_idx = 0;
|
|
|
|
//- Compile C
|
|
{
|
|
if (lane->idx == WaveLaneIdxFromTaskIdx(lane, task_idx++) && GetBuildStatus() == 0)
|
|
{
|
|
build.c_obj.obj_file = StringF(perm, "%F_gen_c.obj", FmtString(cmdline.leaf_layer_name));
|
|
String cmd = StringF(perm,
|
|
"cl.exe /c %F -Fo:%F %F %F %F %F",
|
|
FmtString(c_out_file),
|
|
FmtString(build.c_obj.obj_file),
|
|
FmtString(StringFromList(perm, cp.flags_msvc, Lit(" "))),
|
|
FmtString(StringFromList(perm, cp.compiler_only_flags_msvc, Lit(" "))),
|
|
FmtString(StringFromList(perm, cp.warnings_msvc, Lit(" "))),
|
|
FmtString(StringFromList(perm, cp.defs, Lit(" "))));
|
|
OS_CommandResult cmd_result = OS_RunCommand(perm, cmd);
|
|
String cmd_output = TrimWhitespace(cmd_result.output);
|
|
build.c_obj.output = cmd_output;
|
|
build.c_obj.return_code = cmd_result.code;
|
|
|
|
/* Ignore MSVC file-name echo */
|
|
if (MatchString(TrimWhitespace(build.c_obj.output), F_GetFileName(c_out_file)))
|
|
{
|
|
build.c_obj.output = Zstr;
|
|
}
|
|
|
|
SetBuildStatus(build.c_obj.return_code);
|
|
}
|
|
}
|
|
|
|
//- Compile shaders
|
|
{
|
|
u32 gpu_obj_idx = 0;
|
|
for (ShaderEntry *e = build.gpu_parse.first_shader_entry; e; e = e->next)
|
|
{
|
|
if (lane->idx == WaveLaneIdxFromTaskIdx(lane, task_idx++) && GetBuildStatus() == 0)
|
|
{
|
|
String shader_name = e->name;
|
|
GpuObj *gpu_obj = &build.gpu_objs.array[gpu_obj_idx];
|
|
String out_file = StringF(perm, "%F/%F", FmtString(shader_store_name), FmtString(e->name));
|
|
String target = e->kind == ShaderEntryKind_VS ? Lit("vs_6_6")
|
|
: e->kind == ShaderEntryKind_PS ? Lit("ps_6_6")
|
|
: e->kind == ShaderEntryKind_CS ? Lit("cs_6_6")
|
|
: Lit("vs_6_6");
|
|
String compile_cmd = StringF(perm,
|
|
"dxc.exe -T %F -E %F -Fo %F %F %F %F",
|
|
FmtString(target),
|
|
FmtString(e->name),
|
|
FmtString(out_file),
|
|
FmtString(gpu_out_file),
|
|
FmtString(StringFromList(perm, cp.defs, Lit(" "))),
|
|
FmtString(StringFromList(perm, cp.flags_dxc, Lit(" "))));
|
|
|
|
OS_CommandResult cmd_result = OS_RunCommand(perm, compile_cmd);
|
|
gpu_obj->output = cmd_result.output;
|
|
gpu_obj->return_code = cmd_result.code;
|
|
|
|
SetBuildStatus(gpu_obj->return_code);
|
|
|
|
/* Final shader compilation lane embeds shader archive */
|
|
u32 finished_count = Atomic32FetchAdd(&build.gpu_objs.finished_count, 1) + 1;
|
|
if (finished_count == build.gpu_objs.count && GetBuildStatus() == 0)
|
|
{
|
|
EmbedObj *embed = &build.embed_objs.array[embed_idx];
|
|
*embed = Embed(shader_store_name, shader_store_name);
|
|
}
|
|
}
|
|
++gpu_obj_idx;
|
|
}
|
|
if (build.gpu_parse.shader_entries_count > 0)
|
|
{
|
|
++embed_idx;
|
|
}
|
|
}
|
|
|
|
//- Embed resource dirs
|
|
{
|
|
for (ResDir *rd = build.res_dir_parse.first_res_dir; rd; rd = rd->next)
|
|
{
|
|
if (lane->idx == WaveLaneIdxFromTaskIdx(lane, task_idx++) && GetBuildStatus() == 0)
|
|
{
|
|
String dir_path = rd->dir_path;
|
|
String store_name = rd->store_name;
|
|
EmbedObj *embed = &build.embed_objs.array[embed_idx];
|
|
*embed = Embed(store_name, dir_path);
|
|
}
|
|
++embed_idx;
|
|
}
|
|
}
|
|
}
|
|
|
|
WaveSync(lane);
|
|
|
|
//////////////////////////////
|
|
//- Phase 3/3: Link
|
|
|
|
if (lane->idx == 0 && GetBuildStatus() == 0)
|
|
{
|
|
String exe_file = StringF(perm, "%F.exe", FmtString(cmdline.leaf_layer_name));
|
|
{
|
|
/* Wait for exe to become writable (wait for program to close) */
|
|
f32 timeout = 1.0;
|
|
OS_File file = OS_OpenFile(exe_file, OS_FileFlag_Write, NsFromSeconds(timeout));
|
|
OS_CloseFile(file);
|
|
}
|
|
|
|
i64 start_ns = TimeNs();
|
|
|
|
String obj_files_str = ZI;
|
|
{
|
|
StringList obj_files = ZI;
|
|
PushStringToList(perm, &obj_files, build.c_obj.obj_file);
|
|
for (u64 embed_obj_idx = 0; embed_obj_idx < build.embed_objs.count; ++embed_obj_idx)
|
|
{
|
|
EmbedObj *embed_obj = &build.embed_objs.array[embed_obj_idx];
|
|
PushStringToList(perm, &obj_files, embed_obj->obj_file);
|
|
}
|
|
obj_files_str = StringFromList(perm, obj_files, Lit(" "));
|
|
}
|
|
|
|
String cmd = StringF(perm,
|
|
"link.exe %F /OUT:%F %F %F",
|
|
FmtString(obj_files_str),
|
|
FmtString(exe_file),
|
|
FmtString(StringFromList(perm, cp.flags_msvc, Lit(" "))),
|
|
FmtString(StringFromList(perm, cp.linker_only_flags_msvc, Lit(" "))));
|
|
OS_CommandResult result = OS_RunCommand(perm, cmd);
|
|
build.link.output = TrimWhitespace(result.output);
|
|
build.link.return_code = result.code;
|
|
i64 link_elapsed_ns = TimeNs() - start_ns;
|
|
// EchoLine(StringF(perm, ">>>>> Linked in %Fs", FmtFloat(SecondsFromNs(link_elapsed_ns))));
|
|
|
|
SetBuildStatus(build.link.return_code);
|
|
}
|
|
|
|
//////////////////////////////
|
|
//- Display build results
|
|
|
|
if (lane->idx == 0)
|
|
{
|
|
String gpu_obj_output = ZI;
|
|
{
|
|
GpuObj *disp_obj = 0;
|
|
for (u32 gpu_obj_idx = 0; gpu_obj_idx < build.gpu_objs.count; ++gpu_obj_idx)
|
|
{
|
|
GpuObj *gpu_obj = &build.gpu_objs.array[gpu_obj_idx];
|
|
if (!disp_obj || TrimWhitespace(disp_obj->output).len == 0 || disp_obj->return_code != 0)
|
|
{
|
|
disp_obj = gpu_obj;
|
|
}
|
|
}
|
|
if (disp_obj)
|
|
{
|
|
gpu_obj_output = TrimWhitespace(disp_obj->output);
|
|
}
|
|
}
|
|
String embed_obj_output = ZI;
|
|
{
|
|
StringList embed_obj_outputs = ZI;
|
|
for (u32 embed_obj_idx = 0; embed_obj_idx < build.embed_objs.count; ++embed_obj_idx)
|
|
{
|
|
EmbedObj *embed_obj = &build.embed_objs.array[embed_obj_idx];
|
|
PushStringToList(perm, &embed_obj_outputs, embed_obj->output);
|
|
}
|
|
embed_obj_output = StringFromList(perm, embed_obj_outputs, Lit("\n"));
|
|
}
|
|
|
|
EchoLineOrNothing(StringFromMetaErrors(perm, build.layers_parse.errors));
|
|
EchoLineOrNothing(StringFromMetaErrors(perm, build.c_parse.errors));
|
|
EchoLineOrNothing(StringFromMetaErrors(perm, build.gpu_parse.errors));
|
|
EchoLineOrNothing(build.c_obj.output);
|
|
EchoLineOrNothing(gpu_obj_output);
|
|
EchoLineOrNothing(StringFromMetaErrors(perm, build.res_dir_parse.errors));
|
|
EchoLineOrNothing(embed_obj_output);
|
|
EchoLineOrNothing(build.link.output);
|
|
}
|
|
|
|
//////////////////////////////
|
|
//- Exit
|
|
|
|
if (lane->idx == 0)
|
|
{
|
|
EchoLine(StringF(perm, "Runtime: %Fs", FmtFloat(SecondsFromNs(TimeNs()))));
|
|
ExitNow(GetBuildStatus());
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ @hookimpl Startup
|
|
|
|
void StartupLayers(void)
|
|
{
|
|
OS_Startup();
|
|
CpuTopologyInfo cpu_info = GetCpuTopologyInfo();
|
|
i32 meta_lanes_count = cpu_info.num_logical_cores - 1;
|
|
DispatchWave(Lit("Meta"), MaxI32(meta_lanes_count, 1), BuildEntryPoint, 0);
|
|
}
|