Title: Theory / Important / CS / Programming / Build / Bazel
Tags: automatedNewLineAfterCommaSpace type variable computer_to_instruction declaration empty_card_examples todo_declaration

Name: Bazel
Definition: Build system created at Google
Page: https://bazel.build/

Question: Advantages of bazel?
Answer: Speed up your builds and tests
Bazel only rebuilds what is necessary. With advanced local and distributed caching, optimized dependency analysis and parallel execution, you get fast and incremental builds.
Bazel caches all previously done work and tracks changes to both file content and build commands. This way, Bazel knows when something needs to be rebuilt, and rebuilds only that. To further speed up your builds, you can set up your project to build in a highly parallel and incremental fashion.
Page: https://bazel.build/
https://docs.bazel.build/versions/4.0.0/bazel-overview.html
Answer2: One tool, multiple languages
Build and test Java, C++, Android, iOS, Go and a wide variety of other language platforms. Bazel runs on Windows, macOS, and Linux.
Answer3: Scalable
Bazel helps you scale your organization, codebase and Continuous Integration system. It handles codebases of any size, in multiple repositories or a huge monorepo.
Answer4: Extensible to your needs
Easily add support for new languages and platforms with Bazel's familiar extension language. Share and re-use language rules written by the growing Bazel community.
Answer5: High-level build language. Bazel uses an abstract, human-readable language to describe the build properties of your project at a high semantical level. Unlike other tools, Bazel operates on the concepts of libraries, binaries, scripts, and data sets, shielding you from the complexity of writing individual calls to tools such as compilers and linkers.

Name: BUILD file
Definition: import of build rules in starlark
Page: https://docs.bazel.build/versions/4.0.0/bazel-overview.html
Context: Bazel
Definition2: build target
Conjdef: A list of

Name: BUILD targets
Definition: specifies a set of input artifacts that Bazel will build plus their dependencies, the build rule Bazel will use to build it, and options that configure the build rule.
Page: https://docs.bazel.build/versions/4.0.0/bazel-overview.html
Context: Bazel

Name: BUILD rule
Definition: specifies the relationship between a set of input and a set of output files, including the necessary steps to derive the outputs from the inputs
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html#dependencies
Context: Bazel

Question: How does Bazel work?
When running a build or a test, Bazel does the following:
Answer: Loads the BUILD files relevant to the target.
Page: https://docs.bazel.build/versions/4.0.0/bazel-overview.html
Context: Bazel
Answer2: Analyzes the inputs and their dependencies, applies the specified build rules, and produces an action graph.
Answer3: Executes the build actions on the inputs until the final build outputs are produced.

Name: action graph
Definition: represents the build artifacts, the relationships between them, and the build actions that Bazel will perform.
Page: https://docs.bazel.build/versions/4.0.0/bazel-overview.html
Context: Bazel
Extra: Thanks to this graph, Bazel can track changes to file content as well as changes to actions, such as build or test commands, and know what build work has previously been done. The graph also enables you to easily trace dependencies in your code.

Name: Workspace
Definition: A directory contains a WORKSPACE field excluding subdirectories which also contains a WORKSPACE field
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel
Intuition: that contains the source files for the software you want to build, as well as symbolic links to directories that contain the build outputs

Name: Root repository
Definition: The top level repository
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel workspace
Notation: @

Name: Package
Definition: containing a file named BUILD or BUILD.bazel
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel workspace
Definition2: residing beneath the top-level directory in the workspace
Conjdef: a directory
Definition3: Excluding subdirectories containing a BUILD file
Intuition: a collection of related files and a specification of the dependencies among them.

Question: Kind of targets ?
Answer: File
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel
Answer2: rules
Answer3: groups

Question: Kind of files ?
Answer: source
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel
Answer2: generated

Name: Source file
Definition: File checked in the repository
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel
Definition2: generally created by the efforts of people

Name: Generated file
Definition: generally not checked in the repository
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel
Definition2: generated by the build tool from source files according to specific rules
Conjdef: File
Name2: Derived file

Question: Rule input
Answer: files
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel
Answer2: other rules

Question: Rule output
Answer: files in the same package
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel

