-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR [workspace] Add reusable macro and documentation for vendor_cxx
Update yaml_cpp_internal to follow the new style.
- Loading branch information
1 parent
4cfbb2e
commit fbf7bc6
Showing
7 changed files
with
178 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- python -*- | ||
|
||
def cc_library_vendored( | ||
name, | ||
hdrs = None, | ||
hdrs_vendored = None, | ||
edit_include = None, | ||
srcs = None, | ||
srcs_vendored = None, | ||
**kwargs): | ||
""" | ||
Compiles a third-party C++ library using altered include paths and | ||
namespaces so that it will not interfere with co-habitating builds | ||
of the same library by others. | ||
The lists of hdrs and hdrs_vendored paths must be equal in length and | ||
correspond as elementwise pairs. The hdrs gives the list of library | ||
header file paths as found in the third-party source layout; the | ||
hdrs_vendored gives the list of header file paths to use for Drake's | ||
vendored build. Typically we will prefix "drake_vendor/" to the path. | ||
The edit_include mapping provides #include patterns that should be | ||
rewritten in all of the source files (both hdrs and srcs). The patterns | ||
are implicitly anchored at the start of the #include statements. | ||
For example, {"yaml-cpp/": "drake_vendor/yaml-cpp/"} would edit this line: | ||
#include <yaml-cpp/node.h> | ||
into this line instead: | ||
#include <drake_vendor/yaml-cpp/node.h> | ||
The lists of srcs and srcs_vendored paths must be equal in length and | ||
correspond as elementwise pairs. The srcs gives the list of library | ||
source file paths as found in the third-party source layout; the | ||
srcs_vendored gives the list of source file paths to use for Drake's | ||
vendored build. | ||
""" | ||
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import unittest | ||
|
||
from drake.tools.workspace.vendor_cxx import _rewrite_one_text | ||
|
||
|
||
class TestVendorCxx(unittest.TestCase): | ||
|
||
def setUp(self): | ||
# Display all test output. | ||
self.maxDiff = None | ||
|
||
# These are the include paths that we want to rewrite. | ||
self._edit_include = { | ||
'somelib/': 'drake_vendor/somelib/', | ||
} | ||
|
||
# These are the boilerplate lines that vendor_cxx injects. | ||
self._open = 'inline namespace drake_vendor __attribute__ ((visibility ("hidden"))) {' # noqa | ||
self._close = '} /* inline namespace drake_vendor */' | ||
|
||
def _check(self, old_lines, expected_new_lines): | ||
"""Tests one call to _rewrite_one_text for expected output.""" | ||
old_text = '\n'.join(old_lines) + '\n' | ||
new_text = _rewrite_one_text( | ||
text=old_text, edit_include=self._edit_include.items()) | ||
expected_new_text = '\n'.join(expected_new_lines) + '\n' | ||
self.assertMultiLineEqual(expected_new_text, new_text) | ||
|
||
def test_simple(self): | ||
self._check([ | ||
'#include "somelib/somefile.h"', | ||
'#include "unrelated/thing.h"', | ||
'int foo();', | ||
], [ | ||
# Adjacent pairs of open/close are useless; ideally, we teach | ||
# vendor_cxx to skip these. | ||
self._open, self._close, # TODO(jwnimmer-tri) Useless filler. | ||
|
||
# All include statements must NOT be within an open-namespace. | ||
'#include "drake_vendor/somelib/somefile.h"', | ||
|
||
self._open, self._close, # TODO(jwnimmer-tri) Useless filler. | ||
'#include "unrelated/thing.h"', | ||
|
||
# All code MUST be within an open-namespace. | ||
self._open, | ||
'int foo();', | ||
self._close, | ||
]) | ||
|
||
|
||
assert __name__ == '__main__' | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters