adb40源代码移植到window64位系统

将adb移植到windows上,使用vc编译

由于需要在Windows64位计算机上使用adb 64位的功能,在adb升级到40版本后,变动比较大。如果使用32位的adb,则可以从android的开发环境中直接获取,但是如果需要在64的进程中,调用adb的动态库和功能,使用vc进行编译的话,则需要对代码进行移植。在移植过程中,遇到许多问题,有的问题也非常让人崩溃,改动也比较多,在此记录下移植过程。

1. 源代码下载

a) aosp下载,是从google的国外网站下载的,但是由于墙的原因,在国内不能直接下载。可以从清华大学的网站上下载。
b) 首先设定repo命令

mkdir /bin
ln -s bin/ ~/bin   
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo 

c) 下载并生成

wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar 
# 下载初始化包这里20G以上
tar xvf aosp-latest.tar
cd AOSP  # 解压得到的 AOSP 工程目录
//这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录
repo sync # 正常同步一遍即可得到完整目录(因为已经下载了大部分,更新很快)

d)

2. Adb在源代码中的位置

a) adbwinapi.dll和adbwinusbapi.dll两个在aosp/development/host/windows/usb目录下。其中api是adbwinapi的源代码,winusb是adbwinusbapi的源码,稍加改动,就可以在vc上编译通过。如果这个都没有通过,建议你后面的就不要看了。
b) adb的源码在aosp/system/core/adb目录下,但是不能直接在vc中编译,需要进行修改。

3. 移植到windows

a) 移植AdbWinApi.dll,AdbWinUsbApi.dll。移植方法很简单,就不在赘述。
b) 移植adb

  1. 生成unistd.h文件,放到vc的include目录中
/** This file is part of the Mingw32 package.
 *  unistd.h maps     (roughly) to io.h
 */
#ifndef _UNISTD_H
#define _UNISTD_H
#include <io.h>
#include <process.h>
#endif /* _UNISTD_H */
  1. error C4996: ‘close’: The POSIX name for this item is deprecated
    在项目的属性->c/c+±>预处理器->预处理器定义,增加
_CRT_NONSTDC_NO_DEPRECATE
_CRT_SECURE_NO_WARNINGS
_CRT_NON_CONFORMING_SWPRINTFS
  1. error C3646: “attribute”: 未知重写说明符
    将这个删除掉
  2. fatal error C1083: 无法打开包括文件: “dirent.h”: No such file or directory
    github下载mingw-w64源码包,从中找到同名文件,并对文件进行适当修改。
    对其中的direct.c文件进行修改,增加w的处理
  3. __builtin_expect
    ++++ include/linux/compiler.h
13: #define likely(x)       __builtin_expect((x),1)
14: #define unlikely(x)     __builtin_expect((x),0)

++++ kernel/sched.c

564:         if (unlikely(in_interrupt())) {
565:                 printk("Scheduling in interrupt/n");
566:                 BUG();
567:         }
这个内建函数的语义是 EXP 的预期值是 C,编译器可以根据这个信息适当地重排语句块的顺序,使程序在预期的情况下有更高的执行效率。上面的例子表示处于中断上下文是很少发生的,第 565-566 行的目标码可能会放在较远的位置,以保证经常执行的目标码更紧凑。

在了解了__builtin_expect 的语义后,在MSVC下可以简单的将__builtin_expect 定义下面这个宏:
#define __builtin_expect(EXP, C) (EXP)
这样并不会影响程序的执行语义。
6. the attribute mechanism

#ifndef __GNUC__
# define  __attribute__(x)  /*NOTHING*/
#endif
  1. fatal error C1083: 无法打开包括文件: “openssl/rsa.h”: No such file or directory
    下载使用openssl
  2. android_pubkey_encode
    在安卓源代码/aosp/system/core/libcrypto_utils中,拷贝出android_pubkey.h,android_pubkey.c文件到工程。将auth.c中的#include <crypto_utils/android_pubkey.h>修改为#include “crypto_utils/android_pubkey.h”
  3. error C3861: “EVP_EncodedLength”: 找不到标识符
    是boringssl中base64计算编码后长度的函数,载openssl1.0.2I中,增加了对这个函数的支持,因此不再需要base64.h文件,只需包含openssl/evp.h。修改util/libeay.num文件,增加android-base/stringprintf.h vc
    BN_bn2bin_padded 142 EXIST::FUNCTION:
    EVP_EncodedLength 776 EXIST::FUNCTION:
  4. T max = std::numeric_limits::max(),error C2589: “(”:“::”右边的非法标记
    将T max = std::numeric_limits::max()修改为T max = (std::numeric_limits::max)()
  5. commandline.cpp(266): error C2668: “std::make_unique”: 对重载函数的调用不明确
    删除sysdeps文件夹内的memory文件,并删除源码中对这个文件的引用,因为vc的memory中已经不需要这个memory文件的存在
  6. getopt
    从网上https://blog.csdn.net/zhucunzeng/article/details/24699457下载getopt的c++代码,添加到工程中
  7. adb_pollfd pfd = {.fd = fd, .events = POLLIN };error C2059: 语法错误:“.”
    结构初始化问题,修改为adb_pollfd pfd = {fd, POLLIN };
  8. transport_mdns.cpp(31): fatal error C1083: 无法打开包括文件: “dns_sd.h”: No such file or directory
    需要下载苹果公司bonjour项目的开发包:You can download a tar file here: http://www.opensource.apple.com/tarballs/mDNSResponder。里面有dns_sd.h文件,但是需要使用vc2010以后的版本进行编译
  9. static_assert(SIZEOF_MEMBER(adb_iovec, iov_len) == SIZEOF_MEMBER(WSABUF, len), “”);
    将sysdeps/uio.h中的
    struct adb_iovec {
    size_t iov_len;
    void* iov_base;
    };
    修改为:
    struct adb_iovec {
    unsigned long iov_len;
    void* iov_base;
    };
  10. openssl1.0.2I,不能使用动态连接的形式,必须使用静态连接库,否在运行崩溃,具体原因需要深入分析。
  11. comment_ls中,有代码引起程序异常,需要注释掉。
  12. 因为是后期整理,有些问题已经忘记了,请在移植时自行补齐。

猜你喜欢

转载自blog.csdn.net/harborian/article/details/84310249