]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Add experimental vala tool for use with #!
authorJürg Billeter <j@bitron.ch>
Wed, 30 Jun 2010 21:49:59 +0000 (23:49 +0200)
committerJürg Billeter <j@bitron.ch>
Wed, 30 Jun 2010 21:49:59 +0000 (23:49 +0200)
compiler/Makefile.am
compiler/valacompiler.vala
configure.ac
vala/valacodecontext.vala
vala/valaparser.vala
vala/valasourcefile.vala
vapi/glib-2.0.vapi

index e4763b3d83a5504a7bc1670d7bcd7030df3f7128..bbc9335804271f3db8b98a8277363ad1d7e43f7d 100644 (file)
@@ -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)
index 6374f907bc2b4088d3a6414e5c5ab0c2b1dd67be..3c5f7338268f9e94fb5ccb6b4c011d7fc90a77cc 100644 (file)
@@ -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);
index bdd9878dd3e5d6a795277189609bd5ad23764588..3522be59ad3cd3f46ee9f5072cfa5bcb163c4e3b 100644 (file)
@@ -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
index 2c9e32694cff2fd4e2e5cff7c751228094e7a808..0bce4bfca513398f02271b92a5c4698486ae9da1 100644 (file)
@@ -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);
index e6c73785fb95d69cdce740c72c934836af18be7c..4c767eee5ac1505718b12e7ddc56c02720dd7cfc 100644 (file)
@@ -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);
                }
        }
index 97c6a2fd44c8c5033d6ae1c48ade0e60db496b17..cff51b7030c2f6b7fd6804e8f3e8d6a03b46f323 100644 (file)
@@ -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
index 0894c8fcf5ca889d5103c4d4eec96e6420424d54..759c2307d77a7c809727858c6b2ebafa58ac1203 100644 (file)
@@ -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")]