macOS下使用vagrant编译WebRTC Android 源码

前言

本文的前提条件:

  • 你需要编译WebRTC Android版本源码
  • 你只有macOS,没有Linux环境

安装Linux虚拟机

安装vagrant和VitualBox

Vagrant是一款用于构建及配置虚拟开发环境的软件,基于Ruby, 主要以命令行的方式运行。 主要配合使用Oracle的开源Virtual Box 虚拟化系统。

分别在官网下载并安装这两个软件。

注意:安装VirtualBox时若遇到:

The Installation Failed

需要在设置中允许安装Oracle的软件

详细请参考:Fixing ‘The Installation Failed’ VirtualBox Error on Mac High Sierra

创建Linux虚拟机(Ubuntu16.04)

mkdir webrtc-builder
cd webrtc-builder
vagrant init ubuntu/xenial64

编辑配置文件(重要)

这一步需要将虚拟机的配置设定好,否则使用默认配置后面编译会遇到诸多问题。

首先我们在webrtc-builder目录下打开配置文件Vagrantfile

vi Vagrantfile

这是我已经配置好的配置文件(省略了注释,方便阅读)

Vagrant.configure("2") do |config| 
  config.vm.box = "ubuntu/xenial64"
  config.disksize.size = '45GB'
  config.vm.provider "virtualbox" do |v|
    v.memory = 4096
    v.cpus = 2
  end
  config.vm.network "private_network", ip: "192.168.50.4"
  config.vm.synced_folder "/Volumes/fss/webrtc_linux", "/home/vagrant/webrtc-android-linux", type: :nfs
end

以下是配置文件中需要注意的几点:

  • config.disksize.size

    磁盘容量,默认是10GB,由于WebRTC Android的源码有将近20G左右,后续编译还会生成一些文件,建议将size设置为40G以上。

  • v.memory

    内存大小,默认是1024MB。建议将其设置为4096MB,否则编译时可能会遇到ninja: build stopped: subcommand failed.问题(内存不足)

  • config.vm.synced_folder

    共享目录,为了方便之后的访问,这里需要将虚拟机中的目录共享出来。

    "/Volumes/fss/webrtc_linux":这里是mac上的地址(需要新建卷宗,稍后我会讲到)

    "/home/vagrant/webrtc-android-linux":这是虚拟机中对应的目录

    注意一:type为nfs需要设置config.vm.network "private_network", ip: "192.168.50.4",这里根据我的这个配置设定即可。

    注意二:默认macOS中的APFS文件系统是大小写不敏感的,而WebRTC源码中使用了大小写来进行区分,这样的话后面在编译时会出现一些问题,导致找不到路径。解决办法是在磁盘工具(Disk Utinity)中新建一个卷宗(例如fss),选择文件系统为APFS(Case-sensitive)(中文是大小写敏感)。之后将挂载的目录设置为此卷宗上的目录即可。

配置文件编辑好后,即可开机。

附上vagrant常用命令:

vagrant up #开机
vagrant halt #关机
vagrant reload #类似重启(重新加载配置文件)
vagrant ssh #ssh登录到虚拟机

在编译之前,我们先ssh登录到虚拟机:

vagrant ssh

接下来的操作在虚拟机上进行。

WebRTC Android 源码编译

准备工作

官方教程: Reference Link

Depot Tools

depot_toolsChromium官方提供的工具包,主要用于Chromium开发。

clone depot_tools repository:

git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

将刚才clone下来的目录添加到运行环境中。可以在终端进行单次export,也可以通过配置文件设置全局生效(具体请Google)

这里的/path/to/depot_tools换成自己的对应路径

export PATH=/path/to/depot_tools:$PATH

下载源码

mkdir webrtc-android-linux
cd webrtc-android-linux
fetch --nohooks webrtc_android
gclient sync

新建一个工作目录

fetch Android版本的源码

fetch完成后,再和远端代码库进行同步,即完成了源码的下载。

注意:此过程需要保持网络畅通(可以访问google)。下载下来的源码大概有将近20G(一半是WebRTC源码,另外一部分是Android的NDK和SDK等特殊部分)

安装依赖

./src/build/install-build-deps.sh
./src/build/install-build-deps-android.sh

编译

使用GN生成工程

cd src
gn gen out/Debug --args='target_os="android" target_cpu="arm"'

参数可以修改,详细请参考官网。

编译

ninja -C out/Debug

全量编译,用时较长,与机器性能有关系。

使用和测试

编译完成后,在out/Debug下生成编译好的工程。

可以在out/Debug/apks/目录下找到AppRTCMobile.apk, 安装到手机即可测试。

附问题总结

