Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stubgen: Add wasm2c-generated header importer
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# | ||
# INPUTs: | ||
# IN: full path to header | ||
# OUT: full path to .cmake output | ||
# | ||
|
||
if(NOT IN) | ||
message(FATAL_ERROR "Huh?") | ||
endif() | ||
|
||
if(NOT OUT) | ||
message(FATAL_ERROR "Huh?") | ||
endif() | ||
|
||
file(STRINGS ${IN} header) | ||
|
||
|
||
set(syms) | ||
foreach(l ${header}) | ||
# Parse header line: | ||
# | ||
# Init: | ||
# extern void WASM_RT_ADD_PREFIX(init)(void); | ||
# | ||
# Import Function: | ||
# extern void (*Z_envZ_glClearColorZ_vffff)(f32, f32, f32, f32); | ||
# | ||
# Export Function: | ||
# extern u32 (*WASM_RT_ADD_PREFIX(Z_mainZ_iii))(u32, u32); | ||
# | ||
# Export Variable: | ||
# extern wasm_rt_memory_t (*WASM_RT_ADD_PREFIX(Z_memory)); | ||
# extern wasm_rt_table_t (*WASM_RT_ADD_PREFIX(Z___indirect_function_table)); | ||
# | ||
|
||
if(l MATCHES "extern ([^ ]+) (.+)*;") | ||
set(type ${CMAKE_MATCH_1}) | ||
set(decl ${CMAKE_MATCH_2}) | ||
set(args) | ||
set(name) | ||
set(symtype UNKNOWN) | ||
if(l MATCHES "extern void WASM_RT_ADD_PREFIX") | ||
message(STATUS "Ignore: ${l}") | ||
elseif(decl MATCHES "..WASM_RT_ADD_PREFIX.([^)]+)..(.*)") | ||
set(symtype EXPORT) | ||
set(name ${CMAKE_MATCH_1}) | ||
set(args ${CMAKE_MATCH_2}) | ||
elseif(decl MATCHES "..([^)]+).(.+)") | ||
set(symtype IMPORT) | ||
set(name ${CMAKE_MATCH_1}) | ||
set(args ${CMAKE_MATCH_2}) | ||
endif() | ||
if(args) | ||
set(symtype ${symtype}_FUNCTION) | ||
if(${args} MATCHES ".([^)]+).") | ||
set(acc ${CMAKE_MATCH_1}) | ||
set(argcount 0) | ||
while(acc) | ||
if(acc MATCHES "([^,)]+),? ?(.*)") | ||
set(argtype_${argcount} ${CMAKE_MATCH_1}) | ||
set(acc ${CMAKE_MATCH_2}) | ||
math(EXPR argcount "${argcount}+1") | ||
else() | ||
break() | ||
endif() | ||
endwhile() | ||
else() | ||
message(FATAL_ERROR "Malformed line: ${l}") | ||
endif() | ||
|
||
else() | ||
set(symtype ${symtype}_VARIABLE) | ||
set(argcount) | ||
endif() | ||
if(name) # Skip UNKNOWN type | ||
list(APPEND syms ${name}) | ||
set(sym_${name}_symtype ${symtype}) | ||
set(sym_${name}_datatype ${type}) | ||
set(sym_${name}_argcount ${argcount}) | ||
if(argcount) | ||
set(i 0) | ||
while(NOT i EQUAL argcount) | ||
set(sym_${name}_argtype${i} ${argtype_${i}}) | ||
math(EXPR i "${i}+1") | ||
endwhile() | ||
endif() | ||
endif() | ||
endif() | ||
endforeach() | ||
|
||
file(WRITE ${OUT} "# autogenerated\n") | ||
|
||
file(APPEND ${OUT} "set(syms ${syms})\n") | ||
|
||
foreach(s ${syms}) | ||
file(APPEND ${OUT} "set(sym_${s}_symtype ${sym_${s}_symtype})\n") | ||
file(APPEND ${OUT} "set(sym_${s}_datatype ${sym_${s}_datatype})\n") | ||
file(APPEND ${OUT} "set(sym_${s}_argcount ${sym_${s}_argcount})\n") | ||
if(sym_${s}_argcount) | ||
set(i 0) | ||
set(argcount ${sym_${s}_argcount}) | ||
while(NOT i EQUAL argcount) | ||
file(APPEND ${OUT} "set(sym_${s}_argtype${i} ${sym_${s}_argtype${i}})\n") | ||
math(EXPR i "${i}+1") | ||
endwhile() | ||
endif() | ||
file(APPEND ${OUT} "\n") | ||
endforeach() |