|
7 | 7 | from pathlib import Path
|
8 | 8 | from typing import TYPE_CHECKING, List, Optional, Dict, TypeVar
|
9 | 9 |
|
10 |
| -import toml |
| 10 | +import json |
11 | 11 |
|
12 | 12 | from crytic_compile.platform.abstract_platform import AbstractPlatform, PlatformConfig
|
13 | 13 | from crytic_compile.platform.types import Type
|
@@ -63,7 +63,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
|
63 | 63 | compile_all = kwargs.get("foundry_compile_all", False)
|
64 | 64 |
|
65 | 65 | if not compile_all:
|
66 |
| - foundry_config = self.config(str(crytic_compile.working_dir.absolute())) |
| 66 | + foundry_config = self.config(self._target) |
67 | 67 | if foundry_config:
|
68 | 68 | compilation_command += [
|
69 | 69 | "--skip",
|
@@ -118,68 +118,62 @@ def is_supported(target: str, **kwargs: str) -> bool:
|
118 | 118 | return os.path.isfile(os.path.join(target, "foundry.toml"))
|
119 | 119 |
|
120 | 120 | @staticmethod
|
121 |
| - def config(working_dir: str) -> Optional[PlatformConfig]: |
| 121 | + def config(target: str) -> Optional[PlatformConfig]: |
122 | 122 | """Return configuration data that should be passed to solc, such as remappings.
|
123 | 123 |
|
124 | 124 | Args:
|
125 |
| - working_dir (str): path to the working directory |
| 125 | + target (str): path to the target |
126 | 126 |
|
127 | 127 | Returns:
|
128 | 128 | Optional[PlatformConfig]: Platform configuration data such as optimization, remappings...
|
129 | 129 | """
|
130 | 130 | result = PlatformConfig()
|
131 |
| - result.remappings = ( |
132 |
| - subprocess.run(["forge", "remappings"], stdout=subprocess.PIPE, check=True) |
133 |
| - .stdout.decode("utf-8") |
134 |
| - .replace("\n", " ") |
135 |
| - .strip() |
| 131 | + LOGGER.info("'forge config --json' running") |
| 132 | + json_config = json.loads( |
| 133 | + subprocess.run(["forge", "config", "--json"], stdout=subprocess.PIPE, check=True).stdout |
136 | 134 | )
|
137 |
| - with open("foundry.toml", "r", encoding="utf-8") as f: |
138 |
| - foundry_toml = toml.loads(f.read()) |
139 |
| - default_profile = foundry_toml["profile"]["default"] |
140 |
| - |
141 |
| - def lookup_by_keys(keys: List[str], dictionary: Dict[str, T]) -> Optional[T]: |
142 |
| - for key in keys: |
143 |
| - if key in dictionary: |
144 |
| - return dictionary[key] |
145 |
| - return None |
146 |
| - |
147 |
| - # Foundry supports snake and kebab case. |
148 |
| - result.solc_version = lookup_by_keys( |
149 |
| - ["solc", "solc_version", "solc-version"], default_profile |
150 |
| - ) |
151 |
| - via_ir = lookup_by_keys(["via_ir", "via-ir"], default_profile) |
152 |
| - if via_ir: |
153 |
| - result.via_ir = via_ir |
154 |
| - result.allow_paths = lookup_by_keys(["allow_paths", "allow-paths"], default_profile) |
155 |
| - |
156 |
| - if "offline" in default_profile: |
157 |
| - result.offline = default_profile["offline"] |
158 |
| - if "optimizer" in default_profile: |
159 |
| - result.optimizer = default_profile["optimizer"] |
160 |
| - else: |
161 |
| - # Default to true |
162 |
| - result.optimizer = True |
163 |
| - optimizer_runs = lookup_by_keys(["optimizer_runs", "optimizer-runs"], default_profile) |
164 |
| - if optimizer_runs is None: |
165 |
| - # Default to 200 |
166 |
| - result.optimizer_runs = 200 |
167 |
| - else: |
168 |
| - result.optimizer_runs = optimizer_runs |
169 |
| - evm_version = lookup_by_keys(["evm_version", "evm-version"], default_profile) |
170 |
| - if evm_version is None: |
171 |
| - result.evm_version = evm_version |
172 |
| - else: |
173 |
| - # Default to london |
174 |
| - result.evm_version = "london" |
175 |
| - if "src" in default_profile: |
176 |
| - result.src_path = default_profile["src"] |
177 |
| - if "test" in default_profile: |
178 |
| - result.tests_path = default_profile["test"] |
179 |
| - if "libs" in default_profile: |
180 |
| - result.libs_path = default_profile["libs"] |
181 |
| - if "script" in default_profile: |
182 |
| - result.scripts_path = default_profile["script"] |
| 135 | + result.remappings = json_config["remappings"] |
| 136 | + |
| 137 | + def lookup_by_keys(keys: List[str], dictionary: Dict[str, T]) -> Optional[T]: |
| 138 | + for key in keys: |
| 139 | + if key in dictionary: |
| 140 | + return dictionary[key] |
| 141 | + return None |
| 142 | + |
| 143 | + # Foundry supports snake and kebab case. |
| 144 | + result.solc_version = lookup_by_keys(["solc", "solc_version", "solc-version"], json_config) |
| 145 | + via_ir = lookup_by_keys(["via_ir", "via-ir"], json_config) |
| 146 | + if via_ir: |
| 147 | + result.via_ir = via_ir |
| 148 | + result.allow_paths = lookup_by_keys(["allow_paths", "allow-paths"], json_config) |
| 149 | + |
| 150 | + if "offline" in json_config: |
| 151 | + result.offline = json_config["offline"] |
| 152 | + if "optimizer" in json_config: |
| 153 | + result.optimizer = json_config["optimizer"] |
| 154 | + else: |
| 155 | + # Default to true |
| 156 | + result.optimizer = True |
| 157 | + optimizer_runs = lookup_by_keys(["optimizer_runs", "optimizer-runs"], json_config) |
| 158 | + if optimizer_runs is None: |
| 159 | + # Default to 200 |
| 160 | + result.optimizer_runs = 200 |
| 161 | + else: |
| 162 | + result.optimizer_runs = optimizer_runs |
| 163 | + evm_version = lookup_by_keys(["evm_version", "evm-version"], json_config) |
| 164 | + if evm_version is None: |
| 165 | + result.evm_version = evm_version |
| 166 | + else: |
| 167 | + # Default to london |
| 168 | + result.evm_version = "london" |
| 169 | + if "src" in json_config: |
| 170 | + result.src_path = json_config["src"] |
| 171 | + if "test" in json_config: |
| 172 | + result.tests_path = json_config["test"] |
| 173 | + if "libs" in json_config: |
| 174 | + result.libs_path = json_config["libs"] |
| 175 | + if "script" in json_config: |
| 176 | + result.scripts_path = json_config["script"] |
183 | 177 |
|
184 | 178 | return result
|
185 | 179 |
|
|
0 commit comments