Buck is a fast build tool developed by Facebook. There are many reasons to choose Buck, but how should you get started?

This walk-through will cover:

TL;DR

Browse the project files on GitHub, or clone them onto your system:

git clone [email protected]:njlr/buck-cpp-example.git

Install existing Buck ports from Buckaroo.pm.

How Buck Works

Before we start, it might be helpful to understand how Buck works. Rather than retread old ground, take a look at this talk from the Buck devs.

Ludicrously Fast Builds with Buck

Installing Buck

The official guide for installing Buck is available on their website. However, the quickest way is probably to use Homebrew or Linuxbrew.

Once you have a “brew” installed, add the Facebook tap and install Buck:

brew tap facebook/fbbrew install --HEAD facebook/fb/buck

Verify your installation with:

buck --version

Organizing Your Files

Once Buck is installed, it’s time to create the project folders. Buck makes it easy to support any folder structure you like, but for this demo we will be following the C/C++ convention of src and include folders.

This project will consist of two parts:

Note: This is a simple example for demonstration purposes; Buck is capabable of building complex C/C++ projects!

First, create the following structure somewhere on your drive:

.├── .buckconfig├── BUCK└── demo├── include└── src└── main.cpp

Using the command-line:

touch .buckconfigtouch BUCK

mkdir demomkdir demo/includemkdir demo/srctouch demo/src/main.cpp

That’s quite a few files — let’s run through them:

main.cpp

To get started, we will write a simple hello-world program. Paste the following into main.cpp:

BUCK

To build the main.cpp file, we need to write a Buck target for it. Into the BUCK file, paste the following:

BUCK files are written in a dialect of Python 2, with a few extensions. When you call buck build, Buck will execute the Python and record any targets the are defined. Once the list of targets has been resolved, each target is built in accordance with its type.

You can see the full list of target-types in the Buck docs, but the important ones for C/C++ are cxx_binary, cxx_library and prebuilt_cxx_library.

Buck Commands

Now that the BUCK file is in place, Buck can build the target. Run the following:

buck build //:demo

The command tells Buck to build the target :demo found in the BUCK file adjacent to .buckconfig.

Buck uses a simple addressing system for targets based on the actual folder-structure of the project. For example, //examples/basic/:demo refers to the target demo defined in examples/basic/BUCK.

After the build completes, you should find an executable at buck-out/gen/demo. You can build and run this using:

buck build //:demo && ./buck-out/gen/demo

Or, Buck can do it for you:

buck run //:buck-cpp-example

You will notice that running the build for a second time is extremely fast. This is because Buck caches everything, including the output of the Python scripts!

Adding a Dependency

Let’s implement mathutils so that we can use it in the demo application.

Create the following folder structure in your project:

.└── mathutils├── BUCK├── include│ └── add.hpp└── src└── add.cpp

Using the command-line:

mkdir mathutilsmkdir mathutils/includemkdir mathutils/src

touch mathutils/BUCKtouch mathutils/include/add.hpptouch mathutils/src/add.cpp

And the files themselves:

There are a few important points about this BUCK file:

Using the Add Function

Now we can use the mathutils library in the demo executable.

First, declare the dependency of demo on mathutils. Change the BUCK file at the root of the project to:

Now update main.cpp to:

Use Buck to run demo to see the result. You will notice that Buck knows how to link mathutils for you.

Google Test

Our application is working, but to be diligent we should add some unit-tests!

Buck supports all C/C++ testing frameworks via buck run, but it provides additional integration with Google Test.

Fetching the Google Test Source-code

Git provides a simple way to grab the Google Test source-code using submodules. We will be using a fork that contains a BUCK file, but you can use the master and write your own if desired.

git submodule add [email protected]:njlr/googletest.gitcd googletest/git checkout 48072820e47a607d000b101c05d796ebf9c4aad2cd ../

Now we need to tell Buck where to find the Google Test sources. Open the .buckconfig and add the following:

This tells Buck where to find the Google Test target that it can use for your tests. There are other config properties that can be set; have a browse in the Buck docs.

Writing a Test

We will put the tests into a mathutils/test, alongsidemathutils/src and mathutils/include:

.└── mathutils├── BUCK├── include│ └── add.hpp├── src│ └── add.cpp└── test├── BUCK└── add.cpp

Using the command-line:

mkdir mathutils/testtouch mathutils/test/add.cpp

And the test itself:

Finally, we need to declare the test in the BUCK file:

Now the tests can be run by Buck:

buck test //mathutils/test:add

Or, to run all tests:

buck test

Conclusion

And that’s it! Buck is a powerful tool that will save you hours of waiting over the development cycle of a project. To learn more, read the docs or watch some of the Buck presentations.

If there is a library you need to port to Buck, take a look at Buckaroo.pm. We’ve already ported 300 projects, and are working on even more!