### Introduction

bazelizing, bazelization, to bazelize, bazelized: The act of converting some existing software artifact to be easily consumed by the Bazel build tool

Embree is middleware for ray tracing that computes intersections points between rays and different shape types. The source code of Embree is under the Apache 2.0 license and hosted on GitHub. Embree targets CPU ray tracing and contains special code to make use of SSE, AVX, AVX2, and AVX-512 instructions if the CPU has supports it.

This post will cover how I bazelized Embree.

Luckily I was not the first person who tried to bazelise Embree. I found this project here that targets Embree version 2.16.5. I tested the project and it seems to work on Windows 10 x64 with Visual Studio 2019, but not on Ubuntu 20.04 using GCC.

I did not found any other attempt or project that makes use of Bazel and Embree. If you know any other open source project that can be shared here, drop me a mail or post a comment.

### Own attempt

Since I wanted to make it also work with Ubuntu 20.04 I started to come up with my own bazalization and decided to take the newest state of Embree.

I took the current state of the master branch (head when writting this was commit 69bd4c272f1ed608494f233ecfff3feec516880b).

The first thing I did was to try to understand what is needed to build Embree. I come up with the following dependency graph:

Unfortunately, the graph contains also some type of cyclic dependencies. For example, simd includes some header files that belong to sys and math. Maybe I misunderstand here something, but for me it looks a bit unclean. Anyway it is not a blocker. Maybe this shows where a build system like Bazel can show you design flaws that are hidden by CMake.

The dependency graph was derived from the CMakeLists.txt files that are part of Embree. The CMake generated Visual Studio 2019 x64 project looks like this:

When Bazelizing a library there are different options. You could add to every folder a BUILD.bazel file. I decided to go for a solution that does not pollute the Embree source code folder too much. Therefore I decided to have only one BUILD file, that contains everything. I think this is a good option here since the main build system is still CMake and many BUILD.bazel files at different places might only confuse people that are not familiar with Bazel.

In my first attempt to bazelize everything I wanted to include several kernels of Embree (AVX, AVX2, SSE42) and some other optional options. I run into some trouble and compiler errors and decided then to go for a minimal Embree build, without optional stuff. I deactivated all ISAs except SSE2. Here you can see the configuration that I tried to meet:

Finally, I succeded in Building Embree for Windows. I got the build also working for Ubuntu.

My current status can be found here.

## Future work

Activation of other kernels (AVX, AVX2, etc.) is an open point on the todo list.

It would also make sense to use not the internal tasking library provided by embree, but instead the newest version on Intel oneTBB library. I did already a port for this here.

It seems that some guys did also an investigation on poring Embree to GPU. This would be also a very interesting extension point to follow.