Getting Started with Bazel ========================== Setup ----- Every Bazel project is contained in a directory called a _build root_, which holds the inputs, outputs, and build rules for the project. To create a Bazel project, first clone the [Github repo](https://github.com/google/bazel) and build Bazel (follow the instructions in the [README](install.md) to install prerequisites): ```bash $ git clone https://github.com/google/bazel.git $ cd bazel $ ./compile.sh ``` `./compile.sh` populates the _base_workspace_ subdirectory with the tools Bazel needs to do builds. Suppose that you have an existing project in a directory, say, _~/gitroot/my-project/_. Recursively copy _base_workspace/_ and all of its contents to wherever you'd like your build root and then move _my-project/_ to be a subdirectory of _base_workspace/_: ```bash $ cp -R ~/gitroot/bazel/base_workspace ~/gitroot $ mv ~/gitroot/my-project ~/gitroot/base_workspace ``` At this point, you should have the following directory structure: ``` base_workspace/ examples/ my-project/ tools/ WORKSPACE ``` You can rename _base_workspace/_ to something more descriptive, if you prefer. Sanity Check: Building an Example --------------------------------- To make sure everything is set up correctly in your build root, build one of the examples from the _examples/_ directory. ```bash $ cd ~/gitroot/base_workspace $ bazel build examples/java:hello-world Extracting Bazel installation... ........... INFO: Found 1 target... Target //examples/java:hello-world up-to-date: bazel-bin/examples/java/hello-world.jar bazel-bin/examples/java/hello-world INFO: Elapsed time: 3.040s, Critical Path: 1.14s $ bazel-bin/examples/java/hello-world Hello world ``` Bazel puts binaries it has built under _bazel-bin/_. Note that you can always look at the `build` command's output to find output file paths. Creating Your Own Build File ---------------------------- Now you can create your own BUILD file and start adding build rules. This example assumes that _my-project/_ is a Java project. See the [build encyclopedia](build-encyclopedia.html) for advice on adding build rules for other languages. Note that when we ran "bazel build" above, the third argument started with a filesystem path ("examples/java"), followed by a colon. When you run `bazel build examples/java:hello-world`, Bazel will look for a special file named BUILD in the _examples/java/_ subdirectory. This BUILD file defines rules about how Bazel should build things in this subdirectory. Thus, to add build rules to my-project, create a file named `BUILD` in the _my-project/_ directory. Add the following lines to this BUILD file: ```python # ~/gitroot/base_workspace/my-project/BUILD java_binary( name = "my-runner", srcs = glob(["**/*.java"]), main_class = "com.example.ProjectRunner", ) ``` BUILD files are Python-like scripts. BUILD files cannot contain arbitrary Python, but each build rule looks like a Python function call and you can use "#" to start a single-line comment. `java_binary` is the type of thing this rule will build. `name` is how you'll refer to the rule when you run "bazel build" (in the "examples/java:hello-world" build above the `name` was "hello-world"). `srcs` lists the Java source files Bazel should compile into a Java binary. `glob(["**/*.java"])` is a handy shorthand for "recursively include every file that ends with .java" (see the [user manual](bazel-user-manual.html) for more information about globbing). Replace `com.example.ProjectRunner` with the class that contains the main method. If you have no actual Java project you're using, you can use the following commands to make a fake project for this example: ```bash $ # If you're not already there, move to your build root directory. $ cd ~/gitroot/base_workspace $ mkdir -p my-project/java/com/example $ cat > my-project/java/com/example/ProjectRunner.java < my-project/java/com/example/Greeting.java <greeter is unchanged). Using Multiple Packages ----------------------- For larger projects, you will often be dealing with several directories. You can refer to targets defined in other BUILD files using the syntax `//package-name:target-name`. For example, suppose _my-project/java/com/example/_ has a _cmdline/_ subdirectory with the following file: ```bash $ mkdir my-project/java/com/example/cmdline $ cat > my-project/java/com/example/cmdline/Runner.java <