ninja在BC-Linux服务器上源码编译和安装

本文讲述的是如何从GitHub上下载 ninja 源码并编译安装。
提示:一旦涉及编译源码安装,你需要安装的基本的依赖项有:git、autoconf、automake、libtool、make、cmake

从GitHub上下载ninja源码

1. 下载项目源码

下载源码的过程请参考《re2c在BC-Linux服务器上源码编译和安装》中的下载过程。此处不在累赘,需要注意的是我们找的项目仓库是 ninja-build/ninja。我们将下载的源码 .tar.gz 文件放到路径 /home/code/mygithub/ 下面,这里的路径你可以按照自己的喜爱创建。mygithub 是我创建的一个本地 git 仓库,用来存放 GitHub 上下载的项目源码的。然后解压文件,如下。

[root@localhost mygithub]# ls -d ninja*
ninja-1.11.1  ninja-1.11.1.tar.gz
[root@localhost mygithub]# cd ninja-1.11.1/
[root@localhost ninja-1.11.1]# ls
appveyor.yml    configure.py     COPYING  misc       RELEASING  windows
CMakeLists.txt  CONTRIBUTING.md  doc      README.md  src
[root@localhost ninja-1.11.1]# pwd
/home/code/mygithub/ninja-1.11.1
[root@localhost ninja-1.11.1]# 

上面解压后的文件夹目录为 /home/code/mygithub/ninja-1.11.1

2. 编译项目源码

根据一开始黄色文字提到的,我们需要先安装好 autoconf、automake、libtool 这些依赖项

查看编译命令

根据项目的 README.md 中介绍如下:

Building Ninja itself
You can either build Ninja via the custom generator script written in Python or via CMake. For more details see the wiki.

Python
./configure.py --bootstrap
This will generate the ninja binary and a build.ninja file you can now use to build Ninja with itself.

可知需要运行 ./configure.py --bootstrap 命令来编译代码。
这里需要注意的是脚本 configure.py 是python脚本,执行需要对应的python解释器。可以打开这个脚本来查看内容。如下:

#!/usr/bin/env python
#
# Copyright 2001 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the “License”);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

“”“Script that generates the build.ninja for ninja itself.

Projects that use ninja themselves should either write a similar script
or use a meta-build system that supports Ninja output.”“”

由脚本开头第一行的注释可知,运行此脚本需要系统自带的 Python 的版本就可以了,一般为 python2.xGitHub 上最新的项目源码中 configure.py 第一行写的是 python3,因此你如果编译的是最新的项目源码,则需要 先安装Python3。这里是最新的 release 项目源码版本 1.11.1。具体的版本根据具体脚本来确定。

另外,GitHub 中最新的项目源码也对 cmake 有要求,规定了使用 cmake 的最低版本,详情请参考项目说明

编译源代码:

  • 运行命令 ./configure.py --bootstrap
