Skip to content

Commit 1e5deb2

Browse files
committed
feat: PoC of auto-generating solc-remaps file
1 parent 1b2998e commit 1e5deb2

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

crytic_compile/platform/etherscan.py

+45
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,51 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
424424
via_ir=via_ir_enabled,
425425
)
426426

427+
# Process filenames in target compilationUnit to build solc_remaps.txt
428+
remaps = self.solc_remaps_generator(compilation_unit)
429+
430+
# Convert to string to use with crytic-compile Target.sol --solc-remaps $(cat solc_remaps.txt)
431+
remaps_str = ",".join(remaps)
432+
with open(os.path.join(working_dir, "solc_remaps.txt"), "w") as f:
433+
f.write(f'"{remaps_str}"')
434+
435+
def solc_remaps_generator(self, compilationTarget: CompilationUnit) -> List[str]:
436+
"""Generate remapings for --solc-remaps argument. Uses absolute paths.
437+
438+
Args:
439+
compilationTarget (CompilationUnit): current compilation target
440+
441+
Returns:
442+
List[str]: List of remappings deduced from filenames and compilationUnit.source_units
443+
"""
444+
solc_remaps = []
445+
processed_remaps = set() # Avoid duplicates
446+
source_units = compilationTarget.source_units
447+
for filename, source_unit in source_units.items():
448+
short_name = filename.short
449+
if short_name.startswith("@"):
450+
remap_name = short_name.split("/", 1)[0]
451+
452+
# Skip if this remap_name has already been processed
453+
if remap_name in processed_remaps:
454+
continue
455+
456+
remap_path = filename.absolute
457+
458+
# Extract the path up to and including the remap_name directory
459+
remap_index = remap_path.find(remap_name + "/")
460+
if remap_index != -1:
461+
remap_path = remap_path[: remap_index + len(remap_name)]
462+
463+
print(f"{remap_name}={remap_path}")
464+
465+
solc_remaps.append(f"{remap_name}={remap_path}")
466+
467+
# Mark this remap_name as processed
468+
processed_remaps.add(remap_name)
469+
470+
return solc_remaps
471+
427472
def clean(self, **_kwargs: str) -> None:
428473
pass
429474

0 commit comments

Comments
 (0)