我遇到的两个比较大的问题都发生在编译阶段。

  • 编译到7000/11838左右时卡住,killed,报错:ninja: build stopped: subcommand failed Android

    vagrant@ubuntu-xenial:~/webrtc-checkout-android/src$ ninja -C out/Debug
    ninja: Entering directory `out/Debug'
    [6902/11838] CXX obj/pc/peerconnection/peer_connection.o
    FAILED: obj/pc/peerconnection/peer_connection.o 
    ../../third_party/llvm-build/Release+Asserts/bin/clang++ -MMD -MF obj/pc/peerconnection/peer_connection.o.d -D_GNU_SOURCE -DANDROID -DHAVE_SYS_UIO_H -DANDROID_NDK_VERSION_ROLL=r20_1 -DCR_CLANG_REVISION=\"llvmorg-12-init-1771-g1bd7046e-3\" -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_LIBCPP_ABI_UNSTABLE -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_ENABLE_NODISCARD -D_LIBCPP_DEBUG=0 -DCR_LIBCXX_REVISION=375504 -D_DEBUG -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DWEBRTC_ENABLE_PROTOBUF=1 -DWEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE -DRTC_ENABLE_VP9 -DHAVE_SCTP -DWEBRTC_ARCH_ARM -DWEBRTC_ARCH_ARM_V7 -DWEBRTC_HAS_NEON -DWEBRTC_LIBRARY_IMPL -DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=1 -DWEBRTC_POSIX -DWEBRTC_LINUX -DWEBRTC_ANDROID -DABSL_ALLOCATOR_NOTHROW=1 -DHAVE_SCTP -I../.. -Igen -I../../third_party/abseil-cpp -I../../third_party/libyuv/include -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -funwind-tables -fPIC -fcolor-diagnostics -fmerge-all-constants -fcrash-diagnostics-dir=../../tools/clang/crashreports -mllvm -instcombine-lower-dbg-declare=0 -fcomplete-member-pointers -ffunction-sections -fno-short-enums --target=arm-linux-androideabi16 -DHAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC=1 -march=armv7-a -mfloat-abi=softfp -mtune=generic-armv7-a -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -Xclang -fdebug-compilation-dir -Xclang . -no-canonical-prefixes -mfpu=neon -mthumb -Wall -Werror -Wextra -Wimplicit-fallthrough -Wunreachable-code -Wthread-safety -Wextra-semi -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-unneeded-internal-declaration -Wno-undefined-var-template -Wno-psabi -Wno-ignored-pragma-optimize -Wno-implicit-int-float-conversion -Wno-final-dtor-non-final-class -Wno-builtin-assume-aligned-alignment -Wno-deprecated-copy -Wno-non-c-typedef-for-linkage -Wmax-tokens -Oz -fno-ident -fdata-sections -ffunction-sections -fomit-frame-pointer -gdwarf-3 -g1 -fdebug-info-for-profiling -fvisibility=hidden -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -Wexit-time-destructors -Wglobal-constructors -Wc++11-narrowing -Wimplicit-fallthrough -Wthread-safety -Winconsistent-missing-override -Wundef -Wunused-lambda-capture -Wno-undefined-bool-conversion -Wno-tautological-undefined-compare -std=c++14 -fno-trigraphs -Wno-trigraphs -fno-exceptions -fno-rtti -nostdinc++ -isystem../../buildtools/third_party/libc++/trunk/include -isystem../../buildtools/third_party/libc++abi/trunk/include --sysroot=../../third_party/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fvisibility-inlines-hidden -Wnon-virtual-dtor -Woverloaded-virtual -c ../../pc/peer_connection.cc -o obj/pc/peerconnection/peer_connection.o
    Killed
    [6904/11838] CXX obj/pc/peerconnection/rtc_stats_collector.o
    ninja: build stopped: subcommand failed.
    

    查询了一下,许多人在编译Android源码时遇到过这个问题,说是因为系统同时打开的文件数有限。我根据这个链接修改了一下,将文件数扩大到20480后依然出现问题。

    随后我查看了虚拟机的配置,发现它的运存也很小,这个原因也可能导致killed。因此将运存调整到4096MB。

  • Invalid argument问题

    vagrant@ubuntu-xenial:~/webrtc-android-linux/src$ ninja -C out/Debug
    ninja: Entering directory `out/Debug'
    [6519/11838] ACTION //third_party/android_deps:b...der(//build/toolchain/android:android_clang_arm)
    FAILED: obj/third_party/android_deps/backport_util_concurrent_backport_util_concurrent.ijar.jar 
    python ../../build/android/gyp/ijar.py clang_x64/ijar ../../third_party/android_deps/libs/backport_util_concurrent_backport_util_concurrent/backport-util-concurrent-3.1.jar obj/third_party/android_deps/backport_util_concurrent_backport_util_concurrent.ijar.jar
    Traceback (most recent call last):
      File "../../build/android/gyp/ijar.py", line 34, in 
        main()
      File "../../build/android/gyp/ijar.py", line 30, in main
        build_utils.CheckOutput([ijar_bin, in_jar, f.name])
      File "/home/vagrant/webrtc-android-linux/src/build/android/gyp/util/build_utils.py", line 254, in CheckOutput
        raise CalledProcessError(cwd, args, stdout + stderr)
    util.build_utils.CalledProcessError: Command failed: ( cd /home/vagrant/webrtc-android-linux/src/out/Debug; clang_x64/ijar ../../third_party/android_deps/libs/backport_util_concurrent_backport_util_concurrent/backport-util-concurrent-3.1.jar /home/vagrant/webrtc-android-linux/src/out/Debug/obj/third_party/android_deps/tmp15NzRwbackport_util_concurrent_backport_util_concurrent.ijar.jar )
    mmap(): Invalid argument
    
    [6521/11838] CXX clang_x64/obj/tools/android/md5sum/md5sum_bin/md5sum.o
    ninja: build stopped: subcommand failed.
    

    这个问题是设置了共享目录之后出现的(当时映射的mac本地目录是APFS大小写不敏感)。

    查看这个报错,应该是在out/Debug中查找不到路径,那就是路径有误。

    后续将挂载目录改为APFS大小写敏感即可。

参考

WebRTC 开发实践:编译 Android 平台源码

How to build webrtc library for Android in OSX host

你可能感兴趣的:(macOS下使用vagrant编译WebRTC Android 源码)