]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: use #if GLIB_CHECK_VERSION for init functions
authorMarc-André Lureau <marcandre.lureau@gmail.com>
Mon, 21 Jan 2013 17:06:12 +0000 (18:06 +0100)
committerLuca Bruno <lucabru@src.gnome.org>
Sat, 14 Dec 2013 13:49:20 +0000 (14:49 +0100)
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
ccode/valaccodeifsection.vala [new file with mode: 0644]
codegen/valaccodemethodmodule.vala

index c63dd06e09c687676b46dfb884fa4797ae48910d..5be8d793ffe50e55aa3e53514790157c863087c6 100644 (file)
@@ -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 (file)
index 0000000..dd51743
--- /dev/null
@@ -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) {
+       }
+}
index e6069f61156111ecb9033fa0e093553b7fab3918..8a4333c3d4526d2c7bbdb4f16672d007d30f281e 100644 (file)
@@ -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"));