@@ -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,17 @@ 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+ if IS_LINUX :
242+ bazel_argv .append ("--stripopt=--strip-all" )
243+ elif IS_MAC :
244+ bazel_argv .append ("--stripopt=-S" )
245+
233246 if is_cibuildwheel () and IS_LINUX :
234247 # TODO(#2249): OpenMP is disabled for manylinux, as libomp is not on the allowed libraries list,
235248 # and statically bundling OpenMP is non-trivial and left as future work.
@@ -288,9 +301,10 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
288301 | stat .S_IXOTH ,
289302 )
290303
291- # Also copy binaries to project root so they can be included in data_files
304+ # Also copy binaries to project root so they are visible when running from
305+ # Python as a subprocess.
292306 root_path = Path (ext .target_file )
293- print (f"Copying { srcdir_path } to { root_path } for data_files " )
307+ print (f"Copying { srcdir_path } to { root_path } " )
294308 shutil .copyfile (srcdir_path , root_path )
295309 os .chmod (
296310 root_path ,
@@ -313,27 +327,30 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
313327 ext_modules = [
314328 BazelExtension (
315329 name = "heir_py._heir_opt" ,
316- bazel_target = "//tools:heir-opt" ,
317- generated_so_file = Path ("tools" ) / "heir-opt" ,
330+ bazel_target = "//tools:heir-opt.stripped " ,
331+ generated_so_file = Path ("tools" ) / "heir-opt.stripped " ,
318332 target_file = "heir-opt" ,
319333 py_limited_api = py_limited_api ,
320334 is_binary = True ,
335+ aggressive_strip = is_cibuildwheel (),
321336 ),
322337 BazelExtension (
323338 name = "heir_py._heir_translate" ,
324- bazel_target = "//tools:heir-translate" ,
325- generated_so_file = Path ("tools" ) / "heir-translate" ,
339+ bazel_target = "//tools:heir-translate.stripped " ,
340+ generated_so_file = Path ("tools" ) / "heir-translate.stripped " ,
326341 target_file = "heir-translate" ,
327342 py_limited_api = py_limited_api ,
328343 is_binary = True ,
344+ aggressive_strip = is_cibuildwheel (),
329345 ),
330346 BazelExtension (
331347 name = "heir_py._abc" ,
332- bazel_target = "@abc//:abc_bin" ,
333- generated_so_file = Path ("external" ) / "abc+" / "abc_bin" ,
348+ bazel_target = "@abc//:abc_bin.stripped " ,
349+ generated_so_file = Path ("external" ) / "abc+" / "abc_bin.stripped " ,
334350 target_file = "abc_bin" ,
335351 py_limited_api = py_limited_api ,
336352 is_binary = True ,
353+ aggressive_strip = is_cibuildwheel (),
337354 ),
338355 BazelExtension (
339356 name = "heir_py._libopenfhe" ,
@@ -342,8 +359,8 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
342359 target_file = "libopenfhe.so" ,
343360 py_limited_api = py_limited_api ,
344361 copy_include_files = True ,
362+ aggressive_strip = False ,
345363 ),
346364 ],
347- data_files = [("bin" , ["heir-opt" , "heir-translate" ])],
348365 options = options ,
349366)
0 commit comments