lbguilherme / crystal

The Crystal Programming Language
http://crystal-lang.org
Apache License 2.0
1 stars 0 forks source link

Generate LibWindows bindings instead of write them? #5

Open lbguilherme opened 7 years ago

lbguilherme commented 7 years ago

Similar to what is done for LibC bindings on other plataforms. The source could be windows.h and related files from some C compiler, could be extracted from the MSDN documentation itself (page scraping?), or some other more authoritative source. Maybe from C# sources?

ysbaddaden commented 7 years ago

It would be a good idea. It should always be generated from the official C headers. Documentation may omit constant values, private struct properties, ... which would lead to invalid bindings.

The POSIX bindings generator tries to map header files one-to-one (for example sys/time.h to c/sys/time.cr) and keep naming as close as possible; even if official naming is sometimes weird, it's the official one. Same for the case, except for downcase types; for example size_t is changed to SizeT but FILE is kept as FILE.

I suppose the POSIX generator can be used to create bindings for the Windows API, too —in which case I shall rename it. If someone can send me the Windows include PATH, I could hack something.

lbguilherme commented 7 years ago

I installed Windows SDK 7.0 in a VM. It contains headers, system libraries for linking and a working C/C++ compiler and linker. Unfortunately it seems these files cannot be packed with crystal itself, they would need to be installed manually by users (not hard, just an extra manual step).

Headers are attached here, it is as official as it can get. The mingw version of them seem to be written from scratch. So if this version is hard to use for any reason, we can try with mingw headers.

I packed all headers that are provided with the SDK, they are many. The only thing that matters is windows.h and whatever it includes. Can you give it a try?

windows-includes.zip

ysbaddaden commented 7 years ago

I'm missing some headers, those distributed with MSVC I believe, for example excpt.h or stdarg.h. I'm using mingw & win32api in the meantime, but I should have better results with the MSVC headers (MinGW seems to require some GCC extensions, like _BEGIN_C_DECLS or I'm missing more headers...).

lbguilherme commented 7 years ago

Here are the ones that came with the Visual C compiler (also bundled in the SDK, but was in a different location). Is that it?

msvc-includes.zip

ysbaddaden commented 7 years ago

Exactly. But now I have an issue with filenames, since windows is case insensitive... I thus have #include <winnt.h> but it can't find it because it's WinNT.h (on my Linux EXT4 system) :sob:

ysbaddaden commented 7 years ago

I got a case-insensitive filesystem... yet libclang doesn't seem to like the C syntax from MSVC. I have too many errors. I assume a newer version of libclang would behave better, but crystal_lib fails miserably with linked against libclang 3.9, so I can't try (yet).

After a few #define I seem to have a correct support using the MinGW + win32api headers. There are a bunch of missing constants, structs and functions thought; most shouldn't be very important, but some may (GetDynamicTimeZoneInformation or DYNAMIC_TIME_ZONE_INFORMATION), and the generated bindings will certainly have to be tweaked a bit by hand —like some other bindings (OpenBSD, Android).

You may try https://github.com/ysbaddaden/posix/tree/windows, extract the win32api/include as c_include/windows/win32api and the mingw/include as c_include/windows/mingw then type make win32 or win64 depending if you want i686 or x86_64 respectively (an advantage of having a generator is that it deals automatically with architectures, we may have arm and aarch64 later).

The definitions are YAML files under include/windows, and the resulting bindings will be in targets/<arch>-pc-win32 —is seems to be the expected host-target for windows?