SanderMertens / bake

Bake, A build system for building, testing and running C & C++ projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compiling bake for Android, is it possible?

hunar1997 opened this issue · comments

commented

Hello,
There is an app on Android called Termux, and it has a large number of linux packages compiled in its repository, among the build tools it has make,cmake,ninja,(i don't know other ones) .. so since this project was small i thought there are good chances that i don't get a lot of dependencies, i cloned it and tried compiling and it failed because it couldn't find #include <execinfo.h> inside unil.h which is provided by perl package

Are there any workarounds? even if that was solved, is there a larger reason that it couldn't work on Android?

The execinfo header is actually not very important. It is just used to produce backtraces for the logging API. I added a few statements to the code that disable backtraces when the library is built on Android. Let me know if you can build it with this fix!

commented

Ok cool :D
now it says this:
screenshot_20190106-080816
I also tried on build-Darwin, since in Trmux gcc is actually linked to clang i thought it will make a difference, it just removed the warning:
screenshot_20190106-080921
I couldn't figure out what that error means, there is literally zero results for UT_CPU_STRING in google :/

Ah, the reason this is happening, is that the bake utility layer does not recognize the Android platform. Macro's that start with UT_ and functions that start with ut_ are part of bake's utility layer. This is the offending code:
https://github.com/SanderMertens/bake/blob/master/util/include/os.h#L49

Do you happen to know the kind of CPU you are running on?

commented

I'm not sure, do these information help?
output of cat lscpu

Architecture:         aarch64
Byte Order:           Little Endian
CPU(s):               8
On-line CPU(s) list:  2-7
Off-line CPU(s) list: 0,1
Thread(s) per core:   1
Core(s) per socket:   3
Socket(s):            2
Vendor ID:            ARM
Model:                4
Model name:           Cortex-A53
Stepping:             r0p4
CPU max MHz:          1401.0000
CPU min MHz:          768.0000
BogoMIPS:             38.40
Flags:                fp asimd evtstrm aes pmull sha1 sha2 crc32

and info by CPU X app
screenshot_20190106-163724
screenshot_20190106-163732

Yes, that's very helpful! Looks like you're running on 64bit ARM, and the bake code wasn't detecting this. I checked in a fix for that, and also for the warning.

commented

Cool :D now 2/3 of the files compile, it fails at log.c, here is the error :)
screenshot_20190107-002238

I wasn't aware that some platforms didn't treat va_list as a scalar. I checked in a (bit hacky but portable) change that should hopefully fix it.

commented

That did fix it :) but now it generates a warning and another error on os.c :(
screenshot_20190107-094845

commented

I fixed it by changing __arm__ to __aarch64__ on line 68
then adding || to the end of line 72

and compiled, now its this error

strbuf.c
../util/src/strbuf.c:261:29: error: passing 'void *' to parameter
      of incompatible type 'va_list' (aka '__builtin_va_list')
        b, str, len, false, NULL
                            ^~~~
/data/data/com.termux/files/usr/lib/clang/7.0.1/include/stddef.h:105:16: note:
      expanded from macro 'NULL'
#  define NULL ((void*)0)
               ^~~~~~~~~~
../util/src/strbuf.c:122:13: note: passing argument to parameter         'args' here
    va_list args)
            ^
../util/src/strbuf.c:290:28: error: passing 'void *' to parameter
      of incompatible type 'va_list' (aka '__builtin_va_list')
        b, str, -1, false, NULL
                           ^~~~
/data/data/com.termux/files/usr/lib/clang/7.0.1/include/stddef.h:105:16: note:
      expanded from macro 'NULL'
#  define NULL ((void*)0)
               ^~~~~~~~~~
../util/src/strbuf.c:122:13: note: passing argument to parameter
      'args' here
    va_list args)
            ^                                                      2 errors generated.

i'm on my phone i don't know an easy way to pull request or add the error as a screenshot

Looks like more of the same va_list problems. I'll scan through the code to find all the occurrences, and check in fixes. Thanks for your testing & patience!

I pushed a new commit with the va_list fix. I tried to find other occurrences of the same problem but didn't any. Hopefully that was all of them.

commented

