11"""Common definitions for D rules."""
22
3+ load ("@aspect_bazel_lib//lib:expand_make_vars.bzl" , "expand_variables" )
4+ load ("@bazel_skylib//lib:dicts.bzl" , "dicts" )
35load ("@bazel_skylib//lib:paths.bzl" , "paths" )
46load ("@rules_cc//cc/common:cc_info.bzl" , "CcInfo" )
57load ("//d/private:providers.bzl" , "DInfo" )
@@ -30,6 +32,21 @@ common_attrs = {
3032 "_windows_constraint" : attr .label (default = "@platforms//os:windows" , doc = "Windows platform constraint" ),
3133}
3234
35+ runnable_attrs = dicts .add (
36+ common_attrs ,
37+ {
38+ "env" : attr .string_dict (doc = "Environment variables for the binary at runtime. Subject of location and make variable expansion." ),
39+ "data" : attr .label_list (allow_files = True , doc = "List of files to be made available at runtime." ),
40+ },
41+ )
42+
43+ library_attrs = dicts .add (
44+ common_attrs ,
45+ {
46+ "source_only" : attr .bool (doc = "If true, the source files are compiled, but not library is produced." ),
47+ },
48+ )
49+
3350def _get_os (ctx ):
3451 if ctx .target_platform_has_constraint (ctx .attr ._linux_constraint [platform_common .ConstraintValueInfo ]):
3552 return "linux"
@@ -38,7 +55,7 @@ def _get_os(ctx):
3855 elif ctx .target_platform_has_constraint (ctx .attr ._windows_constraint [platform_common .ConstraintValueInfo ]):
3956 return "windows"
4057 else :
41- fail ("Unsupported OS: %s" % ctx . label )
58+ fail ("OS not supported" )
4259
4360def _binary_name (ctx , name ):
4461 """Returns the name of the binary based on the OS.
@@ -104,13 +121,13 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
104121 d_deps = [d [DInfo ] for d in ctx .attr .deps if DInfo in d ]
105122 compiler_flags = depset (ctx .attr .dopts , transitive = [d .compiler_flags for d in d_deps ])
106123 imports = depset (
107- [paths .join (ctx .label .package , imp ) for imp in ctx .attr .imports ],
124+ [paths .join (ctx .label .workspace_root , ctx . label . package , imp ) for imp in ctx .attr .imports ],
108125 transitive = [d .imports for d in d_deps ],
109126 )
110127 linker_flags = depset (ctx .attr .linkopts , transitive = [d .linker_flags for d in d_deps ])
111128 string_imports = depset (
112- ([ctx .label_package ] if ctx .files .string_srcs else []) +
113- [paths .join (ctx .label .package , imp ) for imp in ctx .attr .string_imports ],
129+ ([paths . join ( ctx .label . workspace_root , ctx . label . package ) ] if ctx .files .string_srcs else []) +
130+ [paths .join (ctx .label .workspace_root , ctx . label . package , imp ) for imp in ctx .attr .string_imports ],
114131 transitive = [d .string_imports for d in d_deps ],
115132 )
116133 versions = depset (ctx .attr .versions , transitive = [d .versions for d in d_deps ])
@@ -162,8 +179,8 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
162179 DInfo (
163180 compiler_flags = compiler_flags ,
164181 imports = depset (
165- [ctx .label .package ] +
166- [paths .join (ctx .label .package , imp ) for imp in ctx .attr .imports ],
182+ [paths . join ( ctx .label .workspace_root , ctx . label . package ) ] +
183+ [paths .join (ctx .label .workspace_root , ctx . label . package , imp ) for imp in ctx .attr .imports ],
167184 transitive = [d .imports for d in d_deps ],
168185 ),
169186 interface_srcs = depset (
@@ -178,12 +195,22 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
178195 ),
179196 linker_flags = linker_flags ,
180197 string_imports = depset (
181- ([ctx .label .package ] if ctx .files .string_srcs else []) +
182- [paths .join (ctx .label .package , imp ) for imp in ctx .attr .string_imports ],
198+ ([paths . join ( ctx .label .workspace_root , ctx . label . package ) ] if ctx .files .string_srcs else []) +
199+ [paths .join (ctx .label .workspace_root , ctx . label . package , imp ) for imp in ctx .attr .string_imports ],
183200 transitive = [d .string_imports for d in d_deps ],
184201 ),
185202 versions = versions ,
186203 ),
187204 ]
188205 else :
189- return [DefaultInfo (executable = output )]
206+ env_with_expansions = {
207+ k : expand_variables (ctx , ctx .expand_location (v , ctx .files .data ), [output ], "env" )
208+ for k , v in ctx .attr .env .items ()
209+ }
210+ return [
211+ DefaultInfo (
212+ executable = output ,
213+ runfiles = ctx .runfiles (files = ctx .files .data ),
214+ ),
215+ RunEnvironmentInfo (environment = env_with_expansions ),
216+ ]
0 commit comments