Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] MSVC support #22

Closed
mikejsavage opened this issue Sep 20, 2024 · 16 comments
Closed

[Feature Request] MSVC support #22

mikejsavage opened this issue Sep 20, 2024 · 16 comments
Assignees
Labels
Attempted Fix A fix has been implemented but not yet confirmed by the issue reporter.

Comments

@mikejsavage
Copy link
Contributor

mikejsavage commented Sep 20, 2024

Clay currently doesn't compile under MSVC because:

  1. It doesn't support C designated initializers
  2. It doesn't support __attribute__((packed))__
  3. It doesn't like CLAY__ALIGNMENT, #define CLAY__ALIGNMENT(type) _Alignof(type) works though

1 and 2 are not very nice to solve with Microsoft flavoured C, although I will say all of these have direct solutions if you don't mind C++

@nicbarker
Copy link
Owner

nicbarker commented Sep 20, 2024

Thanks for opening this one! I had been putting it off until the core implementation stabilized, but we can definitely begin to address each of these

It doesn't support C designated initializers

AFAIK there should be a way at the very least for the library syntax to be compatible with both C99 designated initializers as well as C++20 designated initializers. C++20 is much more picky but should be roughly syntax equivalent if the members are in declaration order

It doesn't support attribute((packed))__

Yep nice, should be able to replace this with a #ifdef of msvc and swap out for whatever equivalent to size enums into 1 byte

It doesn't like CLAY__ALIGNMENT, #define CLAY__ALIGNMENT(type) _Alignof(type) works though

This one should be reasonably easy to #ifdef in as well.

As for supporting non C99 versions of C, the best way forward might be to compile clay as a lib and write a second set of macros / declarations that are compatible with C11 etc... 🤔

@nicbarker nicbarker self-assigned this Sep 20, 2024
@nicbarker
Copy link
Owner

From my investigations so far, it looks like I'm probably going to be able to get it in line with MSVC C++20. Microsoft flavoured C though, might not be possible. I don't think I'm willing to drop to C89 for the whole library just to support those versions on MSVC. Probably would be best in that case to just compile as a lib and link in 👍

@nicbarker
Copy link
Owner

Follow up: Successfully compiling as C++20 with MSVC here https://github.com/nicbarker/clay/actions/runs/11083013067/job/30796749136 😁

@nicbarker nicbarker added the Attempted Fix A fix has been implemented but not yet confirmed by the issue reporter. label Sep 28, 2024
@nicbarker
Copy link
Owner

I'm going to mark this as completed for now, when clay stabilizes a little more we can reopen a discussion into whether it's worth dropping to C89 for the library itself in order to support C11 and beyond 🙂

@mikejsavage
Copy link
Contributor Author

Yep I've been messing with clay in our engine, it seems fine so far but I haven't done much with it yet

@nicbarker
Copy link
Owner

@mikejsavage Great, please let me know if you end up building something with it!

@MiraiMindz
Copy link

I'm going to mark this as completed for now, when clay stabilizes a little more we can reopen a discussion into whether it's worth dropping to C89 for the library itself in order to support C11 and beyond 🙂

Sorry for necro-bumping but, I was wondering, why C99? If CLAY doesn't have any depedency (not even stdlib as you claimed) and bundles everything it uses why not C89? I know that macros are not quite powerful in C89 as they are in C99, but we can get around that.

I write software mainly on C89 for compatibility and portability reasons, and I was thinking of porting CLAY to strict ansi C89 myself (that would be feasible but quite challenging), so if you want help with porting it to C89 (strict or not) I would appreciate to help.

I was thinking about making my own layout lib (in C89) to make GUIs with RayLib before I saw your clay video, but know I think it would be better to contribute to this lib and help it reach a more mature state, thanks for the work man, I really do appreciate it.

@nicbarker
Copy link
Owner

@MiraiMindz thanks for reaching out 🙂
The main reason for not dropping to ansi C at this point is that the quality of life features in C99 really make building and changing code so much easier (for me), specifically variadic macros, anywhere declarations and designated initializers.
I'm definitely keeping the option open for when the library stabilizes to just bug fixes & platform specific changes (which I intend to happen eventually) but for now I think it will make dev a little too difficult for me. Sorry if this wasn't the answer you were after, but of course feel free to adapt the core parts of the lib for your own use.

@namandixit
Copy link

namandixit commented Dec 21, 2024

@nicbarker MSVC does seem to support designated initializer in general (Godbolt). Is there some special kind of syntax/extension required that is unsupported by MSVC?

@nicbarker
Copy link
Owner

@namandixit that is very interesting, I'll have to review and see how close we are or what the specific problems are. It's possible that by getting to c++20 support I actually got closer to msvc C11 / C17 than I realised.

@Robert-M-Muench
Copy link

I'm just trying to compile with MSVC as well and face the same problems. To use packing attributes, they need to be specified in the correct syntax and position:

  1. For MSVC (Microsoft Visual C++), packing must be specified using pragma directives before and after the enum:
#pragma pack(push, 1)
typedef enum {  // No identifier needed here
    // enum values...
} Clay__ElementConfigType;
#pragma pack(pop)
  1. For GCC/Clang, the attribute must be specified using the correct attribute syntax:
typedef enum __attribute__((packed)) {  // Attribute goes here, not an identifier
    // enum values...
} Clay__ElementConfigType;

If you want to create a cross-platform solution, you would typically define a macro that expands to the appropriate syntax based on the compiler:

#ifdef _MSC_VER
    #define CLAY_PACKED_ENUM
    #define CLAY_PACK_BEGIN #pragma pack(push, 1)
    #define CLAY_PACK_END #pragma pack(pop)
#else
    #define CLAY_PACKED_ENUM __attribute__((packed))
    #define CLAY_PACK_BEGIN
    #define CLAY_PACK_END
#endif

Then you could use it like this:

CLAY_PACK_BEGIN
typedef enum CLAY_PACKED_ENUM {
    // enum values...
} Clay__ElementConfigType;
CLAY_PACK_END

Maybe this can be somehow added/handled via a generator as well.

But, what to do until then?

@FintasticMan
Copy link
Contributor

I've implemented this based on feedback in #118.

@Robert-M-Muench
Copy link

Yes, sorry, I saw it now.

I am looking forward to getting it merged into the master. Will wait so long.

@codewithpapakwame
Copy link

ERRor

Bro I am facing the same problem hope we can fix this now

@vulcan-dev
Copy link

+1

I'm currently using C++20 and clang-cl, but clang-cl has some issues and prevents me from compiling a static library (not related with clay), so MSVC support would be great

@nicbarker
Copy link
Owner

As of #178 merging, we should have decent MSVC support across the board, including for C99, C11 and C23 via MSVC 🙂
Please reach out or open a new issue if there are any other problems!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Attempted Fix A fix has been implemented but not yet confirmed by the issue reporter.
Projects
None yet
Development

No branches or pull requests

8 participants