Skip to contentSkip to conversation

Fix building with clang

PATCH Superseded Darren Kenny 1 participant 3 messages Oct 14, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix building with clang

The abiltiy to build with clang was broken in the last release after the upgrade of gnulib.

There were two main issues:

- The use of __builtin_trap in the abort() macro.

This builtin doesn't exist for clang builds

After some discussion between Daniel and Vladimir, it was requested that I should revert some past changes in this area, and re-introduce the use of grub_abort().

- The is some use of variable length arrays (vla) in gnulib's code, and when an attempt was made to resolve this in gnulib itself, I was informed that we shouldn't be building gnulib with -Werror.

Rather than totally disabling -Werror, it seemed better to just limit it for the specific case that is causing problems, i.e. vla.

Thanks,

Darren.

Darren Kenny (2):
gnulib: Provide abort() implementation for gnulib
configure: Fix building with clang

 config.h.in                       | 10 ----------
 configure.ac                      |  4 ++++
 grub-core/kern/misc.c             |  2 +-
 grub-core/lib/posix_wrap/stdlib.h |  6 ++++++
 include/grub/misc.h               |  5 +----
 5 files changed, 12 insertions(+), 15 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/2 gnulib: Provide abort() implementation for gnulib

The recent gnulib updates require an implemention of abort(), but the current macro provided by changeset:

cd37d3d3916c gnulib: Drop no-abort.patch

to config.h.in does not work with the clang compiler since it doesn't provide a __builtin_trap implementation, so this element of the changeset needs to be reverted, and replaced.

After some discussion with Vladimir 'phcoder' Serbinenko and Daniel Kiper it was suggested to bring back in the change from the changeset:

db7337a3d353 "* grub-core/gnulib/regcomp.c (regerror): ..."

Which implements abort() as an inline call to grub_abort(), but since that was made static by changeset:

a8f15bceeafe "* grub-core/kern/misc.c (grub_abort): Make static"

it is also necessary to revert the specific part that makes it a static function too.

---
 config.h.in                       | 10 ----------
 grub-core/kern/misc.c             |  2 +-
 grub-core/lib/posix_wrap/stdlib.h |  6 ++++++
 include/grub/misc.h               |  5 +----
 4 files changed, 8 insertions(+), 15 deletions(-)
config.h.in +0 -10
index 01dcbbfc82f0..4d1e50eba79c 100644
@@ -137,16 +137,6 @@ typedef __UINT_FAST32_TYPE__ uint_fast32_t;
     void *								\
     reallocarray (void *ptr, unsigned int nmemb, unsigned int size);
 #    define _GL_INLINE_HEADER_END   _Pragma ("GCC diagnostic pop")
-
-/*
- * We don't have an abort() for gnulib to call in regexp.  Because gnulib is
- * built as a separate object that is then linked with, it doesn't have any
- * of our headers or functions around to use - so we unfortunately can't
- * print anything.  Builds of emu include the system's stdlib, which includes
- * a prototype for abort(), so leave this as a macro that doesn't take
- * arguments.
- */
-#    define abort __builtin_trap
 #  endif /* !_GL_INLINE_HEADER_BEGIN */
 
 /* gnulib doesn't build cleanly with older compilers. */
grub-core/kern/misc.c +1 -1
index 6c0221cc336d..dfae4f9d7897 100644
@@ -1249,7 +1249,7 @@ grub_printf_fmt_check (const char *fmt, const char *fmt_expected)
 
 
 /* Abort GRUB. This function does not return.  */
-static void __attribute__ ((noreturn))
+void __attribute__ ((noreturn))
 grub_abort (void)
 {
   grub_printf ("\nAborted.");
grub-core/lib/posix_wrap/stdlib.h +6 -0
index 148e9d94bde0..f5279756abef 100644
@@ -58,4 +58,10 @@ abs (int c)
   return (c >= 0) ? c : -c;
 }
 
+static inline void __attribute__ ((noreturn))
+abort (void)
+{
+  grub_abort ();
+}
+
 #endif
include/grub/misc.h +1 -4
index 892ac6a42dda..ddac3aae8bcc 100644
@@ -385,6 +385,7 @@ char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...)
      __attribute__ ((format (GNU_PRINTF, 1, 2))) WARN_UNUSED_RESULT;
 char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) WARN_UNUSED_RESULT;
 void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
+void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn));
 grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
 					  grub_uint64_t d,
 					  grub_uint64_t *r);
@@ -454,10 +455,6 @@ void EXPORT_FUNC(grub_reboot) (void) __attribute__ ((noreturn));
 void grub_reboot (void) __attribute__ ((noreturn));
 #endif
 
-#if defined (__clang__) && !defined (GRUB_UTIL)
-void __attribute__ ((noreturn)) EXPORT_FUNC (abort) (void);
-#endif
-
 #ifdef GRUB_MACHINE_PCBIOS
 /* Halt the system, using APM if possible. If NO_APM is true, don't
  * use APM even if it is available.  */
Signature · 1 line
2.31.1
#2 lore raw

PATCH 2/2 configure: Fix building with clang

In reply to Darren Kenny

Building the current code with clang and the latest gnulib fails due to the use of a variable-length-array (vla) warning, which turns in to an error due to the presence of the -Werror during the build.

The gnulib team stated that their code should not be built with -Werror.

At present, the only way to do this is for the complete code-base, by using the --disable-werror option to configure.

Rather than doing this, and failing to gain any benefit that it provides, instead, if building with clang, this patch makes it possible to specifically not error on vlas, while retaining the -Werror functionality otherwise.

---
 configure.ac | 4 ++++
 1 file changed, 4 insertions(+)
configure.ac +4 -0
index 1348b06a985a..93626b7982d4 100644
@@ -1939,6 +1939,10 @@ AC_ARG_ENABLE([werror],
 if test x"$enable_werror" != xno ; then
   TARGET_CFLAGS="$TARGET_CFLAGS -Werror"
   HOST_CFLAGS="$HOST_CFLAGS -Werror"
+  if test "x$grub_cv_cc_target_clang" = xyes; then
+    TARGET_CFLAGS="$TARGET_CFLAGS -Wno-error=vla"
+    HOST_CFLAGS="$HOST_CFLAGS -Wno-error=vla"
+  fi
 fi
 
 TARGET_CPP="$TARGET_CC -E"
Signature · 1 line
2.31.1