소스 검색

Merge pull request #5202 from Feoramund/fix-2035

Add `-build-only`, `-keep-test-executable`, delete test executable after running
Jeroen van Rijn 3 달 전
부모
커밋
82c9681e28
2개의 변경된 파일42개의 추가작업 그리고 1개의 파일을 삭제
  1. 2 0
      src/build_settings.cpp
  2. 40 1
      src/main.cpp

+ 2 - 0
src/build_settings.cpp

@@ -441,6 +441,8 @@ struct BuildContext {
 	String extra_assembler_flags;
 	String microarch;
 	BuildModeKind build_mode;
+	bool   build_only;
+	bool   keep_test_executable;
 	bool   generate_docs;
 	bool   custom_optimization_level;
 	i32    optimization_level;

+ 40 - 1
src/main.cpp

@@ -312,6 +312,8 @@ enum BuildFlagKind {
 	BuildFlag_Collection,
 	BuildFlag_Define,
 	BuildFlag_BuildMode,
+	BuildFlag_BuildOnly,
+	BuildFlag_KeepTestExecutable,
 	BuildFlag_Target,
 	BuildFlag_Subtarget,
 	BuildFlag_Debug,
@@ -531,6 +533,8 @@ gb_internal bool parse_build_flags(Array<String> args) {
 	add_flag(&build_flags, BuildFlag_Collection,              str_lit("collection"),                BuildFlagParam_String,  Command__does_check);
 	add_flag(&build_flags, BuildFlag_Define,                  str_lit("define"),                    BuildFlagParam_String,  Command__does_check, true);
 	add_flag(&build_flags, BuildFlag_BuildMode,               str_lit("build-mode"),                BuildFlagParam_String,  Command__does_build); // Commands_build is not used to allow for a better error message
+	add_flag(&build_flags, BuildFlag_BuildOnly,               str_lit("build-only"),                BuildFlagParam_None,    Command_test);
+	add_flag(&build_flags, BuildFlag_KeepTestExecutable,      str_lit("keep-test-executable"),      BuildFlagParam_None,    Command_test);
 	add_flag(&build_flags, BuildFlag_Target,                  str_lit("target"),                    BuildFlagParam_String,  Command__does_check);
 	add_flag(&build_flags, BuildFlag_Subtarget,               str_lit("subtarget"),                 BuildFlagParam_String,  Command__does_check);
 	add_flag(&build_flags, BuildFlag_Debug,                   str_lit("debug"),                     BuildFlagParam_None,    Command__does_check);
@@ -1193,6 +1197,24 @@ gb_internal bool parse_build_flags(Array<String> args) {
 
 							break;
 						}
+						case BuildFlag_BuildOnly:
+							if (build_context.keep_test_executable) {
+								gb_printf_err("`-keep-test-executable` is mutually exclusive with `-build-only`.\n");
+								gb_printf_err("We either only build or run the test and optionally keep the executable.\n");
+								bad_flags = true;
+							} else {
+								build_context.build_only = true;
+							}
+							break;
+						case BuildFlag_KeepTestExecutable:
+							if (build_context.build_only) {
+								gb_printf_err("`-build-only` is mutually exclusive with `-keep-test-executable`.\n");
+								gb_printf_err("We either only build or run the test and optionally keep the executable.\n");
+								bad_flags = true;
+							} else {
+								build_context.keep_test_executable = true;
+							}
+							break;
 
 						case BuildFlag_Debug:
 							build_context.ODIN_DEBUG = true;
@@ -2381,6 +2403,12 @@ gb_internal int print_show_help(String const arg0, String command, String option
 		}
 	}
 
+	if (test_only) {
+		if (print_flag("-build-only")) {
+			print_usage_line(2, "Only builds the test executable; does not automatically run it afterwards.");
+		}
+	}
+
 	if (check) {
 		if (print_flag("-collection:<name>=<filepath>")) {
 			print_usage_line(2, "Defines a library collection used for imports.");
@@ -2543,6 +2571,12 @@ gb_internal int print_show_help(String const arg0, String command, String option
 		}
 	}
 
+	if (test_only) {
+		if (print_flag("-keep-test-executable")) {
+			print_usage_line(2, "Keep the test executable after running it instead of deleting it normally.");
+		}
+	}
+
 	if (run_or_build) {
 		if (print_flag("-linker:<string>")) {
 			print_usage_line(2, "Specify the linker to use.");
@@ -3861,11 +3895,16 @@ end_of_code_gen:;
 		show_timings(checker, &global_timings);
 	}
 
-	if (run_output) {
+	if (!build_context.build_only && run_output) {
 		String exe_name = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_Output]);
 		defer (gb_free(heap_allocator(), exe_name.text));
 
 		system_must_exec_command_line_app("odin run", "\"%.*s\" %.*s", LIT(exe_name), LIT(run_args_string));
+
+		if (build_context.command_kind == Command_test && !build_context.keep_test_executable) {
+			char const *filename = cast(char const *)exe_name.text;
+			gb_file_remove(filename);
+		}
 	}
 	return 0;
 }