|
2 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this file, |
3 | 3 | # You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 | # |
5 | | -# Copyright (c) 2014-2023, Lars Asplund [email protected] |
| 5 | +# Copyright (c) 2014-2024, Lars Asplund [email protected] |
6 | 6 |
|
7 | 7 | """ |
8 | 8 | Temporary helper module to compile C-code used by python_pkg. |
@@ -212,3 +212,59 @@ def compile_vhpidirect_nvc_application(run_script_root, vu): |
212 | 212 | print(proc.stdout) |
213 | 213 | print(proc.stderr) |
214 | 214 | raise RuntimeError("Failed to link NVC VHPIDIRECT application") |
| 215 | + |
| 216 | + |
| 217 | +def compile_vhpidirect_ghdl_application(run_script_root, vu): # pylint: disable=unused-argument |
| 218 | + """ |
| 219 | + Compile VHPIDIRECT application for GHDL. |
| 220 | + """ |
| 221 | + # TODO: Avoid putting in root # pylint: disable=fixme |
| 222 | + path_to_shared_lib = (run_script_root).resolve() |
| 223 | + if not path_to_shared_lib.exists(): |
| 224 | + path_to_shared_lib.mkdir(parents=True, exist_ok=True) |
| 225 | + shared_lib = path_to_shared_lib / "python.so" |
| 226 | + path_to_python_include = ( |
| 227 | + Path(sys.executable).parent.parent.resolve() / "include" / f"python{sys.version_info[0]}.{sys.version_info[1]}" |
| 228 | + ) |
| 229 | + path_to_python_libs = Path(sys.executable).parent.parent.resolve() / "bin" |
| 230 | + python_shared_lib = f"libpython{sys.version_info[0]}.{sys.version_info[1]}" |
| 231 | + path_to_python_pkg = Path(__file__).parent.resolve() / "vhdl" / "python" / "src" |
| 232 | + |
| 233 | + c_file_names = ["python_pkg_vhpidirect_ghdl.c", "python_pkg.c"] |
| 234 | + |
| 235 | + for c_file_name in c_file_names: |
| 236 | + args = [ |
| 237 | + "gcc", |
| 238 | + "-c", |
| 239 | + "-I", |
| 240 | + str(path_to_python_include), |
| 241 | + str(path_to_python_pkg / c_file_name), |
| 242 | + "-o", |
| 243 | + str(path_to_shared_lib / (c_file_name[:-1] + "o")), |
| 244 | + ] |
| 245 | + |
| 246 | + proc = subprocess.run(args, capture_output=True, text=True, check=False, cwd=str(path_to_shared_lib / "..")) |
| 247 | + if proc.returncode != 0: |
| 248 | + print(proc.stdout) |
| 249 | + print(proc.stderr) |
| 250 | + raise RuntimeError("Failed to compile GHDL VHPIDIRECT application") |
| 251 | + |
| 252 | + args = [ |
| 253 | + "gcc", |
| 254 | + "-shared", |
| 255 | + "-fPIC", |
| 256 | + "-o", |
| 257 | + str(shared_lib), |
| 258 | + str(path_to_shared_lib / "python_pkg.o"), |
| 259 | + str(path_to_shared_lib / "python_pkg_vhpidirect_ghdl.o"), |
| 260 | + "-l", |
| 261 | + python_shared_lib, |
| 262 | + "-L", |
| 263 | + str(path_to_python_libs), |
| 264 | + ] |
| 265 | + |
| 266 | + proc = subprocess.run(args, capture_output=True, text=True, check=False, cwd=str(path_to_shared_lib / "..")) |
| 267 | + if proc.returncode != 0: |
| 268 | + print(proc.stdout) |
| 269 | + print(proc.stderr) |
| 270 | + raise RuntimeError("Failed to link GHDL VHPIDIRECT application") |
0 commit comments