@@ -101,6 +101,7 @@ def __init__(
101101 target_file : str ,
102102 is_binary : bool = False ,
103103 copy_include_files : bool = False ,
104+ aggressive_strip : bool = False ,
104105 ** kwargs : Any ,
105106 ):
106107 super ().__init__ (name = name , sources = [], ** kwargs )
@@ -115,6 +116,11 @@ def __init__(
115116 self .is_binary = is_binary
116117 self .copy_include_files = copy_include_files
117118
119+ # Determines whether to `strip-all` when building a target for PyPI.
120+ # Critically, this should only be used for cc_binary targets that are run as
121+ # a subprocess, not cc_library targets that are loaded into Python.
122+ self .aggressive_strip = aggressive_strip
123+
118124
119125class BuildBazelExtension (build_ext .build_ext ):
120126 """A command that runs Bazel to build a C/C++ extension."""
@@ -226,10 +232,14 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
226232 "--ui_event_filters=ERROR" ,
227233 f"--symlink_prefix={ temp_path / 'bazel-' } " ,
228234 "--compilation_mode=opt" ,
235+ "--strip=always" ,
229236 f"--cxxopt={ '/std:c++20' if IS_WINDOWS else '-std=c++20' } " ,
230237 f"--@rules_python//python/config_settings:python_version={ python_version } " ,
231238 ]
232239
240+ if ext .aggressive_strip :
241+ bazel_argv .append ("--stripopt=--strip-all" )
242+
233243 if is_cibuildwheel () and IS_LINUX :
234244 # TODO(#2249): OpenMP is disabled for manylinux, as libomp is not on the allowed libraries list,
235245 # and statically bundling OpenMP is non-trivial and left as future work.
@@ -288,9 +298,10 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
288298 | stat .S_IXOTH ,
289299 )
290300
291- # Also copy binaries to project root so they can be included in data_files
301+ # Also copy binaries to project root so they are visible when running from
302+ # Python as a subprocess.
292303 root_path = Path (ext .target_file )
293- print (f"Copying { srcdir_path } to { root_path } for data_files " )
304+ print (f"Copying { srcdir_path } to { root_path } " )
294305 shutil .copyfile (srcdir_path , root_path )
295306 os .chmod (
296307 root_path ,
@@ -313,27 +324,30 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
313324 ext_modules = [
314325 BazelExtension (
315326 name = "heir_py._heir_opt" ,
316- bazel_target = "//tools:heir-opt" ,
317- generated_so_file = Path ("tools" ) / "heir-opt" ,
327+ bazel_target = "//tools:heir-opt.stripped " ,
328+ generated_so_file = Path ("tools" ) / "heir-opt.stripped " ,
318329 target_file = "heir-opt" ,
319330 py_limited_api = py_limited_api ,
320331 is_binary = True ,
332+ aggressive_strip = is_cibuildwheel (),
321333 ),
322334 BazelExtension (
323335 name = "heir_py._heir_translate" ,
324- bazel_target = "//tools:heir-translate" ,
325- generated_so_file = Path ("tools" ) / "heir-translate" ,
336+ bazel_target = "//tools:heir-translate.stripped " ,
337+ generated_so_file = Path ("tools" ) / "heir-translate.stripped " ,
326338 target_file = "heir-translate" ,
327339 py_limited_api = py_limited_api ,
328340 is_binary = True ,
341+ aggressive_strip = is_cibuildwheel (),
329342 ),
330343 BazelExtension (
331344 name = "heir_py._abc" ,
332- bazel_target = "@abc//:abc_bin" ,
333- generated_so_file = Path ("external" ) / "abc+" / "abc_bin" ,
345+ bazel_target = "@abc//:abc_bin.stripped " ,
346+ generated_so_file = Path ("external" ) / "abc+" / "abc_bin.stripped " ,
334347 target_file = "abc_bin" ,
335348 py_limited_api = py_limited_api ,
336349 is_binary = True ,
350+ aggressive_strip = is_cibuildwheel (),
337351 ),
338352 BazelExtension (
339353 name = "heir_py._libopenfhe" ,
@@ -342,8 +356,8 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
342356 target_file = "libopenfhe.so" ,
343357 py_limited_api = py_limited_api ,
344358 copy_include_files = True ,
359+ aggressive_strip = False ,
345360 ),
346361 ],
347- data_files = [("bin" , ["heir-opt" , "heir-translate" ])],
348362 options = options ,
349363)
0 commit comments