Name: Package group
Definition: A sets of package whose purpose is to limit accessibility of certain rules
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel

Variable: default_visibility
Meaning: The visibility of the file of a package when no other rules are defined
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel package

Name: Label
Definition: Name of a target
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel package

Instruction: @repository_name//package/names:target_name
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel label
Returns: The target target_name of package/names of repository_name

Question: What does it mean if target name is omitted from label
Answer: the target has the same name as the repository
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel label

Question: What does it mean if repository is omitted from label
Answer: current repository is used
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel label

Question: What does it mean if package is omitted from label
Answer: current package is used
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel label

Name: Package name
Definition: the name of the directory containing its BUILD file, relative to the top-level directory of the source tree
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel label

Name: srcs
Definition: a list of labels, each being the name of a target that is an input to this rule.
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel rule
Typ: list of labels

Name: outs
Definition: a list of labels, each being the name of a generated file of this rule
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel rule
Typ: list of output labels

Name: Target graph
Definition: nodes are target
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel rule
Definition2: edges are dependencies
Conjdef: Graph
Name2: Build dependency graph

Name: Starlark
Definition: Imperative language used for bazel build file
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: Bazel rule

Extension: .bzl
Description: Starlark file
Means: bazel
Other fields (not shown on cards):
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html

Instruction: load(
  "foo",
  "bar_1",
  ..., "bar_n"
)
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Effect: Add symbols bar_1, ..., bar_n, from file foo, in the environment
Language: bazel

Instruction: load(
  "foo",
  plop = "bar"
)
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Effect: Create a symbol plop whose value is the value of bar in file foo
Language: bazel

Variable: symbol starting with _
Meaning: symbol which can not be exported
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Language: bazel

Question: How are rules named?
Answer: language_goal, e.g cc_binary or java_test
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule

Name: *_binary
Definition: build executable programs in a given language. After a build, the executable will reside in the build tool's binary output tree at the corresponding name for the rule's label
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule
Extra: //my:program would appear at (e.g.) $(BINDIR)/my/program.

Name: *_test
Definition: a specialization of a *_binary rule, used for automated testing. Tests are simply programs that return zero on success.
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule

Variable: data
Meaning: All files actually required to run some build.
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule
Example: expected output of a function may be saved in a file to create a unit test

Definition: *_library
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule
Definition2: specify separately-compiled modules in the given programming language.

Name: A depends on B
Definition: The target B is required to execute target A
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule

Name: A actually depends on B
Definition: The target B must be present, built and up-to-date in order for A to be built correctly
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule

Name: A has a declared dependency on B
Definition: The package state that A requires the package B
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule

Question: Relation between the garph of actual dependency and the graph of declared dependency?
Answer: The graph of actual dependencies must be a subgraph of the graph of declared dependencies
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule

Question: Why to avoid to declare useless dependencies?
Answer: It makes build slower by building useless targets
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule
Answer2: Graph of dependencies must be acyclic. If there is a declared dependency from A to B, an actual dependency from B to A can't be added

Question: Why should a dependency from A to C be listed, even if A already depends on B which depends on C
Answer: While build works succesfully in the hypothesis, it will breaks as soon as A stops depending on B or B on C
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule

Variable: deps
Meaning: Rule pointing to separately-compiled modules providing header files, symbols, libraries, data, etc.
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule

Question: How to add an entire directory to data ?
Answer: Use the label "directory_name/**", where "**" represents recursive expension in globbing
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule

Question: How to add a data file whose name does not respect bazel names syntax? E.g. with comma
Answer: import the entire dictionnary
Page: https://docs.bazel.build/versions/4.0.0/build-ref.html
Context: bazel rule

Name: External dependencies
Definition: targets from other projects.
Page: https://docs.bazel.build/versions/4.0.0/external.html
Context: bazel

Name: Repository rule
Definition: A rule in WORKSPACE explaining how to get external dependencies
Page: https://docs.bazel.build/versions/4.0.0/external.html
Context: bazel
Name2: Workspace rule

Instruction: local_repository
Page: https://docs.bazel.build/versions/4.0.0/external.html
Effect: Add an external repository, using also Bazel, on the same computer
Context: bazel Repository rule

