From 5d0cb1437c124a6cb2ae5ecd591c146d172bcc69 Mon Sep 17 00:00:00 2001
From: Delyan Angelov <delian66@gmail.com>
Date: Tue, 16 Jul 2019 18:07:28 +0300
Subject: [PATCH] Add support for VFLAGS environment variable, merged with cmd
 args.

Using VFLAGS, you can pass common options through it to the V compiler,
without having to manually specify them everytime when you type V ...
In addition, since environment variables are *inherited*, all subprocess
V compilers, which V launches (for example when compiling with -live),
will *also* use the same VFLAGS environment variable.

Example usage:
  export VFLAGS="-debug -show_c_cmd"
  v -live message.v

=> This will keep *both* of the generated C source files .message.c
   *AND* .message_shared_lib.c . It will also cause both V compile
   subprocesses to print their resulting C compiler backend lines.
   This is very useful when using GDB to debug problems.
---
 compiler/main.v | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/compiler/main.v b/compiler/main.v
index 37f540d632..1ccc437d53 100644
--- a/compiler/main.v
+++ b/compiler/main.v
@@ -96,7 +96,7 @@ mut:
 
 fn main() {
 	// There's no `flags` module yet, so args have to be parsed manually
-	args := os.args
+	args := env_vflags_and_os_args()
 	// Print the version and exit.
 	if '-v' in args || '--version' in args || 'version' in args {
 		println('V $Version')
@@ -1116,3 +1116,18 @@ v -nofmt file.v
 are working on vlib) 
 v -embed_vlib file.v 
 */
+
+fn env_vflags_and_os_args() []string {
+   mut args := []string
+   vflags := os.getenv('VFLAGS')
+   if '' != vflags {
+     args << os.args[0]
+     args << vflags.split(' ')
+     if os.args.len > 1 {
+       args << os.args.right(1)
+     }
+   }else{
+     args << os.args
+   }
+   return args
+}