Building the Chromium/Base Library

There's many general purpose C++ utility libraries in the wild, and a lot of them implement functionality "the old way" from the dark ages before C++11/14. Writing new applications without some of the nice functionality in C++11 can be a pain, but Chromium's libbase contains a number of shims to support new-ish features on older compilers. There's a slight irony the developers stated that the library was never intended to be used outside of Chromium, but then again it doesn't try to replace something like Boost, either.

If you need some context about the type of functionality libbase gives you, take a gander at the master branch. Specifically, check out the classes for atomics, callbacks, scoped pointers (and containers), and the cross-platform string/path utilities -- they're all quite readable.

The process to get the base library up and running (libbase) is heavy since it uses the same build system as Chromium/Chrome (GYP, Ninja). This approach requires a first-time sync with the Chromium codebase (~5 GB). Luckily, libbase is the root dependency for many Chromium modules, so the build and link is quick.

OSX 10.8.4 + XCode 5.0

As for dependencies: XCode 5 + XCode Command Line Tools. I made sure Brew was up to date and upgraded Git and SVN to their latest available versions. Make sure you also have Python 2.7. Keep in mind that all of these versions are sure to change with time.

Generate Your Project (GYP) is a build tool used by Chrome and other Google projects (like V8, and later adopted by Node.js). Download and install like so:

#> git clone https://chromium.googlesource.com/external/gyp.git
#> cd gyp/
#> sudo python setup.py install

Now it's time to create the directory where you want to put everything:

#> mkdir ~/Chromium
#> cd ~/Chromium

Chromium uses a set of utilities called Depot Tools to help the build process. Clone Depot Tools and use the bundled gclient tool:

#> git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
#> export PATH="$PATH":`pwd`/depot_tools
#> gclient config https://src.chromium.org/svn/trunk/src https://chromium-status.appspot.com/lkgr

Edit the .gclient file to specify which dependencies shouldn't be used (because they aren't useful to us):

solutions = [
  { "name"        : "src",
    "url"         : "https://src.chromium.org/svn/trunk/src",
    "deps_file"   : "DEPS",
    "managed"     : True,
    "custom_deps" : {
    "src/third_party/WebKit/LayoutTests": None,
    "src/chrome/tools/test/reference_build/chrome": None,
    "src/chrome_frame/tools/test/reference_build/chrome": None,
    "src/chrome/tools/test/reference_build/chrome_linux": None,
    "src/chrome/tools/test/reference_build/chrome_mac": None,
    "src/third_party/hunspell_dictionaries": None,
    },
    "safesync_url": "https://chromium-status.appspot.com/lkgr",
  },
]

At this stage everything is set up to download the Chromium code! One thing to note here is the Chromium Buildbot. Google has an epic continuous integration system to keep track of build status. If the bar at the top is green, you're good to go. If it's red, hunt around the web interface to figure out what the last building revision was.

#> gclient sync

Use this command to checkout an older working revision if the tree isn't green:

#> gclient sync --revision src@270334

With this command, there was an error about accepting the SSL ceritificate. If this happens, run svn list first to permanently accept the certificate.

#> svn list https://src.chromium.org/svn/trunk/src

After the ~5gb checkout, most next steps are related to build configuration. We can be confident from Google's Buildbot that it's likely going to work.

First, the gyp bootstrapping script (located in /build/gyp_chromium) needs to generate all the makefiles to build the project and libraries. This is the command that needs to be run every time you want to specify a new architecture or library output (dynamic or static).

The default settings (no arguments) will let us build libbase as a static library for i386.

#> GYP_GENERATORS=ninja GYP_DEFINES=mac_sdk=10.8 ./build/gyp_chromium

From here, ninja can be used to initiate the build. GYP has configurations for all of the Chromium libraries and the application itself, so it's just a matter of specifying what we want to build:

#> ninja -C out/Debug base

Magic! This should have generated libbase.a as an i386 debug static library in the Chromium/out/Debug directory. If you'd prefer to use x86_64 or create a dynamic library, regenerate the configuration with some additional defines:

#> GYP_GENERATORS=ninja GYP_DEFINES=mac_sdk=10.8 ./build/gyp_chromium -Dtarget_arch=x64 -Dhost_arch=x64 -Dcomponent=shared_library

Running Ninja again with the release directory will build libbase.dylib for x86_64:

   #> ninja -C out/Release base

Consuming the Library

Since Chromium keeps includes and source files in the same directory (for me it's ~/Chromium/base), we want to keep the header files in another location for our own projects. I made a new folder to keep the static library and include files like so:

~/CodeUtils
    /libbase_chromium
        /include
            - (copy all .h files from ~/Chromium/base here)
        /lib
            /release
                libbase.a
            /debug
                libbase.a

After this, it's typical library stuff. Add include directory to include paths. Add libbase.a to a project and link against it. There's one caveat: the library will only work for applications compiled with libstdc++, not the LLVM 5.0 default libc++.

Windows 8.1 + Visual Studio 2013

The process is largely the same as OSX. The build toolchain is based on Visual Studio 2013, though you don't need it installed separately. The bootstrapping script (gclient sync) will install VS2013 as a locally contained environment specifically for Chromium. With all the extra dependencies, the whole Chromium folder will end up around ~14.0 gb.

All the same gyp defines are applicable to the Windows build as well:

#> GYP_GENERATORS=ninja ./build/gyp_chromium -Dtarget_arch=x64 -Dhost_arch=x64 -Dcomponent=shared_library

Wrapping Up

Also, have you ever tried to search for any other sane alternative to libcurl? It should also be noted that the net/ and crypto/ chromium libraries can be compiled with this same process.

Happy hacking!

Comment -

Profile Image

Dimitri Diakopoulos

www.dimitridiakopoulos.com

Creative Technologist @ Intel. Opinions on here are my own.