@@ -4,6 +4,8 @@ load("//tests/bazel_cc_import/bazel:name.bzl", "bp_target_name")
44load ("@bazel_skylib//lib:collections.bzl" , "collections" )
55load ("@bazel_skylib//lib:paths.bzl" , "paths" )
66
7+ ImportCcAspectInfo = provider (fields = ["defines" , "headers" ])
8+
79def _include_dir_relative_path (header , include_dirs ):
810 best = None
911 for include_dir in include_dirs :
@@ -22,6 +24,13 @@ def _get_headers(compilation_info):
2224
2325 headers = {}
2426 for header in compilation_info .headers .to_list ():
27+ if headers .get ("" ):
28+ fail ("More than one header dir" )
29+
30+ if header .is_directory :
31+ headers ["" ] = header
32+ continue
33+
2534 include_path = _include_dir_relative_path (header , include_dirs )
2635 if include_path :
2736 headers [paths .normalize (include_path )] = header
@@ -30,43 +39,99 @@ def _get_headers(compilation_info):
3039
3140 return headers
3241
42+ def _merge_import_info (info_sets ):
43+ headers = {}
44+ defines = []
45+
46+ for info in info_sets :
47+ headers .update (info .headers )
48+ defines .extend (info .defines )
49+
50+ return headers , collections .uniq (defines )
51+
52+ def _compilation_info (target , ctx ):
53+ info_sets = []
54+
55+ if CcInfo in target :
56+ compilation_context = target [CcInfo ].compilation_context
57+ info_sets .append (
58+ ImportCcAspectInfo (
59+ headers = _get_headers (compilation_context ),
60+ defines = compilation_context .defines .to_list (),
61+ ),
62+ )
63+
64+ for attr_name in ["deps" , "srcs" ]:
65+ for dep in getattr (ctx .rule .attr , attr_name , []):
66+ if ImportCcAspectInfo in dep :
67+ info_sets .append (dep [ImportCcAspectInfo ])
68+
69+ if not info_sets :
70+ return {}, []
71+
72+ return _merge_import_info (info_sets )
73+
3374def _symlink_headers (ctx , module_dir , dir_name , headers ):
3475 outputs = []
3576
3677 for include_path in sorted (headers .keys ()):
37- out = ctx .actions .declare_file (module_dir + "/" + dir_name + "/" + include_path )
78+ if include_path == "" :
79+ out = ctx .actions .declare_directory (module_dir + "/" + dir_name )
80+ else :
81+ out = ctx .actions .declare_file (module_dir + "/" + dir_name + "/" + include_path )
3882 ctx .actions .symlink (output = out , target_file = headers [include_path ])
3983 outputs .append (out )
4084
4185 return outputs
4286
43- def _bob_import_cc_aspect_impl (target , ctx ):
44- defines = []
45- src = None
46- header_outputs = []
47- include_destination = "include"
48-
49- target_name = bp_target_name (ctx .label )
50- defines = []
51- if CcInfo in target :
52- compilation_context = target [CcInfo ].compilation_context
53- defines = compilation_context .defines .to_list ()
54- headers = _get_headers (compilation_context )
55- header_outputs = _symlink_headers (ctx , target_name , include_destination , headers )
56-
57- target_name = bp_target_name (ctx .label )
58- includes = [include_destination ]
59-
87+ def _write_bp (ctx , target_name , src , defines , includes ):
6088 out = ctx .actions .declare_file (target_name + "/build.bp" )
6189 ctx .actions .write (out , bp_content (target_name , src , includes , defines ))
90+ return out
6291
63- outputs = [out ] + header_outputs
92+ def _bob_import_cc_aspect_impl (target , ctx ):
93+ headers , defines = _compilation_info (target , ctx )
6494
6595 return [
66- OutputGroupInfo (bob_import_cc_bp = depset (outputs )),
96+ ImportCcAspectInfo (
97+ headers = headers ,
98+ defines = defines ,
99+ ),
67100 ]
68101
69102bob_import_cc_aspect = aspect (
70103 implementation = _bob_import_cc_aspect_impl ,
71- attr_aspects = [], # TODO we will have to traverse things for more complex cases.
104+ attr_aspects = ["deps" , "srcs" ],
105+ )
106+
107+ def _gen_bob_import_impl (ctx ):
108+ output = []
109+ for dep in ctx .attr .deps :
110+ target_name = bp_target_name (dep .label )
111+ include_destination = "include"
112+ library_destination = "lib"
113+
114+ outputs = []
115+
116+ headers = dep [ImportCcAspectInfo ].headers
117+ defines = dep [ImportCcAspectInfo ].defines
118+ outputs .extend (_symlink_headers (ctx , target_name , include_destination , headers ))
119+
120+ includes = [include_destination ]
121+ src = None
122+
123+ outputs .append (_write_bp (ctx , target_name , src , defines , includes ))
124+ output .extend (outputs )
125+
126+ return [
127+ DefaultInfo (
128+ files = depset (output ),
129+ ),
130+ ]
131+
132+ gen_bob_import = rule (
133+ implementation = _gen_bob_import_impl ,
134+ attrs = {
135+ "deps" : attr .label_list (aspects = [bob_import_cc_aspect ]),
136+ },
72137)
0 commit comments