[vala] codegen: use #if GLIB_CHECK_VERSION for init functions



commit 591340a3a06b9f25db7d1a5dba53270e543e55c1
Author: Marc-André Lureau <marcandre lureau gmail com>
Date:   Mon Jan 21 18:06:12 2013 +0100

    codegen: use #if GLIB_CHECK_VERSION for init functions
    
    Tarballs with generated code should compile without warnings, and work
    with various versions of glib (assuming the rest of the code is
    correctly up to date, which is often the case if you don't use newer
    functions)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=692218

 ccode/Makefile.am                  |    1 +
 ccode/valaccodeifsection.vala      |   50 ++++++++++++++++++++++++++++++++++++
 codegen/valaccodemethodmodule.vala |   13 ++++++---
 3 files changed, 59 insertions(+), 5 deletions(-)
---
diff --git a/ccode/Makefile.am b/ccode/Makefile.am
index c63dd06..5be8d79 100644
--- a/ccode/Makefile.am
+++ b/ccode/Makefile.am
@@ -46,6 +46,7 @@ libvalaccode_la_VALASOURCES = \
        valaccodeinvalidexpression.vala \
        valaccodelabel.vala \
        valaccodelinedirective.vala \
+       valaccodeifsection.vala \
        valaccodemacroreplacement.vala \
        valaccodememberaccess.vala \
        valaccodemodifiers.vala \
diff --git a/ccode/valaccodeifsection.vala b/ccode/valaccodeifsection.vala
new file mode 100644
index 0000000..dd51743
--- /dev/null
+++ b/ccode/valaccodeifsection.vala
@@ -0,0 +1,50 @@
+/* valaccodeifsection.vala
+ *
+ * Copyright (C) 2013  Jürg Billeter
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Marc-André Lurau <marcandre lureau redhat com>
+ */
+
+using GLib;
+
+/**
+ * Represents a section that should be processed on condition.
+ */
+public class Vala.CCodeIfSection : CCodeFragment {
+       /**
+        * The expression
+        */
+       public string expression { get; set; }
+
+       public CCodeIfSection (string expr) {
+               expression = expr;
+       }
+
+       public override void write (CCodeWriter writer) {
+               writer.write_string ("#if ");
+               writer.write_string (expression);
+               foreach (CCodeNode node in get_children ()) {
+                       node.write_combined (writer);
+               }
+               writer.write_string ("#endif");
+               writer.write_newline ();
+       }
+
+       public override void write_declaration (CCodeWriter writer) {
+       }
+}
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index e6069f6..8a4333c 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -829,17 +829,20 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                                ccode.add_expression (mem_profiler_init_call);
                        }
 
-                       if (context.thread && !context.require_glib_version (2, 32)) {
+                       if (context.thread) {
                                var thread_init_call = new CCodeFunctionCall (new CCodeIdentifier 
("g_thread_init"));
                                thread_init_call.line = cmain.line;
                                thread_init_call.add_argument (new CCodeConstant ("NULL"));
-                               ccode.add_expression (thread_init_call);
-                       }
 
-                       if (!context.require_glib_version (2, 36)) {
-                               ccode.add_expression (new CCodeFunctionCall (new CCodeIdentifier 
("g_type_init")));
+                               var cond = new CCodeIfSection ("!GLIB_CHECK_VERSION (2,32,0)");
+                               ccode.add_statement (cond);
+                               cond.append (new CCodeExpressionStatement (thread_init_call));
                        }
 
+                       var cond = new CCodeIfSection ("!GLIB_CHECK_VERSION (2,35,0)");
+                       ccode.add_statement (cond);
+                       cond.append (new CCodeExpressionStatement (new CCodeFunctionCall (new CCodeIdentifier 
("g_type_init"))));
+
                        var main_call = new CCodeFunctionCall (new CCodeIdentifier (function.name));
                        if (m.get_parameters ().size == 1) {
                                main_call.add_argument (new CCodeIdentifier ("argv"));


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