4coder/4coder_build_commands.cpp

195 lines
6.6 KiB
C++
Raw Normal View History

/*
4coder_build_commands.cpp - Commands for building.
*/
// TOP
2019-06-01 23:58:28 +00:00
static String_Const_u8
push_build_directory_at_file(Application_Links *app, Arena *arena, Buffer_ID buffer){
String_Const_u8 result = {};
String_Const_u8 file_name = push_buffer_file_name(app, arena, buffer);
2019-06-01 23:58:28 +00:00
Temp_Memory restore_point = begin_temp(arena);
String_Const_u8 base_name = push_buffer_base_name(app, arena, buffer);
2019-06-01 23:58:28 +00:00
b32 is_match = string_match(file_name, base_name);
end_temp(restore_point);
if (!is_match){
2019-07-13 00:43:17 +00:00
result = push_string_copy(arena, string_remove_last_folder(file_name));
}
return(result);
}
2019-06-01 23:58:28 +00:00
#if OS_WINDOWS
2019-06-01 23:58:28 +00:00
global String_Const_u8 standard_build_file_name_array[] = {
string_u8_litinit("build.bat"),
};
global String_Const_u8 standard_build_cmd_string_array[] = {
string_u8_litinit("build"),
};
2019-06-01 23:58:28 +00:00
#elif OS_LINUX || OS_MAC
global String_Const_u8 standard_build_file_name_array[] = {
string_u8_litinit("build.sh"),
string_u8_litinit("Makefile"),
};
global String_Const_u8 standard_build_cmd_string_array[] = {
string_u8_litinit("build.sh"),
string_u8_litinit("make"),
};
#else
#error OS needs standard search and build rules
#endif
static String_Const_u8
push_fallback_command(Arena *arena, String_Const_u8 file_name){
2019-06-18 22:56:09 +00:00
return(push_u8_stringf(arena, "echo could not find %.*s", string_expand(file_name)));
}
2019-06-01 23:58:28 +00:00
static String_Const_u8
push_fallback_command(Arena *arena){
return(push_fallback_command(arena, standard_build_file_name_array[0]));
}
2019-06-01 23:58:28 +00:00
global_const Buffer_Identifier standard_build_build_buffer_identifier = buffer_identifier(string_u8_litexpr("*compilation*"));
global_const u32 standard_build_exec_flags = CLI_OverlapWithConflict|CLI_SendEndSignal;
static void
standard_build_exec_command(Application_Links *app, View_ID view, String_Const_u8 dir, String_Const_u8 cmd){
exec_system_command(app, view, standard_build_build_buffer_identifier,
dir, cmd,
standard_build_exec_flags);
}
static b32
standard_search_and_build_from_dir(Application_Links *app, View_ID view, String_Const_u8 start_dir){
Scratch_Block scratch(app);
// NOTE(allen): Search
String_Const_u8 full_file_path = {};
String_Const_u8 cmd_string = {};
for (i32 i = 0; i < ArrayCount(standard_build_file_name_array); i += 1){
full_file_path = push_file_search_up_path(app, scratch, start_dir, standard_build_file_name_array[i]);
if (full_file_path.size > 0){
cmd_string = standard_build_cmd_string_array[i];
break;
}
}
b32 result = (full_file_path.size > 0);
if (result){
// NOTE(allen): Build
String_Const_u8 path = string_remove_last_folder(full_file_path);
#if OS_WINDOWS
2019-06-18 22:56:09 +00:00
String_Const_u8 command = push_u8_stringf(scratch, "%.*s/%.*s",
2019-06-01 23:58:28 +00:00
string_expand(path),
string_expand(cmd_string));
#elif OS_LINUX || OS_MAC
String_Const_u8 command = cmd_string;
#else
2019-06-01 23:58:28 +00:00
#error OS needs standard search and build rules
#endif
2019-06-01 23:58:28 +00:00
if (global_config.automatically_save_changes_on_build){
save_all_dirty_buffers(app);
}
standard_build_exec_command(app, view, path, command);
2019-06-18 22:56:09 +00:00
print_message(app, push_u8_stringf(scratch, "Building with: %.*s\n",
2019-06-01 23:58:28 +00:00
string_expand(full_file_path)));
}
return(result);
}
// NOTE(allen): This searches first using the active file's directory,
// then if no build script is found, it searches from 4coders hot directory.
static void
2019-06-01 23:58:28 +00:00
standard_search_and_build(Application_Links *app, View_ID view, Buffer_ID active_buffer){
Scratch_Block scratch(app);
b32 did_build = false;
String_Const_u8 build_dir = push_build_directory_at_file(app, scratch, active_buffer);
if (build_dir.size > 0){
did_build = standard_search_and_build_from_dir(app, view, build_dir);
}
if (!did_build){
build_dir = push_hot_directory(app, scratch);
if (build_dir.size > 0){
did_build = standard_search_and_build_from_dir(app, view, build_dir);
}
}
2019-06-01 23:58:28 +00:00
if (!did_build){
standard_build_exec_command(app, view,
push_hot_directory(app, scratch),
push_fallback_command(scratch));
}
}
CUSTOM_COMMAND_SIG(build_search)
CUSTOM_DOC("Looks for a build.bat, build.sh, or makefile in the current and parent directories. Runs the first that it finds and prints the output to *compilation*.")
{
2019-06-19 02:31:59 +00:00
View_ID view = get_active_view(app, AccessAll);
Buffer_ID buffer = view_get_buffer(app, view, AccessAll);
2019-06-01 23:58:28 +00:00
standard_search_and_build(app, view, buffer);
2018-05-09 05:22:33 +00:00
memset(&prev_location, 0, sizeof(prev_location));
2019-08-16 02:54:06 +00:00
lock_jump_buffer(app, string_u8_litexpr("*compilation*"));
}
2019-06-19 02:31:59 +00:00
static Buffer_ID
get_comp_buffer(Application_Links *app){
return(get_buffer_by_name(app, string_u8_litexpr("*compilation*"), AccessAll));
2019-06-01 23:58:28 +00:00
}
static View_ID
get_or_open_build_panel(Application_Links *app){
View_ID view = 0;
2019-06-19 02:31:59 +00:00
Buffer_ID buffer = get_comp_buffer(app);
if (buffer != 0){
view = get_first_view_with_buffer(app, buffer);
}
2019-04-07 17:36:24 +00:00
if (view == 0){
view = open_build_footer_panel(app);
}
return(view);
}
static void
set_fancy_compilation_buffer_font(Application_Links *app){
2019-06-19 02:31:59 +00:00
Buffer_ID buffer = get_comp_buffer(app);
Font_Load_Location font = { string_u8_litexpr("Inconsolata-Regular.ttf"), true, };
set_buffer_face_by_font_load_location(app, buffer, &font);
}
CUSTOM_COMMAND_SIG(build_in_build_panel)
CUSTOM_DOC("Looks for a build.bat, build.sh, or makefile in the current and parent directories. Runs the first that it finds and prints the output to *compilation*. Puts the *compilation* buffer in a panel at the footer of the current view.")
{
2019-06-19 02:31:59 +00:00
View_ID view = get_active_view(app, AccessAll);
Buffer_ID buffer = view_get_buffer(app, view, AccessAll);
View_ID build_view = get_or_open_build_panel(app);
2019-06-01 23:58:28 +00:00
standard_search_and_build(app, build_view, buffer);
set_fancy_compilation_buffer_font(app);
2018-05-09 05:22:33 +00:00
memset(&prev_location, 0, sizeof(prev_location));
2019-08-16 02:54:06 +00:00
lock_jump_buffer(app, string_u8_litexpr("*compilation*"));
}
CUSTOM_COMMAND_SIG(close_build_panel)
CUSTOM_DOC("If the special build panel is open, closes it.")
{
2018-06-23 03:03:58 +00:00
close_build_footer_panel(app);
}
CUSTOM_COMMAND_SIG(change_to_build_panel)
CUSTOM_DOC("If the special build panel is open, makes the build panel the active panel.")
{
View_ID view = get_or_open_build_panel(app);
if (view != 0){
view_set_active(app, view);
}
}
// BOTTOM