iotivity/iotivity-lite

Compile error for OC_FALLTHROUGH

Closed this issue · 12 comments

@Danielius1922
When compiling in port/android of the latest master branch, the error below may occur. Does anyone else get this error?
I tried it on Ubuntu 20.04 and 22.04 and it shows the same error.


../GitHub/iotivity-lite/port/android$ make NDK_HOME=~/toolchains/ndk21-android-arm-23 DEBUG=1
"/home/david/toolchains/ndk21-android-arm-23"/bin/arm-linux-androideabi-gcc -c -o arm-linux-androideabi-obj/timestamp_format.o ../../api/c-timestamp/timestamp_format.c -fPIC -fno-asynchronous-unwind-tables -fno-omit-frame-pointer -ffreestanding -Os -fno-stack-protector -ffunction-sections -fdata-sections -fno-strict-overflow -I./ -I../../include/ -I../../deps/tinycbor/src -I../../ -std=gnu99 -Wall -Wextra -Werror -Wno-error=deprecated-declarations -pedantic -DLONG_BIT=64 -D__ANDROID_MIN_SDK_VERSION__=23 -DOC_CLOUD -g -O0 -I../../deps/mbedtls/include -DOC_SECURITY -DOC_DEBUG -DOC_LOG_MAXIMUM_LEVEL=8 -DOC_PKI -DOC_DYNAMIC_ALLOCATION -DOC_INTROSPECTION -DOC_IDD_API -DOC_IPV4 -DOC_TCP
../../api/c-timestamp/timestamp_format.c:138:7: error: declaration does not declare anything [-Werror,-Wmissing-declarations]
      OC_FALLTHROUGH;
      ^
../../util/oc_compiler.h:47:24: note: expanded from macro 'OC_FALLTHROUGH'
#define OC_FALLTHROUGH __attribute__((fallthrough))
                       ^
../../api/c-timestamp/timestamp_format.c:142:7: error: declaration does not declare anything [-Werror,-Wmissing-declarations]
      OC_FALLTHROUGH;
      ^
../../util/oc_compiler.h:47:24: note: expanded from macro 'OC_FALLTHROUGH'
#define OC_FALLTHROUGH __attribute__((fallthrough))
                       ^
../../api/c-timestamp/timestamp_format.c:146:7: error: declaration does not declare anything [-Werror,-Wmissing-declarations]
      OC_FALLTHROUGH;
      ^
../../util/oc_compiler.h:47:24: note: expanded from macro 'OC_FALLTHROUGH'
#define OC_FALLTHROUGH __attribute__((fallthrough))
                       ^

...
...

@Askidea Seems like you have an older gcc that doesn't support the fallthrough attribute yet, can you check the branch from the linked PR? I've tried updating the checks for support of the attribute.

