android系统构建系统_构建系统简介

android系统构建系统

Jan. 21. 2016

2016年1月21日

Roughly speadking, build in software development is the process of “translating” source code files into executable binary code files[1]; and a build system is a collection of software tools that is used to facilitate the build process[2]. Despite the fact that different build systems have been “invented” and used for over three decades, the core algorithms used in most of them are not changed much since the first introduction of the directed acyclic graph ( DAG) by Make[3]. Popular build systems nowadays include the classical GNU Make, CMake, QMake, Ninja, Ant, Scons, and many others. In this note, I am going to demonstrate how to use some of these popular build systems to build C++ projects (to emphasize how the build systems work, the projects are extremely simplified).

大致而言, 内置软件开发是将源代码文件“转换”为可执行二进制代码文件的过程[1]; 构建系统是用于简化构建过程的软件工具的集合[2]。 尽管事实上已经“发明”了不同的构建系统并使用了三十多年,但自Make首次引入有向无环图(DAG)以来,大多数构建系统所使用的核心算法并未发生太大变化[3]。 如今,流行的构建系统包括经典的GNU MakeCMakeQMakeNinjaAntScons等。 在本说明中,我将演示如何使用其中一些流行的构建系统来构建C ++项目(为了强调构建系统的工作方式,这些项目被极大简化了)。

玩具项目 (Toy Project)

The toy project I am going to use will just produce an executable binary code file which simply print out the string “Hello World!” on the screen. Below is the architecture of the project (a name without extension represent a directory).

我将要使用的玩具项目将只产生一个可执行的二进制代码文件,该文件仅打印出字符串“ Hello World!”。 屏幕上。 下面是项目的体系结构(不带扩展名的名称代表目录)。

// Project Directory Tree
/home/[username]/project-hello
bin
include
print.cpp
print.h
lib
obj
src
main.cpp

Below are the code in the three source code files.

下面是三个源代码文件中的代码。

// print.h
#include <iostream>
#include <string>class Print
{
public:
void operator()(const std::string&);
};// print.cpp
#include “print.h”void Print::operator()(const std::string &str)
{
std::cout << str;
}// main.cpp
#include “../include/print.h”int main(int argc, char* argv[])
{
Print print;
print(“Hello World!\n”);
return 0;
}

GNU Make (GNU Make)

GNU Make build projects following the information provided in a file called makefile[4]. To build a project, one needs to write a makefile to tell GNU Make how. In the example below, I demonstrate how to write makefile and build an extremely simplified project. Put the following Makefile into the root directory of the project, i.e. the project-hello directory.

GNU Make构建项目遵循名为makefile [4]的文件中提供的信息。 要构建一个项目,需要编写一个makefile来告诉GNU Make如何。 在下面的示例中,我演示了如何编写makefile并构建一个极其简化的项目。 将以下Makefile放入项目的根目录,即project-hello目录。

