前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Envoy编译篇:bazel构建介绍

Envoy编译篇:bazel构建介绍

作者头像
灰子学技术
发布2022-03-29 15:53:41
1.4K0
发布2022-03-29 15:53:41
举报
文章被收录于专栏:灰子学技术灰子学技术

本篇文章通过https://github.com/bazelbuild/examples/tree/main/cpp-tutorial里面的例子,来简单介绍下bazel构建的基础知识,方便后续查找和学习。

一、设置工作区:

例子:https://github.com/bazelbuild/examples/tree/main/cpp-tutorial/stage3

编译之前的文件内容:

$ ls  // 特别说明:WORKSPACE 
README.md  WORKSPACE  lib    main
$ ls lib
BUILD    hello-time.cc  hello-time.h
$ ls main
BUILD    hello-greet.cc  hello-greet.h  hello-world.cc

设置工作区:新建一个空文件名字叫做WORKSPACE,如此以来该目录及其内容标识为 Bazel 工作区,并位于项目目录结构的根目录中。

二、构建文件BUILD介绍:

BUILD:一个或多个BUILD文件,告诉 Bazel 如何构建项目的不同部分,内容如下所示:

$ cat lib/BUILD
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
    name = "hello-time",
    srcs = ["hello-time.cc"],
    hdrs = ["hello-time.h"],
    visibility = ["//main:__pkg__"], // 使//lib:hello-time目标 在使用该 属性时对目标lib/BUILD显式可见。这是因为默认情况下,目标仅对同一文件中的其他目标可见。
)
$ cat main/BUILD
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

cc_library( // 编译成lib库
    name = "hello-greet",
    srcs = ["hello-greet.cc"],
    hdrs = ["hello-greet.h"],
)

cc_binary( // 编译成可执行文件
    name = "hello-world", // 对应的可执行文件名
    srcs = ["hello-world.cc"], // 编译可执行文件所使用的源文件
    deps = [ // 编译可执行文件依赖的lib库
        ":hello-greet",     // 表明在当前目录下
        "//lib:hello-time", // 表明在lib文件夹下面
    ],
)

三、构建操作:

$ bazel build //main:hello-world // 构建命令
Starting local Bazel server and connecting to it...
INFO: Analyzed target //main:hello-world (17 packages loaded, 146 targets configured).
INFO: Found 1 target...
Target //main:hello-world up-to-date:
  bazel-bin/main/hello-world
INFO: Elapsed time: 15.454s, Critical Path: 1.27s
INFO: 12 processes: 6 internal, 6 darwin-sandbox.
INFO: Build completed successfully, 12 total actions

构建生成的文件:

$ ls
README.md  WORKSPACE  bazel-bin  bazel-out  bazel-stage3  bazel-testlogs  lib    main
$ ls bazel-bin/ // 目标文件
lib  main
$ ls bazel-bin/lib
_objs        libhello-time.a      libhello-time.a-2.params
$ ls bazel-bin/main
_objs        hello-world-2.params    hello-world.runfiles_manifest  libhello-greet.a-2.params
hello-world      hello-world.runfiles    libhello-greet.a

执行命令:

$ ./bazel-bin/main/hello-world
Hello world
Mon Feb  7 18:19:17 2022

参考文档:

https://docs.bazel.build/versions/4.2.1/skylark/concepts.html

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-02-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 灰子学技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档