@@ -57,9 +57,34 @@ def _build_contract_data(compilation_unit: "CompilationUnit") -> Dict:
5757 return contracts
5858
5959
60+ def _export_link_info (compilation_unit : "CompilationUnit" , key : str , autolink_path : str ) -> None :
61+ """Export linking information to a separate file"""
62+
63+ # Get library addresses if they exist
64+ library_addresses = {}
65+ if compilation_unit .crytic_compile .libraries :
66+ library_addresses = {
67+ name : f"0x{ addr :040x} "
68+ for name , addr in compilation_unit .crytic_compile .libraries .items ()
69+ }
70+
71+ # Filter deployment order to only include libraries that have addresses
72+ full_deployment_order = compilation_unit .crytic_compile .deployment_order or []
73+ filtered_deployment_order = [lib for lib in full_deployment_order if lib in library_addresses ]
74+
75+ # Create autolink output with deployment order and library addresses
76+ autolink_output = {
77+ "deployment_order" : filtered_deployment_order ,
78+ "library_addresses" : library_addresses ,
79+ }
80+
81+ with open (autolink_path , "w" , encoding = "utf8" ) as file_desc :
82+ json .dump (autolink_output , file_desc , indent = 2 )
83+
84+
6085def export_to_solc_from_compilation_unit (
6186 compilation_unit : "CompilationUnit" , key : str , export_dir : str
62- ) -> Optional [str ]:
87+ ) -> Optional [List [ str ] ]:
6388 """Export the compilation unit to the standard solc output format.
6489 The exported file will be $key.json
6590
@@ -69,7 +94,7 @@ def export_to_solc_from_compilation_unit(
6994 export_dir (str): Export directory
7095
7196 Returns:
72- Optional[str]: path to the file generated
97+ Optional[List[ str]] : path to the files generated
7398 """
7499 contracts = _build_contract_data (compilation_unit )
75100
@@ -88,7 +113,16 @@ def export_to_solc_from_compilation_unit(
88113
89114 with open (path , "w" , encoding = "utf8" ) as file_desc :
90115 json .dump (output , file_desc )
91- return path
116+
117+ paths = [path ]
118+
119+ # Export link info if compile_autolink or compile_libraries was used
120+ if compilation_unit .crytic_compile .libraries :
121+ link_path = os .path .join (export_dir , f"{ key } .link" )
122+ _export_link_info (compilation_unit , key , link_path )
123+ paths .append (link_path )
124+
125+ return paths
92126 return None
93127
94128
@@ -110,17 +144,18 @@ def export_to_solc(crytic_compile: "CryticCompile", **kwargs: str) -> List[str]:
110144
111145 if len (crytic_compile .compilation_units ) == 1 :
112146 compilation_unit = list (crytic_compile .compilation_units .values ())[0 ]
113- path = export_to_solc_from_compilation_unit (compilation_unit , "combined_solc" , export_dir )
114- if path :
115- return [ path ]
147+ paths = export_to_solc_from_compilation_unit (compilation_unit , "combined_solc" , export_dir )
148+ if paths :
149+ return paths
116150 return []
117151
118- paths = []
152+ all_paths = []
119153 for key , compilation_unit in crytic_compile .compilation_units .items ():
120- path = export_to_solc_from_compilation_unit (compilation_unit , key , export_dir )
121- if path :
122- paths .append (path )
123- return paths
154+ paths = export_to_solc_from_compilation_unit (compilation_unit , key , export_dir )
155+ if paths :
156+ all_paths .extend (paths )
157+
158+ return all_paths
124159
125160
126161class Solc (AbstractPlatform ):
0 commit comments