[vala] Add experimental vala tool for use with #!



commit ce27f41ee5f37a2baa90ad3f0057346c1eaac4ad
Author: Jürg Billeter <j bitron ch>
Date:   Wed Jun 30 23:49:59 2010 +0200

    Add experimental vala tool for use with #!

 compiler/Makefile.am       |    3 +
 compiler/valacompiler.vala |   89 +++++++++++++++++++++++++++++++++++++++++++-
 configure.ac               |    1 +
 vala/valacodecontext.vala  |    2 +
 vala/valaparser.vala       |    2 +-
 vala/valasourcefile.vala   |    4 +-
 vapi/glib-2.0.vapi         |    6 ++-
 7 files changed, 103 insertions(+), 4 deletions(-)
---
diff --git a/compiler/Makefile.am b/compiler/Makefile.am
index e4763b3..bbc9335 100644
--- a/compiler/Makefile.am
+++ b/compiler/Makefile.am
@@ -37,6 +37,9 @@ valac_LDADD = \
 
 EXTRA_DIST = $(valac_VALASOURCES) valac.vala.stamp
 
+install-exec-hook:
+	cd $(DESTDIR)$(bindir) && $(LN_S) -f valac$(EXEEXT) vala$(EXEEXT)
+
 MAINTAINERCLEANFILES = \
 	$(valac_VALASOURCES:.vala=.c) \
 	$(NULL)
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index 6374f90..3c5f733 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -75,6 +75,8 @@ class Vala.Compiler {
 
 	static string entry_point;
 
+	static bool run_output;
+
 	private CodeContext context;
 
 	const OptionEntry[] options = {
@@ -256,6 +258,8 @@ class Vala.Compiler {
 
 		context.entry_point_name = entry_point;
 
+		context.run_output = run_output;
+
 		if (defines != null) {
 			foreach (string define in defines) {
 				context.add_define (define);
@@ -327,7 +331,7 @@ class Vala.Compiler {
 		foreach (string source in sources) {
 			if (FileUtils.test (source, FileTest.EXISTS)) {
 				var rpath = realpath (source);
-				if (source.has_suffix (".vala") || source.has_suffix (".gs")) {
+				if (run_output || source.has_suffix (".vala") || source.has_suffix (".gs")) {
 					var source_file = new SourceFile (context, rpath);
 
 					if (context.profile == Profile.POSIX) {
@@ -586,7 +590,90 @@ class Vala.Compiler {
 		return rpath;
 	}
 
+	static int run_source (string[] args) {
+		int i = 1;
+		if (args[i] != null && args[i].has_prefix ("-")) {
+			try {
+				string[] compile_args;
+				Shell.parse_argv ("valac " + args[1], out compile_args);
+
+				var opt_context = new OptionContext ("- Vala");
+				opt_context.set_help_enabled (true);
+				opt_context.add_main_entries (options, null);
+				opt_context.parse (ref compile_args);
+			} catch (ShellError e) {
+				stdout.printf ("%s\n", e.message);
+				return 1;
+			} catch (OptionError e) {
+				stdout.printf ("%s\n", e.message);
+				stdout.printf ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
+				return 1;
+			}
+
+			i++;
+		}
+
+		if (args[i] == null) {
+			stderr.printf ("No source file specified.\n");
+			return 1;
+		}
+
+		sources = { args[i] };
+		output = "%s/%s.XXXXXX".printf (Environment.get_tmp_dir (), Path.get_basename (args[i]));
+		int outputfd = FileUtils.mkstemp (output);
+		if (outputfd < 0) {
+			return 1;
+		}
+
+		run_output = true;
+		disable_warnings = true;
+		quiet_mode = true;
+
+		var compiler = new Compiler ();
+		int ret = compiler.run ();
+		if (ret != 0) {
+			return ret;
+		}
+
+		FileUtils.close (outputfd);
+		if (FileUtils.chmod (output, 0700) != 0) {
+			FileUtils.unlink (output);
+			return 1;
+		}
+
+		string[] target_args = { output };
+		while (i < args.length) {
+			target_args += args[i];
+			i++;
+		}
+
+		try {
+			Pid pid;
+			var loop = new MainLoop ();
+			int child_status = 0;
+
+			Process.spawn_async (null, target_args, null, SpawnFlags.CHILD_INHERITS_STDIN | SpawnFlags.DO_NOT_REAP_CHILD | SpawnFlags.FILE_AND_ARGV_ZERO, null, out pid);
+
+			FileUtils.unlink (output);
+			ChildWatch.add (pid, (pid, status) => {
+				child_status = (status & 0xff00) >> 8;
+				loop.quit ();
+			});
+
+			loop.run ();
+
+			return child_status;
+		} catch (SpawnError e) {
+			stdout.printf ("%s\n", e.message);
+			return 1;
+		}
+	}
+
 	static int main (string[] args) {
+		if (Path.get_basename (args[0]) == "vala") {
+			return run_source (args);
+		}
+
 		try {
 			var opt_context = new OptionContext ("- Vala Compiler");
 			opt_context.set_help_enabled (true);
diff --git a/configure.ac b/configure.ac
index bdd9878..3522be5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -11,6 +11,7 @@ AC_PROG_CC
 AM_PROG_CC_C_O
 AC_DISABLE_STATIC
 AC_PROG_LIBTOOL
+AC_PROG_LN_S
 
 AC_PROG_LEX
 if test "$LEX" = :; then
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index 2c9e326..0bce4bf 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -167,6 +167,8 @@ public class Vala.CodeContext {
 
 	public string entry_point_name { get; set; }
 
+	public bool run_output { get; set; }
+
 	private List<SourceFile> source_files = new ArrayList<SourceFile> ();
 	private List<string> c_source_files = new ArrayList<string> ();
 	private Namespace _root = new Namespace (null);
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index e6c7378..4c767ee 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -76,7 +76,7 @@ public class Vala.Parser : CodeVisitor {
 	}
 
 	public override void visit_source_file (SourceFile source_file) {
-		if (source_file.filename.has_suffix (".vala") || source_file.filename.has_suffix (".vapi")) {
+		if (context.run_output || source_file.filename.has_suffix (".vala") || source_file.filename.has_suffix (".vapi")) {
 			parse_file (source_file);
 		}
 	}
diff --git a/vala/valasourcefile.vala b/vala/valasourcefile.vala
index 97c6a2f..cff51b7 100644
--- a/vala/valasourcefile.vala
+++ b/vala/valasourcefile.vala
@@ -195,7 +195,9 @@ public class Vala.SourceFile {
 	 */
 	public string get_csource_filename () {
 		if (csource_filename == null) {
-			if (context.ccode_only || context.save_csources) {
+			if (context.run_output) {
+				csource_filename = context.output + ".c";
+			} else if (context.ccode_only || context.save_csources) {
 				csource_filename = "%s%s.c".printf (get_destination_directory (), get_basename ());
 			} else {
 				// temporary file
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index 0894c8f..759c230 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -1476,7 +1476,8 @@ namespace GLib {
 	}
 	
 	namespace ChildWatch {
-		public static uint add (Pid pid, ChildWatchFunc function);
+		[CCode (cname = "g_child_watch_add_full")]
+		public static uint add (Pid pid, owned ChildWatchFunc function, [CCode (pos = 0.1)] int priority = Priority.DEFAULT_IDLE);
 		public static uint add_full (int priority, Pid pid, owned ChildWatchFunc function);
 	}
 	
@@ -2727,6 +2728,9 @@ namespace GLib {
 		
 		[CCode (cname = "symlink")]
 		public static int symlink (string oldpath, string newpath);
+
+		[CCode (cname = "close", cheader_filename = "unistd.h")]
+		public static int close (int fd);
 	}
 
 	[CCode (cname = "stat")]



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]