1515
1616# pylint: disable=relative-beyond-top-level,invalid-name,missing-function-docstring,missing-class-docstring
1717
18+ from collections .abc import Callable , Collection
1819import hashlib
1920import io
2021import itertools
2122import os
2223import pathlib
23- from typing import Callable , Collection , List , Optional , cast
24+ from typing import cast
2425
2526from .. import native_rules_cc
2627from ..cmake_builder import CMakeBuilder
4041from ..starlark .scope_common import ScopeCommon
4142from ..starlark .select import Configurable
4243from ..util import cmake_is_true
44+ from ..util import make_relative_path
4345from ..util import partition_by
4446from ..util import quote_list
4547from ..util import quote_path
@@ -68,7 +70,7 @@ class RulesNasmLibrary(ScopeCommon):
6870 def bazel_nasm_library (
6971 self ,
7072 name : str ,
71- visibility : Optional [ List [ RelativeLabel ]] = None ,
73+ visibility : list [ RelativeLabel ] | None = None ,
7274 ** kwargs ,
7375 ):
7476 context = self ._context .snapshot ()
@@ -89,7 +91,7 @@ class RulesNasmCcLibrary(ScopeCommon):
8991 def bazel_nasm_cc_library (
9092 self ,
9193 name : str ,
92- visibility : Optional [ List [ RelativeLabel ]] = None ,
94+ visibility : list [ RelativeLabel ] | None = None ,
9395 ** kwargs ,
9496 ):
9597 context = self ._context .snapshot ()
@@ -145,11 +147,11 @@ def _common_nasm_resolve(
145147 _next : Callable [..., None ],
146148 _context : InvocationContext ,
147149 _target : TargetId ,
148- srcs : Optional [ Configurable [List [RelativeLabel ]]] = None ,
149- hdrs : Optional [ Configurable [List [RelativeLabel ]]] = None ,
150- preincs : Optional [ Configurable [List [RelativeLabel ]]] = None ,
151- copts : Optional [ Configurable [List [str ]]] = None ,
152- includes : Optional [ Configurable [List [str ]]] = None ,
150+ srcs : Configurable [list [RelativeLabel ]] | None = None ,
151+ hdrs : Configurable [list [RelativeLabel ]] | None = None ,
152+ preincs : Configurable [list [RelativeLabel ]] | None = None ,
153+ copts : Configurable [list [str ]] | None = None ,
154+ includes : Configurable [list [str ]] | None = None ,
153155 ** kwargs ,
154156):
155157 """Applies evaluate_configurable to common (and uncommon) nasm arguments."""
@@ -175,7 +177,7 @@ def _construct_nasm_includes(
175177 current_package_id : PackageId ,
176178 source_directory : pathlib .PurePath ,
177179 cmake_binary_dir : pathlib .PurePath ,
178- includes : Optional [ Collection [str ]] = None ,
180+ includes : Collection [str ] | None = None ,
179181):
180182 """Returns the set of includes for the configuration."""
181183
@@ -212,11 +214,11 @@ def _nasm_library_impl(
212214 _context : InvocationContext ,
213215 _target : TargetId ,
214216 * ,
215- srcs : List [TargetId ],
216- hdrs : List [TargetId ],
217- preincs : List [TargetId ],
218- copts : List [str ],
219- includes : List [str ],
217+ srcs : list [TargetId ],
218+ hdrs : list [TargetId ],
219+ preincs : list [TargetId ],
220+ copts : list [str ],
221+ includes : list [str ],
220222 ** kwargs ,
221223):
222224 del kwargs
@@ -229,7 +231,7 @@ def _nasm_library_impl(
229231 srcs_collector = state .collect_targets (srcs )
230232 preincs_collector = state .collect_targets (preincs )
231233
232- cmake_deps : List [CMakeTarget ] = list (
234+ cmake_deps : list [CMakeTarget ] = list (
233235 itertools .chain (
234236 srcs_collector .add_dependencies (),
235237 preincs_collector .add_dependencies (),
@@ -259,12 +261,18 @@ def _nasm_library_impl(
259261 o_ext = _get_obj_suffix (state )
260262
261263 def _output_file (src : pathlib .PurePath ):
264+ # Use relative path for hashing to ensure stability across workspaces.
265+ _ , relative_src = make_relative_path (
266+ src ,
267+ (None , repo .source_directory ),
268+ (None , repo .cmake_binary_dir ),
269+ )
262270 return os .path .join (
263271 repo .cmake_binary_dir ,
264272 "_nasm" ,
265273 _target .target_name ,
266274 hashlib .sha256 (
267- ( _target .as_label () + str ( src )) .encode ("utf-8" )
275+ f" { _target .as_label ()} { relative_src . as_posix () } " .encode ("utf-8" )
268276 ).hexdigest ()[:8 ],
269277 os .path .basename (src ) + o_ext ,
270278 )
@@ -304,13 +312,13 @@ def _emit_nasm_assemble(
304312 out : io .StringIO ,
305313 src : pathlib .PurePath ,
306314 generated_obj : pathlib .PurePath ,
307- copts : List [str ],
308- cmake_deps : List [CMakeTarget ],
315+ copts : list [str ],
316+ cmake_deps : list [CMakeTarget ],
309317):
310318 """Generates an NASM library target."""
311319 out .write (f"""add_custom_command(
312320 OUTPUT { quote_path (generated_obj )}
313- DEPENDS { quote_path_list ([src ] + cast (List [str ], cmake_deps ))}
321+ DEPENDS { quote_path_list ([src ] + cast (list [str ], cmake_deps ))}
314322 COMMAND ${{CMAKE_ASM_NASM_COMPILER}}
315323 -f ${{CMAKE_ASM_NASM_OBJECT_FORMAT}}
316324 ${{CMAKE_ASM_NASM_FLAGS}}
@@ -329,11 +337,11 @@ def _nasm_cc_library_builtin_impl(
329337 _context : InvocationContext ,
330338 _target : TargetId ,
331339 * ,
332- srcs : List [TargetId ],
333- hdrs : List [TargetId ],
334- preincs : List [TargetId ],
335- copts : List [str ],
336- includes : List [str ],
340+ srcs : list [TargetId ],
341+ hdrs : list [TargetId ],
342+ preincs : list [TargetId ],
343+ copts : list [str ],
344+ includes : list [str ],
337345 alwayslink : bool = False ,
338346 linkstatic : bool = False ,
339347 ** kwargs ,
@@ -348,7 +356,7 @@ def _nasm_cc_library_builtin_impl(
348356 srcs_collector = state .collect_targets (srcs )
349357 preincs_collector = state .collect_targets (preincs )
350358
351- cmake_deps : List [CMakeTarget ] = list (
359+ cmake_deps : list [CMakeTarget ] = list (
352360 itertools .chain (
353361 srcs_collector .add_dependencies (),
354362 preincs_collector .add_dependencies (),
@@ -430,9 +438,9 @@ def _emit_nasm_sources(
430438 out : io .StringIO ,
431439 target_name : CMakeTarget ,
432440 srcs : set [pathlib .PurePath ],
433- includes : List [pathlib .PurePath ],
434- copts : List [str ],
435- cmake_deps : List [CMakeTarget ],
441+ includes : list [pathlib .PurePath ],
442+ copts : list [str ],
443+ cmake_deps : list [CMakeTarget ],
436444):
437445 """Generates an NASM library target."""
438446 _sep = "\n "
0 commit comments