3
3
load ("@bazel_skylib//lib:paths.bzl" , "paths" )
4
4
load ("@bazel_skylib//rules:common_settings.bzl" , "BuildSettingInfo" )
5
5
load ("@bazel_tools//tools/cpp:toolchain_utils.bzl" , "find_cpp_toolchain" , "use_cpp_toolchain" )
6
+ load ("@rules_python//python:py_info.bzl" , "PyInfo" )
6
7
load ("//mojo:providers.bzl" , "MojoInfo" )
7
8
load (":utils.bzl" , "MOJO_EXTENSIONS" , "collect_mojoinfo" )
8
9
@@ -16,7 +17,7 @@ _ATTRS = {
16
17
),
17
18
"copts" : attr .string_list (),
18
19
"deps" : attr .label_list (
19
- providers = [[CcInfo ], [MojoInfo ]],
20
+ providers = [[CcInfo ], [MojoInfo ], [ PyInfo ] ],
20
21
),
21
22
"data" : attr .label_list (allow_files = True ),
22
23
"enable_assertions" : attr .bool (default = True ),
@@ -28,6 +29,7 @@ _ATTRS = {
28
29
29
30
_TOOLCHAINS = use_cpp_toolchain () + [
30
31
"//:toolchain_type" ,
32
+ "@bazel_tools//tools/python:toolchain_type" ,
31
33
]
32
34
33
35
def _find_main (name , srcs , main ):
@@ -56,8 +58,9 @@ def _find_main(name, srcs, main):
56
58
fail ("Multiple Mojo files provided, but no main file specified. Please set 'main = \" foo.mojo\" ' to disambiguate." )
57
59
58
60
def _mojo_binary_test_implementation (ctx ):
59
- mojo_toolchain = ctx .toolchains ["//:toolchain_type" ].mojo_toolchain_info
60
61
cc_toolchain = find_cpp_toolchain (ctx )
62
+ mojo_toolchain = ctx .toolchains ["//:toolchain_type" ].mojo_toolchain_info
63
+ py_toolchain = ctx .toolchains ["@bazel_tools//tools/python:toolchain_type" ]
61
64
62
65
object_file = ctx .actions .declare_file (ctx .label .name + ".lo" )
63
66
args = ctx .actions .args ()
@@ -138,20 +141,53 @@ def _mojo_binary_test_implementation(ctx):
138
141
139
142
data = ctx .attr .data
140
143
runfiles = ctx .runfiles (ctx .files .data )
141
- transitive_runfiles = []
144
+ transitive_runfiles = [
145
+ ctx .runfiles (transitive_files = py_toolchain .py3_runtime .files ),
146
+ ]
142
147
for target in data :
143
148
transitive_runfiles .append (target [DefaultInfo ].default_runfiles )
144
149
145
150
# Collect transitive shared libraries that must exist at runtime
151
+ python_imports = []
146
152
for target in ctx .attr .deps + mojo_toolchain .implicit_deps :
153
+ transitive_runfiles .append (target [DefaultInfo ].default_runfiles )
154
+
155
+ if PyInfo in target :
156
+ python_imports .append (target [PyInfo ].imports )
157
+ transitive_runfiles .append (
158
+ ctx .runfiles (transitive_files = target [PyInfo ].transitive_sources ),
159
+ )
160
+
147
161
if CcInfo not in target :
148
162
continue
149
163
for linker_input in target [CcInfo ].linking_context .linker_inputs .to_list ():
150
164
for library in linker_input .libraries :
151
165
if library .dynamic_library and not library .pic_static_library and not library .static_library :
152
166
transitive_runfiles .append (ctx .runfiles (transitive_files = depset ([library .dynamic_library ])))
153
167
154
- runtime_env = dict (ctx .attr .env )
168
+ python_path = ""
169
+ for path in depset (transitive = python_imports ).to_list ():
170
+ python_path += "../" + path + ":"
171
+
172
+ # https://github.com/bazelbuild/rules_python/issues/2262
173
+ libpython = None
174
+ for file in py_toolchain .py3_runtime .files .to_list ():
175
+ if file .basename .startswith ("libpython" ):
176
+ libpython = file .short_path
177
+ break # if there are multiple any of them should work and they are likely symlinks to each other
178
+
179
+ if not libpython :
180
+ fail ("failed to find libpython, please report this at https://github.com/modular/rules_mojo/issues" )
181
+
182
+ runtime_env = dict (ctx .attr .env ) | {
183
+ "MODULAR_PYTHON_EXECUTABLE" : py_toolchain .py3_runtime .interpreter .short_path ,
184
+ "MOJO_PYTHON" : py_toolchain .py3_runtime .interpreter .short_path ,
185
+ "MOJO_PYTHON_LIBRARY" : libpython ,
186
+ "PYTHONEXECUTABLE" : py_toolchain .py3_runtime .interpreter .short_path ,
187
+ "PYTHONNOUSERSITE" : "affirmative" ,
188
+ "PYTHONPATH" : python_path ,
189
+ "PYTHONSAFEPATH" : "affirmative" ,
190
+ }
155
191
for key , value in runtime_env .items ():
156
192
runtime_env [key ] = ctx .expand_make_variables (
157
193
"env" ,
0 commit comments