使用 Buck 时如何为 cxx_test 目标指定 gtest 依赖项

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

我正在尝试转换现有的 googletest testcase to be built using Buck. This looks like it is straight forward using the cxx_test 构建目标。

但是,在尝试构建时出现错误:

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

我的问题是 .buckconfig 设置应该设置成什么? googletest repo 的路径?构建 .so 或 .a 文件?查看源代码,它似乎需要成为另一个 Buck 构建目标。某处是否有工作 Buck cxx_test 目标的示例?

您应该指定一个 build target 指向存储库中 gtest 库的位置。例如,您可以将它放在 third-party/cxx/google-test 中,而您在该目录中的 BUCK 文件将如下所示:

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',
  ],
)

然后在您的 .buckconfig 中,您将拥有:

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

请注意,subdir glob 是 Buck 将来可能提供的东西。