[vala/wip/valadoc-code-context: 1/2] vala: Move setting of target profile and standard packages into CodeContext



commit 87304876678ba79ff1e3d2be2ba2e0e786173b7f
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Jan 3 12:15:21 2019 +0100

    vala: Move setting of target profile and standard packages into CodeContext

 compiler/valacompiler.vala | 49 ++++++++++++++++++----------------------------
 libvaladoc/settings.vala   |  4 ++--
 vala/valacodecontext.vala  | 38 ++++++++++++++++++++++++++++++++---
 valadoc/treebuilder.vala   | 23 +++-------------------
 valadoc/valadoc.vala       | 15 ++++++++++++--
 vapigen/valavapigen.vala   |  9 +--------
 6 files changed, 73 insertions(+), 65 deletions(-)
---
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index 3be65377b..4a08adf41 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -87,7 +87,7 @@ class Vala.Compiler {
        static string[] defines;
        static bool quiet_mode;
        static bool verbose_mode;
-       static string profile;
+       static Profile profile;
        static bool nostdpkg;
        static bool enable_version_header;
        static bool disable_version_header;
@@ -154,7 +154,7 @@ class Vala.Compiler {
                { "pkg-config", 0, 0, OptionArg.STRING, ref pkg_config_command, "Use COMMAND as pkg-config 
command", "COMMAND" },
                { "dump-tree", 0, 0, OptionArg.FILENAME, ref dump_tree, "Write code tree to FILE", "FILE" },
                { "save-temps", 0, 0, OptionArg.NONE, ref save_temps, "Keep temporary files", null },
-               { "profile", 0, 0, OptionArg.STRING, ref profile, "Use the given profile instead of the 
default", "PROFILE" },
+               { "profile", 0, OptionFlags.OPTIONAL_ARG, OptionArg.CALLBACK, (void*) option_parse_profile, 
"Use the given profile instead of the default, options are 'gobject' or 'posix'", "PROFILE" },
                { "quiet", 'q', 0, OptionArg.NONE, ref quiet_mode, "Do not print messages to the console", 
null },
                { "verbose", 'v', 0, OptionArg.NONE, ref verbose_mode, "Print additional messages to the 
console", null },
                { "no-color", 0, 0, OptionArg.NONE, ref disable_colored_output, "Disable colored output, 
alias for --color=never", null },
@@ -181,6 +181,17 @@ class Vala.Compiler {
                return true;
        }
 
+       static bool option_parse_profile (string option_name, string? val, void* data) throws OptionError {
+               switch (val) {
+                       case null:
+                       case "gobject-2.0":
+                       case "gobject": profile = Profile.GOBJECT; break;
+                       case "posix": profile = Profile.POSIX; break;
+                       default: throw new OptionError.FAILED ("Invalid --profile argument '%s'", val);
+               }
+               return true;
+       }
+
        static bool option_deprecated (string option_name, string? val, void* data) throws OptionError {
                stdout.printf ("Command-line option `%s` is deprecated and will be ignored\n", option_name);
                return true;
@@ -283,18 +294,7 @@ class Vala.Compiler {
                if (ccode_only && save_temps) {
                        Report.warning (null, "--save-temps has no effect when -C or --ccode is set");
                }
-               if (profile == "posix") {
-                       context.profile = Profile.POSIX;
-                       context.add_define ("POSIX");
-               } else if (profile == "gobject-2.0" || profile == "gobject" || profile == null) {
-                       // default profile
-                       context.profile = Profile.GOBJECT;
-                       context.add_define ("GOBJECT");
-               } else {
-                       Report.error (null, "Unknown profile %s".printf (profile));
-               }
                nostdpkg |= fast_vapi_filename != null;
-               context.nostdpkg = nostdpkg;
 
                context.entry_point_name = entry_point;
 
@@ -305,29 +305,18 @@ class Vala.Compiler {
                }
                context.pkg_config_command = pkg_config_command;
 
+               context.set_target_profile (profile, !nostdpkg);
+
+               if (target_glib != null) {
+                       context.set_target_glib_version (target_glib);
+               }
+
                if (defines != null) {
                        foreach (string define in defines) {
                                context.add_define (define);
                        }
                }
 
-               if (context.profile == Profile.POSIX) {
-                       if (!nostdpkg) {
-                               /* default package */
-                               context.add_external_package ("posix");
-                       }
-               } else if (context.profile == Profile.GOBJECT) {
-                       if (target_glib != null) {
-                               context.set_target_glib_version (target_glib);
-                       }
-
-                       if (!nostdpkg) {
-                               /* default packages */
-                               context.add_external_package ("glib-2.0");
-                               context.add_external_package ("gobject-2.0");
-                       }
-               }
-
                if (packages != null) {
                        foreach (string package in packages) {
                                context.add_external_package (package);
diff --git a/libvaladoc/settings.vala b/libvaladoc/settings.vala
index 823dd5b20..67ef42aa0 100644
--- a/libvaladoc/settings.vala
+++ b/libvaladoc/settings.vala
@@ -91,9 +91,9 @@ public class Valadoc.Settings : Object {
        public bool experimental_non_null;
 
        /**
-        * Use the given profile (dova, gobject, posix, ...) instead of the defaul
+        * Use the given profile (gobject, posix, ...) instead of the default
         */
-       public string? profile;
+       public Vala.Profile profile;
 
        /**
         * Base source directory.
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index 1c3ad77cd..38e993070 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -163,14 +163,12 @@ public class Vala.CodeContext {
         */
        public bool save_temps { get; set; }
 
-       public Profile profile { get; set; }
+       public Profile profile { get; private set; }
 
        public bool verbose_mode { get; set; }
 
        public bool version_header { get; set; }
 
-       public bool nostdpkg { get; set; }
-
        public bool use_fast_vapi { get; set; }
 
        /**
@@ -574,6 +572,40 @@ public class Vala.CodeContext {
                }
        }
 
+       /**
+        * Set the target profile for code generation.
+        *
+        * This must be called once.
+        *
+        * @param profile the profile to use
+        * @param include_stdpkg whether to include profile-specific default packages
+        */
+       public void set_target_profile (Profile profile, bool include_stdpkg = true) {
+               switch (profile) {
+               default:
+               case Profile.GOBJECT:
+                       // default profile
+                       this.profile = profile;
+                       add_define ("GOBJECT");
+
+                       if (include_stdpkg) {
+                               /* default packages */
+                               add_external_package ("glib-2.0");
+                               add_external_package ("gobject-2.0");
+                       }
+                       break;
+               case Profile.POSIX:
+                       this.profile = profile;
+                       add_define ("POSIX");
+
+                       if (include_stdpkg) {
+                               /* default package */
+                               add_external_package ("posix");
+                       }
+                       break;
+               }
+       }
+
        /**
         * Set the target version of glib for code generation.
         *
diff --git a/valadoc/treebuilder.vala b/valadoc/treebuilder.vala
index ed6dced10..d6e7534a8 100644
--- a/valadoc/treebuilder.vala
+++ b/valadoc/treebuilder.vala
@@ -489,35 +489,18 @@ public class Valadoc.TreeBuilder : Vala.CodeVisitor {
                        context.directory = context.basedir;
                }
 
+               context.set_target_profile (settings.profile, true);
 
-               // add default packages:
-               if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == 
null) {
-                       context.profile = Vala.Profile.GOBJECT;
-                       context.add_define ("GOBJECT");
+               if (settings.target_glib != null) {
+                       context.set_target_glib_version (settings.target_glib);
                }
 
-
                if (settings.defines != null) {
                        foreach (string define in settings.defines) {
                                context.add_define (define);
                        }
                }
 
-               if (context.profile == Vala.Profile.GOBJECT) {
-                       if (settings.target_glib != null) {
-                               context.set_target_glib_version (settings.target_glib);
-                       }
-
-                       // default packages
-                       if (!this.add_package (context, "glib-2.0")) { //
-                               Vala.Report.error (null, "glib-2.0 not found in specified Vala API 
directories");
-                       }
-
-                       if (!this.add_package (context, "gobject-2.0")) { //
-                               Vala.Report.error (null, "gobject-2.0 not found in specified Vala API 
directories");
-                       }
-               }
-
                // add user defined files:
                add_depencies (context, settings.packages);
                if (reporter.errors > 0) {
diff --git a/valadoc/valadoc.vala b/valadoc/valadoc.vala
index 2bb6558f9..2769e9a21 100644
--- a/valadoc/valadoc.vala
+++ b/valadoc/valadoc.vala
@@ -55,7 +55,7 @@ public class ValaDoc : Object {
        private static string[] defines;
        private static bool experimental;
        private static bool experimental_non_null = false;
-       private static string profile;
+       private static Vala.Profile profile;
        [CCode (array_length = false, array_null_terminated = true)]
        private static string[] import_packages;
        [CCode (array_length = false, array_null_terminated = true)]
@@ -79,7 +79,7 @@ public class ValaDoc : Object {
 
                { "basedir", 'b', 0, OptionArg.FILENAME, ref basedir, "Base source directory", "DIRECTORY" },
                { "define", 'D', 0, OptionArg.STRING_ARRAY, ref defines, "Define SYMBOL", "SYMBOL..." },
-               { "profile", 0, 0, OptionArg.STRING, ref profile, "Use the given profile instead of the 
default", "PROFILE" },
+               { "profile", 0, OptionFlags.OPTIONAL_ARG, OptionArg.CALLBACK, (void*) option_parse_profile, 
"Use the given profile instead of the default, options are 'gobject' or 'posix'", "PROFILE" },
 
                { "enable-experimental", 0, 0, OptionArg.NONE, ref experimental, "Enable experimental 
features", null },
                { "enable-experimental-non-null", 0, 0, OptionArg.NONE, ref experimental_non_null, "Enable 
experimental enhancements for non-null types", null },
@@ -123,6 +123,17 @@ public class ValaDoc : Object {
                { null }
        };
 
+       static bool option_parse_profile (string option_name, string? val, void* data) throws OptionError {
+               switch (val) {
+                       case null:
+                       case "gobject-2.0":
+                       case "gobject": profile = Vala.Profile.GOBJECT; break;
+                       case "posix": profile = Vala.Profile.POSIX; break;
+                       default: throw new OptionError.FAILED ("Invalid --profile argument '%s'", val);
+               }
+               return true;
+       }
+
        static bool option_deprecated (string option_name, string? val, void* data) throws OptionError {
                stdout.printf ("Command-line option `%s` is deprecated and will be ignored\n", option_name);
                return true;
diff --git a/vapigen/valavapigen.vala b/vapigen/valavapigen.vala
index ef0b37200..c73e3ab54 100644
--- a/vapigen/valavapigen.vala
+++ b/vapigen/valavapigen.vala
@@ -73,20 +73,13 @@ class Vala.VAPIGen {
        
        private int run () {
                context = new CodeContext ();
-               context.profile = Profile.GOBJECT;
                context.vapi_directories = vapi_directories;
                context.gir_directories = gir_directories;
                context.metadata_directories = metadata_directories;
                context.report.enable_warnings = !disable_warnings;
                context.report.set_verbose_errors (!quiet_mode);
                CodeContext.push (context);
-               context.nostdpkg = nostdpkg;
-
-               if (!nostdpkg) {
-                       /* default package */
-                       context.add_external_package ("glib-2.0");
-                       context.add_external_package ("gobject-2.0");
-               }
+               context.set_target_profile (Profile.GOBJECT, !nostdpkg);
 
                if (context.report.get_errors () > 0) {
                        return quit ();


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