From 99336d62f016373ae9d327433fdfecb7592f593e Mon Sep 17 00:00:00 2001 From: Vinnie Magro Date: Wed, 29 Jan 2025 09:45:49 -0800 Subject: [PATCH] [antlir2][toolchain] easy test generation for deps tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: While it appears that we can link against `rpm_library` targets that have (but do not express) transitive dependencies, dependencies often still need to be correctly expressed for header `#include`s to work. This diff adds a simple way to define a test binary that will only compile if certain header files are able to be `#include`d. By default, no headers are included, but the binary will not launch if dependencies are not correctly specified. Test Plan: ``` ❯ buck uquery 'attrfilter(labels, antlir-distro-dep-test, fbcode//antlir/distro/deps/...)' | xargs buck test --target-platforms fbcode//antlir/distro/platform:centos9-x86_64 Buck UI: https://www.internalfb.com/buck2/29f47e8d-ebc9-4ce4-afcf-c0937666287a Test UI: https://www.internalfb.com/intern/testinfra/testrun/12384899041396371 Tests finished: Pass 42. Fail 0. Fatal 0. Skip 0. Build failure 0 ``` Perhaps more importantly, it fails when incorrect ``` ❯ hg diff diff --git a/fbcode/antlir/distro/deps/glog/BUCK b/fbcode/antlir/distro/deps/glog/BUCK --- a/fbcode/antlir/distro/deps/glog/BUCK +++ b/fbcode/antlir/distro/deps/glog/BUCK @@ -5,10 +5,10 @@ rpm_library( name = "glog", test_include_headers = [ - "glog/logging.h", + # "glog/logging.h", ], visibility = ["PUBLIC"], exported_deps = [ - "//antlir/distro/deps/gflags:gflags", + # "//antlir/distro/deps/gflags:gflags", ], ) ❯ buck2 test --target-platforms fbcode//antlir/distro/platform:centos9-x86_64 fbcode//antlir/distro/deps/glog: fbcode//antlir/distro/deps/jsoncpp: ✗ Fail: antlir/distro/deps/glog:glog--test-deps - main (0.0s) Test returned with non-zero exit code stdout: stderr: /data/users/vmagro/fbsource/buck-out/v2/gen/fbcode/6700a0e98152df5f/antlir/distro/deps/glog/__glog--test-deps-binary__/glog--test-deps-binary: error while loading shared libraries: libgflags.so.2.2: cannot open shared object file: No such file or directory Buck UI: https://www.internalfb.com/buck2/27a0f966-73b6-4adf-8b0d-0dd29bea5bfc Network: Up: 0B Down: 0B Command: test. Time elapsed: 2.3s Tests finished: Pass 3. Fail 1. Fatal 0. Skip 0. Build failure 0 1 TESTS FAILED ✗ antlir/distro/deps/glog:glog--test-deps - main ``` Reviewed By: epilatow Differential Revision: D68785473 fbshipit-source-id: 739eccc7c5fb86e996c7d3d7387d144900d25b79 --- antlir/distro/deps/glog/BUCK | 3 ++ antlir/distro/deps/google-perftools/BUCK | 3 ++ antlir/distro/deps/gtest/BUCK | 8 ++++++ antlir/distro/deps/libunwind/BUCK | 9 ++++++ antlir/distro/deps/ncurses/BUCK | 3 ++ antlir/distro/deps/pcre/BUCK | 1 + antlir/distro/deps/projects.bzl | 4 ++- antlir/distro/deps/rpm_library.bzl | 36 ++++++++++++++++++++++++ antlir/distro/platform/defs.bzl | 4 ++- antlir/distro/toolchain/cxx/tests/BUCK | 4 +-- 10 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 antlir/distro/deps/gtest/BUCK create mode 100644 antlir/distro/deps/libunwind/BUCK diff --git a/antlir/distro/deps/glog/BUCK b/antlir/distro/deps/glog/BUCK index cb4e3d3ee6c..2bcd105a54c 100644 --- a/antlir/distro/deps/glog/BUCK +++ b/antlir/distro/deps/glog/BUCK @@ -4,6 +4,9 @@ oncall("antlir") rpm_library( name = "glog", + test_include_headers = [ + "glog/logging.h", + ], visibility = ["PUBLIC"], exported_deps = [ "//antlir/distro/deps/gflags:gflags", diff --git a/antlir/distro/deps/google-perftools/BUCK b/antlir/distro/deps/google-perftools/BUCK index 315aac1cc1f..37a5dca9960 100644 --- a/antlir/distro/deps/google-perftools/BUCK +++ b/antlir/distro/deps/google-perftools/BUCK @@ -11,4 +11,7 @@ rpm_library( name = "profiler", rpm = "gperftools-devel", visibility = ["PUBLIC"], + exported_deps = [ + "//antlir/distro/deps/libunwind:unwind", + ], ) diff --git a/antlir/distro/deps/gtest/BUCK b/antlir/distro/deps/gtest/BUCK new file mode 100644 index 00000000000..4583460f979 --- /dev/null +++ b/antlir/distro/deps/gtest/BUCK @@ -0,0 +1,8 @@ +load("//antlir/distro/deps:rpm_library.bzl", "rpm_library") + +oncall("antlir") + +rpm_library( + name = "gtest", + visibility = ["PUBLIC"], +) diff --git a/antlir/distro/deps/libunwind/BUCK b/antlir/distro/deps/libunwind/BUCK new file mode 100644 index 00000000000..141784f1b64 --- /dev/null +++ b/antlir/distro/deps/libunwind/BUCK @@ -0,0 +1,9 @@ +load("//antlir/distro/deps:rpm_library.bzl", "rpm_library") + +oncall("antlir") + +rpm_library( + name = "unwind", + rpm = "libunwind-devel", + visibility = ["PUBLIC"], +) diff --git a/antlir/distro/deps/ncurses/BUCK b/antlir/distro/deps/ncurses/BUCK index 745053c7b88..bf26f2b0fa3 100644 --- a/antlir/distro/deps/ncurses/BUCK +++ b/antlir/distro/deps/ncurses/BUCK @@ -4,6 +4,9 @@ oncall("antlir") rpm_library( name = "ncursesw", + # TODO(T213442795): hardcoding this version is gross but antlir doesn't + # understand ld scripts + lib = "ncursesw.so.6", rpm = "ncurses-devel", visibility = ["PUBLIC"], ) diff --git a/antlir/distro/deps/pcre/BUCK b/antlir/distro/deps/pcre/BUCK index ef403d19737..6240f2eb555 100644 --- a/antlir/distro/deps/pcre/BUCK +++ b/antlir/distro/deps/pcre/BUCK @@ -12,4 +12,5 @@ rpm_library( name = "pcrecpp", rpm = "pcre-devel", visibility = ["PUBLIC"], + exported_deps = [":pcre"], ) diff --git a/antlir/distro/deps/projects.bzl b/antlir/distro/deps/projects.bzl index 93a1d52a21c..823061e2f05 100644 --- a/antlir/distro/deps/projects.bzl +++ b/antlir/distro/deps/projects.bzl @@ -1,4 +1,4 @@ -# @generated SignedSource<<73bf36b41772c2f946665830cb8de42a>> +# @generated SignedSource<<068e07a809d9f013ae626dae5da1fbbc>> PROJECTS = [ ("binutils", "iberty"), ("boost", "boost"), @@ -34,6 +34,7 @@ PROJECTS = [ ("glibc", "rt"), ("glog", "glog"), ("google-perftools", "profiler"), + ("gtest", "gtest"), ("jemalloc", "headers"), ("jemalloc", "jemalloc"), ("jsoncpp", "jsoncpp"), @@ -51,6 +52,7 @@ PROJECTS = [ ("libmcrypt", "libmcrypt"), ("libsodium", "sodium"), ("libtirpc", "tirpc"), + ("libunwind", "unwind"), ("libuuid", "libuuid"), ("libzip", "libzip"), ("llvm-fb", "clang_rt.asan"), diff --git a/antlir/distro/deps/rpm_library.bzl b/antlir/distro/deps/rpm_library.bzl index e567b82c9ee..bbc2c0a4906 100644 --- a/antlir/distro/deps/rpm_library.bzl +++ b/antlir/distro/deps/rpm_library.bzl @@ -3,9 +3,13 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. +load("//antlir/antlir2/bzl:selects.bzl", "selects") load("//antlir/antlir2/bzl/feature:defs.bzl", "feature") load("//antlir/antlir2/bzl/image:defs.bzl", "image") load("//antlir/antlir2/genrule_in_image:genrule_in_image.bzl", "genrule_in_image") +load("//antlir/antlir2/testing:image_test.bzl", "image_sh_test") +load("//antlir/bzl:build_defs.bzl", "cpp_binary", "write_file") +load("//antlir/distro/platform:defs.bzl", "default_image_platform") load(":prebuilt_cxx_library.bzl", "prebuilt_cxx_library") def rpm_library( @@ -18,6 +22,7 @@ def rpm_library( header_only: bool = False, visibility: list[str] = ["PUBLIC"], compatible_with_os: list[str] = [], + test_include_headers: list[str] | Select = [], **kwargs): """ Define a cxx_library target that can be used in Buck builds to depend on a @@ -111,3 +116,34 @@ def rpm_library( ], **kwargs ) + + write_file( + name = "{}--test-deps-main.cpp".format(name), + out = "main.cpp", + content = selects.apply( + test_include_headers or [], + lambda headers: ['#include "{}"'.format(h) for h in headers], + ) + [ + "int main(int argc, char **argv) {", + "return 0;", + "}", + ], + ) + + cpp_binary( + name = "{}--test-deps-binary".format(name), + srcs = {":{}--test-deps-main.cpp".format(name): "test.cpp"}, + default_target_platform = default_image_platform(), + deps = [ + ":" + name, + ], + ) + + image_sh_test( + name = "{}--test-deps".format(name), + test = ":{}--test-deps-binary".format(name), + layer = "antlir//antlir/distro/deps:base", + default_target_platform = default_image_platform(), + rootless = True, + labels = ["antlir-distro-dep-test"], + ) diff --git a/antlir/distro/platform/defs.bzl b/antlir/distro/platform/defs.bzl index eaefb9d5959..5e50aac6c1e 100644 --- a/antlir/distro/platform/defs.bzl +++ b/antlir/distro/platform/defs.bzl @@ -6,6 +6,7 @@ # @oss-disable load("@prelude//:rules.bzl", "platform") load("//antlir/antlir2/os:oses.bzl", "OSES", "arch_t", "new_arch_t", "os_t") +load("//antlir/antlir2/os:package.bzl", "get_default_os_for_package") def _cpu_label(arch: arch_t, *, constraint: bool = False) -> str: sk = arch.select_key @@ -87,7 +88,8 @@ def alias_for_current_image_platform(*, name: str, actual: str): platform = select(platform), ) -def default_image_platform(os: str): +def default_image_platform(os: str | None = None): + os = os or get_default_os_for_package() # @oss-disable default_arch = "aarch64" if native.host_info().arch.is_aarch64 else "x86_64" # @oss-enable diff --git a/antlir/distro/toolchain/cxx/tests/BUCK b/antlir/distro/toolchain/cxx/tests/BUCK index b4403492d49..3795557f22b 100644 --- a/antlir/distro/toolchain/cxx/tests/BUCK +++ b/antlir/distro/toolchain/cxx/tests/BUCK @@ -20,7 +20,7 @@ cpp_binary( compatible_with = [ "//antlir/distro:build-for-distro", ], - default_target_platform = default_image_platform("centos9"), + default_target_platform = default_image_platform(), deps = [ third_party.library( "jsoncpp", @@ -37,7 +37,7 @@ cpp_library( name = "dep", srcs = ["dep.c"], headers = ["dep.h"], - default_target_platform = default_image_platform("centos9"), + default_target_platform = default_image_platform(), deps = [ third_party.library( "rpm",