Files
text_editor/build_file.cpp
2024-08-08 10:49:13 +02:00

487 lines
14 KiB
C++

#include "src/build_tool/library.cpp"
enum {
PROFILE_DEBUG,
PROFILE_RELEASE,
};
int Profile = PROFILE_DEBUG;
S8_String Compiler = "cl.exe";
void AddCommonFlags(Array<S8_String> *cmd) {
if (Compiler == "cl.exe") {
cmd->add("/MP");
}
cmd->add("/Zi /FC /nologo");
cmd->add("/WX /W3 /wd4200 /diagnostics:column");
if (Compiler == "clang-cl.exe") {
cmd->add("-fdiagnostics-absolute-paths");
cmd->add("-Wno-missing-braces");
cmd->add("-Wno-writable-strings");
cmd->add("-Wno-unused-function");
}
cmd->add("/Oi");
cmd->add("-D_CRT_SECURE_NO_WARNINGS");
if (Profile == PROFILE_DEBUG) {
cmd->add("-DDEBUG_BUILD=1");
cmd->add("-DRELEASE_BUILD=0");
// cmd->add("-fsanitize=address");
// cmd->add("-DUSE_ADDRESS_SANITIZER");
// cmd->add("-D_DEBUG /MDd");
} else {
cmd->add("-DDEBUG_BUILD=0");
cmd->add("-DRELEASE_BUILD=1");
cmd->add("/O2 /MT /DNDEBUG /GL");
}
}
struct Library {
Array<S8_String> sources;
Array<S8_String> objects;
Array<S8_String> include_paths;
Array<S8_String> defines;
Array<S8_String> link;
};
Library PrepareSDL() {
Library l = {};
l.include_paths.add("../src/external/SDL/include");
l.objects.add("../src/external/SDL/VisualC/static_x64/Release/SDL3.lib");
l.link.add("/SUBSYSTEM:WINDOWS");
l.link.add("opengl32.lib");
l.link.add("imm32.lib");
l.link.add("gdi32.lib");
l.link.add("winmm.lib");
l.link.add("shell32.lib");
l.link.add("user32.lib");
l.link.add("advapi32.lib");
l.link.add("Setupapi.lib");
l.link.add("ole32.lib");
l.link.add("oleaut32.lib");
l.link.add("Version.lib");
return l;
}
Library PrepareSDLDynamic() {
Library l = {};
l.include_paths.add("../src/external/SDL/include");
l.objects.add("../src/external/SDL/VisualC/x64/Release/SDL3.lib");
l.link.add("/SUBSYSTEM:WINDOWS");
OS_CopyFile("../src/external/SDL/VisualC/x64/Release/SDL3.dll", "./SDL3.dll", false);
return l;
}
Library PrepareRaylib() {
Library l = {};
l.include_paths.add("../src/external/raylib/include");
if (0) {
l.objects.add("../src/external/raylib/lib/raylib.lib");
} else {
l.objects.add("../src/external/raylib/lib/raylibdll.lib");
OS_CopyFile("../src/external/raylib/lib/raylib.dll", "./raylib.dll", false);
}
return l;
}
Library PrepareLua() {
Library l = {};
l.include_paths.add("../src/external/lua/src");
MA_Scratch scratch;
for (OS_FileIter it = OS_IterateFiles(scratch, "../src/external/lua/src"); OS_IsValid(it); OS_Advance(&it)) {
if (it.filename == "luac.c") continue;
if (it.filename == "lua.c") continue;
if (S8_EndsWith(it.filename, ".c", true)) {
l.sources.add(it.absolute_path);
S8_String file = S8_ChopLastPeriod(it.filename);
l.objects.add(Fmt("%.*s.obj", S8_Expand(file)));
}
}
if (!OS_FileExists(l.objects[0])) {
Array<S8_String> cmd = {};
cmd.add(Compiler);
cmd.add("-c");
AddCommonFlags(&cmd);
For(l.include_paths) cmd.add(S8_Format(Perm, "-I %.*s ", S8_Expand(it)));
cmd += l.sources;
Run(cmd);
}
return l;
}
Library PrepareGlad() {
Library l = {};
l.sources.add("../src/external/glad/glad.c");
l.include_paths.add("../src/external/glad/");
l.objects.add("glad.obj");
if (!OS_FileExists(l.objects[0])) {
Array<S8_String> cmd = {};
cmd.add(Compiler);
cmd.add("-c");
AddCommonFlags(&cmd);
For(l.include_paths) cmd.add(S8_Format(Perm, "-I %.*s ", S8_Expand(it)));
cmd += l.sources;
Run(cmd);
}
return l;
}
int CompileTextEditor() {
int result = 0;
Array<Library> libs = {};
libs.add(PrepareSDLDynamic());
libs.add(PrepareLua());
libs.add(PrepareGlad());
Array<S8_String> cmd = {};
cmd.add(Compiler);
cmd.add("-Fe:te.exe");
cmd.add("-Fd:te.pdb");
cmd.add("-I ../src");
AddCommonFlags(&cmd);
For2(lib, libs) For(lib.defines) cmd.add(it);
cmd.add("../src/text_editor/text_editor.cpp");
cmd.add("../src/basic/win32.cpp");
For2(lib, libs) For(lib.include_paths) cmd.add(Fmt("-I %.*s", S8_Expand(it)));
cmd.add("/link");
cmd.add("/incremental:no");
// cmd.add("/SUBSYSTEM:WINDOWS");
// cmd.add("opengl32.lib gdi32.lib winmm.lib shell32.lib user32.lib msvcrt.lib /NODEFAULTLIB:LIBCMT");
For2(lib, libs) For(lib.link) cmd.add(it);
For(libs) For2(o, it.objects) cmd.add(o);
cmd.add("icon.res");
OS_DeleteFile("te.pdb");
// For(cmd) IO_Printf("%.*s\n", S8_Expand(it));
if (!OS_FileExists("icon.res")) {
OS_CopyFile("../data/icon.ico", "icon.ico", true);
OS_CopyFile("../data/icon.rc", "icon.rc", true);
Run("rc icon.rc");
}
result += Run(cmd);
return result;
}
char *C(const char *value) {
if (value[0] == '0' && value[1] == 'x') {
S8_String f = Fmt("{0x%.*s, 0x%.*s, 0x%.*s, 0x%.*s}", 2, value + 2, 2, value + 4, 2, value + 6, 2, value + 8);
return f.str;
} else {
return (char *)value;
}
}
S8_String LuaScript = R"==(
-- @todo: should we rewrite linux paths to windows on windows and vice-versa?
function ExtractLineAndColumn(s)
local line = 1
local col = 1
-- filename:line:column
local lci, lcj = s:find(":%d+:%d+$")
if lci then
local line_and_col = s:sub(lci, lcj)
local i, j = line_and_col:find("%d+")
line = line_and_col:sub(i, j)
col = line_and_col:sub(j + 2, -1)
s = s:sub(1, lci - 1)
return line, col, s
else
local li, lj = s:find(":%d+$")
if li then
local line_string = s:sub(li, lj)
line = line_string:sub(2, -1)
s = s:sub(1, li - 1)
return line, col, s
end
end
-- filename(line,col):
local lci, lcj = s:find("%(%d+,%d+%):$")
if lci then
local line_and_col = s:sub(lci, lcj)
local i, j = line_and_col:find("%d+")
line = line_and_col:sub(i, j)
col = line_and_col:sub(j + 2, -3)
s = s:sub(1, lci - 1)
return line, col, s
else
local li, lj = s:find("%(%d+%):$")
if li then
local line_string = s:sub(li, lj)
line = line_string:sub(2, -3)
s = s:sub(1, li - 1)
return line, col, s
end
end
-- filename(line,col)
local lci, lcj = s:find("%(%d+,%d+%)$")
if lci then
local line_and_col = s:sub(lci, lcj)
local i, j = line_and_col:find("%d+")
line = line_and_col:sub(i, j)
col = line_and_col:sub(j + 2, -2)
s = s:sub(1, lci - 1)
return line, col, s
else
local li, lj = s:find("%(%d+%)$")
if li then
local line_string = s:sub(li, lj)
line = line_string:sub(2, -2)
s = s:sub(1, li - 1)
return line, col, s
end
end
return -1, -1, s
end
function BufferNameExists(name)
buffers = GetBufferList()
for i = 1,#buffers do
buff = buffers[i]
if buff == name then
return true
end
end
return false
end
function GenericTextFileRule(_s)
function match_path(s)
-- ./something
-- add working directory
local working_dir_i, working_dir_j = string.find(s, "^%./.+")
if working_dir_j then
result = s:sub(3)
result = GetWorkingDir().."/"..result
return result
end
-- C:/something
local abs_i, abs_j = string.find(s, "^%a:/.+")
if abs_i then
return s
end
-- C:\something
local abs_i, abs_j = s:find("^%a:\\.+")
if abs_i then
s = s:gsub("\\", "/")
return s
end
-- /mnt/something
local abs_i, abs_j = s:find("^/.*")
if abs_i then
return s
end
if BufferNameExists(s) then
return s
end
-- other_random_filename
buffer_dir = GetCurrentBufferDir()
if buffer_dir:len() == 0 then
return s
else
return buffer_dir..'/'..s
end
end
line, col, _s = ExtractLineAndColumn(_s)
file_path = match_path(_s)
return {kind = "text", file_path = file_path, line = line, col = col}
end
function MatchAgainstIncludes(s)
line, col, s = ExtractLineAndColumn(s)
include_paths = {
"C:/Work/text_editor/src",
"C:/Work/text_editor/src/text_editor",
}
for i = 1,#include_paths do
file_path = include_paths[i] .. '/' .. s
if FileExists(file_path) then
return {kind = "text", file_path = file_path, line = line, col = col}
end
end
return nil
end
function MatchGitCommit(s)
local i, j = string.find(s, "^commit ([a-zA-Z0-9]+)")
if i then
s = s:sub(8)
local command = "git --no-pager show "..s
return {kind = "exec", cmd = command, working_dir = GetCurrentBufferDir()}
end
return nil
end
Rules = {
GenericTextFileRule,
MatchAgainstIncludes,
MatchGitCommit,
}
function ApplyRules(s)
for i = #Rules,1,-1 do
rule = Rules[i]
result = rule(s)
if result then
return result
end
end
return nil
end
)==";
void GenerateConfig() {
struct Var {
const char *name;
const char *value;
};
Array<Var> gruvbox = {};
gruvbox.add({"GruvboxDark0Hard", "0x1d2021ff"});
gruvbox.add({"GruvboxDark0", "0x282828ff"});
gruvbox.add({"GruvboxDark0Soft", "0x32302fff"});
gruvbox.add({"GruvboxDark1", "0x3c3836ff"});
gruvbox.add({"GruvboxDark2", "0x504945ff"});
gruvbox.add({"GruvboxDark3", "0x665c54ff"});
gruvbox.add({"GruvboxDark4", "0x7c6f64ff"});
gruvbox.add({"GruvboxGray245", "0x928374ff"});
gruvbox.add({"GruvboxGray244", "0x928374ff"});
gruvbox.add({"GruvboxLight0Hard", "0xf9f5d7ff"});
gruvbox.add({"GruvboxLight0", "0xfbf1c7ff"});
gruvbox.add({"GruvboxLight0Soft", "0xf2e5bcff"});
gruvbox.add({"GruvboxLight1", "0xebdbb2ff"});
gruvbox.add({"GruvboxLight2", "0xd5c4a1ff"});
gruvbox.add({"GruvboxLight3", "0xbdae93ff"});
gruvbox.add({"GruvboxLight4", "0xa89984ff"});
gruvbox.add({"GruvboxBrightRed", "0xfb4934ff"});
gruvbox.add({"GruvboxBrightGreen", "0xb8bb26ff"});
gruvbox.add({"GruvboxBrightYellow", "0xfabd2fff"});
gruvbox.add({"GruvboxBrightBlue", "0x83a598ff"});
gruvbox.add({"GruvboxBrightPurple", "0xd3869bff"});
gruvbox.add({"GruvboxBrightAqua", "0x8ec07cff"});
gruvbox.add({"GruvboxBrightOrange", "0xfe8019ff"});
gruvbox.add({"GruvboxNeutralRed", "0xcc241dff"});
gruvbox.add({"GruvboxNeutralGreen", "0x98971aff"});
gruvbox.add({"GruvboxNeutralYellow", "0xd79921ff"});
gruvbox.add({"GruvboxNeutralBlue", "0x458588ff"});
gruvbox.add({"GruvboxNeutralPurple", "0xb16286ff"});
gruvbox.add({"GruvboxNeutralAqua", "0x689d6aff"});
gruvbox.add({"GruvboxNeutralOrange", "0xd65d0eff"});
gruvbox.add({"GruvboxFadedRed", "0x9d0006ff"});
gruvbox.add({"GruvboxFadedGreen", "0x79740eff"});
gruvbox.add({"GruvboxFadedYellow", "0xb57614ff"});
gruvbox.add({"GruvboxFadedBlue", "0x076678ff"});
gruvbox.add({"GruvboxFadedPurple", "0x8f3f71ff"});
gruvbox.add({"GruvboxFadedAqua", "0x427b58ff"});
gruvbox.add({"GruvboxFadedOrange", "0xaf3a03ff"});
Array<Var> colors = {};
colors.add({"Text", "GruvboxDark0Hard"});
colors.add({"LoadTextHighlight", "0x0000000F"});
colors.add({"Background", "GruvboxLight0Hard"});
colors.add({"InactiveWindow", "0x0000000F"});
colors.add({"TextLineNumbers", "GruvboxDark4"});
colors.add({"LineHighlight", "GruvboxLight0Soft"});
colors.add({"MainCaret", "GruvboxDark0Hard"});
colors.add({"SubCaret", "GruvboxGray245"});
colors.add({"Selection", "GruvboxLight1"});
colors.add({"WhitespaceDuringSelection", "GruvboxLight4"});
colors.add({"FuzzySearchLineHighlight", "GruvboxDark0"});
colors.add({"ScrollbarBackground", "GruvboxLight2"});
colors.add({"ScrollbarScroller", "GruvboxLight1"});
colors.add({"ScrollbarScrollerSelected", "GruvboxLight0Hard"});
colors.add({"TitleBarText", "GruvboxDark2"});
colors.add({"TitleBarBackground", "GruvboxLight1"});
colors.add({"TitleBarSelection", "GruvboxLight3"});
Array<Var> style = {};
style.add({"DrawLineNumbers", "1"});
style.add({"DrawScrollbar", "1"});
style.add({"IndentSize", "4"});
style.add({"TrimWhitespaceOnSave", "1"});
style.add({"FontSize", "12"});
style.add({"FontFilter", "0"}); // nearest = 0, linear = 1 - seems like nearest always better?
style.add({"Font", "C:/Windows/Fonts/consola.ttf"});
{
MA_Scratch scratch;
Array<S8_String> sb = {MA_GetAllocator(scratch)};
{
For(gruvbox) sb.add(Fmt("Color %s = %s;", it.name, C(it.value)));
For(colors) sb.add(Fmt("Color Color%s = %s;", it.name, C(it.value)));
For(style) {
if (CHAR_IsDigit(it.value[0])) sb.add(Fmt("Int Style%s = %s;", it.name, it.value));
else sb.add(Fmt("String Style%s = \"%s\";", it.name, it.value));
}
}
S8_String string = Merge(scratch, sb, "\n");
OS_WriteFile("../src/text_editor/generated_variables.cpp", string);
sb = {};
sb.add("String BaseLuaConfig = R\"==(");
{
For(gruvbox) sb.add(Fmt("local %s = %s", it.name, it.value));
sb.add("Color = {}");
For(colors) sb.add(Fmt("Color.%s = %s", it.name, it.value));
sb.add("Style = {}");
For(style) {
if (CHAR_IsDigit(it.value[0])) sb.add(Fmt("Style.%s = %s", it.name, it.value));
else sb.add(Fmt("Style.%s = \"%s\"", it.name, it.value));
}
sb.add(LuaScript);
}
sb.add(")==\";");
sb.add("void ReloadStyle() {");
{
For(colors) sb.add(Fmt(" Color%s = GetColor(\"%s\", Color%s);", it.name, it.name, it.name));
For(style) {
if (CHAR_IsDigit(it.value[0])) sb.add(Fmt(" Style%s = GetStyleInt(\"%s\", Style%s);", it.name, it.name, it.name));
else sb.add(Fmt(" Style%s = GetStyleString(\"%s\", Style%s);", it.name, it.name, it.name));
}
}
sb.add("}");
string = Merge(scratch, sb, "\n");
OS_WriteFile("../src/text_editor/generated.cpp", string);
}
}
int main() {
MA_InitScratch();
SRC_InitCache(Perm, "te.cache");
GenerateConfig();
int result = CompileTextEditor();
// int result = CompileNewPlatform();
if (result != 0) {
OS_DeleteFile("te.cache");
return result;
}
SRC_SaveCache();
return 0;
}