Skip to content

Commit 9f71f05

Browse files
jordan-bonserJonathan Watson
authored andcommitted
test(import_cc): add library support to aspect.
support both static and shared library imports in the Bazel import_cc aspect. Change-Id: Ic6bc068c4be074cf2ea3f81dd6eda8f35011d3b6 Signed-off-by: Jordan Bonser <jordan.bonser@arm.com>
1 parent 3986e83 commit 9f71f05

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

tests/bazel_cc_import/bazel/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@ gen_bob_import(
66
"//tests/bazel_cc_import/bazel/header_only/includes",
77
"//tests/bazel_cc_import/bazel/header_only/normal",
88
"//tests/bazel_cc_import/bazel/header_only/strip_prefix",
9+
"//tests/bazel_cc_import/bazel/library/cmake:shared",
10+
"//tests/bazel_cc_import/bazel/library/cmake:static",
11+
"//tests/bazel_cc_import/bazel/library/dependency:shared",
12+
"//tests/bazel_cc_import/bazel/library/dependency:static",
13+
"//tests/bazel_cc_import/bazel/library/simple:shared",
14+
"//tests/bazel_cc_import/bazel/library/simple:static",
915
],
1016
)

tests/bazel_cc_import/bazel/bob_import_cc_aspect.bzl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,42 @@ def _symlink_headers(ctx, module_dir, dir_name, headers):
8484

8585
return outputs
8686

87+
def _library_file(target):
88+
candidates = []
89+
90+
def add_candidate(file):
91+
if file.path.endswith(".a") or file.path.endswith(".so"):
92+
candidates.append(file)
93+
94+
# cc_shared_library has this
95+
for file in target[DefaultInfo].files.to_list():
96+
add_candidate(file)
97+
98+
if not candidates and CcInfo in target:
99+
linking_context = target[CcInfo].linking_context
100+
for linker_input in linking_context.linker_inputs.to_list():
101+
for library in linker_input.libraries:
102+
for file in [
103+
library.dynamic_library,
104+
library.static_library,
105+
]:
106+
add_candidate(file)
107+
108+
if len(candidates) > 1:
109+
fail("More than one lib output in '" + str(target) + "' " + str(candidates))
110+
111+
if len(candidates) == 0:
112+
# header only lib
113+
return None
114+
115+
return candidates[0]
116+
117+
def _symlink_library(ctx, module_dir, dir_name, library):
118+
include_path = paths.join(dir_name, library.basename)
119+
out = ctx.actions.declare_file(module_dir + "/" + include_path)
120+
ctx.actions.symlink(output = out, target_file = library)
121+
return include_path, out
122+
87123
def _write_bp(ctx, target_name, src, defines, includes):
88124
out = ctx.actions.declare_file(target_name + "/build.bp")
89125
ctx.actions.write(out, bp_content(target_name, src, includes, defines))
@@ -120,6 +156,11 @@ def _gen_bob_import_impl(ctx):
120156
includes = [include_destination]
121157
src = None
122158

159+
library = _library_file(dep)
160+
if library:
161+
src, library_out = _symlink_library(ctx, target_name, library_destination, library)
162+
outputs.append(library_out)
163+
123164
outputs.append(_write_bp(ctx, target_name, src, defines, includes))
124165
output.extend(outputs)
125166

tests/bazel_cc_import/build.bp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,53 @@ bob_binary {
1616
header_libs: ["tests_bazel_cc_import_bazel_header_only_strip_prefix_strip_prefix"],
1717
}
1818

19+
bob_binary {
20+
name: "bob_test_bazel_cc_import_library_simple_shared",
21+
srcs: ["consumer/library/simple.c"],
22+
shared_libs: ["tests_bazel_cc_import_bazel_library_simple_shared"],
23+
}
24+
25+
bob_binary {
26+
name: "bob_test_bazel_cc_import_library_simple_static",
27+
srcs: ["consumer/library/simple.c"],
28+
static_libs: ["tests_bazel_cc_import_bazel_library_simple_static"],
29+
}
30+
31+
bob_binary {
32+
name: "bob_test_bazel_cc_import_library_dependency_shared",
33+
srcs: ["consumer/library/dependency.c"],
34+
shared_libs: ["tests_bazel_cc_import_bazel_library_dependency_shared"],
35+
}
36+
37+
bob_binary {
38+
name: "bob_test_bazel_cc_import_library_dependency_static",
39+
srcs: ["consumer/library/dependency.c"],
40+
static_libs: ["tests_bazel_cc_import_bazel_library_dependency_static"],
41+
}
42+
43+
bob_binary {
44+
name: "bob_test_bazel_cc_import_library_cmake_static",
45+
srcs: ["consumer/library/cmake.c"],
46+
static_libs: ["tests_bazel_cc_import_bazel_library_cmake_static"],
47+
}
48+
49+
bob_binary {
50+
name: "bob_test_bazel_cc_import_library_cmake_shared",
51+
srcs: ["consumer/library/cmake.c"],
52+
shared_libs: ["tests_bazel_cc_import_bazel_library_cmake_shared"],
53+
}
54+
1955
bob_alias {
2056
name: "bob_test_bazel_import",
2157
srcs: [
2258
"bob_test_bazel_cc_import_header_only_normal",
2359
"bob_test_bazel_cc_import_header_only_includes",
2460
"bob_test_bazel_cc_import_header_only_strip_prefix",
61+
"bob_test_bazel_cc_import_library_simple_shared",
62+
"bob_test_bazel_cc_import_library_simple_static",
63+
"bob_test_bazel_cc_import_library_dependency_shared",
64+
"bob_test_bazel_cc_import_library_dependency_static",
65+
"bob_test_bazel_cc_import_library_cmake_static",
66+
"bob_test_bazel_cc_import_library_cmake_shared",
2567
],
2668
}

tests/bazel_cc_import/run_test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ TEST_EXECUTABLES=(
7979
bob_test_bazel_cc_import_header_only_normal
8080
bob_test_bazel_cc_import_header_only_includes
8181
bob_test_bazel_cc_import_header_only_strip_prefix
82+
bob_test_bazel_cc_import_library_simple_shared
83+
bob_test_bazel_cc_import_library_simple_static
84+
bob_test_bazel_cc_import_library_dependency_shared
85+
bob_test_bazel_cc_import_library_dependency_static
8286
)
8387

8488
for executable in "${TEST_EXECUTABLES[@]}"; do

0 commit comments

Comments
 (0)