2017-01-29 00:03:23 +00:00
/*
4 coder_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 = { } ;
2019-06-02 03:07:57 +00:00
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 ) ;
2019-06-02 03:07:57 +00:00
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 ) ) ;
2017-01-29 00:03:23 +00:00
}
return ( result ) ;
}
2019-06-01 23:58:28 +00:00
# if OS_WINDOWS
2017-01-29 00:03:23 +00:00
2019-06-01 23:58:28 +00:00
global String_Const_u8 standard_build_file_name_array [ ] = {
2020-12-31 00:55:11 +00:00
str8_lit ( " build.bat " ) ,
2019-06-01 23:58:28 +00:00
} ;
global String_Const_u8 standard_build_cmd_string_array [ ] = {
2020-12-31 00:55:11 +00:00
str8_lit ( " build " ) ,
2019-06-01 23:58:28 +00:00
} ;
2017-01-29 00:03:23 +00:00
2019-06-01 23:58:28 +00:00
# elif OS_LINUX || OS_MAC
global String_Const_u8 standard_build_file_name_array [ ] = {
2020-12-31 00:55:11 +00:00
str8_lit ( " build.sh " ) ,
str8_lit ( " Makefile " ) ,
2019-06-01 23:58:28 +00:00
} ;
global String_Const_u8 standard_build_cmd_string_array [ ] = {
2020-12-31 00:55:11 +00:00
str8_lit ( " build.sh " ) ,
str8_lit ( " make " ) ,
2019-06-01 23:58:28 +00:00
} ;
# 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 ) ) ) ;
2017-01-29 00:03:23 +00:00
}
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 ] ) ) ;
2017-01-29 00:03:23 +00:00
}
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 ) ;
}
2020-01-31 05:13:11 +00:00
function b32
2019-06-01 23:58:28 +00:00
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 ) ;
2020-01-02 22:37:01 +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 ) ) ;
2020-11-26 00:56:02 +00:00
b32 auto_save = def_get_config_b32 ( vars_save_string_lit ( " automatically_save_changes_on_build " ) ) ;
if ( auto_save ) {
2019-06-01 23:58:28 +00:00
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 ) ;
}
2017-01-29 00:03:23 +00:00
// 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 ) ;
2017-01-29 00:03:23 +00:00
}
}
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 ) ) ;
2017-01-29 00:03:23 +00:00
}
}
2017-11-15 23:57:21 +00:00
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-10-18 02:54:02 +00:00
View_ID view = get_active_view ( app , Access_Always ) ;
Buffer_ID buffer = view_get_buffer ( app , view , Access_Always ) ;
2019-06-01 23:58:28 +00:00
standard_search_and_build ( app , view , buffer ) ;
2019-08-24 01:34:42 +00:00
block_zero_struct ( & prev_location ) ;
2019-08-16 02:54:06 +00:00
lock_jump_buffer ( app , string_u8_litexpr ( " *compilation* " ) ) ;
2017-01-29 00:03:23 +00:00
}
2019-06-19 02:31:59 +00:00
static Buffer_ID
get_comp_buffer ( Application_Links * app ) {
2019-10-18 02:54:02 +00:00
return ( get_buffer_by_name ( app , string_u8_litexpr ( " *compilation* " ) , Access_Always ) ) ;
2019-06-01 23:58:28 +00:00
}
2017-01-29 00:03:23 +00:00
2019-04-06 19:40:36 +00:00
static View_ID
2017-01-29 00:03:23 +00:00
get_or_open_build_panel ( Application_Links * app ) {
2019-04-06 19:40:36 +00:00
View_ID view = 0 ;
2019-06-19 02:31:59 +00:00
Buffer_ID buffer = get_comp_buffer ( app ) ;
2019-04-05 02:03:36 +00:00
if ( buffer ! = 0 ) {
view = get_first_view_with_buffer ( app , buffer ) ;
2017-01-29 00:03:23 +00:00
}
2019-04-07 17:36:24 +00:00
if ( view = = 0 ) {
view = open_build_footer_panel ( app ) ;
2017-01-29 00:03:23 +00:00
}
return ( view ) ;
}
2020-01-31 05:13:11 +00:00
function void
2017-01-29 00:03:23 +00:00
set_fancy_compilation_buffer_font ( Application_Links * app ) {
2019-06-19 02:31:59 +00:00
Buffer_ID buffer = get_comp_buffer ( app ) ;
2019-11-22 05:37:11 +00:00
Font_Load_Location font = { } ;
Scratch_Block scratch ( app ) ;
2021-01-17 00:14:12 +00:00
font . file_name = def_search_normal_full_path ( scratch , str8_lit ( " fonts/Inconsolata-Regular.ttf " ) ) ;
2019-07-24 07:41:40 +00:00
set_buffer_face_by_font_load_location ( app , buffer , & font ) ;
2017-01-29 00:03:23 +00:00
}
2017-11-15 23:57:21 +00:00
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-10-18 02:54:02 +00:00
View_ID view = get_active_view ( app , Access_Always ) ;
Buffer_ID buffer = view_get_buffer ( app , view , Access_Always ) ;
2017-01-29 00:03:23 +00:00
2019-04-06 19:40:36 +00:00
View_ID build_view = get_or_open_build_panel ( app ) ;
2017-01-29 00:03:23 +00:00
2019-06-01 23:58:28 +00:00
standard_search_and_build ( app , build_view , buffer ) ;
2017-01-29 00:03:23 +00:00
set_fancy_compilation_buffer_font ( app ) ;
2019-08-24 01:34:42 +00:00
block_zero_struct ( & prev_location ) ;
2019-08-16 02:54:06 +00:00
lock_jump_buffer ( app , string_u8_litexpr ( " *compilation* " ) ) ;
2017-01-29 00:03:23 +00:00
}
2017-11-15 23:57:21 +00:00
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 ) ;
2017-01-29 00:03:23 +00:00
}
2017-11-15 23:57:21 +00:00
CUSTOM_COMMAND_SIG ( change_to_build_panel )
CUSTOM_DOC ( " If the special build panel is open, makes the build panel the active panel. " )
{
2019-04-06 19:40:36 +00:00
View_ID view = get_or_open_build_panel ( app ) ;
if ( view ! = 0 ) {
view_set_active ( app , view ) ;
2017-01-29 00:03:23 +00:00
}
}
// BOTTOM