(I've also invited you as a contributor to the repository, so you can review the PR)

@Danielius1922
My gcc version is 11.4.0.

../GitHub/iotivity-lite/port/android$ gcc --version
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0

When compiling from your branch, another error appears.


../GitHub/iotivity-lite/port/android$ make NDK_HOME=~/toolchains/ndk21-android-arm-23 DEBUG=1
"/home/david/toolchains/ndk21-android-arm-23"/bin/arm-linux-androideabi-gcc -c -o arm-linux-androideabi-obj/server/oc_core_res.o ../../api/oc_core_res.c -fPIC -fno-asynchronous-unwind-tables -fno-omit-frame-pointer -ffreestanding -Os -fno-stack-protector -ffunction-sections -fdata-sections -fno-strict-overflow -I./ -I../../include/ -I../../deps/tinycbor/src -I../../ -std=gnu99 -Wall -Wextra -Werror -Wno-error=deprecated-declarations -pedantic -DLONG_BIT=64 -D__ANDROID_MIN_SDK_VERSION__=23 -DOC_CLOUD -g -O0 -I../../deps/mbedtls/include -DOC_SECURITY -DOC_DEBUG -DOC_LOG_MAXIMUM_LEVEL=8 -DOC_PKI -DOC_DYNAMIC_ALLOCATION -DOC_INTROSPECTION -DOC_IDD_API -DOC_IPV4 -DOC_TCP -DOC_SERVER
../../api/oc_core_res.c:84:50: error: suggest braces around initialization of subobject [-Werror,-Wmissing-braces]
static oc_platform_info_t g_oc_platform_info = { 0 };
                                                 ^
                                                 {}
1 error generated.
make: *** [Makefile:319: arm-linux-androideabi-obj/server/oc_core_res.o] Error 1

If I recall correctly, the android build doesn't use the default gcc of your machine. You can see it in the compilation command:

"/home/david/toolchains/ndk21-android-arm-23"/bin/arm-linux-androideabi-gcc

That should be the GCC used. It seems to require for the -Wmissing-braces warning that during initialization subobjects have braces - which is a bit annoying to do, I expect that it will fail in multiple places, to be honest I'm inclined to disable the warning for Android.

@Askidea I've updated some initialization expressions in the PR branch, lets see if it works now.

@Danielius1922 Which NDK version are you using?

It's been a while since I had Android studio installed on my Ubuntu machine, so I don't have any NDK installed right know. I'm doing code updates based on the logs you provide.

We do have an Android test job - https://github.com/iotivity/iotivity-lite/blob/master/.github/workflows/android.yml - which I guess is using NDK v25? Maybe I could extend the workflow to make the NDK version configurable when invoking the workflow manually, that way we could also test other NDKs. Which version are you using, v21 going by the log?

@Danielius1922
Currently I am using v21, but I can install and test any version. If you have installed Android Studio, you can also easily install the NDK and compile the modified code. This will allow you to fix compilation errors right away and I think it will help speed up the development process.
Anyway, I'm trying to build it by applying Ubuntu 22.04, /usr/bin/gcc, and NDK v25 according to your development environment.

Using NDK v25.2

../iotivity-lite/port/android$ make NDK_HOME=~/toolchains/ndk25-android-arm-23 DEBUG=1

/usr/bin/gcc -c -o arm-linux-androideabi-obj/server/ifaddrs-android.o ifaddrs-android.c -fPIC -fno-asynchronous-unwind-tables -fno-omit-frame-pointer -ffreestanding -Os -fno-stack-protector -ffunction-sections -fdata-sections -fno-strict-overflow -I./ -I../../include/ -I../../deps/tinycbor/src -I../../ -std=gnu99 -Wall -Wextra -Werror -Wno-error=deprecated-declarations -pedantic -DLONG_BIT=64 -D__ANDROID_MIN_SDK_VERSION__=23 -DOC_CLOUD -g -O0 -I../../deps/mbedtls/include -DOC_SECURITY -DOC_DEBUG -DOC_LOG_MAXIMUM_LEVEL=8 -DOC_PKI -DOC_DYNAMIC_ALLOCATION -DOC_INTROSPECTION -DOC_IDD_API -DOC_IPV4 -DOC_TCP -DOC_SERVER
ifaddrs-android.c: In function ‘make_prefixes’:
ifaddrs-android.c:139:17: error: overflow in conversion from ‘int’ to ‘char’ changes value from ‘255’ to ‘-1’ [-Werror=overflow]
  139 |     *prefix++ = 0xFF;
      |                 ^~~~
ifaddrs-android.c:141:20: error: overflow in conversion from ‘int’ to ‘char’ changes value from ‘255’ to ‘-1’ [-Werror=overflow]
  141 |   char remainder = 0xff;
      |                    ^~~~
cc1: all warnings being treated as errors
make: *** [Makefile:320: arm-linux-androideabi-obj/server/ifaddrs-android.o] Error 1

This seems to be a x86 vs. ARM thing, because I recall my compilation being exactly the same as the one on the GitHub job, whilst yours is different (you get arm-linux-* compiler whilst the GH job has aarch64-linux-*). So I'm 99% sure that I won't be able to replicate your compilation anyway.

Lets try this, can you locally remove the -Werror flag from the Makefile in port/android and paste here all the compilation warnings you get? This way your compilation should run in full, print all warnings and not abort on the first warning.

@Danielius1922
The build process stopped as shown below without -Werror.

../iotivity-lite/port/android$ make NDK_HOME=~/toolchains/ndk25-android-arm-23 DEBUG=1
:
:
/usr/bin/gcc -c -o arm-linux-androideabi-obj/server/clock.o clock.c -fPIC -fno-asynchronous-unwind-tables -fno-omit-frame-pointer -ffreestanding -Os -fno-stack-protector -ffunction-sections -fdata-sections -fno-strict-overflow -I./ -I../../include/ -I../../deps/tinycbor/src -I../../ -std=gnu99 -Wall -Wextra -Wno-error=deprecated-declarations -pedantic -DLONG_BIT=64 -D__ANDROID_MIN_SDK_VERSION__=23 -DOC_CLOUD -g -O0 -I../../deps/mbedtls/include -DOC_SECURITY -DOC_DEBUG -DOC_LOG_MAXIMUM_LEVEL=8 -DOC_PKI -DOC_DYNAMIC_ALLOCATION -DOC_INTROSPECTION -DOC_IDD_API -DOC_IPV4 -DOC_TCP -DOC_SERVER
/usr/bin/gcc -c -o arm-linux-androideabi-obj/server/ifaddrs-android.o ifaddrs-android.c -fPIC -fno-asynchronous-unwind-tables -fno-omit-frame-pointer -ffreestanding -Os -fno-stack-protector -ffunction-sections -fdata-sections -fno-strict-overflow -I./ -I../../include/ -I../../deps/tinycbor/src -I../../ -std=gnu99 -Wall -Wextra -Wno-error=deprecated-declarations -pedantic -DLONG_BIT=64 -D__ANDROID_MIN_SDK_VERSION__=23 -DOC_CLOUD -g -O0 -I../../deps/mbedtls/include -DOC_SECURITY -DOC_DEBUG -DOC_LOG_MAXIMUM_LEVEL=8 -DOC_PKI -DOC_DYNAMIC_ALLOCATION -DOC_INTROSPECTION -DOC_IDD_API -DOC_IPV4 -DOC_TCP -DOC_SERVER
ifaddrs-android.c: In function ‘make_prefixes’:
ifaddrs-android.c:139:17: warning: overflow in conversion from ‘int’ to ‘char’ changes value from ‘255’ to ‘-1’ [-Woverflow]
  139 |     *prefix++ = 0xFF;
      |                 ^~~~
ifaddrs-android.c:141:20: warning: overflow in conversion from ‘int’ to ‘char’ changes value from ‘255’ to ‘-1’ [-Woverflow]
  141 |   char remainder = 0xff;
      |                    ^~~~
/usr/bin/gcc -c -o arm-linux-androideabi-obj/server/ipadapter.o ipadapter.c -fPIC -fno-asynchronous-unwind-tables -fno-omit-frame-pointer -ffreestanding -Os -fno-stack-protector -ffunction-sections -fdata-sections -fno-strict-overflow -I./ -I../../include/ -I../../deps/tinycbor/src -I../../ -std=gnu99 -Wall -Wextra -Wno-error=deprecated-declarations -pedantic -DLONG_BIT=64 -D__ANDROID_MIN_SDK_VERSION__=23 -DOC_CLOUD -g -O0 -I../../deps/mbedtls/include -DOC_SECURITY -DOC_DEBUG -DOC_LOG_MAXIMUM_LEVEL=8 -DOC_PKI -DOC_DYNAMIC_ALLOCATION -DOC_INTROSPECTION -DOC_IDD_API -DOC_IPV4 -DOC_TCP -DOC_SERVER
ipadapter.c:21:10: fatal error: android/api-level.h: No such file or directory
   21 | #include <android/api-level.h>
      |          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:321: arm-linux-androideabi-obj/server/ipadapter.o] Error 1

@Danielius1922
I created the aarch64 toolchain as you said above. And building the iotivity library was successful. At this time, I applied the gcc included in the toolchain. However, many errors occurred when /usr/bin/gcc was used.

user@askidea:~/GitHub/iotivity-lite/port/android$ make NDK_HOME=~/toolchains/ndk25-android-arm64-23 ANDROID_ABI=arm64-v8a DEBUG=1
Build OK!!

Yeah, the toolchain compilers have to be used, which then gets picked depends on the ANDROID_API and ANDROID_ABI parameters. Seems like aarch64 and arm64-v8a work ABIs work.