C内核流量计算方法优化,前端文件名称读取优化
This commit is contained in:
1 parent
5d5a2e1843
commit
808c484f5b
94 files changed
+20163
-4418
No files matched your search
@@ -33,10 +33,16 @@ def _hash(path: Path) -> str:
|
||||
|
||||
|
||||
def toolchain() -> tuple[str, Path, str]:
|
||||
if os.name != "nt" and not sys.platform.startswith("linux"):
|
||||
raise RuntimeError("Native builds support Windows and Linux; this platform is not supported.")
|
||||
compiler = os.environ.get("SIMULATION_NATIVE_CC") or shutil.which("gcc")
|
||||
if not compiler:
|
||||
raise RuntimeError("C compiler not found; set SIMULATION_NATIVE_CC to gcc.")
|
||||
base = Path(os.environ.get("SUNDIALS_ROOT", str(Path(sys.base_prefix) / "Library")))
|
||||
configured = os.environ.get("SUNDIALS_ROOT")
|
||||
candidates = ([Path(configured)] if configured else
|
||||
[Path(sys.base_prefix) / "Library"] if os.name == "nt" else
|
||||
[Path(sys.prefix) / "native/sundials-7.4.0", Path("/usr/local"), Path("/usr")])
|
||||
base = next((path for path in candidates if (path / "include/cvode/cvode.h").is_file()), candidates[0])
|
||||
if not (base / "include/cvode/cvode.h").is_file():
|
||||
raise RuntimeError("SUNDIALS C development files not found; set SUNDIALS_ROOT.")
|
||||
version = subprocess.run([compiler, "--version"], capture_output=True, text=True, check=True, timeout=15).stdout.splitlines()[0]
|
||||
@@ -48,6 +54,7 @@ def build_native(program: NativeProgram, *, cache_dir: Path | None = None) -> Na
|
||||
compiler, sundials, compiler_version = toolchain()
|
||||
runtime = sorted(NATIVE.rglob("*.c")) + sorted((NATIVE / "include").glob("*.h"))
|
||||
flags = ["-std=c11", "-O3", "-Wall", "-Wextra", "-Werror", "-ffp-contract=off", "-fno-fast-math"]
|
||||
executable_name = "model.exe" if os.name == "nt" else "model"
|
||||
if os.name == "nt":
|
||||
flags += ["-D__USE_MINGW_ANSI_STDIO=1", "-static-libgcc"]
|
||||
libraries = [sundials / "lib" / f"sundials_{name}.lib" for name in LIBRARIES]
|
||||
@@ -55,8 +62,20 @@ def build_native(program: NativeProgram, *, cache_dir: Path | None = None) -> Na
|
||||
vc_runtime = sundials / "bin/vcruntime140.dll"
|
||||
if vc_runtime.is_file():
|
||||
dlls.append(vc_runtime)
|
||||
elif sys.platform.startswith("linux"):
|
||||
flags += ["-D_POSIX_C_SOURCE=200809L"]
|
||||
library_dirs = [sundials / "lib", sundials / "lib64", sundials / "lib/x86_64-linux-gnu"]
|
||||
library_dir = next((path for path in library_dirs if all(
|
||||
(path / f"libsundials_{name}.a").is_file() for name in LIBRARIES)), None)
|
||||
if library_dir is None:
|
||||
raise RuntimeError("SUNDIALS static libraries not found; build with BUILD_STATIC_LIBS=ON and set SUNDIALS_ROOT.")
|
||||
libraries = [library_dir / f"libsundials_{name}.a" for name in LIBRARIES]
|
||||
dlls = []
|
||||
else:
|
||||
raise RuntimeError("Native v1 build packaging currently supports Windows x64; Linux packaging is pending.")
|
||||
raise RuntimeError("Native builds support Windows and Linux; this platform is not supported.")
|
||||
link_libraries = list(map(str, libraries))
|
||||
if sys.platform.startswith("linux"):
|
||||
link_libraries = ["-Wl,--start-group", *link_libraries, "-Wl,--end-group"]
|
||||
sources = {str(p.relative_to(ROOT)): _hash(p) for p in runtime}
|
||||
sources["native/THIRD_PARTY_NOTICES.txt"] = _hash(NATIVE / "THIRD_PARTY_NOTICES.txt")
|
||||
dependencies = {str(p.name): _hash(p) for p in libraries + dlls}
|
||||
@@ -75,7 +94,7 @@ def build_native(program: NativeProgram, *, cache_dir: Path | None = None) -> Na
|
||||
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
if all((target / name).is_file() and _hash(target / name) == digest
|
||||
for name, digest in manifest["artifacts"].items()):
|
||||
return NativeBuild(target / "model.exe", manifest, True, time.perf_counter()-start)
|
||||
return NativeBuild(target / executable_name, manifest, True, time.perf_counter()-start)
|
||||
raise RuntimeError(f"Native cache integrity check failed: {target}")
|
||||
cache.mkdir(parents=True, exist_ok=True)
|
||||
stage = Path(tempfile.mkdtemp(prefix="building-", dir=cache))
|
||||
@@ -84,7 +103,7 @@ def build_native(program: NativeProgram, *, cache_dir: Path | None = None) -> Na
|
||||
command = [compiler, *flags, "-I", str(stage), "-I", str(NATIVE / "include"),
|
||||
"-I", str(sundials / "include"), str(stage / "model.c"),
|
||||
*[str(p) for p in runtime if p.suffix == ".c"],
|
||||
*map(str, libraries), "-lm", "-o", str(stage / "model.exe")]
|
||||
*link_libraries, "-lm", "-o", str(stage / executable_name)]
|
||||
result = subprocess.run(command, capture_output=True, text=True, timeout=120)
|
||||
(stage / "build.log").write_text(result.stdout + result.stderr, encoding="utf-8")
|
||||
if result.returncode:
|
||||
@@ -116,4 +135,4 @@ def build_native(program: NativeProgram, *, cache_dir: Path | None = None) -> Na
|
||||
if stage.resolve().parent != cache:
|
||||
raise RuntimeError("Unexpected native build staging directory.")
|
||||
shutil.rmtree(stage)
|
||||
return NativeBuild(target / "model.exe", manifest, False, time.perf_counter()-start)
|
||||
return NativeBuild(target / executable_name, manifest, False, time.perf_counter()-start)
|
||||
Reference in new issue
Block a user