cross compile Alexa to MIPS MT7621

  1. download mips compile toolchain
    gdrive download 1EYjxWtwv4VH7Ak0jzW47Mv3smbwv6Yu-
  2. unzip and copy to /opt/buildroot-gcc463/, so that we have /opt/buildroot-gcc463/usr/bin/mipsel-buildroot-linux-uclibc-cc
  3. follow https://github.com/alexa/avs-device-sdk/wiki/Raspberry-Pi-Quick-Start-Guide
  4. mkdir sdk-folder && cd sdk-folder && mkdir sdk-build sdk-source third-party application-necessities && cd application-necessities && mkdir sound-files
  5. cd sdk-folder/third-party/
  6. wget http://www.portaudio.com/archives/pa_stable_v190600_20161030.tgz
  7. ./configure --build=i686-linux --host=mipsel-linux --without-jack --with-alsa CC=$TOOLCHAIN/bin/mipsel-linux-cc CXX=$TOOLCHAIN/bin/mipsel-linux-gcc
  8. make -B, dont know why make not work
  9. cd sdk-folder/sdk-source
  10. git clone git://github.com/alexa/avs-device-sdk.git
  11. cd sdk-folder/thrid-party
  12. git clone git://github.com/Sensory/alexa-rpi.git
  13. cd /home/pi/sdk-folder/sdk-build
  14. cmake ~/disk2/MT7621/sdk-folder/sdk-source/avs-device-sdk \
    -DCMAKE_TOOLCHAIN_FILE=~/disk2/MT7621/toolChain.cmake \
    -DSENSORY_KEY_WORD_DETECTOR=ON \
    -DSENSORY_KEY_WORD_DETECTOR_LIB_PATH=~/disk2/MT7621/sdk-folder/third-party/alexa-rpi/lib/libsnsr.a \
    -DSENSORY_KEY_WORD_DETECTOR_INCLUDE_DIR=~/disk2/MT7621/sdk-folder/third-party/alexa-rpi/include \
    -DGSTREAMER_MEDIA_PLAYER=ON -DPORTAUDIO=ON \
    -DPORTAUDIO_LIB_PATH=~/disk2/MT7621/sdk-folder/third-party/portaudio/lib/.libs/libportaudio.a \
    -DPORTAUDIO_INCLUDE_DIR=~/disk2/MT7621/sdk-folder/third-party/portaudio/include
  15. then you will fail and see below
    -- Checking for modules 'gstreamer-1.0>=1.8;gstreamer-app-1.0>=1.8'
    --
    --
    CMake Error at /usr/share/cmake-3.5/Modules/FindPkgConfig.cmake:367 (message):
    A required package was not found
  16. check what lib we have in system by below command, and found that we don’t have gstreamer
    $ /opt/buildroot-gcc463/usr/bin/pkg-config --list-all
    libdaemon libdaemon - a lightweight C library that eases the writing of UNIX daemons
  17. follow this to build gstreamer sdk for mips https://pragmaticjoe.blogspot.hk/2015/07/building-gstreamer-10-sdk-for-android.html?showComment=1510132896564#c6471417005627656215
    1. for ppl reference, download android--ndk-r8e here http://dl.google.com/android/ndk/android-ndk-r8e-linux-x86.tar.bz2

    2. fetch libvpx fail
      -----> Fetching tarball http://webm.googlecode.com/files/libvpx-v1.3.0.tar.bz2 to /home/owenwen/cerbero/sources/local/libvpx-v1.3.0/libvpx-v1.3.0.tar.bz2
      Downloading http://webm.googlecode.com/files/libvpx-v1.3.0.tar.bz2
      Running command 'wget http://webm.googlecode.com/files/libvpx-v1.3.0.tar.bz2 -O /home/owenwen/cerbero/sources/local/libvpx-v1.3.0/libvpx-v1.3.0.tar.bz2 --no-check-certificate'
      --2017-11-08 18:16:42-- http://webm.googlecode.com/files/libvpx-v1.3.0.tar.bz2
      Resolving webm.googlecode.com (webm.googlecode.com)... 64.233.189.82, 2404:6800:4008:c03::52
      Connecting to webm.googlecode.com (webm.googlecode.com)|64.233.189.82|:80... connected.
      HTTP request sent, awaiting response... 404 Not Found
      2017-11-08 18:16:42 ERROR 404: Not Found.***** Error running 'package' command:
      Recipe 'libvpx' failed at the build step 'fetch'vim recipes/libvpx.recipe and replace below line
      url = 'http://webm.googlecode.com/files/libvpx-v1.3.0.tar.bz2'
      to
      url = 'https://download.videolan.org/pub/contrib/vpx/libvpx-v1.3.0.tar.bz2'
  18. after build pass, you will something like below
    [(68/68) gst-libav-1.0-static -> post_install ]
    WARNING: No specific packager available for the distro version android_gingerbread, using generic packager for distro android
    -----> Creating package for gstreamer-1.0-sdk
    -----> Package successfully created in /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/gstreamer-1.0-sdk-android-mips-2013.6-runtime.tar.bz2 /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/gstreamer-1.0-sdk-android-mips-2013.6-runtime.zip /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/gstreamer-1.0-sdk-android-mips-2013.6.tar.bz2 /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/gstreamer-1.0-sdk-android-mips-2013.6.zip
  19. copy libgstreamer to toolchain sysroot
    cp /home/owenwen/cerbero/dist/android_mips/lib/libgstreamer* /opt/buildroot-gcc463/usr/mipsel-buildroot-linux-uclibc/sysroot/usr/lib
  20. copy libgstapp(gstreamer-app) to toolchain sysrootcp /home/owenwen/cerbero/dist/android_mips/lib/libgstapp-1.0.* /opt/buildroot-gcc463/usr/mipsel-buildroot-linux-uclibc/sysroot/usr/lib/
  21. copy libgstreamer pkgconfig file to toolchain sysroot
    cp /home/owenwen/cerbero/dist/android_mips/lib/pkgconfig/gstreamer-1.0.pc /opt/buildroot-gcc463/usr/mipsel-buildroot-linux-uclibc/sysroot/usr/lib/pkgconfig/
  22. copy libgstapp pkgconfig file to toolchain sysroot
    cp /home/owenwen/cerbero/dist/android_mips/lib/pkgconfig/gstreamer-app-1.0.pc /opt/buildroot-gcc463/usr/mipsel-buildroot-linux-uclibc/sysroot/usr/lib/pkgconfig/
  23. check if pkg-conf can list gstreamer
    $ /opt/buildroot-gcc463/usr/bin/pkg-config --list-all
    libdaemon libdaemon - a lightweight C library that eases the writing of UNIX daemons
    gstreamer-1.0 GStreamer - Streaming media framework
  24. then I found that, alexa need gstreamer-1.0>=1.8;gstreamer-app-1.0>=1.8, but the this page https://pragmaticjoe.blogspot.hk/2015/07/building-gstreamer-10-sdk-for-android.html?showComment=1510132896564#c6471417005627656215 shows for 1.0 not 1.8, so I look into the mips patch and git clone git://anongit.freedesktop.org/gstreamer/cerbero
  25. shows below error while build new version of gstreamer
    /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/android-ndk-r13b/toolchains/mipsel-linux-android-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/mipsel-linux-android/4.9.x/../../../../mipsel-linux-android/bin/ld: warning: libm.so, needed by /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/build/dist/android_mips/lib/libfreetype.so, not found (try using -rpath or -rpath-link)
    /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/build/dist/android_mips/lib/libpng16.so: undefined reference to `frexp@LIBC'
    /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/build/dist/android_mips/lib/libpng16.so: undefined reference to `pow@LIBC'
    /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/build/dist/android_mips/lib/libpng16.so: undefined reference to `modf@LIBC'
    /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/build/dist/android_mips/lib/libpng16.so: undefined reference to `floor@LIBC'
    collect2: error: ld returned 1 exit status
    Makefile:768: recipe for target 'test-blob' failed
    make[3]: *** [test-blob] Error 1
    make[3]: Leaving directory '/home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/build/sources/android_mips/harfbuzz-1.2.7/test/api'
    Makefile:398: recipe for target 'all-recursive' failed
    make[2]: *** [all-recursive] Error 1
    make[2]: Leaving directory '/home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/build/sources/android_mips/harfbuzz-1.2.7/test'
    Makefile:489: recipe for target 'all-recursive' failed
    make[1]: *** [all-recursive] Error 1
    make[1]: Leaving directory '/home/owenwen/disk2/MT7621/sdk-folder/sdk-source/cerbero/build/sources/android_mips/harfbuzz-1.2.7'
    Makefile:420: recipe for target 'all' failed
    make: *** [all] Error 2
  26. 先放棄gstreamer 了
  27. gstreamer 先不設定之後 整個早上都在看這個錯誤訊息 Could NOT find CURL (missing: CURL_LIBRARY) (found suitable version “7.52.1″, minimum required is “7.52.1″) 版本號碼都對了還抱怨not found 把curl header file, so file 複製到整台電腦漫出來 最後運氣好想到弄個 link, ln -fs libcurl.so.4.4.0 libcurl.so
    cmake ~/disk2/MT7621/sdk-folder/sdk-source/avs-device-sdk pass了
    現在可以make了,究竟又會死在哪裡呢 ㄏㄏ
  28. curl download from https://packages.debian.org/stretch/curl http://security.debian.org/debian-security/pool/updates/main/c/curl/libcurl3_7.52.1-5+deb9u2_mipsel.deb
  29. 順便紀錄一下cmake pass log https://pastebin.com/EGjp5evJ
  30. 目前一步步跟著這個 https://github.com/alexa/avs-device-sdk/wiki/Raspberry-Pi-Quick-Start-Guide 走完了cmake ,然後遇到了這個錯誤
    /home/owenwen/disk2/MT7621/sdk-folder/sdk-source/avs-device-sdk/AVSCommon/AVS/src/AbstractConnection.cpp:72:6: error: prototype for 'void alexaClientSDK::avsCommon::avs::AbstractConnection::updateConnectionStatus(alexaClientSDK::avsCommon::sdkInterfaces::ConnectionStatusObserverInterface::Status, alexaClientSDK::avsCommon::sdkInterfaces::ConnectionStatusObserverInterface::ChangedReason)' does not match any in class 'alexaClientSDK::avsCommon::avs::AbstractConnection'
    然後在另一個頁面 https://github.com/alexa/avs-device-sdk/wiki/Linux-Quick-Start-Guide 看到這個需求 GNU Compiler Collection (GCC) 4.8.5 or later
    https://gcc.gnu.org/gcc-4.8/buildstat.html GCC官網沒有 mips 4.8.5 的 build status
    https://packages.ubuntu.com/xenial/devel/ Ubuntu 官網有 mips 5.3.1 ,版本差很多,感覺坑很大,先不跳進去
    https://downloads.openwrt.org/chaos_calmer/15.05/ramips/mt7621/  openwrt有支援這CPU, gcc version 只到mipsel-openwrt-linux-gcc-4.8.3
  31. 跳入Ubuntu 的坑了
    sudo apt-get install g++-5-mipsel-linux-gnu
    sudo apt-get install cpp-5-mipsel-linux-gnu
    vim toolChain.cmake
    SET(CMAKE_C_COMPILER /usr/bin/mipsel-linux-gnu-gcc-5)
    SET(CMAKE_CXX_COMPILER /usr/bin/mipsel-linux-gnu-g++-5)
    Screenshot from 2017-11-09 15-41-22.png看起來只剩下link 問題了
  32. 把上面那堆缺少的lib 下載與解壓縮之後,幾乎完成
  33. 最後卡在libsnsr.a, 這是key word detect 需要的lib, www.sensory.com 公司開發
    Screenshot from 2017-11-09 18-52-52
  34. 嘗試一些把 arm convert to mips 的工具
    https://forum.xda-developers.com/showthread.php?t=1411879 這個要先安裝在mips android 上面,然後才能把arm so convert to mips
    $ unzip -d MagicCode_V1.5.1 MagicCode_V1.5.1.apk
    Archive: MagicCode_V1.5.1.apk
    inflating: MagicCode_V1.5.1/META-INF/MANIFEST.MF
    inflating: MagicCode_V1.5.1/META-INF/CERT.SF
    inflating: MagicCode_V1.5.1/META-INF/CERT.RSA
    inflating: MagicCode_V1.5.1/AndroidManifest.xml
    inflating: MagicCode_V1.5.1/classes.dex
    inflating: MagicCode_V1.5.1/lib/mips/libavm3.so
    inflating: MagicCode_V1.5.1/lib/mips/libavm401.so
    inflating: MagicCode_V1.5.1/lib/mips/libavm403.so
    inflating: MagicCode_V1.5.1/lib/mips/libavm_cpuinfo.so
    inflating: MagicCode_V1.5.1/lib/mips/libfilter3.so
    inflating: MagicCode_V1.5.1/lib/mips/libfilter401.so
    inflating: MagicCode_V1.5.1/lib/mips/libfilter403.so
    inflating: MagicCode_V1.5.1/res/drawable-mdpi/background.jpg
    extracting: MagicCode_V1.5.1/res/drawable-mdpi/bnhit.png
    extracting: MagicCode_V1.5.1/res/drawable-mdpi/bnoff.png
    extracting: MagicCode_V1.5.1/res/drawable-mdpi/bnon.png
    extracting: MagicCode_V1.5.1/res/drawable-mdpi/icon.png
    inflating: MagicCode_V1.5.1/res/drawable-mdpi/textback.png
    inflating: MagicCode_V1.5.1/res/layout/main.xml
    inflating: MagicCode_V1.5.1/resources.arsc
  35. add below to *.link since MT7621 using /lib/ld-uClibc.so.0 as ld
    -Wl,--dynamic-linker=/lib/ld-uClibc.so.0
  36. google assistant SDK also only support arm(armv7) https://developers.google.com/assistant/sdk/overview#features
  37. I using https://retdec.com/decompilation-run/ to decompile libsnsr.a to machine language
  38. decompile all 162 so file and compile with below command
    mipsel-linux-gnu-gcc-5 -shared -o libsnsrarm.o -fPIC *.c

    1. but compile error as belows
    2. libsnsr12846.c:1083:13: error: label ‘lab_0x8bc_4’ used but not defined
      goto lab_0x8bc_4;
    3. libsnsr19151.c:879:49: error: ‘v15’ undeclared (first use in this function)
      switch (*(int32_t *)v15) {
    4. libsnsr23710.c:224:33: error: ‘v83’ undeclared (first use in this function)
      v83 = result2;
    5. libsnsr24486.c:181:53: error: invalid operands to binary * (have ‘int’ and ‘int32_t (*)(int32_t *, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t) {aka int (*)(int *, int, int, int, int, int, int, int, int, int, int)}’)
      int32_t v16 = 4 * aa0db;
    6. libsnsr24486.c:185:86: error: invalid operands to binary * (have ‘int’ and ‘int32_t (*)(int32_t *, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t) {aka int (*)(int *, int, int, int, int, int, int, int, int, int, int)}’)
      if (v8 == *(int32_t *)(*(int32_t *)(v6 + 56) + 4 * aa0db)) {
    7. libsnsr24486.c:203:88: error: invalid operands to binary - (have ‘int’ and ‘int32_t (*)(int32_t *, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t) {aka int (*)(int *, int, int, int, int, int, int, int, int, int, int)}’)
      if (v21 == aa0db + 1 || -3 - aa0db + v21 > v21) {
    8. libsnsr24971.c:1134:31: error: invalid operands to binary % (have ‘int32_t (*)(int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t) {aka int (*)(int, int, int, int, int, int, int, int, int, int, int, int, int)}’ and ‘int’)
      v10 = (mae1x + 1) % 0x10000;
    9. libsnsr24971.c:1369:23: error: invalid operands to binary % (have ‘int32_t (*)(int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t) {aka int (*)(int, int, int, int, int, int, int, int, int, int, int, int, int)}’ and ‘int’)
    10. build log uploaded here https://pastebin.com/0Muwfwv9
    11. decompiled file uploaded here https://drive.google.com/file/d/1jiP1G5_pOCMj7XOT_WQ_4tsN4h7Piz_F/view?usp=sharing
  39. maybe need to find another decompile tool