Instruction: git_repository
Page: https://docs.bazel.build/versions/4.0.0/external.html
Effect: Add an external repository, using also Bazel, from a git repo
Context: bazel Repository rule

Instruction: http_archive
Page: https://docs.bazel.build/versions/4.0.0/external.html
Effect: Add an external repository, using also Bazel, from a http archive
Context: bazel Repository rule

Instruction: new_local_repository
Page: https://docs.bazel.build/versions/4.0.0/external.html
Effect: Add a repository to a non-bazel project on same computer
Context: bazel Repository rule

Instruction: fetch target
Page: https://docs.bazel.build/versions/4.0.0/external.html
Effect: Fetch all external dependencies for target
Context: bazel

Instruction: sync
Page: https://docs.bazel.build/versions/4.0.0/external.html
Effect: Fetch all external dependencies
Context: bazel

Instruction: build target
Page: https://docs.bazel.build/versions/4.0.0/external.html
Effect: Build the target and all values it depends on
Context: bazel

Instruction: sha256 = foo
Page: https://docs.bazel.build/versions/4.0.0/external.html
Effect: The target is expected to have sha256 value foo
Context: bazel external dependencies

Instruction: repo_mapping = {"foo": "bar"}
Page: https://docs.bazel.build/versions/4.0.0/external.html
Effect: The extrenal repository foo has name bar internally
Context: bazel external dependencies

Question: How to deal with transitive dependancies
Answer: They all must be explicitly listed. This ensure that a single version is used
Page: https://docs.bazel.build/versions/4.0.0/external.html
Context: bazel

Page: https://docs.bazel.build/versions/4.0.0/external.html
Implementation: Repository to save external dependencies (symlinks are used for local ones)
Context: bazel repository
Instruction: ./external

Fla: --nofetch
Effect: Do not fetch external dependencies
Page: c https://docs.bazel.build/versions/4.0.0/external.html
Prefix: bazel

Question: Which external rule to prefer between http and git ?
Answer: http. Allows mirror and built in bazel
Page: c https://docs.bazel.build/versions/4.0.0/external.html
Context: bazel

Question: What are repository's rule responsible for ?
Answer: - Detecting system settings and writing them to files
Page: c https://docs.bazel.build/versions/4.0.0/external.html
Context: bazel
Answer2: - Finding resources elsewhere on the system
Answer3: - Downloading resources from URLs
Answer4: - Generating or symlinking BUILD files into the external repository directory.

Name: Configurable build attribute
Definition: A rule that depends on some configuration option
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Context: Bazel
Example: Different compiler depending on architecture

Instruction: select (
  {"foo_1": bar_1,
  ..., "foo_n": bar_n,
  "//conditions:default": plop}
)
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Context: Bazel
Returns: bar_i if foo_i is the first truthy value, else plop

Instruction: config_setting (
  name="foo",
  values = {
    "bar_1": "plop_1",
    ...
    "bar_n": "plop_n",
  }
)
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Context: Bazel
Returns: foo becomes truthy if blaze is build with flag bar_i with value plop_i for each i

Fla: --cpu=foo
Effect: State that build is done for cpu foo
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Prefix: bazel

Name: Build-in flag
Definition: A flag known by bazel by default
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Context: Bazel

Name: Custom flag
Definition: A flag defined in a build file
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Context: Bazel

Instruction: config_setting(
    name = "foo",
    constraint_values = [
        ":bar1",
        ...
        ":barn",
    ],
)
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Effect: Define a configuration type foo whose values are one of the bar_i
Context: Bazel

Instruction: constraint_setting(name = "foo")
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Effect: Define a configuration type foo whose values are to be specified later
Context: Bazel

Instruction: constraint_value(
  name = "name",
  constraint_setting = "setting"
)
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Effect: State that name is a value of type setting
Context: Bazel

Question: How to deal with nested select ?
Answer: Create an intermediary target
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Context: Bazel

Instruction: foo = bar
Page: https://docs.bazel.build/versions/4.0.0/configurable-attributes.html
Meaning: Introduce foo as a variable representing the value bar
Context: Bazel