Android boost的移植

1 C++编译器分类
1.1 C++ history
School: C++98
Work: C++11/14/17/20, 2006 ~ 2021

1.2 GNU gcc
源代码路径:bionic/libstdc++
工具链:
/path/to/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9/bin

1.3 clang LLVM
clang是LLVM的前端,LLVM为后端。
源代码路径:external/libcxx
工具链:
/path/to/prebuilts/clang/host/linux-x86/clang-4053586/bin

1.4 Android NDK c++库的选择
默认情况下,NDK使用gcc libstdc++ (/system/lib/libstdc++.so,仅仅包含new和delete)。

如果要使用LLVM libc++,请在Application.mk中添加
APP_STL := c++_shared
LLVM libc++不是系统库,使用libc++_shared.so,必须将其包含在APK中,如果使用Gradle构建应用,此步骤会自动完成。

从NDK r18开始LLVM libc++成为NDK中唯一可用的STL。
readelf -d sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so

1.5 AS
1) JDK
2) AS
http://www.android-studio.org
released in 2013, from the end of 2015, does not maintain Eclipse.
3) Android SDK
不再提供独立下载,需要通过AS安装。

2 静态代码检查工具
1) GrammaTech CodeSonar
2) JLint
3) Open Source SonarQube
4) Synopsys Black Duck
5) Synopsys Coverity

3 Android打印C++调用栈
#include <utils/CallStack.h> 
在需要打印的地方加如下的定义。
android::CallStack stack("oem");

4 Android boost库的移植
4.1 Android boost的编译
https://www.boost.org/users/download/

下载2017版本1.66.0,编写Android.bp文件。
cc_library_static {
    name: "libboost",
    vendor_available: true,

    rtti: true,
    cppflags: [
        "-fexceptions",
        //"-mavx2",
    ],

    local_include_dirs: [
        "./",
        "./libs/log/src/",
    ],
    export_include_dirs: ["./"],
    srcs: [
        "libs/chrono/src/*.cpp",
        "libs/date_time/src/gregorian/*.cpp",
        "libs/log/src/setup/*.cpp",
        "libs/log/src/*.cpp",
        "libs/log/src/posix/*.cpp",
        "libs/filesystem/src/*.cpp",
        "libs/system/src/error_code.cpp",
        "libs/thread/src/future.cpp",
        "libs/thread/src/pthread/thread.cpp",
        "libs/thread/src/pthread/once.cpp",
    ],
}
boost.log库在APL A39X0 Gen9平台加上-mavx2后,编译可以通过,但是在Android上运行时会出现“Illegal instruction”的错误。可以通过cat /proc/cpuinfo查看CPU支持的特殊指令集。

为了简化Android.bp的编写,libs/log/src/*.cpp会包含所有的cpp文件,要解决在APL A39X0 Gen9平台上的运行错误,按照如下步骤修改即可。
1)注释Android.bp中的-mavx2
2)将文件libs/log/src/dump_avx2.cpp重命名成libs/log/src/dump_avx2-cpp-
3)将文件libs/log/src/dump_ssse3.cpp重命名成libs/log/src/dump_ssse3-cpp-
4)再重新编译生成libboost.a即可

4.2 如何使用
编译其它APP时,在Android.bp中添加如下的boost库依赖。
rtti: true,
cppflags: [
    "-fexceptions",
],
static_libs: [
    "libboost",
],

4.3 Serialization
I. Payload is Big-Endian byte ordering, refer to functions int serialize() and int deserialize()
II. array: 4-byte length + array
III. structure: 4-byte length + structure, refer to deserialize_EMMCInfoQuery_OutParams() and DVRSensorStatusReport_deserialize()
IV. string: 4-byte length (including BOM) + 3-byte BOM (EF BB BF) + string
V. Pay attention to the nested array, structure and string
for example:
struct A {
    int i;
    int j;
};
struct B {
    int k;
    struct A a;
};
struct B array[2];
4-byte length (sizeof(array)) +
4-byte length (sizeof(struct B)) + k + 4-byte length (sizeof(struct A)) + i + j +
4-byte length (sizeof(struct B)) + k + 4-byte length (sizeof(struct A)) + i + j +

4.4 Java HIDL
[28th-Mar-2022]
Meld for source code compare in Windows.
G is for getter method, S is for setter method and N is for notification.
./hardware/interfaces/update-makefiles.sh
Android.mk
LOCAL_STATIC_JAVA_LIBRARIES += \
android.hardware.foo-V1.0-java
Android.bp
static_libs: [
"android.hardware.foo-V1.0-java",
],

// out/target/common/gen/JAVA_LIBRARIES
import android.hardware.foo.V1_0.IFoo;
IFoo server = IFoo.getService();

5 Abbreviations
boost.msm:Boost.MetaStateMachine
cin:console in
cout:console out
cerr:console error
endl:end-of-line;windows是\r\n,unix/linux是\n
Clang:/ˈklæŋ/
Peer-reviewed: 同行评审
RTTI:Runtime Type Identification,运行时类型识别

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值