From: Jean Privat Date: Tue, 9 May 2017 19:27:28 +0000 (-0400) Subject: nitc: add option --run to execute after a successful compilation. X-Git-Url: http://nitlanguage.org nitc: add option --run to execute after a successful compilation. Signed-off-by: Jean Privat --- diff --git a/share/man/nitc.md b/share/man/nitc.md index 77c8aa8..c241573 100644 --- a/share/man/nitc.md +++ b/share/man/nitc.md @@ -7,6 +7,8 @@ nitc - compiles Nit programs. nitc [*options*] FILE... +nitc [*options*] --run FILE [ARG]... + # DESCRIPTION @@ -163,6 +165,21 @@ Has precedence over the environment variable `NIT_DIR`. ## COMPILATION +### `--run` +Execute the binary after the compilation. + +The binary is generated as expected then it is executed directly. +After the execution, the binary is not removed. + +When `--run` is used, the first argument is the program to compile, the rest of the arguments are the arguments of the program. +Note that you MUST use `--` before the program arguments if one of them is an option starting with a `-`. + +~~~bash +$ nitc --run foo.nit arg # compile foo.nit, then executes `./foo arg` +$ nitc --run foo.nit arg -W # compile foo.nit with warnings, then executes `./foo arg` +$ nitc --run foo.nit -- arg -W # compile foo.nit, then executes `./foo arg -W` +~~~ + ### `--compile-dir` Directory used to generate temporary files. diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index 9ec830a..31795fc 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -31,6 +31,8 @@ redef class ToolContext var opt_output = new OptionString("Filename of the generated executable", "-o", "--output") # --dir var opt_dir = new OptionString("Output directory", "--dir") + # --run + var opt_run = new OptionBool("Execute the binary after the compilation", "--run") # --no-cc var opt_no_cc = new OptionBool("Do not invoke the C compiler", "--no-cc") # --no-main @@ -75,7 +77,7 @@ redef class ToolContext redef init do super - self.option_context.add_option(self.opt_output, self.opt_dir, self.opt_no_cc, self.opt_no_main, self.opt_make_flags, self.opt_compile_dir, self.opt_hardening) + self.option_context.add_option(self.opt_output, self.opt_dir, self.opt_run, self.opt_no_cc, self.opt_no_main, self.opt_make_flags, self.opt_compile_dir, self.opt_hardening) self.option_context.add_option(self.opt_no_check_covariance, self.opt_no_check_attr_isset, self.opt_no_check_assert, self.opt_no_check_autocast, self.opt_no_check_null, self.opt_no_check_all) self.option_context.add_option(self.opt_typing_test_metrics, self.opt_invocation_metrics, self.opt_isset_checks_metrics) self.option_context.add_option(self.opt_no_stacktrace) @@ -199,6 +201,14 @@ class MakefileToolchain sys.system("rm -r -- '{root_compile_dir.escape_to_sh}/'") end + if toolcontext.opt_run.value then + var mainmodule = compiler.mainmodule + var out = outfile(mainmodule) + var cmd = ["."/out] + cmd.append toolcontext.option_context.rest + toolcontext.exec_and_check(cmd, "--run") + end + time1 = get_time self.toolcontext.info("*** END COMPILING C: {time1-time0} ***", 2) end @@ -4194,6 +4204,10 @@ var model = new Model var modelbuilder = new ModelBuilder(model, toolcontext) var arguments = toolcontext.option_context.rest +if toolcontext.opt_run.value then + # When --run, only the first is the program, the rest is the run arguments + arguments = [toolcontext.option_context.rest.shift] +end if arguments.length > 1 and toolcontext.opt_output.value != null then print "Option Error: --output needs a single source file. Do you prefer --dir?" exit 1 diff --git a/tests/nitc.args b/tests/nitc.args index e6e6aff..b68b2f2 100644 --- a/tests/nitc.args +++ b/tests/nitc.args @@ -8,3 +8,4 @@ base_simple_import.nit base_simple.nit --dir out/ ; out/base_simple ; out/base_s test_define.nit -D text=hello -D num=42 -D flag --dir out/ ; out/test_define --log --log-dir $WRITE test_prog -o out/test_prog.bin test_define.nit --semi-global -D text=hello -D num=42 -D flag --dir out/ ; out/test_define +--run ../examples/print_arguments.nit 1 2 3 --dir out/ diff --git a/tests/sav/nitc_args11.res b/tests/sav/nitc_args11.res new file mode 100644 index 0000000..01e79c3 --- /dev/null +++ b/tests/sav/nitc_args11.res @@ -0,0 +1,3 @@ +1 +2 +3