This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Introduction to the

Blade is an easy-to-use, high-performance, modern code building system that is particularly suited for agile building of large code bases within a company, with built-in direct support for multiple programming languages and unit testing frameworks.

GitHub – chen3feng/blade-build: Blade is a powerful build system from Tencent, supports many mainstream programming languages, such as C/C++, java, scala, python, protobuf…

Blade is a construction system developed by Mr. Chen of Tencent. The main goal of Blade is to manage and build the declaration cycle of large-scale c++ projects under Linux. Blade is widely used in Tencent’s advertising system, wechat background service, Tencent game background service and Tencent infrastructure.

A Blade builds the entire code base into libraries with a “target” defined in the BUILD file at each “library” level, depending on each other by the name of “target”, and automatically recompilers when the libraries that the BUILD target depends on are updated. The parts that have not been updated will be linked directly to the results of the last compilation, which automatically manages dependent updates and greatly speeds up compilation.

The above dry words are not clear, so let’s look directly at how Blade builds a simple c++ project.

Quick learning

The process of installing a Blade is detailed in the Github documentation, and you can follow the process to install a Blade into your own development environment, which is not covered here. After the installation is complete, run the blade – verison command. If the blade version is displayed, the installation is successful.

The sample

A single file

First we create a hello world c++ source code

#include <iostream>

using namespace std;

int main() {
  cout << "Hello world!" << endl;
	return 0;
}
Copy the code

In order for Balde to identify the project, we need to create a blank file “BLADE_ROOT”. The Blade compiles to look up from the current folder to the location of the nearest BLADE_ROOT file and treats it as the root of the project.

We are compiling a c++ executable object, so we need to write the cc_binary rule:

cc_binary(
  name = 'hello',
  srcs = [
    'hello.cc',
  ],
  deps = []
)
Copy the code

Write the CC or CPP file name in SRCS, and list the dependent target in DEPS.

Then, after running blade build on the terminal, hello can be compiled and linked. The generated binary file is in the build64_release folder, open and run, and we can see the Familiar Hello World

$ build build .
$ cd build64_release
$ ./hello
Copy the code

Target positioning rule

When a target depends on another target, it is necessary to accurately locate the dependent path. The basic locating rules are as follows:

//path/to/dep:target_name
Copy the code

“//” indicates the path relative to the project root; After the colon is the name of the target; If the current target and the dependency are in the same path, you don’t need the previous path, just use the colon +target.

:target_name
Copy the code

A multi-objective

When we build a static library target, we can use the CC_library build rule to output a static library after the build. Let’s create a c++ file.

#include "say.h"

#include <iostream>

void say() {
	std::cout << "Hello world!" << std::endl;
}
Copy the code

Declare the say function in the header file

#ifndef SAY_H_
#define SAY_H_

void say();

#endif
Copy the code

Writing BUILD files

cc_library(
  name = 'say',
  srcs = 'say.cc',
  hdrs = ['say.h'],
  visibility = ['PUBLIC'],
)
Copy the code

Because the include header file is required externally, the HDRS in target lists the header files that are open to the PUBLIC. Visibility specifies the scope of use of the library, and PUBLIC indicates that all build targets can rely on the library.

The say library functions are used in hello.cc, so you need to add the say library to build the target dependency

#include <iostream>

#include "say.h"

using namespace std;

int main() {
  say();
	return 0;
}
Copy the code

BUILD

cc_binary(
  name = 'hello',
  srcs = [
    'hello.cc',
  ],
  deps = [':say']
)
Copy the code

Run Blade build. Build the build target in the current folder. You can see that blade first compiles the Say static link library, and then compiles the Hello build target linked to the main function, generating the Hello executable file.

test

Blade provides comprehensive support for test-driven development and automatically runs tests by command.

Use the build target CC_test to build a gTest-based test target when running the Blade test. , you can test the test target in the current directory.

cc_test(
    name = 'zookeeper_test',
    srcs = 'zookeeper_test.cc',
)
Copy the code

summary

Blade provides an extremely convenient building tool for c++. This article just briefly introduces the method of blade for building c++ projects and its support for testing. Next, I will further summarize and share other features of blade.