GN的编译及使用

编译ninja

$ git clone git://github.com/ninja-build/ninja.git && cd ninja
$ git checkout release
$ ./configure.py --bootstrap
$ ls -alh # ninja可执行文件在当前目录中
total 3.5M
drwxrwxr-x 7 wangzhen wangzhen 4.0K 9月   5 10:57 .
drwxrwxr-x 4 wangzhen wangzhen 4.0K 9月   5 10:55 ..
-rwxrwxr-x 1 wangzhen wangzhen  858 9月   5 10:56 bootstrap.py
drwxrwxr-x 2 wangzhen wangzhen 4.0K 9月   5 10:57 build
-rw-rw-r-- 1 wangzhen wangzhen 7.7K 9月   5 10:57 build.ninja
-rw-rw-r-- 1 wangzhen wangzhen 1.1K 9月   5 10:56 .clang-format
-rwxrwxr-x 1 wangzhen wangzhen  22K 9月   5 10:56 configure.py
-rw-rw-r-- 1 wangzhen wangzhen  12K 9月   5 10:56 COPYING
drwxrwxr-x 2 wangzhen wangzhen 4.0K 9月   5 10:56 doc
drwxrwxr-x 8 wangzhen wangzhen 4.0K 9月   5 10:56 .git
-rw-rw-r-- 1 wangzhen wangzhen  431 9月   5 10:56 .gitignore
-rw-rw-r-- 1 wangzhen wangzhen 7.6K 9月   5 10:56 HACKING.md
drwxrwxr-x 5 wangzhen wangzhen 4.0K 9月   5 10:57 misc
-rwxrwxr-x 1 wangzhen wangzhen 3.4M 9月   5 10:57 ninja
-rw-rw-r-- 1 wangzhen wangzhen  877 9月   5 10:56 README
-rw-rw-r-- 1 wangzhen wangzhen 1.4K 9月   5 10:56 RELEASING
drwxrwxr-x 2 wangzhen wangzhen 4.0K 9月   5 10:56 src
-rw-rw-r-- 1 wangzhen wangzhen  194 9月   5 10:56 .travis.yml

将目录/home/wangzhen/WorkSpace/ninja加入到环境变量中

$ pwd
/home/wangzhen/WorkSpace/ninja

$ vi ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
export PATH=$PATH:/home/wangzhen/WorkSpace/ninja

$ source ~/.bashrc

编译GN

$ sudo apt-get install clang
$ git clone https://gn.googlesource.com/gn
$ cd gn
$ python build/gen.py
$ ninja -C out

将目录/home/wangzhen/WorkSpace/gn/out加入到环境变量中

$ vi ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
export PATH=$PATH:/home/wangzhen/WorkSpace/ninja:/home/wangzhen/WorkSpace/gn/out

$ source ~/.bashrc

使用gn编译demo:

$ cd gn/tools/gn/example
$ gn gen out/
$ ninja -C out/
$ cd out
$ ./hello
Hello, world

expample中代码目录:

wangzhen@wangzhen-VirtualBox:~/WorkSpace/gn/tools/gn/example$ ll
total 60
drwxrwxr-x 4 wangzhen wangzhen  4096 9月   5 11:01 ./
drwxrwxr-x 5 wangzhen wangzhen 16384 9月   5 09:48 ../
drwxrwxr-x 3 wangzhen wangzhen  4096 9月   5 09:48 build/
-rw-rw-r-- 1 wangzhen wangzhen   524 9月   5 09:48 BUILD.gn
-rw-rw-r-- 1 wangzhen wangzhen    87 9月   5 09:48 .gn
-rw-rw-r-- 1 wangzhen wangzhen   344 9月   5 09:48 hello.cc
-rw-rw-r-- 1 wangzhen wangzhen   243 9月   5 09:48 hello_shared.cc
-rw-rw-r-- 1 wangzhen wangzhen   929 9月   5 09:48 hello_shared.h
-rw-rw-r-- 1 wangzhen wangzhen   243 9月   5 09:48 hello_static.cc
-rw-rw-r-- 1 wangzhen wangzhen   323 9月   5 09:48 hello_static.h
drwx------ 3 wangzhen wangzhen  4096 9月   5 11:01 out/
-rw-rw-r-- 1 wangzhen wangzhen   186 9月   5 09:48 README.txt

wangzhen@wangzhen-VirtualBox:~/WorkSpace/gn/tools/gn/example$ tree
.
├── build
│   ├── BUILDCONFIG.gn
│   ├── BUILD.gn
│   └── toolchain
│       └── BUILD.gn
├── BUILD.gn
├── hello.cc
├── hello_shared.cc
├── hello_shared.h
├── hello_static.cc
├── hello_static.h
└── README.txt

2 directories, 10 files

wangzhen@wangzhen-VirtualBox:~/WorkSpace/gn/tools/gn/example$ cat BUILD.gn 
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

executable("hello") {
  sources = [
    "hello.cc",
  ]

  deps = [
    ":hello_shared",
    ":hello_static",
  ]
}

shared_library("hello_shared") {
  sources = [
    "hello_shared.cc",
    "hello_shared.h",
  ]

  defines = [ "HELLO_SHARED_IMPLEMENTATION" ]
}

static_library("hello_static") {
  sources = [
    "hello_static.cc",
    "hello_static.h",
  ]
}

.gn不能忽略

wangzhen@wangzhen-VirtualBox:~/WorkSpace/gn/tools/gn/example$ cat .gn 
# The location of the build configuration file.
buildconfig = "//build/BUILDCONFIG.gn"

你可能感兴趣的:(GN的编译及使用)