Something weird happened :(

I git pulled the changes (but the previously compiled files until that error was still there thus it used those .o objects) and the build succeeded, then i tried ./bake setup and found a new problem, in android /tmp requires root permissions to use, the solution is to use $PREFIX/tmp instead of /tmp ..that would be compatible with PC as well since $PREFIX returns nothing it becomes /tmp, but in android (Or maybe just Termux), $PREFIX/tmp becomes /data/data/com.termux/files/usr/tmp which is the correct /tmp

The weird part is here, i removed all the files and git cloneed all the new changes (thus removing the .o files) and did make again, now your latest commit was compiled and it caused an issue that wasn't there before, it causes the linker to fail
screenshot_20190108-164102
so something you did in 65c0087 to the files prior to strbuf.c made that linker error to appear, you also forgot to redo the fix that i did #16 (comment) and didn't commit

So in short these are the steps
1- file util/src/os.c line 68, change defined(__arm__) && defined(UT_CPU_64BIT) to defined(__aarch64__)
2- same file, line 72 add || to the end
3- change all the references of /tmp to $PREFIX/tmp or use a better solution
4- find out what change made that linker error shown in the screenshot
5- fix the Warning shown in the screenshot

Sorry i don't have enough free time other than for testing, my free days ended :( i'm a 4th year physics student, we are on opposite timezones so this communication looks like it's going to take a while, do you have an Android device around? if you have then do these steps
1- download Termux (it's free and doesn't require rooting your device)
2- type these commands apt update apt install git mkdir BAKE (since your bake setup creates the bake folder in the same location) cd BAKE git clone https://github.com/SanderMertens/bake cd bake/build-Linux make ../bake setup

It actually looks like we're close. The link step is the last step that happens in the build process, which means that all files were successfully compiled.

The error is caused by the compiler not being able to find the log10 symbol in any of the provided libraries. On Linux, bake links with "rt", "dl", "pthread", "m", and log10 is provided by m (libm.so). I'll look into what the equivalent library is on Android. Looks like we may need a specific makefile for Android.

I'll also apply the suggested changes. Thanks for all your help so far!

commented

That can't be it .. the m library does exist on android, unless the make somehow didn't provide -lm, look:

$ cat a.c      
#include <stdio.h>    
#include <math.h>      
       
int main () {        
    printf ("%f\n", log10(100) );    
    return 0;     
}     
$ gcc a.c     
/data/data/com.termux/files/usr/bin     /aarch64-linux-android-ld: /data/data/com.termux/files/usr/tmp/a-6b147e.o: in function `main':
a.c:(.text+0x2c): undefined reference to `log10'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
$ gcc a.c -lm
$ ./a.out
2.000000
commented

WAIT, i just tried make again and no linker error happened .. i don't know what was different before :/
now make in build-Linux is fine but in build-Darwin generates the linker error

Whatever it's somehow fixed now

I've made the remaining changes:

  • Fix the warning in file.c
  • Update the macro in os.c (__aarch64__)
  • Changed /tmp/bake to ~/bake/bake.sh
commented

You forgot one thing :D [util/src/os.c line 72 add || to the end]
while i did that and (compile/link) succeeds, now another problem :( it asks for sudo, is it a must? we are working in the home directory, we can use ~/../usr/bin it's owned by the user
screenshot_20190112-095815

Sorry for the late reply- have been working on some other features!

The setup doesn't require sudo, it continues the installation, but won't install the bake script to /usr/local. It'll throw a warning though, as you can see in the attached trace.

I'm a bit confused as to why it is not able to build the util library. The bake executable should've been built with the same compiler that the make script uses for the util library. I can't tell from the compiler log what's causing this.

Which compiler are you using? Is it clang?

commented

No problem :D
Yes it's clang

clang version 7.0.1 (tags/RELEASE_701/final)  
Target: aarch64--linux-android   
Thread model: posix 
InstalledDir: /data/data/com.termux/files/usr/bin
commented

I'm veeeery late, i totally forgot about this post .. everything works now :D, here are the steps if anyone wants it:

  • Install termux app on android, open it and do what it says on screen to update packages
  • Install git pkg install git
  • Type git clone https://github.com/SanderMertens/bake
  • Compile the program make -C bake/build-$(uname)
  • Change the downloaded folder name to something other than bake, for example mv bake baker
  • Install locally (If you haven't rooted your phone) baker/bake setup --local
  • When it finishes, it asks you to export the variables, copy the stated line and add it to the .bashrc file
  • Open a new bash shell or exit and reopen termux for .bashrc to be reloaded
  • Now everything works, try bake new my_app bake run my_app
  • You can also delete rm -r baker since you don't need it for use

Thank you very much for this project :D i hope it gets more attention