Skip to content

Commit

Permalink
[antlir2][toolchain] easy test generation for deps tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vmagro authored and facebook-github-bot committed Jan 29, 2025
1 parent 5f0ec0d commit 99336d6
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 4 deletions.
3 changes: 3 additions & 0 deletions antlir/distro/deps/glog/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions antlir/distro/deps/google-perftools/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ rpm_library(
name = "profiler",
rpm = "gperftools-devel",
visibility = ["PUBLIC"],
exported_deps = [
"//antlir/distro/deps/libunwind:unwind",
],
)
8 changes: 8 additions & 0 deletions antlir/distro/deps/gtest/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//antlir/distro/deps:rpm_library.bzl", "rpm_library")

oncall("antlir")

rpm_library(
name = "gtest",
visibility = ["PUBLIC"],
)
9 changes: 9 additions & 0 deletions antlir/distro/deps/libunwind/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("//antlir/distro/deps:rpm_library.bzl", "rpm_library")

oncall("antlir")

rpm_library(
name = "unwind",
rpm = "libunwind-devel",
visibility = ["PUBLIC"],
)
3 changes: 3 additions & 0 deletions antlir/distro/deps/ncurses/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Expand Down
1 change: 1 addition & 0 deletions antlir/distro/deps/pcre/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ rpm_library(
name = "pcrecpp",
rpm = "pcre-devel",
visibility = ["PUBLIC"],
exported_deps = [":pcre"],
)
4 changes: 3 additions & 1 deletion antlir/distro/deps/projects.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @generated SignedSource<<73bf36b41772c2f946665830cb8de42a>>
# @generated SignedSource<<068e07a809d9f013ae626dae5da1fbbc>>
PROJECTS = [
("binutils", "iberty"),
("boost", "boost"),
Expand Down Expand Up @@ -34,6 +34,7 @@ PROJECTS = [
("glibc", "rt"),
("glog", "glog"),
("google-perftools", "profiler"),
("gtest", "gtest"),
("jemalloc", "headers"),
("jemalloc", "jemalloc"),
("jsoncpp", "jsoncpp"),
Expand All @@ -51,6 +52,7 @@ PROJECTS = [
("libmcrypt", "libmcrypt"),
("libsodium", "sodium"),
("libtirpc", "tirpc"),
("libunwind", "unwind"),
("libuuid", "libuuid"),
("libzip", "libzip"),
("llvm-fb", "clang_rt.asan"),
Expand Down
36 changes: 36 additions & 0 deletions antlir/distro/deps/rpm_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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"],
)
4 changes: 3 additions & 1 deletion antlir/distro/platform/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions antlir/distro/toolchain/cxx/tests/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 99336d6

Please sign in to comment.