11"""Common definitions for D rules."""
22
3+ load ("@bazel_skylib//lib:paths.bzl" , "paths" )
34load ("@rules_cc//cc/common:cc_info.bzl" , "CcInfo" )
45load ("//d/private:providers.bzl" , "DInfo" )
56
@@ -18,6 +19,8 @@ common_attrs = {
1819 allow_empty = False ,
1920 ),
2021 "deps" : attr .label_list (doc = "List of dependencies." , providers = [[CcInfo ], [DInfo ]]),
22+ "imports" : attr .string_list (doc = "List of import paths." ),
23+ "string_imports" : attr .string_list (doc = "List of string import paths." ),
2124 "string_srcs" : attr .label_list (doc = "List of string import source files." ),
2225 "versions" : attr .string_list (doc = "List of version identifiers." ),
2326 "_linux_constraint" : attr .label (default = "@platforms//os:linux" , doc = "Linux platform constraint" ),
@@ -97,17 +100,21 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
97100 for lib in li .libraries
98101 ])
99102 d_deps = [d [DInfo ] for d in ctx .attr .deps if DInfo in d ]
100- import_paths = depset (transitive = [d .import_paths for d in d_deps ])
101- string_import_paths = depset (
102- ["." ] if ctx .files .string_srcs else [],
103- transitive = [d .string_import_paths for d in d_deps ],
103+ imports = depset (
104+ [paths .join (ctx .label .package , imp ) for imp in ctx .attr .imports ],
105+ transitive = [d .imports for d in d_deps ],
106+ )
107+ string_imports = depset (
108+ ([ctx .label_package ] if ctx .files .string_srcs else []) +
109+ [paths .join (ctx .label .package , imp ) for imp in ctx .attr .string_imports ],
110+ transitive = [d .string_imports for d in d_deps ],
104111 )
105112 versions = depset (ctx .attr .versions , transitive = [d .versions for d in d_deps ])
106113 args = ctx .actions .args ()
107114 args .add_all (COMPILATION_MODE_FLAGS [ctx .var ["COMPILATION_MODE" ]])
108115 args .add_all (ctx .files .srcs )
109- args .add_all (import_paths .to_list (), format_each = "-I=%s" )
110- args .add_all (string_import_paths .to_list (), format_each = "-J=%s" )
116+ args .add_all (imports .to_list (), format_each = "-I=%s" )
117+ args .add_all (string_imports .to_list (), format_each = "-J=%s" )
111118 args .add_all (toolchain .compiler_flags )
112119 args .add_all (versions .to_list (), format_each = "-version=%s" )
113120 args .add_all (toolchain .linker_flags )
@@ -131,7 +138,7 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
131138 inputs = depset (
132139 ctx .files .srcs + ctx .files .string_srcs ,
133140 transitive = [toolchain .d_compiler [DefaultInfo ].default_runfiles .files ] +
134- [d .imports for d in d_deps ] +
141+ [d .interface_srcs for d in d_deps ] +
135142 [d .libraries for d in d_deps ] +
136143 [c_libraries ],
137144 ),
@@ -147,23 +154,25 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
147154 return [
148155 DefaultInfo (files = depset ([output ])),
149156 DInfo (
150- import_paths = depset (
151- [ctx .label .package ],
152- transitive = [d .import_paths for d in d_deps ],
153- ),
154157 imports = depset (
155- ctx .files .srcs + ctx .files .string_srcs ,
158+ [ctx .label .package ] +
159+ [paths .join (ctx .label .package , imp ) for imp in ctx .attr .imports ],
156160 transitive = [d .imports for d in d_deps ],
157161 ),
162+ interface_srcs = depset (
163+ ctx .files .srcs + ctx .files .string_srcs ,
164+ transitive = [d .interface_srcs for d in d_deps ],
165+ ),
158166 libraries = depset (
159167 [] if ctx .attr .source_only else [output ],
160168 order = "topological" ,
161169 transitive = [d .libraries for d in d_deps ] +
162170 [c_libraries ],
163171 ),
164- string_import_paths = depset (
165- [ctx .label .package ] if ctx .files .string_srcs else [],
166- transitive = [d .string_import_paths for d in d_deps ],
172+ string_imports = depset (
173+ ([ctx .label .package ] if ctx .files .string_srcs else []) +
174+ [paths .join (ctx .label .package , imp ) for imp in ctx .attr .string_imports ],
175+ transitive = [d .string_imports for d in d_deps ],
167176 ),
168177 versions = versions ,
169178 ),
0 commit comments