[vala/wip/arch-defines] vala: Add define VALA_ARCH_* to allow buildtime architecture check




commit 0f97b92f29d05247151920c6fc14eebe87d0a9bb
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Mon Jan 17 13:29:59 2022 +0100

    vala: Add define VALA_ARCH_* to allow buildtime architecture check

 configure.ac                        |  4 +++
 tests/Makefile.am                   |  1 +
 tests/parser/preprocessor-arch.vala | 55 +++++++++++++++++++++++++++++++++++++
 vala/valacodecontext.vala           |  2 ++
 vapi/config.vapi                    |  1 +
 5 files changed, 63 insertions(+)
---
diff --git a/configure.ac b/configure.ac
index cd036c78e..7887ecd39 100644
--- a/configure.ac
+++ b/configure.ac
@@ -44,6 +44,7 @@ AC_PROG_INSTALL
 AC_PROG_MKDIR_P
 
 AC_CANONICAL_HOST
+AC_CANONICAL_CPU_ARCH
 
 # Detect the target system
 build_linux=no
@@ -56,6 +57,9 @@ case "${host_os}" in
 esac
 AM_CONDITIONAL([HAVE_LINUX], [test "$build_linux" = "yes"])
 
+AC_SUBST(PACKAGE_TARGET_CPU_ARCH)
+AC_DEFINE_UNQUOTED(PACKAGE_TARGET_CPU_ARCH, "$target_cpu_arch", [Define to the target architecture])
+
 LT_PREREQ([2.2.6])
 LT_INIT([disable-static])
 PKG_PROG_PKG_CONFIG([0.21])
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5156172d2..be96cf7ec 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -934,6 +934,7 @@ TESTS = \
        parser/namespaces.vala \
        parser/namespace-missing-bracket.test \
        parser/preprocessor.vala \
+       parser/preprocessor-arch.vala \
        parser/preprocessor-glib.vala \
        parser/preprocessor-vala.vala \
        parser/property-default-redefined.test \
diff --git a/tests/parser/preprocessor-arch.vala b/tests/parser/preprocessor-arch.vala
new file mode 100644
index 000000000..198ed4345
--- /dev/null
+++ b/tests/parser/preprocessor-arch.vala
@@ -0,0 +1,55 @@
+/*
+ x86_64
+ i386
+ ppc64el
+ ppc
+ aarch64
+ arm
+ sparc64
+ sparc
+ mips64
+ mips
+ alpha
+ hppa1
+ hppa2
+ m68k
+ s390x
+*/
+void main () {
+#if VALA_ARCH_X86_64
+       assert (sizeof (void*) == 8);
+       assert (sizeof (int) == 4);
+       assert (sizeof (uint8) == 1);
+       assert (sizeof (uint16) == 2);
+       assert (sizeof (uint32) == 4);
+       assert (sizeof (size_t) == 8);
+       assert (sizeof (ssize_t) == 8);
+       assert (sizeof (uint64) == 8);
+       assert (sizeof (int64) == 8);
+#elif VALA_ARCH_I386
+       assert (sizeof (void*) == 4);
+       assert (sizeof (int) == 4);
+       assert (sizeof (uint8) == 1);
+       assert (sizeof (uint16) == 2);
+       assert (sizeof (uint32) == 4);
+       assert (sizeof (size_t) == 4);
+       assert (sizeof (ssize_t) == 4);
+       assert (sizeof (uint64) == 8);
+       assert (sizeof (int64) == 8);
+#elif VALA_ARCH_PPC64EL
+#elif VALA_ARCH_PPC
+#elif VALA_ARCH_AARCH64
+#elif VALA_ARCH_ARM
+#elif VALA_ARCH_SPARC64
+#elif VALA_ARCH_SPARC
+#elif VALA_ARCH_MIPS64
+#elif VALA_ARCH_MIPS
+#elif VALA_ARCH_ALPHA
+#elif VALA_ARCH_HPPA1
+#elif VALA_ARCH_HPPA2
+#elif VALA_ARCH_M68K
+#elif VALA_ARCH_S390X
+#else
+       message ("Targeting an unkown arch");
+#endif
+}
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index 9fc211f7a..707f69d67 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -582,6 +582,8 @@ public class Vala.CodeContext {
                for (int i = 16; i <= target_glib_minor; i += 2) {
                        defines.add ("GLIB_2_%d".printf (i));
                }
+
+               defines.add ("VALA_ARCH_%s".printf (Config.PACKAGE_TARGET_CPU_ARCH.ascii_up ().replace ("-", 
"_")));
        }
 
        /**
diff --git a/vapi/config.vapi b/vapi/config.vapi
index c8b5d58a1..b67b67160 100644
--- a/vapi/config.vapi
+++ b/vapi/config.vapi
@@ -24,6 +24,7 @@
 namespace Config {
        public const string PACKAGE_DATADIR;
        public const string PACKAGE_SUFFIX;
+       public const string PACKAGE_TARGET_CPU_ARCH;
        public const string PACKAGE_VALADOC_LIBDIR;
        public const string PACKAGE_VALADOC_ICONDIR;
 }


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