Skip to content

Commit

Permalink
[workspace] Add reusable macro and documentation for vendor_cxx
Browse files Browse the repository at this point in the history
Update yaml_cpp_internal to follow the new style.
  • Loading branch information
jwnimmer-tri committed May 23, 2022
1 parent 53571e1 commit a5c310f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 35 deletions.
2 changes: 1 addition & 1 deletion common/yaml/yaml_read_archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <algorithm>
#include <cstring>

#include <drake-yaml-cpp/yaml.h>
#include <drake_vendor/yaml-cpp/yaml.h>
#include <fmt/ostream.h>

#include "drake/common/nice_type_name.h"
Expand Down
4 changes: 2 additions & 2 deletions common/yaml/yaml_write_archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <utility>
#include <vector>

#include <drake-yaml-cpp/emitfromevents.h>
#include <drake-yaml-cpp/yaml.h>
#include <drake_vendor/yaml-cpp/emitfromevents.h>
#include <drake_vendor/yaml-cpp/yaml.h>

#include "drake/common/drake_assert.h"
#include "drake/common/unused.h"
Expand Down
48 changes: 48 additions & 0 deletions tools/workspace/vendor_cxx.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- python -*-

def cc_library_vendored(
name,
hdrs = None,
hdrs_vendored = None,
edit_include = None,
srcs = None,
srcs_vendored = None,
**kwargs):
# XXX In the next few stanzas, we'll rewrite the upstream headers and sources to
# use a different include paths and a private namespace. This gives Drake a
# completely independent build of the upstream library, even when statically
# linking other versions of the library into the same DSO.
"""..."""
hdrs = hdrs or []
hdrs_vendored = hdrs_vendored or []
edit_include = edit_include or {}
srcs = srcs or []
srcs_vendored = srcs_vendored or []
if len(hdrs) != len(hdrs_vendored):
fail("The hdrs= and hdrs_vendored= list lengths must match")
if len(srcs) != len(srcs_vendored):
fail("The srcs= and srcs_vendored= list lengths must match")
native.genrule(
name = "_{}_vendoring".format(name),
srcs = hdrs + srcs,
outs = hdrs_vendored + srcs_vendored,
cmd = " ".join([
"$(execpath @drake//tools/workspace:vendor_cxx)",
] + [
"'--edit-include={}:{}'".format(k, v)
for k, v in edit_include.items()
] + [
"$(execpath {}):$(execpath {})".format(old, new)
for old, new in (zip(hdrs, hdrs_vendored) +
zip(srcs, srcs_vendored))
]),
tools = ["@drake//tools/workspace:vendor_cxx"],
tags = ["manual"],
visibility = ["//visibility:private"],
)
native.cc_library(
name = name,
hdrs = hdrs_vendored,
srcs = srcs_vendored,
**kwargs
)
51 changes: 19 additions & 32 deletions tools/workspace/yaml_cpp_internal/package.BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ load(
"@drake//tools/install:install.bzl",
"install",
)
load(
"@drake//tools/workspace:vendor_cxx.bzl",
"cc_library_vendored",
)

licenses(["notice"]) # MIT

Expand All @@ -20,39 +24,22 @@ _SRCS = glob([
"src/*.h",
], allow_empty = False)

# In the next few stanzas, we'll rewrite the upstream headers and sources to
# use a different include paths and a private namespace. This gives Drake a
# completely independent build of the upstream library, even when statically
# linking other versions of the library into the same DSO.
_VENDORED_HDRS = [
x.replace("include/yaml-cpp/", "include/drake-yaml-cpp/")
for x in _HDRS
]

_VENDORED_SRCS = [
x.replace("src/", "drake-src/")
for x in _SRCS
]

genrule(
name = "vendoring",
srcs = _HDRS + _SRCS,
outs = _VENDORED_HDRS + _VENDORED_SRCS,
cmd = " ".join([
"$(execpath @drake//tools/workspace:vendor_cxx)",
"--edit-include=yaml-cpp/:drake-yaml-cpp/",
] + [
"$(execpath {}):$(execpath {})".format(old, new)
for old, new in zip(_HDRS, _VENDORED_HDRS) + zip(_SRCS, _VENDORED_SRCS)
]),
tools = ["@drake//tools/workspace:vendor_cxx"],
)

cc_library(
cc_library_vendored(
name = "yaml_cpp",
hdrs = _VENDORED_HDRS,
srcs = _VENDORED_SRCS,
includes = ["include"],
hdrs = _HDRS,
hdrs_vendored = [
x.replace("include/yaml-cpp/", "drake_src/drake_vendor/yaml-cpp/")
for x in _HDRS
],
edit_include = {
"yaml-cpp/": "drake_vendor/yaml-cpp/",
},
includes = ["drake_src"],
srcs = _SRCS,
srcs_vendored = [
x.replace("src/", "drake_src/")
for x in _SRCS
],
linkstatic = 1,
visibility = ["//visibility:public"],
)
Expand Down

0 comments on commit a5c310f

Please sign in to comment.