From f0269ffe365c203adbe78accf3ae37c67b65f6e3 Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Sat, 25 Nov 2023 09:20:00 -0800 Subject: [PATCH] [workspace] Factor glx and x11 to share a common implementation The logic for "Call pkg-config using deferred error handling on macOS" is common to these two modules, and can be moved up into pkg_config.bzl directly. --- tools/workspace/glx/package-macos.BUILD.bazel | 10 ---- .../workspace/glx/package-ubuntu.BUILD.bazel | 20 -------- tools/workspace/glx/repository.bzl | 50 +++++-------------- tools/workspace/pkg_config.BUILD.tpl | 2 + tools/workspace/pkg_config.bzl | 26 ++++++++++ tools/workspace/x11/package-macos.BUILD.bazel | 11 ---- tools/workspace/x11/repository.bzl | 34 +++---------- 7 files changed, 48 insertions(+), 105 deletions(-) delete mode 100644 tools/workspace/glx/package-macos.BUILD.bazel delete mode 100644 tools/workspace/glx/package-ubuntu.BUILD.bazel delete mode 100644 tools/workspace/x11/package-macos.BUILD.bazel diff --git a/tools/workspace/glx/package-macos.BUILD.bazel b/tools/workspace/glx/package-macos.BUILD.bazel deleted file mode 100644 index 31e02a04104c..000000000000 --- a/tools/workspace/glx/package-macos.BUILD.bazel +++ /dev/null @@ -1,10 +0,0 @@ -# -*- bazel -*- - -load("@drake//tools/skylark:cc.bzl", "cc_library") - -# On macOS, no targets should depend on @glx. -cc_library( - name = "glx", - srcs = ["missing-macos.cc"], - visibility = ["//visibility:public"], -) diff --git a/tools/workspace/glx/package-ubuntu.BUILD.bazel b/tools/workspace/glx/package-ubuntu.BUILD.bazel deleted file mode 100644 index 78b8e71eaf7e..000000000000 --- a/tools/workspace/glx/package-ubuntu.BUILD.bazel +++ /dev/null @@ -1,20 +0,0 @@ -# -*- bazel -*- - -load("@drake//tools/skylark:cc.bzl", "cc_library") - -licenses(["notice"]) # SGI-B-2.0 - -cc_library( - name = "glx", - hdrs = glob(["include/**/*.h"]), - includes = ["include"], - linkopts = [ - "-L/usr/lib/x86_64-linux-gnu", - "-lGLX", - ], - visibility = ["//visibility:public"], - deps = [ - "@opengl", - "@x11", - ], -) diff --git a/tools/workspace/glx/repository.bzl b/tools/workspace/glx/repository.bzl index 325277790d75..e5261ae0ec92 100644 --- a/tools/workspace/glx/repository.bzl +++ b/tools/workspace/glx/repository.bzl @@ -1,39 +1,13 @@ -load("//tools/workspace:os.bzl", "determine_os") - -def _impl(repository_ctx): - os_result = determine_os(repository_ctx) - - if os_result.error != None: - fail(os_result.error) - - if os_result.is_macos: - # On macOS, no targets should depend on @glx. - build_flavor = "macos" - elif os_result.is_ubuntu or os_result.is_manylinux: - build_flavor = "ubuntu" - hdrs = [ - "GL/glx.h", - "GL/glxext.h", - ] - for hdr in hdrs: - repository_ctx.symlink( - "/usr/include/{}".format(hdr), - "include/{}".format(hdr), - ) - else: - fail("Operating system is NOT supported {}".format(os_result)) - - repository_ctx.symlink( - Label( - "@drake//tools/workspace/glx:package-{}.BUILD.bazel".format( - build_flavor, - ), - ), - "BUILD.bazel", +load("//tools/workspace:pkg_config.bzl", "pkg_config_repository") + +def glx_repository(name): + pkg_config_repository( + name = name, + licenses = ["notice"], # SGI-B-2.0 + modname = "glx", + extra_deps = [ + "@opengl", + "@x11", + ], + defer_error_os_names = ["mac os x"], ) - -glx_repository = repository_rule( - local = True, - configure = True, - implementation = _impl, -) diff --git a/tools/workspace/pkg_config.BUILD.tpl b/tools/workspace/pkg_config.BUILD.tpl index 2a361676d51e..e30075603e62 100644 --- a/tools/workspace/pkg_config.BUILD.tpl +++ b/tools/workspace/pkg_config.BUILD.tpl @@ -2,6 +2,8 @@ # %{topcomment} +load("@drake//tools/skylark:cc.bzl", "cc_library") + licenses(%{licenses}) package(default_visibility = ["//visibility:public"]) diff --git a/tools/workspace/pkg_config.bzl b/tools/workspace/pkg_config.bzl index 786c14606dc8..dddbdde8d9b9 100644 --- a/tools/workspace/pkg_config.bzl +++ b/tools/workspace/pkg_config.bzl @@ -73,6 +73,26 @@ def setup_pkg_config_repository(repository_ctx): # Check if we can find the required *.pc file of any version. result = _run_pkg_config(repository_ctx, args, pkg_config_paths) if result.error != None: + defer_error_os_names = getattr( + repository_ctx.attr, + "defer_error_os_names", + [], + ) + if repository_ctx.os.name in defer_error_os_names: + repository_ctx.file( + "BUILD.bazel", + """ +load("@drake//tools/skylark:cc.bzl", "cc_library") +cc_library( + name = {name}, + srcs = ["pkg_config_failed.cc"], + visibility = ["//visibility:public"], +) + """.format( + name = repr(repository_ctx.name), + ), + ) + return struct(value = True, error = None) return result # If we have a minimum version, enforce that. @@ -326,6 +346,7 @@ _do_pkg_config_repository = repository_rule( "pkg_config_paths": attr.string_list(), "homebrew_subdir": attr.string(), "extra_deprecation": attr.string(), + "defer_error_os_names": attr.string_list(), }, local = True, configure = True, @@ -389,6 +410,11 @@ def pkg_config_repository(**kwargs): "/opt/homebrew/opt/libpng/lib/pkgconfig". extra_deprecation: (Optional) Add a deprecation message to the library BUILD target. + defer_error_os_names: (Optional) On these operating systems (as named + by repository_ctx.os.name), failure to find the + *.pc file will yield a link-time error, not a + fetch-time error. This is useful for externals + that are guarded by select() statements. """ if "deprecation" in kwargs: fail("When calling pkg_config_repository, don't use deprecation=str " + diff --git a/tools/workspace/x11/package-macos.BUILD.bazel b/tools/workspace/x11/package-macos.BUILD.bazel deleted file mode 100644 index 4f8aa0583bfd..000000000000 --- a/tools/workspace/x11/package-macos.BUILD.bazel +++ /dev/null @@ -1,11 +0,0 @@ -# -*- bazel -*- - -package(default_visibility = ["//visibility:public"]) - -load("@drake//tools/skylark:cc.bzl", "cc_library") - -# On macOS, no targets should depend on @x11. -cc_library( - name = "x11", - srcs = ["missing-macos.cc"], -) diff --git a/tools/workspace/x11/repository.bzl b/tools/workspace/x11/repository.bzl index e9a8aad000a2..9389c2b2a57c 100644 --- a/tools/workspace/x11/repository.bzl +++ b/tools/workspace/x11/repository.bzl @@ -1,27 +1,9 @@ -load("//tools/workspace:os.bzl", "determine_os") -load("//tools/workspace:pkg_config.bzl", "setup_pkg_config_repository") +load("//tools/workspace:pkg_config.bzl", "pkg_config_repository") -def _impl(repo_ctx): - # Only available on Ubuntu. On macOS, no targets should depend on @x11. - os_result = determine_os(repo_ctx) - if os_result.error != None: - fail(os_result.error) - if os_result.is_ubuntu or os_result.is_manylinux: - error = setup_pkg_config_repository(repo_ctx).error - if error != None: - fail(error) - else: - repo_ctx.symlink( - Label("@drake//tools/workspace/x11:package-macos.BUILD.bazel"), - "BUILD.bazel", - ) - -x11_repository = repository_rule( - attrs = { - "modname": attr.string(default = "x11"), - "licenses": attr.string_list(default = ["notice"]), # X11/MIT. - }, - local = True, - configure = True, - implementation = _impl, -) +def x11_repository(name): + pkg_config_repository( + name = name, + licenses = ["notice"], # X11/MIT + modname = "x11", + defer_error_os_names = ["mac os x"], + )