Discussion:
[PATCH] Add configure check for _Static_assert()
Thomas Petazzoni
2013-08-28 15:33:51 UTC
Permalink
Commit 8efede20ef ("Use _Static_assert") introduced the usage of
_Static_assert(). However, _Static_assert() is a fairly new thing,
since it was introduced only in gcc 4.6. In order to support older
compilers, this patch adds a configure.in test that checks whether
_Static_assert() is usable or not, and adjust the behavior of the
assert_cc() macro accordingly.

Signed-off-by: Thomas Petazzoni <thomas.petazzoni-wi1+55ScJUtKEb57/***@public.gmane.org>
---
configure.ac | 6 ++++++
libkmod/macro.h | 4 ++++
2 files changed, 10 insertions(+)

diff --git a/configure.ac b/configure.ac
index 40e54cf..cbe12f3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -52,6 +52,12 @@ AC_CHECK_MEMBERS([struct stat.st_mtim], [], [], [#include <sys/stat.h>])
# Check kernel headers
AC_CHECK_HEADERS_ONCE([linux/module.h])

+AC_MSG_CHECKING([whether _Static_assert() is supported])
+AC_COMPILE_IFELSE(
+ [AC_LANG_SOURCE([[_Static_assert(1, "Test");]])],
+ [AC_DEFINE([HAVE_STATIC_ASSERT], [1], [Define is _Static_assert() is available])
+ AC_MSG_RESULT([yes])],
+ [AC_MSG_RESULT([no])])

#####################################################################
# --with-
diff --git a/libkmod/macro.h b/libkmod/macro.h
index c6ba855..5992026 100644
--- a/libkmod/macro.h
+++ b/libkmod/macro.h
@@ -21,8 +21,12 @@

#include <stddef.h>

+#if defined(HAVE_STATIC_ASSERT)
#define assert_cc(expr) \
_Static_assert((expr), #expr)
+#else
+#define assert_cc(expr)
+#endif

#if HAVE_TYPEOF
#define check_types_match(expr1, expr2) \
--
1.8.1.2
Lucas De Marchi
2013-08-29 03:55:04 UTC
Permalink
Hi Thomas,

On Wed, Aug 28, 2013 at 12:33 PM, Thomas Petazzoni
Post by Thomas Petazzoni
Commit 8efede20ef ("Use _Static_assert") introduced the usage of
_Static_assert(). However, _Static_assert() is a fairly new thing,
since it was introduced only in gcc 4.6. In order to support older
What distro is still in gcc 4.6? Is it a LTS one?
Post by Thomas Petazzoni
compilers, this patch adds a configure.in test that checks whether
_Static_assert() is usable or not, and adjust the behavior of the
assert_cc() macro accordingly.
---
configure.ac | 6 ++++++
libkmod/macro.h | 4 ++++
2 files changed, 10 insertions(+)
diff --git a/configure.ac b/configure.ac
index 40e54cf..cbe12f3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -52,6 +52,12 @@ AC_CHECK_MEMBERS([struct stat.st_mtim], [], [], [#include <sys/stat.h>])
# Check kernel headers
AC_CHECK_HEADERS_ONCE([linux/module.h])
+AC_MSG_CHECKING([whether _Static_assert() is supported])
+AC_COMPILE_IFELSE(
+ [AC_LANG_SOURCE([[_Static_assert(1, "Test");]])],
+ [AC_DEFINE([HAVE_STATIC_ASSERT], [1], [Define is _Static_assert() is available])
+ AC_MSG_RESULT([yes])],
+ [AC_MSG_RESULT([no])])
#####################################################################
# --with-
diff --git a/libkmod/macro.h b/libkmod/macro.h
index c6ba855..5992026 100644
--- a/libkmod/macro.h
+++ b/libkmod/macro.h
@@ -21,8 +21,12 @@
#include <stddef.h>
+#if defined(HAVE_STATIC_ASSERT)
#define assert_cc(expr) \
_Static_assert((expr), #expr)
+#else
+#define assert_cc(expr)
could you then define it similarly to what it was before then in this #else?

I think this would do it (totally untested) with the downside of an
ugly build error.

#define assert_cc(expr) \
do { (void) sizeof(char [1 - 2*!(expr)]); } while(0)


thanks
Lucas De Marchi
Thomas Petazzoni
2013-08-29 07:49:32 UTC
Permalink
Dear Lucas De Marchi,
Post by Lucas De Marchi
Hi Thomas,
On Wed, Aug 28, 2013 at 12:33 PM, Thomas Petazzoni
Post by Thomas Petazzoni
Commit 8efede20ef ("Use _Static_assert") introduced the usage of
_Static_assert(). However, _Static_assert() is a fairly new thing,
since it was introduced only in gcc 4.6. In order to support older
What distro is still in gcc 4.6? Is it a LTS one?
It's not a distro. As you know, I work on the Buildroot project, a tool
that builds rootfs for embedded Linux systems using cross-compilation.
One of the package we have is obviously kmod, but since we support a
wide range of architectures, not all of them are necessarily fully
up-to-date in terms of compiler version. The specific example that
raised the problem is a gcc 4.5.x toolchain for PowerPC provided by
Mentor Graphics Sourcery CodeBench (formerly known as CodeSourcery).
Post by Lucas De Marchi
Post by Thomas Petazzoni
+#if defined(HAVE_STATIC_ASSERT)
#define assert_cc(expr) \
_Static_assert((expr), #expr)
+#else
+#define assert_cc(expr)
could you then define it similarly to what it was before then in this #else?
I think this would do it (totally untested) with the downside of an
ugly build error.
#define assert_cc(expr) \
do { (void) sizeof(char [1 - 2*!(expr)]); } while(0)
Sure, will fix this and resend.

Thanks!

Thomas
--
Thomas Petazzoni, Free Electrons
Kernel, drivers, real-time and embedded Linux
development, consulting, training and support.
http://free-electrons.com
Lucas De Marchi
2013-08-29 20:19:34 UTC
Permalink
On Thu, Aug 29, 2013 at 4:49 AM, Thomas Petazzoni
Post by Thomas Petazzoni
Dear Lucas De Marchi,
Post by Lucas De Marchi
Hi Thomas,
On Wed, Aug 28, 2013 at 12:33 PM, Thomas Petazzoni
Post by Thomas Petazzoni
Commit 8efede20ef ("Use _Static_assert") introduced the usage of
_Static_assert(). However, _Static_assert() is a fairly new thing,
since it was introduced only in gcc 4.6. In order to support older
What distro is still in gcc 4.6? Is it a LTS one?
It's not a distro. As you know, I work on the Buildroot project, a tool
that builds rootfs for embedded Linux systems using cross-compilation.
One of the package we have is obviously kmod, but since we support a
wide range of architectures, not all of them are necessarily fully
up-to-date in terms of compiler version. The specific example that
raised the problem is a gcc 4.5.x toolchain for PowerPC provided by
Mentor Graphics Sourcery CodeBench (formerly known as CodeSourcery).
ok
Post by Thomas Petazzoni
Post by Lucas De Marchi
Post by Thomas Petazzoni
+#if defined(HAVE_STATIC_ASSERT)
#define assert_cc(expr) \
_Static_assert((expr), #expr)
+#else
+#define assert_cc(expr)
could you then define it similarly to what it was before then in this #else?
I think this would do it (totally untested) with the downside of an
ugly build error.
#define assert_cc(expr) \
do { (void) sizeof(char [1 - 2*!(expr)]); } while(0)
Sure, will fix this and resend.
ok

Lucas De Marchi
Lucas De Marchi
2013-09-06 12:58:28 UTC
Permalink
On Thu, Aug 29, 2013 at 4:49 AM, Thomas Petazzoni
Post by Thomas Petazzoni
Dear Lucas De Marchi,
Post by Lucas De Marchi
Hi Thomas,
On Wed, Aug 28, 2013 at 12:33 PM, Thomas Petazzoni
Post by Thomas Petazzoni
Commit 8efede20ef ("Use _Static_assert") introduced the usage of
_Static_assert(). However, _Static_assert() is a fairly new thing,
since it was introduced only in gcc 4.6. In order to support older
What distro is still in gcc 4.6? Is it a LTS one?
It's not a distro. As you know, I work on the Buildroot project, a tool
that builds rootfs for embedded Linux systems using cross-compilation.
One of the package we have is obviously kmod, but since we support a
wide range of architectures, not all of them are necessarily fully
up-to-date in terms of compiler version. The specific example that
raised the problem is a gcc 4.5.x toolchain for PowerPC provided by
Mentor Graphics Sourcery CodeBench (formerly known as CodeSourcery).
Post by Lucas De Marchi
Post by Thomas Petazzoni
+#if defined(HAVE_STATIC_ASSERT)
#define assert_cc(expr) \
_Static_assert((expr), #expr)
+#else
+#define assert_cc(expr)
could you then define it similarly to what it was before then in this #else?
I think this would do it (totally untested) with the downside of an
ugly build error.
#define assert_cc(expr) \
do { (void) sizeof(char [1 - 2*!(expr)]); } while(0)
Sure, will fix this and resend.
Ping.

Lucas De Marchi
Thomas Petazzoni
2013-09-06 13:27:04 UTC
Permalink
Commit 8efede20ef ("Use _Static_assert") introduced the usage of
_Static_assert(). However, _Static_assert() is a fairly new thing,
since it was introduced only in gcc 4.6. In order to support older
compilers, this patch adds a configure.in test that checks whether
_Static_assert() is usable or not, and adjust the behavior of the
assert_cc() macro accordingly.

Signed-off-by: Thomas Petazzoni <thomas.petazzoni-wi1+55ScJUtKEb57/***@public.gmane.org>
---
Changes since v1:
* When _Static_assert() is not available, rely on the previously used
sizeof() trick, as suggested by Lucas de Marchi.
---
configure.ac | 6 ++++++
libkmod/macro.h | 5 +++++
2 files changed, 11 insertions(+)

diff --git a/configure.ac b/configure.ac
index 40e54cf..cbe12f3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -52,6 +52,12 @@ AC_CHECK_MEMBERS([struct stat.st_mtim], [], [], [#include <sys/stat.h>])
# Check kernel headers
AC_CHECK_HEADERS_ONCE([linux/module.h])

+AC_MSG_CHECKING([whether _Static_assert() is supported])
+AC_COMPILE_IFELSE(
+ [AC_LANG_SOURCE([[_Static_assert(1, "Test");]])],
+ [AC_DEFINE([HAVE_STATIC_ASSERT], [1], [Define is _Static_assert() is available])
+ AC_MSG_RESULT([yes])],
+ [AC_MSG_RESULT([no])])

#####################################################################
# --with-
diff --git a/libkmod/macro.h b/libkmod/macro.h
index c6ba855..10392a3 100644
--- a/libkmod/macro.h
+++ b/libkmod/macro.h
@@ -21,8 +21,13 @@

#include <stddef.h>

+#if defined(HAVE_STATIC_ASSERT)
#define assert_cc(expr) \
_Static_assert((expr), #expr)
+#else
+#define assert_cc(expr) \
+ do { (void) sizeof(char [1 - 2*!(expr)]); } while(0)
+#endif

#if HAVE_TYPEOF
#define check_types_match(expr1, expr2) \
--
1.8.1.2
Lucas De Marchi
2013-09-06 14:34:00 UTC
Permalink
Hi Thomas,

On Fri, Sep 6, 2013 at 10:27 AM, Thomas Petazzoni
Post by Thomas Petazzoni
Commit 8efede20ef ("Use _Static_assert") introduced the usage of
_Static_assert(). However, _Static_assert() is a fairly new thing,
since it was introduced only in gcc 4.6. In order to support older
compilers, this patch adds a configure.in test that checks whether
_Static_assert() is usable or not, and adjust the behavior of the
assert_cc() macro accordingly.
---
* When _Static_assert() is not available, rely on the previously used
sizeof() trick, as suggested by Lucas de Marchi.
---
configure.ac | 6 ++++++
libkmod/macro.h | 5 +++++
2 files changed, 11 insertions(+)
diff --git a/configure.ac b/configure.ac
index 40e54cf..cbe12f3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -52,6 +52,12 @@ AC_CHECK_MEMBERS([struct stat.st_mtim], [], [], [#include <sys/stat.h>])
# Check kernel headers
AC_CHECK_HEADERS_ONCE([linux/module.h])
+AC_MSG_CHECKING([whether _Static_assert() is supported])
+AC_COMPILE_IFELSE(
+ [AC_LANG_SOURCE([[_Static_assert(1, "Test");]])],
+ [AC_DEFINE([HAVE_STATIC_ASSERT], [1], [Define is _Static_assert() is available])
I've fixed the typo s/is/if/ and pushed.

Thanks
Lucas De Marchi

Loading...