|
5 | 5 | import os
|
6 | 6 | import subprocess
|
7 | 7 | from pathlib import Path
|
8 |
| -from typing import TYPE_CHECKING, List, Optional, Dict, TypeVar |
| 8 | +from typing import TYPE_CHECKING, List, Optional, 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",
|
@@ -122,64 +122,34 @@ def config(working_dir: 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 | + working_dir (str): path to the working_dir |
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( |
| 134 | + ["forge", "config", "--json"], cwd=working_dir, stdout=subprocess.PIPE, check=True |
| 135 | + ).stdout |
136 | 136 | )
|
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"] |
| 137 | + |
| 138 | + # Solc configurations |
| 139 | + result.solc_version = json_config.get("solc") |
| 140 | + result.via_ir = json_config.get("via_ir") |
| 141 | + result.allow_paths = json_config.get("allow_paths") |
| 142 | + result.offline = json_config.get("offline") |
| 143 | + result.evm_version = json_config.get("evm_version") |
| 144 | + result.optimizer = json_config.get("optimizer") |
| 145 | + result.optimizer_runs = json_config.get("optimizer_runs") |
| 146 | + result.remappings = json_config.get("remappings") |
| 147 | + |
| 148 | + # Foundry project configurations |
| 149 | + result.src_path = json_config.get("src") |
| 150 | + result.tests_path = json_config.get("test") |
| 151 | + result.libs_path = json_config.get("libs") |
| 152 | + result.scripts_path = json_config.get("script") |
183 | 153 |
|
184 | 154 | return result
|
185 | 155 |
|
|
0 commit comments