Skip to content

Commit 95771bc

Browse files
committed
TEMPORARY: strip ONNX_API from libonnx function.h templates on win
<details><summary>Claude's draft</summary> With the unvendoring ported to bld.bat, win-64 now configures and generates code, then fails compiling onnxruntime_graph (bert_defs.cc, contrib_defs.cc, diffusion_defs.cc): onnx/defs/function.h(96): error C2491: 'onnx::FunctionBodyHelper::Const': definition of dllimport function not allowed libonnx's patch 0007 makes ONNX_API expand to __declspec(dllimport) for consumers, and function.h puts ONNX_API on nine member function templates defined in the header. MSVC forbids dllimport on a template definition (the same reason patch 0008 already dropped it from RegisterOpSetSchema). Non-template inline members and declaration-only templates are fine, so only function.h is affected. The real fix belongs in onnx-feedstock. Until libonnx carries it, strip ONNX_API from those templates in the host env's copy of the header before building. Revert this commit once a fixed libonnx is published. Resume this Claude session: ``` cd /home/mark/git/feedstock claude --resume 3cf41599-db98-4c6f-a67a-ee7dcacda8b4 ``` </details> Claude-Session: https://claude.ai/code/session_01Jxvsn2W6MfjGKXcabWVHNe
1 parent 1eebb5d commit 95771bc

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎recipe/bld.bat‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ if errorlevel 1 exit 1
3838
python onnxruntime\lora\adapter_format\compile_schema.py --flatc "%BUILD_PREFIX%\Library\bin\flatc.exe"
3939
if errorlevel 1 exit 1
4040

41+
:: TEMPORARY: libonnx's function.h puts ONNX_API (dllimport) on header-defined
42+
:: member function templates, which MSVC rejects with C2491. Remove once libonnx is fixed.
43+
python "%RECIPE_DIR%\strip_onnx_api_from_templates.py" "%LIBRARY_INC%\onnx\defs\function.h"
44+
if errorlevel 1 exit 1
45+
4146
:: Since 1.29.0 telemetry is opt-out rather than opt-in; a conda-forge package should
4247
:: not report usage to Microsoft, so it is disabled explicitly on every platform.
4348
python tools/ci_build/build.py ^
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""TEMPORARY: make libonnx's onnx/defs/function.h usable from MSVC consumers.
2+
3+
libonnx on win-64 defines ONNX_API as __declspec(dllimport) for consumers,
4+
but function.h marks member function templates defined in the header with
5+
ONNX_API, which MSVC rejects (C2491: definition of dllimport function not
6+
allowed). Strip ONNX_API from those templates in the host environment's copy
7+
of the header. Remove once libonnx carries the fix.
8+
"""
9+
10+
import pathlib
11+
import re
12+
import sys
13+
14+
header = pathlib.Path(sys.argv[1])
15+
lines = header.read_text().splitlines(keepends=True)
16+
changed = 0
17+
for i in range(1, len(lines)):
18+
if lines[i - 1].lstrip().startswith("template") and re.match(r"\s*ONNX_API\s", lines[i]):
19+
lines[i] = re.sub(r"ONNX_API\s+", "", lines[i], count=1)
20+
changed += 1
21+
header.write_text("".join(lines))
22+
print(f"stripped ONNX_API from {changed} templates in {header}")

0 commit comments

Comments
 (0)