# Makefile
CXX = g++
AR = arSOURCEPATH = $(PWD)/src
INCLUDEPATH = $(PWD)/include
OBJECTPATH = $(PWD)/obj
LIBRARYPATH = $(PWD)/lib
BINARYPATH = $(PWD)/binall: hello-static hello-sharedhello-static: print-static $(SOURCEPATH)/main.cpp
$(CXX) $(SOURCEPATH)/main.cpp -L$(LIBRARYPATH) -lprint_static -o $(BINARYPATH)/$@hello-shared: print-shared $(SOURCEPATH)/main.cpp
$(CXX) $(SOURCEPATH)/main.cpp -L$(LIBRARYPATH) -lprint_shared -o
$(BINARYPATH)/$@
export LD_LIBRARY_PATH=”$(LIBRARYPATH)”print-static: obj-static
$(AR) -cru $(LIBRARYPATH)/libprint_static.a $(OBJECTPATH)/print_static.oprint-shared: obj-shared
$(CXX) -shared $(OBJECTPATH)/print_shared.o -o $(LIBRARYPATH)/libprint_shared.soobj-static: $(INCLUDEPATH)/print.h
$(CXX) -c -Wall $(INCLUDEPATH)/print.cpp -o $(OBJECTPATH)/print_static.oobj-shared: $(INCLUDEPATH)/print.h
$(CXX) -fPIC -c $(INCLUDEPATH)/print.cpp -o $(OBJECTPATH)/print_shared.oclean:
find . -name “*.so” -type f -print0 | xargs -0 rm -f
find . -name “*.a” -type f -print0 | xargs -0 rm -f
find . -name “*.o” -type f -print0 | xargs -0 rm -f
rm -f $(BINARYPATH)/*

With the Makefile provided about, an executable program, static library, and shared library can be produced by respectively running make all (or simply make), make print-static, and make print-shared commands in terminal. To remove all generated binary code files, one just needs to run the command make clean. For more details, please check [4] or other similar references.

使用提供的Makefile ,可以通过分别在终端中运行make all (或简单地make ), make print-staticmake print-shared命令来生成可执行程序,静态库和共享库。 要删除所有生成的二进制代码文件,只需运行命令make clean 。 有关更多详细信息,请检查[4]或其他类似参考。

QMake (QMake)

Like CMake, QMake is an open-source, cross-platform build system. QMake was created by Trolltech (now owned by Digia), and shipped with the Qt toolkit [5]. Essentially QMake automates the generation of Makefiles. Although Qt has its own moc (meta-object compiler) and uic (user-interface compiler) for its own project building process, qmake can be used for building any C++ projects with or without using these features. Next, I demonstrate how to use qmake to build our toy project.

像CMake一样,QMake是一个开源,跨平台的构建系统。 QMake由Trolltech (现归Digia拥有)创建,并随Qt工具包一起提供[5]。 本质上,QMake使Makefile的生成自动化。 尽管Qt在其自身的项目构建过程中具有自己的moc (元对象编译器)和uic (用户界面编译器),但是无论是否使用这些功能,qmake均可用于构建任何C ++项目。 接下来,我演示如何使用qmake来构建我们的玩具项目

To tell qmake how to generate Makefiles appropriately, project (.pro, .pri) files are needed. Although in some trivial special cases, the .pro file automatically generated by running qmake in -project mode may be enough, in most cases, project files need to be manually edited to meet the production build requirements. Below I give another directory tree to include the project files I added for building the toy project (again names without extension are directories).

为了告诉qmake如何适当地生成Makefile,需要项目( .pro.pri )文件。 尽管在一些琐碎的特殊情况下,通过在-project模式下运行qmake自动生成的.pro文件可能就足够了,但在大多数情况下,需要手动编辑项目文件以满足生产构建的要求。 在下面,我给出了另一个目录树,其中包括为构建玩具项目而添加的项目文件(同样,不带扩展名的目录)。

// Project Directory Tree
project-hello
project-hello.pro
bin
include
print.cpp
print.h
lib
lib.pro
src
main.cpp
src.pro

Below are the three project files.

以下是三个项目文件。

# project-hello.pro
TEMPLATE = subdirs
SUBDIRS += lib
SUBDIRS += src
CONFIG += ordered# src.pro
TEMPLATE = app
TARGET = hello
DESTDIR = ../bin
DEPENDPATH += .LIBS += -L../lib -lprint-static # link against static library
#LIBS += -L../lib -lprint-shared # link against shared librarySOURCES += main.cpp# lib.pro, for building static library
TEMPLATE = lib
TARGET = print-static
VERSION = 1.0.0
CONFIG += staticlib# Directories
DESTDIR = ../lib
INCLUDEPATH += ../include
DEPENDPATH += ../include
HEADERS += ../include/print.h
SOURCES += ../include/print.cpp# lib.pro, for building shared library
TEMPLATE = lib
TARGET = print-shared
VERSION = 1.0.0
CONFIG += dll# Directories
DESTDIR = ../lib
INCLUDEPATH += ../include
DEPENDPATH += ../include
HEADERS += ../include/print.h
SOURCES += ../include/print.cpp

After the project files are created and edited appropriately, we are ready to build our toy project. By running qmake -makefile command (as -makefile is the default mode, we may simply run qmake command) to generate Makefiles, and make command, the project will be build successfully.

在适当地创建并编辑了项目文件之后,我们就可以构建玩具项目了。 通过运行qmake -makefile命令(如-makefile是默认的模式下,我们可以简单地运行qmake命令)生成Makefile s,而make的命令,该项目将是构建成功。

Possibly you have noticed that in the above project files, I gave two versions of the project file, lib.pro. One is for building a static library, the other is for shared library. If the executable binary code file is linked against the static library, it can run without loading anything else. But if it is linked against the shared library, the executable binary code file needs to load the shared library to get the code implementing the functor (aka function object), print. We have three different ways to tell the system how to find the shared library. One is to copy the shared library file to the ./bin directory; the second is to copy the shared library file to the system directory /usr/local/lib; the third is to put the line

可能您已经注意到,在上述项目文件中,我给出了两个版本的项目文件lib.pro 。 一种用于构建静态库 ,另一种用于共享库 。 如果可执行二进制代码文件与静态库链接,则可以运行而无需加载其他任何文件。 但是,如果将其链接到共享库,则可执行二进制代码文件需要加载共享库,以获取实现functor (aka 函数对象 ) print 。 我们有三种不同的方式来告诉系统如何找到共享库。 一种是将共享库文件复制到./bin目录。 第二种是将共享库文件复制到系统目录/usr/local/lib ; 第三是把线

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/[username]/project-hello/lib

into the hidden file /home/[username]/.bash_profile.

到隐藏文件/ /home/[username]/.bash_profile

GYP —元构建系统 (GYP — A Meta-Build System)

GYP, abbreviation for Generate Your Projects, is a Meta-Build system, a build system that generates other build systems[8]. Basically, it is a python dictionary[8], which contains all information on how to generate your project. In the entire gyp file as a python dictionary, you can have four different types of data structures, string, integer, list, and again, dictionary. Not only can you set up your project, but also execute commands. Next, I demonstrate how to use it to generate our toy project, and build it. For a detailed tutorial on gyp, the official documentation [8] is thorough and decent.

GYPGenerate Your Projects的缩写,是一个元构建系统 ,是一个生成其他构建系统的构建系统[8]。 基本上,它是python字典 [8],其中包含有关如何生成项目的所有信息。 在整个gyp文件(作为python字典)中,您可以具有四种不同类型的数据结构, stringintegerlist以及dictionary 。 您不仅可以设置项目,还可以执行命令。 接下来,我演示如何使用它来生成我们的玩具项目并进行构建。 有关gyp的详细教程,官方文档[8]详尽而体面。

# project-hello.gyp
{
‘targets’: [{
‘target_name’: ‘hello-static’,
‘type’: ‘static_library’,
‘include_dirs’: [‘include’,],
‘sources’: [
‘include/print.cpp’,
‘include/print.h’,
],
}, {
‘target_name’: ‘hello-shared’,
‘type’: ‘shared_library’,
‘include_dirs’: [‘include’,],
‘sources’: [
‘include/print.cpp’,
‘include/print.h’,
],
‘cflags’: [‘-fPIC’,],
}, {
‘target_name’: ‘hello-gyp’,
‘type’: ‘executable’,
‘include_dirs’: [
‘<(DEPTH)’,
‘<(DEPTH)/include’,
],
‘dependencies’: [
‘hello-static’,
#’hello-shared’,
],
‘sources’: [
‘src/main.cpp’,
‘include/print.h’,
],
},],
}

Using gyp, projects for most build systems can be generated. To choose a specific build system for out projects, one just needs to specify it when invoking the generator command gyp by setting -f build_system_name. Below, I demonstrate this for generating projects for both GNU Make and Ninja build systems. For instructions for generating projects for other build systems, please refer to the official documentation[8].

使用gyp ,可以生成大多数构建系统的项目。 要选择一个特定的构建系统淘汰项目,一个只需要指定它调用生成命令时gyp通过设置-f build_system_name 。 下面,我将演示如何为GNU MakeNinja构建系统生成项目。 有关为其他构建系统生成项目的说明,请参考官方文档[8]。

gyp project-hello.gyp -f make — depth=. — generator-output=build/makefilescd build/makefilesmakegyp project-hello.gyp -f ninja — depth=. — generator-output=build/ninjacd build/ninjaninja -C out/Default/

Although projects for most build systems can be generated with gyp generator, Qt project can not be generated with gyp. This causes some inconvenience sometimes when people needs to use something like the widgets from Qt framework, but for some reason, switching the build system from gyp to qmake is not possible, at least practically. In this case, one may try to “embed” some qmake build system features, say moc, the meta-object compiler into gyp system. In my BuildSystems repository, I included one example project, which uses both widgets and signal-slot from qt framework. I used gyp with moc “embedded” to build the project. If you are interested, you can check the code and scripts here.

尽管大多数构建系统的项目都可以使用gyp generator生成,但是Qt项目不能使用gyp生成。 有时,当人们需要使用Qt框架中的小部件之类的东西时,这会带来一些不便,但是由于某种原因,至少在实际上,不可能将构建系统从gyp切换到qmake。 在这种情况下,可以尝试“嵌入”一些qmake的构建系统的特点, 交通部说, 元对象编译成GYP系统。 在BuildSystems存储库中,我包括一个示例项目,该项目同时使用了qt框架中的小部件信号插槽 。 我将gyp与“嵌入”的moc一起构建了该项目。 如果您有兴趣,可以在此处检查代码和脚本。

翻译自: https://medium.com/swlh/a-brief-introduction-to-build-systems-1e45cb1cf667

android系统构建系统

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值