RIOT-OS / RIOT

RIOT - The friendly OS for IoT
https://riot-os.org
GNU Lesser General Public License v2.1
4.88k stars 1.98k forks source link

Build on latest FreeBSD: proposed patch #19666

Open BSDer opened 1 year ago

BSDer commented 1 year ago

Description

Cannot build on latest FreeBSD (CURRENT).

This patch fixes the build:

diff --git a/cpu/native/periph/timer.c b/cpu/native/periph/timer.c
index 229ae6efaf..a74fb63fb7 100644
--- a/cpu/native/periph/timer.c
+++ b/cpu/native/periph/timer.c
@@ -111,7 +111,11 @@ static void do_timer_set(unsigned int offset, bool periodic)
         itv.it_interval = itv.it_value;
     }

+    #if defined(__FreeBSD__)
+    DEBUG("timer_set(): setting %u.%06lu\n", itv.it_value.tv_sec, itv.it_value.tv_usec);
+    #else
     DEBUG("timer_set(): setting %lu.%06lu\n", itv.it_value.tv_sec, itv.it_value.tv_usec);
+    #endif
 }

 int timer_set(tim_t dev, int channel, unsigned int offset)
@@ -189,7 +193,11 @@ void timer_stop(tim_t dev)
     }
     _native_syscall_leave();

+#if defined(__FreeBSD__)
+    DEBUG("time left: %u.%06lu\n", itv.it_value.tv_sec, itv.it_value.tv_usec);
+#else
     DEBUG("time left: %lu.%06lu\n", itv.it_value.tv_sec, itv.it_value.tv_usec);
+#endif
 }

 unsigned int timer_read(tim_t dev)
diff --git a/cpu/native/syscalls.c b/cpu/native/syscalls.c
index 6b4dcaaf8f..424ada8375 100644
--- a/cpu/native/syscalls.c
+++ b/cpu/native/syscalls.c
@@ -290,6 +290,10 @@ ssize_t _native_writev(int fd, const struct iovec *iov, int iovcnt)

 #if defined(__FreeBSD__)
 #undef putchar
+#undef putc
+#undef puts
+#undef getchar
+#undef getc
 #endif
 int putchar(int c)
 {
diff --git a/sys/ztimer/util.c b/sys/ztimer/util.c
index 277f4854be..94b4da1b31 100644
--- a/sys/ztimer/util.c
+++ b/sys/ztimer/util.c
@@ -119,7 +119,11 @@ int ztimer_msg_receive_timeout(ztimer_clock_t *clock, msg_t *msg,
     ztimer_remove(clock, &t);
     if (msg->type == MSG_ZTIMER && msg->content.ptr == &m) {
         /* we hit the timeout */
+#if defined(__FreeBSD__)
+        return -ETIMEDOUT;
+#else
         return -ETIME;
+#endif
     }
     else {
         return 1;

Shall I create a pull request?

maribu commented 1 year ago

Yes, please do!

Please note that we do not have any maintainer working with BSD nor any BSD CI coverage. Experience shows that anything not covered by the CI is bound to eventually break. So it is likely that this can be a bit of a whack a mole game - but typically only minor patches are needed. Still, it is annoying when an update breaks things.

I wonder how much effort it would be to add BSD compilation to our Linux+Docker based CI. If that would be in place, a lot of issues would get caught an fixed prior to PRs being merged.

BSDer commented 1 year ago

Ok, will do the pull request but before I would like to make sure RIOT is actually usable on FreeBSD by, e.g. compiling examples.

I just hit an issue that I am unsure how to handle: it seems that RIOT, as a project, relays on -std=c11 (as found in makefiles/cflags.inc.mk), but newlib seems to be based on -std=c99.

I get the following error when building for BOARD = rpi-pico:

...
"gmake" -C RIOT/core
"gmake" -C RIOT/core/lib
In file included from RIOT/core/lib/atomic_c11.c:40:
/usr/local/arm-none-eabi/include/stdatomic.h: In function 'atomic_flag_test_and_set_explicit':
/usr/local/arm-none-eabi/include/stdatomic.h:386:17: error: request for member '__val' in something not a structure or union
  386 |         return (atomic_exchange_explicit(&__object->__flag, 1, __order));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/arm-none-eabi/include/stdatomic.h: In function 'atomic_flag_clear_explicit':
/usr/local/arm-none-eabi/include/stdatomic.h:393:9: error: request for member '__val' in something not a structure or union
  393 |         atomic_store_explicit(&__object->__flag, 0, __order);
      |         ^~~~~~~~~~~~~~~~~~~~~
gmake[2]: *** [RIOT/Makefile.base:146: RIOT/examples/default/bin/rpi-pico/core_lib/atomic_c11.o] Error 1
gmake[1]: *** [RIOT/Makefile.base:31: ALL--RIOT/core/lib] Error 2
gmake: *** [RIOT/Makefile.include:759: application_default.module] Error 2

If I force CFLAGS += -std=c99 in makefiles/libc/newlib.mk, this error would go away but compilation fails earlier with:

...
"gmake" -C RIOT/core
In file included from RIOT/core/sched.c:26:
RIOT/core/lib/include/assert.h:136:5: error: expected identifier or '(' before '{' token
  136 |     { enum { static_assert_failed_on_div_by_0 = 1 / (!!(cond)) }; }
      |     ^
RIOT/core/sched.c:63:1: note: in expansion of macro 'static_assert'
   63 | static_assert(SCHED_PRIO_LEVELS <= 32, "SCHED_PRIO_LEVELS may at most be 32");
      | ^~~~~~~~~~~~~
gmake[2]: *** [RIOT/Makefile.base:146: RIOT/examples/default/bin/rpi-pico/core/sched.o] Error 1
gmake[1]: *** [RIOT/Makefile.base:31: ALL--RIOT/core] Error 2
gmake: *** [RIOT/Makefile.include:759: application_default.module] Error 2

Can you suggest how to handle properly -std? Thank you.