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
- Host: GitHub
- URL: https://github.com/kabragaurav/bazel-fundamentals
- Owner: kabragaurav
- Created: 2024-04-23T07:39:37.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-14T07:54:10.000Z (almost 2 years ago)
- Last Synced: 2025-01-21T19:24:41.782Z (over 1 year ago)
- Topics: bazel, bazel-examples
- Language: Starlark
- Homepage: https://quip.com/U7rcAwFLIRxH/Bazel-Fundamentals
- Size: 936 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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

## 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)