Search code examples
buck

How to specify the gtest dependency for a cxx_test target when using Buck


I am trying to convert an existing googletest testcase to be built using Buck. This looks like it is straight forward using the cxx_test build target.

However, when trying to build I get an error:

BUILD FAILED: .buckconfig: cxx:gtest_dep must be set

My question is what should that .buckconfig setting be set to? The path to the googletest repo? Built .so or .a files? Looking at the source code it seems like it needs to be another Buck build target. Is there an example of a working Buck cxx_test target somewhere?


Solution

  • You should specify a build target that points to the location of the gtest library in your repository. For example, you might put it in third-party/cxx/google-test and your BUCK file in that directory would look something like this:

    import os
    def subdir_glob(glob_specs):
      """
      Given a list of tuples, the form of (relative-sub-directory, glob-pattern),
      return a dict of sub-directory relative paths to full paths.  Useful for
      defining header maps for C/C++ libraries which should be relative the given
      sub-directory.
      """
    
      results = {}
    
      for dirpath, glob_pattern in glob_specs:
        files = glob([os.path.join(dirpath, glob_pattern)])
        for f in files:
          if dirpath:
            results[f[len(dirpath) + 1:]] = f
          else:
            results[f] = f
    
     return results
    
    cxx_library(
      name = 'google-test',
      srcs = glob(['src/**/*.cc'], excludes=['src/gtest-all.cc']),
      # Not all compilers support <tr1/tuple>, so have gtest use it's
      # internal implementation.
      exported_preprocessor_flags = [
        '-DGTEST_USE_OWN_TR1_TUPLE=1',
      ],
      header_namespace = '',
      exported_headers = subdir_glob([
        ('', 'src/**/*.h'),
        ('include', '**/*.h'),
      ]),
      deps = [
        ':pthread',
      ],
      visibility = [
        'PUBLIC',
      ],
    )
    
    # libpthread is implicitly included in the android runtime so, when building
    # on an android platform, we don't do anything.
    prebuilt_cxx_library(
      name = 'pthread',
      header_only = True,
      platform_linker_flags = [
        ('android', []),
        ('', ['-lpthread']),
      ],
      visibility = [
        'PUBLIC',
      ],
    )

    Then in your .buckconfig, you'll have this:

    [cxx]
      gtest_dep = //third-party/cxx/google-test:google-test

    Note that subdir glob is something that Buck is likely to provide in the future.