An open API service indexing awesome lists of open source software.

https://github.com/kabragaurav/bazel-fundamentals

Bazel Fundamentals With Theory + Examples
https://github.com/kabragaurav/bazel-fundamentals

bazel bazel-examples

Last synced: about 1 year ago
JSON representation

Bazel Fundamentals With Theory + Examples

Awesome Lists containing this project

README

          

### Notes can be found [here](https://quip.com/U7rcAwFLIRxH/Bazel-Fundamentals).

---

### Running
```
bazel --version # bazel 7.0.1
```
#### C++ Project
```
cd cpp-project
bazel run //greeting:helloworld
```

#### Java Project
```
cd java-project
bazel run //:ProjectRunner
bazel run //:salutations
bazel run //:restclient
```

Queries:
```
cd java-project
bazel run //:runner
bazel query //src/main/java/com/query/dishes/...
bazel query --noimplicit_deps "deps(:runner)"
bazel query "rdeps(//... , //src/main/java/com/query/ingredients:salt)"
bazel query 'attr(tags, "hcp", //src/main/java/com/query/customers/...)'
```

#### Python3 Project
```
cd py-project/app
bazel run //app:main
```

#### Rules
Gen Rules
```
cd rules/gen-rules
bazel build //:concat # See output (target) in bazel-bin/concat.txt
bazel build //:process_data # See output (target) in bazel-bin/output.txt
bazel build //:copy_files # See output (target) in bazel-bin/copy_file.txt
bazel build //:cat_files # See output (target) in bazel-bin/cat_files.txt
```

Custom Rules

`name` in `BUILD` is the target
```
cd rules/custom-rules
bazel build //:bin1 # See output (target) in bazel-bin/bin1
bazel build //:bin2 # See output (target) in bazel-bin/bin2
bazel build //:bin # See output (target) in bazel-bin/bin
bazel run //:say_hello_to_gaurav # This is equivalent to bazel build //:hello-gaurav; bazel run //:say_hello_to_gaurav
```

#### Macros
```
cd macros
bazel build //:hello_world_code_dive # See output in bazel-bin/hello_world_code_dive
bazel build //:hello_world_everyone # See output in bazel-bin/hello_world_everyone
```

### Visualize dependencies
```
bazel query --notool_deps --noimplicit_deps "deps(//:*)" --output graph
```

Sample here

![Dependency graph](./assets/images/dependencies.svg)

## Some Advanced Concepts

#### Queries
```
cd rules/gen-rules
bazel aquery 'deps(//:concat)' # action query
bazel cquery 'deps(//:concat)' # configurable query
```

#### Toolchain
```
cd pythontoolchain
bazel run //:bin # It should print 3.10 (version mentioned in WORKSPACE) but in latest bazel version, it takes system version like 3.12.2 but you will see 3.10 version is being downloaded
```

### Links
- [Hermetic Python Toolchain with Bazel](https://www.anthonyvardaro.com/blog/hermetic-python-toolchain-with-bazel).
- [Writing custom Bazel C++ rules](https://ltekieli.com/writing-custom-bazel-c-rules/)
- [Writing Bazel rules: library rule, depsets, providers](https://jayconrod.com/posts/107/writing-bazel-rules--library-rule--depsets--providers)
- [User-defined build settings for Bazel builds](https://www.grahambrooks.com/software-development/2021/08/30/user-defined-bazel-arguments.html)

For debugging,
- [Run Configurations IJ](https://ij.bazel.build/docs/run-configurations.html)
- [Bazel VS Marketplace](https://marketplace.visualstudio.com/items?itemName=BazelBuild.vscode-bazel)