AdeptLanguage / Adept

The Adept Programming Language
GNU General Public License v3.0
119 stars 9 forks source link

Make it possible to write a dll without `init fn` and `deinit fn` #231

Open ghost opened 9 months ago

ghost commented 9 months ago

This is something that is always possible with GCC on Windows.

IsaacShelton commented 9 months ago

Yes this should be possible, I just think the compiler doesn't support it yet. I believe DLLs have a way to automatically init/deinit but the compiler doesn't generate the code for that yet.

Even an option to force ignore the current requirement though would be nice. Once I am not super busy, I can take a look at it.

ghost commented 9 months ago

I think this trick could be used: Adept compiles to object files, then invoke gcc to make the dll. However, it would be preferable if Adept could do this without relying on 'gcc'.

Btw, does Adept support making dynamic library on Linux (.so file extension) and macOS (.dylib file extension)? These platforms have no concept of DllMain like Windows, so no init fn or deinit fn is needed. The ability to make dynamic library seems to be added very recently and is Windows-specific.

ghost commented 9 months ago

Update: the ability to make a dll without DllMain or any init fn and deinit fn is present on all Windows compilers. See: https://github.com/LADSoft/OrangeC/discussions/1002

IsaacShelton commented 9 months ago

I think somehow it will have to hook into the ___CTOR_LIST__ and ___DTOR_LIST__ link variables in order to avoid DllMain, or even just create/augment DllMain to handle the initialization/deinitialization of static and global variables.

For other platforms such as macOS and Linux, they have their own special way too to achieve the same effect. I think they both have __attribute__((constructor)) and __attribute__((destructor)), which could be hooked in to avoid having to manually call init/deinit functions.

In the meantime, you can forcefully ignore having init/deinit functions by just passing empty strings for each.

adept --dll "" ""   
adept --shared "" ""

Just keep in mind that global and static variables will not be initialized when doing this of course.

ghost commented 9 months ago

Please have a look:

https://stackoverflow.com/questions/12463718/linux-equivalent-of-dllmain

https://stackoverflow.com/questions/22763945/dll-main-on-windows-vs-attribute-constructor-entry-points-on-linux

ghost commented 9 months ago

In the meantime, you can forcefully ignore having init/deinit functions by just passing empty strings for each.

adept --dll "" ""   
adept --shared "" ""

I can't find the --dll switch and --shared switch on the output of adept --help-advanced.

Just keep in mind that global and static variables will not be initialized when doing this of course.

I believe this is the same as the method using gcc I proposed above. I don't think GCC does anything special here. To use global and static variables, you will need something like DllMain. The fact is I have never need to use global and static variables in my DLLs. My proposal to you here is simple: if the users don't specify init fn and deinit fn, e.g: adept --dylib test.adept, the compiler assumed them to be empty (like your commands above) and still agreed to generate the DLL. This way, the CLI will be more user friendly than forcing the users to input empty strings (like your commands above).

Disclaimer: I don't know about Linux and macOS. I'm strictly a Windows user, and I'm not an expert in this field, too. My knowledge is all from Google.