之前介绍过GYP,它是Google早期用来维护chromium项目的meta-build system,GN则是用来替代GYP的工具,目前chromium及相关的开源项目都迁移到基于GN来管理。并且GN是基于C++编写,效率上比基于python的GYP快了近20倍。
环境准备
配置depot_tools (ninja)
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
配置gn
虽然depot_tools已经带了gn,但它其实只是一个shell脚本,直接使用会报错,找不到真正的gn二进制文件。
如果编译过webrtc或chromium,gn相关的工具链已经下载,可以在src/buildtools/mac/gn
找到。
自己编译可以基于gn的git仓库编译,可以参考:
- #!/bin/bash
- set -e
- set -v
- # Get the sources
- mkdir gn-standalone
- cd gn-standalone
- mkdir tools
- cd tools
- git clone https://chromium.googlesource.com/chromium/src/tools/gn
- cd ..
- mkdir -p third_party/libevent
- cd third_party/libevent
- wget --no-check-certificate https://chromium.googlesource.com/chromium/chromium/+archive/master/third_party/libevent.tar.gz
- tar -xvzf libevent.tar.gz
- cd ../..
- git clone https://chromium.googlesource.com/chromium/src/base
- git clone https://chromium.googlesource.com/chromium/src/build
- git clone https://chromium.googlesource.com/chromium/src/build/config
- mkdir testing
- cd testing
- git clone https://chromium.googlesource.com/chromium/testing/gtest
- cd ..
- # Build
- cd tools/gn
- ./bootstrap/bootstrap.py -s
- # At this point, the resulting binary is at:
- # gn-standalone/out/Release/gn
使用gn的例子
gn的例子可以参考gn的git仓库中带的example来开始。比如,以下测试的代码根目录是src