Compile error on Android Armv7l with Clang

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
SpotlightKid
Posts: 380
Joined: Wed Apr 08, 2015 5:19 am

Compile error on Android Armv7l with Clang

Post by SpotlightKid » Mon Nov 06, 2017 6:40 pm

Out of curiosity, I tried to compile the unix port of MicroPython on my Android phone and tablet.

So installed the Termux app on both, which provides a single-user Linux environment based on a debian-like system, so you can install the necessary build tools with a package manager. The only C compiler available is Clang 5.0.0, though, there doesn't seem to be a GCC package.

Apart from a small issue with the build system, compilation on the tablet, which has an aarch64 architecture, went smoothly and the test suite had only 5 failed tests, which were due to differences in float representation.

On the phone however, which has an armv7l processor, the compilation failed with the following error. Is this expected? Is this architecture supported? Is this an incompatibility with clang?

Code: Select all

CC ../../py/nlrthumb.c
../../py/nlrthumb.c:79:5: error: non-ASM statement in naked function is not supported
    return 0; // needed to silence compiler warning
    ^
../../py/nlrthumb.c:39:16: note: attribute is here
__attribute__((naked)) unsigned int nlr_push(nlr_buf_t *nlr) {
               ^
../../py/nlrthumb.c:95:5: error: non-ASM statement in naked function is not supported
    nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top);
    ^
