Skip to content

Commit

Permalink
[workspace] Factor glx and x11 to share a common implementation (Robo…
Browse files Browse the repository at this point in the history
…tLocomotion#20585)

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.
  • Loading branch information
jwnimmer-tri authored and RussTedrake committed Dec 27, 2023
1 parent 26129cf commit 584ea80
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 105 deletions.
10 changes: 0 additions & 10 deletions tools/workspace/glx/package-macos.BUILD.bazel

This file was deleted.

20 changes: 0 additions & 20 deletions tools/workspace/glx/package-ubuntu.BUILD.bazel

This file was deleted.

50 changes: 12 additions & 38 deletions tools/workspace/glx/repository.bzl
Original file line number Diff line number Diff line change
@@ -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,
)
2 changes: 2 additions & 0 deletions tools/workspace/pkg_config.BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# %{topcomment}

load("@drake//tools/skylark:cc.bzl", "cc_library")

licenses(%{licenses})

package(default_visibility = ["//visibility:public"])
Expand Down
26 changes: 26 additions & 0 deletions tools/workspace/pkg_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 " +
Expand Down
11 changes: 0 additions & 11 deletions tools/workspace/x11/package-macos.BUILD.bazel

This file was deleted.

34 changes: 8 additions & 26 deletions tools/workspace/x11/repository.bzl
Original file line number Diff line number Diff line change
@@ -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"],
)

0 comments on commit 584ea80

Please sign in to comment.