Hello! Does python have any modules/tools that’d help in like, generating .def files in different scenarios?
First scenario being that the waf build system is being used for many files in a folder, but there’s way too many symbols in the source files that aren’t marked with visibility macros (so marking the symbols isn’t a practical option). And MSVC is the compiler, so symbols are hidden by default.
Any other scenario being anything you can think of.
I want to have a solution within Python such that I don’t have to use anything like the DUMPBIN command. I also understand that if any such solution exists, it’d likely need to work across different formats of object/dll files.
I’m not much experienced in Python (yet), so sorry in advance if my question is not clear enough.
Do you mean this build system? If there’s no third party project on PyPi ready to go, I’m sure you can easily write your own code to build .def files with basic file and text manipulation.
Is the def format based on .ini or any other standard?
You’re probably right and I suppose I’ll likely have to take this route… But I’d appreciate if such a tool would exist in the future.
Unless someone knows of a python tool/module that already exists, I’ll mark this as solved tomorrow/later or after any further discussion. I’m also now considering making a separate post with the “Ideas” tag, but I’m not sure if I’d be able to give clear/sufficient answers to follow up. Maybe someone else can make an Idea post for this instead.
Sorry but I don’t know what it’s based on. I merely want to use a .def generator to solve any “unresolved external symbols” errors when something is linked. (Hence the initial post)
Since the def file just lists the public functions, couldn’t you just scan the source files and match the function definition pattern? If they’re marked in a standard way (say, no leading underscore) it would be really trivial to implement.
Edit: Here’s an *incredibly* barebones example
from pathlib import Path
# Replace with any path
src = Path(__file__).parent.parent
# replace with any matching method you need
def match_header(line: str) -> bool:
return (
line.endswith(') {\n')
and not line.startswith(' ')
and not line.startswith('local')
)
# Replace with any name extractor
def extract_func_name(line: str) -> str | None:
for tok in line.split():
if '(' in tok:
return tok.split('(')[0]
return None
def get_funcs(fl: Path):
with fl.open('rt') as f:
for line in filter(match_header, f):
if func := extract_func_name(line):
yield func
# Probably use a system like argparse so you can pipe in files
# as part of your build process
if __name__ == '__main__':
funcs: list[str] = []
for fl in src.glob('*.c'):
print(fl.name)
_funcs = list(get_funcs(fl))
print('\t', _funcs)
funcs.extend(_funcs)
with (Path(__file__).parent / 'pyDef.def').open('wt') as fl:
fl.write('EXPORTS\n')
for func in sorted(funcs):
fl.write(f' {func}\n')
This uses no additional libraries and relies only on basic string operations in the source file which means you can use any marker you want (e.g. a structured comment one line above the function or something)
Edit 2:
Is this WAF build system even maintained anymore? There are 2 repos for it, with the base repo having the last commit being from 2016 and a fork that has more commits up to 2020, but not much after that.
I see they switched to GitLab and haven’t updated their GitHub repos.