../../py/nlrthumb.c:94:25: note: attribute is here
NORETURN __attribute__((naked)) void nlr_jump(void *val) {
                        ^
2 errors generated.
make: *** [../../py/mkrules.mk:47: build/py/nlrthumb.o] Error 1
Here are the steps I carried out to compile MicroPython:
  • install F-Droid app
  • install Termux app from within F-Droid

Code: Select all

pkg upgrade
pkg install python-dev git clang make pkg-config libffi-dev

mkdir -p src
cd src
git clone https://github.com/micropython/micropython
cd micropython/
git submodule update --init
sed -i -e 's|^MAKE_FROZEN = $(TOP)|MAKE_FROZEN = $(PYTHON) $(TOP)|' py/mkenv.mk
cd ports/unix/
export GCC=clang
make axtls
make
Or with the included libffi compiled in statically:

Code: Select all

pkg install autoconf automake libtool
cd ports/unix/
sed -i -e 's|^MICROPY_STANDALONE = 0|MICROPY_STANDALONE = 1|' mpconfigport.mk
export GCC=clang
make deplibs
make
Here's some system info about my phone:

Code: Select all

$ uname -a
Linux localhost 3.4.39-8954887 #1 SMP PREEMPT Fri Dec 9 19:32:20 KST 2016 armv7l Android

$ clang --version
clang version 5.0.0 (tags/RELEASE_500/final)

4 cat /proc/cpuinfo
Target: armv7a--linux-android
Thread model: posix
InstalledDir: /data/data/com.termux/files/usr/bin
Processor	: ARMv7 Processor rev 3 (v7l)
processor	: 0
BogoMIPS	: 1590.88

processor	: 1
BogoMIPS	: 1590.88

processor	: 2
BogoMIPS	: 1590.88

processor	: 3
BogoMIPS	: 1590.88

Features	: swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt 
CPU implementer	: 0x41
CPU architecture: 7
CPU variant	: 0x0
CPU part	: 0xc07
CPU revision	: 3

Hardware	: UNIVERSAL3470
Revision	: 0006
Serial		: 4c31135352000d13

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Compile error on Android Armv7l with Clang

Post by jickster » Tue Nov 07, 2017 2:36 am

It appears to be an issue with the compiler.

That function uses assembly statements to save the context ie the registers; it’s part of the C-based exception handling mechanism of micropython.

If that error message can be read literally, the issue is that inside this NAKED function, normal C ie non assembly code is used in addition to assembly … and your compiler doesn’t like that.

Maybe try a different compiler? FYI I’m using free Codesourcery compiler to target a Cortex M4 controller.




Sent from my iPhone using Tapatalk

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Compile error on Android Armv7l with Clang

Post by jickster » Tue Nov 07, 2017 2:42 am

Another issue (not sure about this): if you’re targeting your smartphone, the registers that nlr_push saves might not be enough or even correct.

Your phone uses Cortex A-series which is completely different than the M-series I’m targeting.

You may be able to bypass this assembly mess if you use setjmp and longjmp, the C-based solution for jumping to arbitrary code locations. I think you can control this choice by setting some macro flags somewhere; check in mpconfigport.h or nlrthumb.c


Sent from my iPhone using Tapatalk

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Compile error on Android Armv7l with Clang

Post by jickster » Wed Nov 15, 2017 4:20 pm

SpotlightKid wrote:
Mon Nov 06, 2017 6:40 pm
Did you solve this?

SpotlightKid
Posts: 380
Joined: Wed Apr 08, 2015 5:19 am

Re: Compile error on Android Armv7l with Clang

Post by SpotlightKid » Wed Nov 15, 2017 5:22 pm

No, not yet.

Since my goal is to compile MicroPython in the given environment (Termux) on the device, I can't easily change the compiler.

I suspect the same problem would affect other devices with the same architecture, e.g. the Raspberry Pi, when using Clang. I intend to try that out later.

My post was also rather intended to find out whether this is a known limitation and if I should report this as a bug on Github.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Compile error on Android Armv7l with Clang

Post by jickster » Wed Nov 15, 2017 5:39 pm

SpotlightKid wrote:
Wed Nov 15, 2017 5:22 pm
No, not yet.

Since my goal is to compile MicroPython in the given environment (Termux) on the device, I can't easily change the compiler.

I suspect the same problem would affect other devices with the same architecture, e.g. the Raspberry Pi, when using Clang. I intend to try that out later.

My post was also rather intended to find out whether this is a known limitation and if I should report this as a bug on Github.
Admittedly, I don't know much about compilers but why can't you use gcc?

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Compile error on Android Armv7l with Clang

Post by jickster » Wed Nov 15, 2017 5:52 pm

SpotlightKid wrote:
Wed Nov 15, 2017 5:22 pm
No, not yet.

Since my goal is to compile MicroPython in the given environment (Termux) on the device, I can't easily change the compiler.

I suspect the same problem would affect other devices with the same architecture, e.g. the Raspberry Pi, when using Clang. I intend to try that out later.

My post was also rather intended to find out whether this is a known limitation and if I should report this as a bug on Github.
If you have to use that compiler, there is a workaround.

Go to nlr.h and at the top, add
#define MICROPY_NLR_SETJMP 1
This will configure the build to not use the functions that use assembly for exceptions. It'll probably be slower if there's a lot of exceptions but should work.

SpotlightKid
Posts: 380
Joined: Wed Apr 08, 2015 5:19 am

Re: Compile error on Android Armv7l with Clang

Post by SpotlightKid » Wed Nov 15, 2017 8:10 pm

jickster wrote:
Wed Nov 15, 2017 5:39 pm
Admittedly, I don't know much about compilers but why can't you use gcc?
AFAICS, GCC is not packaged for Termux, only clang. Compiling GCC myself on my phone seems unlikely to succeed. I suppose I could try to cross-compile on my desktop for armv7l, but I'm more interested in finding out, why it doesn't work with Clang on my phone's architecture.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Compile error on Android Armv7l with Clang

Post by jickster » Wed Nov 15, 2017 8:22 pm

SpotlightKid wrote:
Wed Nov 15, 2017 8:10 pm
jickster wrote:
Wed Nov 15, 2017 5:39 pm
Admittedly, I don't know much about compilers but why can't you use gcc?
AFAICS, GCC is not packaged for Termux, only clang. Compiling GCC myself on my phone seems unlikely to succeed. I suppose I could try to cross-compile on my desktop for armv7l, but I'm more interested in finding out, why it doesn't work with Clang on my phone's architecture.
I gave you a fix.
jickster wrote:
Wed Nov 15, 2017 5:52 pm
SpotlightKid wrote:
Wed Nov 15, 2017 5:22 pm
No, not yet.

Since my goal is to compile MicroPython in the given environment (Termux) on the device, I can't easily change the compiler.

I suspect the same problem would affect other devices with the same architecture, e.g. the Raspberry Pi, when using Clang. I intend to try that out later.

My post was also rather intended to find out whether this is a known limitation and if I should report this as a bug on Github.
If you have to use that compiler, there is a workaround.

Go to nlr.h and at the top, add
#define MICROPY_NLR_SETJMP 1
This will configure the build to not use the functions that use assembly for exceptions. It'll probably be slower if there's a lot of exceptions but should work.

SpotlightKid
Posts: 380
Joined: Wed Apr 08, 2015 5:19 am

Re: Compile error on Android Armv7l with Clang

Post by SpotlightKid » Wed Nov 15, 2017 8:29 pm

I gave you a fix.
Yes, I will test that later.

Post Reply