xingao | ecbf651 | 2018-10-02 07:28:15 -0700 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
Googler | 817e220 | 2020-11-20 19:14:47 -0800 | [diff] [blame] | 3 | title: Bazel container |
xingao | ecbf651 | 2018-10-02 07:28:15 -0700 | [diff] [blame] | 4 | --- |
| 5 | |
Googler | 817e220 | 2020-11-20 19:14:47 -0800 | [diff] [blame] | 6 | # Getting Started with Bazel Docker Container |
xingao | ecbf651 | 2018-10-02 07:28:15 -0700 | [diff] [blame] | 7 | |
Googler | cb395cb | 2020-12-21 10:33:20 -0800 | [diff] [blame] | 8 | This page provides details on the contents of the Bazel container, how to build |
| 9 | the [abseil-cpp](https://github.com/abseil/abseil-cpp) project using Bazel |
| 10 | inside the Bazel container, and how to build this project directly |
| 11 | from the host machine using the Bazel container with directory mounting. |
xingao | ecbf651 | 2018-10-02 07:28:15 -0700 | [diff] [blame] | 12 | |
| 13 | ## Build Abseil project from your host machine with directory mounting |
| 14 | |
| 15 | The instructions in this section allow you to build using the Bazel container |
| 16 | with the sources checked out in your host environment. A container is started up |
| 17 | for each build command you execute. Build results are cached in your host |
| 18 | environment so they can be reused across builds. |
| 19 | |
| 20 | Clone the project to a directory in your host machine. |
| 21 | |
| 22 | ```bash |
| 23 | git clone https://github.com/abseil/abseil-cpp.git /src/workspace |
| 24 | ``` |
| 25 | |
| 26 | Create a folder that will have cached results to be shared across builds. |
| 27 | |
| 28 | ```bash |
| 29 | mkdir -p /tmp/build_output/ |
| 30 | ``` |
| 31 | |
| 32 | Use the Bazel container to build the project and make the build |
| 33 | outputs available in the output folder in your host machine. |
| 34 | |
| 35 | ```bash |
| 36 | docker run \ |
| 37 | -e USER="$(id -u)" \ |
| 38 | -u="$(id -u)" \ |
| 39 | -v /src/workspace:/src/workspace \ |
| 40 | -v /tmp/build_output:/tmp/build_output \ |
| 41 | -w /src/workspace \ |
Rgis Dcamps | 223ef9d | 2019-12-21 17:15:59 -0800 | [diff] [blame] | 42 | l.gcr.io/google/bazel:latest \ |
xingao | ecbf651 | 2018-10-02 07:28:15 -0700 | [diff] [blame] | 43 | --output_user_root=/tmp/build_output \ |
| 44 | build //absl/... |
| 45 | ``` |
| 46 | |
| 47 | Build the project with sanitizers by adding the --config=<asan/tsan/msan> build |
| 48 | flag to select AddressSanitizer (asan), ThreadSanitizer (tsan) or |
| 49 | MemorySanitizer (msan) accordingly. |
| 50 | |
| 51 | ```bash |
| 52 | docker run \ |
| 53 | -e USER="$(id -u)" \ |
| 54 | -u="$(id -u)" \ |
| 55 | -v /src/workspace:/src/workspace \ |
| 56 | -v /tmp/build_output:/tmp/build_output \ |
| 57 | -w /src/workspace \ |
Rgis Dcamps | 223ef9d | 2019-12-21 17:15:59 -0800 | [diff] [blame] | 58 | l.gcr.io/google/bazel:latest \ |
xingao | ecbf651 | 2018-10-02 07:28:15 -0700 | [diff] [blame] | 59 | --output_user_root=/tmp/build_output \ |
| 60 | build --config=<asan/tsan/msan> -- //absl/... -//absl/types:variant_test |
| 61 | ``` |
| 62 | |
| 63 | ## Build Abseil project from inside the container |
| 64 | |
| 65 | The instructions in this section allow you to build using the Bazel container |
Rgis Dcamps | 223ef9d | 2019-12-21 17:15:59 -0800 | [diff] [blame] | 66 | with the sources inside the container. By starting a container at the beginning |
| 67 | of your developement workflow and doing changes in the worskpace within the |
| 68 | container, build results will be cached. |
| 69 | |
| 70 | Start a shell in the Bazel container: |
| 71 | |
| 72 | ```bash |
| 73 | docker run --interactive --entrypoint=/bin/bash l.gcr.io/google/bazel:latest |
| 74 | ``` |
| 75 | |
| 76 | Each container id is unique. In the instructions bellow, the container was 5a99103747c6. |
xingao | ecbf651 | 2018-10-02 07:28:15 -0700 | [diff] [blame] | 77 | |
| 78 | Clone the project. |
| 79 | |
| 80 | ```bash |
| 81 | root@5a99103747c6:~# git clone https://github.com/abseil/abseil-cpp.git && cd abseil-cpp/ |
| 82 | ``` |
| 83 | |
| 84 | Do a regular build. |
| 85 | |
| 86 | ```bash |
| 87 | root@5a99103747c6:~/abseil-cpp# bazel build //absl/... |
| 88 | ``` |
| 89 | |
Rgis Dcamps | 223ef9d | 2019-12-21 17:15:59 -0800 | [diff] [blame] | 90 | Build the project with sanitizers by adding the `--config=<asan/tsan/msan>` build |
xingao | ecbf651 | 2018-10-02 07:28:15 -0700 | [diff] [blame] | 91 | flag to select AddressSanitizer (asan), ThreadSanitizer (tsan) or |
| 92 | MemorySanitizer (msan) accordingly. |
| 93 | |
| 94 | ```bash |
| 95 | root@5a99103747c6:~/abseil-cpp# bazel build --config=<asan/tsan/msan> -- //absl/... -//absl/types:variant_test |
| 96 | ``` |
| 97 | |
| 98 | ## Explore the Bazel container |
| 99 | |
Rgis Dcamps | 223ef9d | 2019-12-21 17:15:59 -0800 | [diff] [blame] | 100 | If you haven't already, start an interactive shell inside the Bazel container. |
xingao | ecbf651 | 2018-10-02 07:28:15 -0700 | [diff] [blame] | 101 | |
| 102 | ```bash |
Rgis Dcamps | 223ef9d | 2019-12-21 17:15:59 -0800 | [diff] [blame] | 103 | docker run -it --entrypoint=/bin/bash l.gcr.io/google/bazel:latest |
xingao | ecbf651 | 2018-10-02 07:28:15 -0700 | [diff] [blame] | 104 | root@5a99103747c6:/# |
| 105 | ``` |
| 106 | |
| 107 | Explore the container contents. |
| 108 | |
| 109 | ```bash |
| 110 | root@5a99103747c6:/# clang --version |
| 111 | clang version 8.0.0 (trunk 340178) |
| 112 | Target: x86_64-unknown-linux-gnu |
| 113 | Thread model: posix |
| 114 | InstalledDir: /usr/local/bin |
| 115 | |
| 116 | root@5a99103747c6:/# java -version |
| 117 | openjdk version "1.8.0_181" |
| 118 | OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.16.04.1-b13) |
| 119 | OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode) |
| 120 | |
| 121 | root@5a99103747c6:/# python -V |
| 122 | Python 2.7.12 |
| 123 | |
| 124 | root@5a99103747c6:/# python3 -V |
| 125 | Python 3.6.6 |
| 126 | |
| 127 | root@5a99103747c6:/# bazel version |
| 128 | Extracting Bazel installation... |
| 129 | Build label: 0.17.1 |
| 130 | Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar |
| 131 | Build time: Fri Sep 14 10:39:25 2018 (1536921565) |
| 132 | Build timestamp: 1536921565 |
| 133 | Build timestamp as int: 1536921565 |
| 134 | ``` |