Skip to content

Commit

Permalink
Use fully qualified paths for C++ includes
Browse files Browse the repository at this point in the history
This fixes #66.

--
MOS_MIGRATED_REVID=91083724
  • Loading branch information
kchodorow authored and philwo committed Apr 14, 2015
1 parent cf71f8b commit feed478
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,14 @@ List<PathFragment> getLooseIncludeDirs() {
List<PathFragment> result = new ArrayList<>();
// The package directory of the rule contributes includes. Note that this also covers all
// non-subpackage sub-directories.
PathFragment rulePackage = ruleContext.getLabel().getPackageFragment();
PathFragment rulePackage = ruleContext.getLabel().getPackageIdentifier().getPathFragment();
result.add(rulePackage);

// Gather up all the dirs from the rule's srcs as well as any of the srcs outputs.
if (hasAttribute("srcs", Type.LABEL_LIST)) {
for (FileProvider src :
ruleContext.getPrerequisites("srcs", Mode.TARGET, FileProvider.class)) {
PathFragment packageDir = src.getLabel().getPackageFragment();
PathFragment packageDir = src.getLabel().getPackageIdentifier().getPathFragment();
for (Artifact a : src.getFilesToBuild()) {
result.add(packageDir);
// Attempt to gather subdirectories that might contain include files.
Expand All @@ -544,7 +544,8 @@ List<PathFragment> getLooseIncludeDirs() {

// Add in any 'includes' attribute values as relative path fragments
if (ruleContext.getRule().isAttributeValueExplicitlySpecified("includes")) {
PathFragment packageFragment = ruleContext.getLabel().getPackageFragment();
PathFragment packageFragment = ruleContext.getLabel().getPackageIdentifier()
.getPathFragment();
// For now, anything with an 'includes' needs a blanket declaration
result.add(packageFragment.getRelative("**"));
}
Expand All @@ -570,7 +571,7 @@ List<PathFragment> getIncludeDirs() {

private List<PathFragment> getIncludeDirsFromIncludesAttribute() {
List<PathFragment> result = new ArrayList<>();
PathFragment packageFragment = ruleContext.getLabel().getPackageFragment();
PathFragment packageFragment = ruleContext.getLabel().getPackageIdentifier().getPathFragment();
for (String includesAttr : ruleContext.attributes().get("includes", Type.STRING_LIST)) {
includesAttr = ruleContext.expandMakeVariables("includes", includesAttr);
if (includesAttr.startsWith("/")) {
Expand Down
55 changes: 55 additions & 0 deletions src/test/shell/bazel/local_repository_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,59 @@ function test_default_ws() {
bazel build //external:java >& $TEST_log || fail "Failed to build java"
}

function test_external_hdrs() {
local external_ws=$TEST_TMPDIR/path/to/my/lib
mkdir -p $external_ws
touch $external_ws/WORKSPACE
cat > $external_ws/greet_lib.h <<EOF
void greet();
EOF
cat > $external_ws/greet_lib.cc <<EOF
#include <stdio.h>
void greet() {
printf("Hello");
}
EOF
cat > $external_ws/BUILD <<EOF
cc_library(
name = "greet_lib",
srcs = ["greet_lib.cc"],
hdrs = ["greet_lib.h"],
includes = [
".",
],
visibility = ["//visibility:public"],
)
EOF

cat > greeter.cc <<EOF
#include "greet_lib.h"
int main() {
greet();
return 0;
}
EOF
cat > BUILD <<EOF
cc_binary(
name = "greeter",
srcs = ["greeter.cc"],
deps = ["//external:greet-lib"],
)
EOF
cat > WORKSPACE <<EOF
local_repository(
name = "greet-ws",
path = "$external_ws",
)
bind(
name = "greet-lib",
actual = "@greet-ws//:greet_lib"
)
EOF

bazel run //:greeter >& $TEST_log || fail "Failed to run greeter"
expect_log "Hello"
}

run_suite "local repository tests"

0 comments on commit feed478

Please sign in to comment.