[root@localhost ninja-1.11.1]# ./configure.py --bootstrap
bootstrapping ninja...
warning: A compatible version of re2c (>= 0.11.3) was not found; changes to src/*.in.cc will not affect your build.
>>>>>>>>>>>>>> 中间的编译过程省略 <<<<<<<<<<<<<<
wrote build.ninja.
bootstrap complete.  rebuilding...
[31/32] CXX build/ninja.o
>>>>>>>>>>>>>> 中间的编译过程省略 <<<<<<<<<<<<<<
[32/32] LINK ninja
[root@localhost ninja-1.11.1]# 

这里提示有一个 re2c 的兼容版本没有找到,但是不影响编译。你可以忽略。
如果想安装 re2c(>=0.11.3) ,关于re2c软件包的安装详见《re2c在BC-Linux服务器上源码编译和安装》。根据文章可以安装最新版本的 re2c 软件包,我写本文的时候最新的版本为3.0版本,已经根据文章描述安装如下:

[root@localhost ninja]# re2c --version
re2c 3.0
[root@localhost ninja]#
  • 再次运行命令 ./configure.py --bootstrap
[root@localhost ninja-1.11.1]# ./configure.py --bootstrap
bootstrapping ninja...
>>>>>>>>>>>>>> 中间的编译过程省略 <<<<<<<<<<<<<<
wrote build.ninja.
bootstrap complete.  rebuilding...
[32/33] CXX build/ninja.o
>>>>>>>>>>>>>> 中间的编译过程省略 <<<<<<<<<<<<<<
[33/33] LINK ninja
[root@localhost ninja-1.11.1]# ls
appveyor.yml  build.ninja     configure.py     COPYING  misc   README.md  src
build         CMakeLists.txt  CONTRIBUTING.md  doc      ninja  RELEASING  windows
[root@localhost ninja-1.11.1]# 

由上可见:re2c 兼容版本找不到的告警已经没有了,并且目录下已经生成了 ninja 二进制可执行文件

安装 ninja

安装ninja,其实就是让系统使用的时候能找到ninja这个二进制文件运行,由两种方式:

  1. /usr/bin/ 目录下生成 ninja 的软链接,命令: ln -s ninja文件所在路径 /usr/bin/ninja
  2. 直接将 ninja 二进制文件拷贝到 /usr/bin/ 目录下
  • 方法1
[root@localhost ninja-1.11.1]# pwd
/home/code/mygithub/ninja-1.11.1
[root@localhost ninja-1.11.1]# find / -name ninja
find: ‘/proc/3276’: No such file or directory
/home/code/mygithub/ninja-1.11.1/ninja
[root@localhost ninja-1.11.1]# ln -s /home/code/mygithub/ninja-1.11.1/ninja /usr/bin/ninja
[root@localhost ninja-1.11.1]# find / -name ninja
/usr/bin/ninja
/home/code/mygithub/ninja-1.11.1/ninja
[root@localhost ninja-1.11.1]# ninja --version
1.11.1
[root@localhost ninja-1.11.1]# 
  • 方法2
    先删除方法1创建的软链接,然后再拷贝。
[root@localhost ninja-1.11.1]# rm -rf /usr/bin/ninja 
[root@localhost ninja-1.11.1]# cp ./ninja /usr/bin/
[root@localhost ninja-1.11.1]# find / -name ninja
/usr/bin/ninja
/home/code/mygithub/ninja-1.11.1/ninja
[root@localhost ninja-1.11.1]# ninja --version
1.11.1
[root@localhost ninja-1.11.1]# 

可见,两种方式都是可以的。


从 EPEL 中直接安装 ninja

安装 ninja 还有另外一种方式,那就是从 EPEL 中直接安装,最为方便快捷,没有上面那么麻烦。

安装步骤分两步:

  1. 安装软件扩展包 EPEL
  2. 安装 ninja

1. 安装 EPEL

EPEL 的安装请参考文章《Linux上安装EPEL扩展包》,这里不再讲述。

2. 安装 ninja

这里先删除之前安装的 ninja,以防混乱。

[root@localhost ~]# rm -rf /usr/bin/ninja
[root@localhost ~]# ninja --version
-bash: /usr/bin/ninja: No such file or directory
[root@localhost ~]# 

然后安装 ninja 软件包, 这里需要注意,ninja 在 EPEL 中的名字为 ninja-build,所以应该安装 ninja-build

[root@localhost ~]# yum install ninja-build
Configuration file /etc/yum/pluginconf.d/license-manager.conf not found
Unable to find configuration file for plugin license-manager
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * epel: hkg.mirror.rackspace.com
Resolving Dependencies
--> Running transaction check
---> Package ninja-build.x86_64 0:1.10.2-3.el7 will be installed

>>>>>>>>>>>>>> 中间的安装过程省略 <<<<<<<<<<<<<<
Installed:
  ninja-build.x86_64 0:1.10.2-3.el7                                                                            

Complete!
[root@localhost ~]#

查看安装目录,可见在 /usr/bin/ 目录下安装了 ninja 二进制文件。

[root@localhost ~]# find / -name ninja
/usr/bin/ninja
/usr/share/bash-completion/completions/ninja
/home/code/mygithub/ninja-1.11.1/ninja
[root@localhost ~]# 

好了,ninja 的安装分享完毕,希望帮到你,谢谢阅览。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫叶2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值