Add support for using Yoga from swift on non Darwin platforms. #1690

Open
iainsmith wants to merge 2 commits from iainsmith/main into main
iainsmith commented 2024-08-24 07:18:50 -07:00 (Migrated from github.com)

Specify that enums are closed for correct swift export on non darwin platforms.

Related Yoga Issue


Swift exports plain c enums as constants. On Darwin the yoga macros hit the NS_ENUM case which generates a swift enum, however on linux NS_ENUM is not defined.

See [This post] for a some more background. https://belkadan.com/blog/2021/10/Swift-Regret-Unannotated-C-Enums/

Tested on a couple of CI builds:

Which results in a compiler error for the missing switch cases.

[303/309] Compiling YogaSwift YGNodeView.swift
/home/runner/work/YogaSwift/YogaSwift/Sources/YogaSwift/YGNode.swift:50:56: error: type 'YGDirection' has no member 'LTR'
48 |     width: Float = YGValueUndefined.value, height: Float = YGValueUndefined.value
49 |   ) {
50 |     YGNodeCalculateLayout(self.__node, width, height, .LTR)
   |                                                        `- error: type 'YGDirection' has no member 'LTR'
51 |   }
52 | }
Specify that enums are closed for correct swift export on non darwin platforms. [Related Yoga Issue](https://github.com/facebook/yoga/issues/620#issuecomment-2308379285) --- Swift exports plain c enums as constants. On Darwin the yoga macros hit the NS_ENUM case which generates a swift enum, however on linux NS_ENUM is not defined. See [This post] for a some more background. https://belkadan.com/blog/2021/10/Swift-Regret-Unannotated-C-Enums/ Tested on a couple of CI builds: - [Failing build](https://github.com/iainsmith/YogaSwift/actions/runs/10478362350/job/29021559312) - [Passing build](https://github.com/iainsmith/YogaSwift/actions/runs/10493885336/job/29068883339) swift build passes on this, but swift test is failing still for unrelated reasons. Which results in a compiler error for the missing switch cases. ```swift [303/309] Compiling YogaSwift YGNodeView.swift /home/runner/work/YogaSwift/YogaSwift/Sources/YogaSwift/YGNode.swift:50:56: error: type 'YGDirection' has no member 'LTR' 48 | width: Float = YGValueUndefined.value, height: Float = YGValueUndefined.value 49 | ) { 50 | YGNodeCalculateLayout(self.__node, width, height, .LTR) | `- error: type 'YGDirection' has no member 'LTR' 51 | } 52 | } ```
NickGerleman (Migrated from github.com) reviewed 2024-08-24 07:18:50 -07:00
vercel[bot] commented 2024-08-24 07:18:54 -07:00 (Migrated from github.com)

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
yoga-website Ready (Inspect) Visit Preview 💬 Add feedback Aug 26, 2024 10:40pm
[vc]: #yf5UayRkX399avOY9U2yivf0LXSO1ZGeEbCKURxO9Dw=:eyJpc01vbm9yZXBvIjp0cnVlLCJ0eXBlIjoiZ2l0aHViIiwicHJvamVjdHMiOlt7Im5hbWUiOiJ5b2dhLXdlYnNpdGUiLCJyb290RGlyZWN0b3J5Ijoid2Vic2l0ZSIsImluc3BlY3RvclVybCI6Imh0dHBzOi8vdmVyY2VsLmNvbS9mYm9wZW5zb3VyY2UveW9nYS13ZWJzaXRlL29UQmpGSnJidW5FSnhuR3pDVDVRS3ZhNlpkN2giLCJwcmV2aWV3VXJsIjoieW9nYS13ZWJzaXRlLWdpdC1mb3JrLWlhaW5zbWl0aC1tYWluLWZib3BlbnNvdXJjZS52ZXJjZWwuYXBwIiwibmV4dENvbW1pdFN0YXR1cyI6IkRFUExPWUVEIiwibGl2ZUZlZWRiYWNrIjp7InJlc29sdmVkIjowLCJ1bnJlc29sdmVkIjowLCJ0b3RhbCI6MCwibGluayI6InlvZ2Etd2Vic2l0ZS1naXQtZm9yay1pYWluc21pdGgtbWFpbi1mYm9wZW5zb3VyY2UudmVyY2VsLmFwcCJ9fV19 **The latest updates on your projects**. Learn more about [Vercel for Git ↗︎](https://vercel.link/github-learn-more) | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **yoga-website** | ✅ Ready ([Inspect](https://vercel.com/fbopensource/yoga-website/oTBjFJrbunEJxnGzCT5QKva6Zd7h)) | [Visit Preview](https://vercel.live/open-feedback/yoga-website-git-fork-iainsmith-main-fbopensource.vercel.app?via=pr-comment-visit-preview-link&passThrough=1) | 💬 [**Add feedback**](https://vercel.live/open-feedback/yoga-website-git-fork-iainsmith-main-fbopensource.vercel.app?via=pr-comment-feedback-link) | Aug 26, 2024 10:40pm |
facebook-github-bot commented 2024-08-24 07:18:56 -07:00 (Migrated from github.com)

Hi @iainsmith!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

Hi @iainsmith! Thank you for your pull request and welcome to our community. # Action Required In order to merge **any pull request** (code, docs, etc.), we **require** contributors to sign our **Contributor License Agreement**, and we don't seem to have one on file for you. # Process In order for us to review and merge your suggested changes, please sign at <https://code.facebook.com/cla>. **If you are contributing on behalf of someone else (eg your employer)**, the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the **pull request will be tagged** with `CLA signed`. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it. If you have received this in error or have any questions, please contact us at [cla@meta.com](mailto:cla@meta.com?subject=CLA%20for%20facebook%2Fyoga%20%231690). Thanks!
facebook-github-bot commented 2024-08-24 10:31:34 -07:00 (Migrated from github.com)

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!
NickGerleman (Migrated from github.com) requested changes 2024-08-26 08:03:35 -07:00
NickGerleman (Migrated from github.com) left a comment

This breaks the build on non-Clang platforms.

This breaks the build on non-Clang platforms.
NickGerleman (Migrated from github.com) reviewed 2024-08-26 08:14:49 -07:00
@@ -50,2 +51,4 @@
#define YG_ENUM_BEGIN(name) enum name
#define YG_ENUM_END(name) __attribute__((enum_extensibility(closed))) name
#else
#define YG_ENUM_BEGIN(name) enum name
NickGerleman (Migrated from github.com) commented 2024-08-26 08:14:49 -07:00

A couple of these are flag enums, which would also need to be declared I think

A couple of these are flag enums, which would also need to be declared I think
iainsmith commented 2024-08-26 09:22:18 -07:00 (Migrated from github.com)

This breaks the build on non-Clang platforms.

Makes sense. I'll push a change to wrap this in a clang check.


Should be fixed now. Quickly tested with
cmake -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ && make

note to self. /usr/bin/gcc on mac is a wrapper around clang.

> This breaks the build on non-Clang platforms. Makes sense. I'll push a change to wrap this in a __clang__ check. --- Should be fixed now. Quickly tested with `cmake -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ && make` note to self. /usr/bin/gcc on mac is a wrapper around clang.
iainsmith (Migrated from github.com) reviewed 2024-08-26 15:44:15 -07:00
@@ -50,2 +51,4 @@
#define YG_ENUM_BEGIN(name) enum name
#define YG_ENUM_END(name) __attribute__((enum_extensibility(closed))) name
#else
#define YG_ENUM_BEGIN(name) enum name
iainsmith (Migrated from github.com) commented 2024-08-26 15:44:15 -07:00

@NickGerleman Are the only bitmask flags YGErrata & YGExperimentalFeature or are there others?

@NickGerleman Are the only bitmask flags `YGErrata & YGExperimentalFeature` or are there others?
iainsmith (Migrated from github.com) reviewed 2024-08-26 15:57:29 -07:00
@@ -50,2 +51,4 @@
#define YG_ENUM_BEGIN(name) enum name
#define YG_ENUM_END(name) __attribute__((enum_extensibility(closed))) name
#else
#define YG_ENUM_BEGIN(name) enum name
iainsmith (Migrated from github.com) commented 2024-08-26 15:57:29 -07:00

Just realised that enums.py specifies that only Errata is a bitset enum.

How do you feel about us adding a YG_ENUM_FLAG_DECL macro, and then updating enums.py to check BITSET_ENUMS to decide on which macro to use?

Just realised that enums.py specifies that only `Errata` is a bitset enum. How do you feel about us adding a `YG_ENUM_FLAG_DECL` macro, and then updating enums.py to check `BITSET_ENUMS` to decide on which macro to use?
NickGerleman (Migrated from github.com) reviewed 2024-08-27 03:40:39 -07:00
@@ -50,2 +51,4 @@
#define YG_ENUM_BEGIN(name) enum name
#define YG_ENUM_END(name) __attribute__((enum_extensibility(closed))) name
#else
#define YG_ENUM_BEGIN(name) enum name
NickGerleman (Migrated from github.com) commented 2024-08-27 03:40:38 -07:00

That sounds good to me!

That sounds good to me!
iainsmith (Migrated from github.com) reviewed 2024-08-29 01:43:04 -07:00
@@ -50,2 +51,4 @@
#define YG_ENUM_BEGIN(name) enum name
#define YG_ENUM_END(name) __attribute__((enum_extensibility(closed))) name
#else
#define YG_ENUM_BEGIN(name) enum name
iainsmith (Migrated from github.com) commented 2024-08-29 01:43:04 -07:00

@NickGerleman , I took a quick stab at this on this branch, but I've run into a compiler error I'm not sure how to approach.

~/Developer/personal/yoga/yoga/../yoga/YGEnums.h:61:5: 
error: enumeration value 'YGErrataClassic' is out of range of flags in enumeration type 'YGErrata' [-Werror,-Wflag-enum]
   61 |     YGErrataClassic = 2147483646,
      |     ^
~/Developer/personal/yoga/yoga/../yoga/YGEnums.h:62:5: 
error: enumeration value 'YGErrataAll' is out of range of flags in enumeration type 'YGErrata' [-Werror,-Wflag-enum]
   62 |     YGErrataAll = 2147483647)

Do you have thoughts on the best way to handle that?

@NickGerleman , I took a quick stab at this on [this branch](https://github.com/facebook/yoga/compare/main...iainsmith:yoga:flag_enums?expand=1), but I've run into a compiler error I'm not sure how to approach. ``` ~/Developer/personal/yoga/yoga/../yoga/YGEnums.h:61:5: error: enumeration value 'YGErrataClassic' is out of range of flags in enumeration type 'YGErrata' [-Werror,-Wflag-enum] 61 | YGErrataClassic = 2147483646, | ^ ~/Developer/personal/yoga/yoga/../yoga/YGEnums.h:62:5: error: enumeration value 'YGErrataAll' is out of range of flags in enumeration type 'YGErrata' [-Werror,-Wflag-enum] 62 | YGErrataAll = 2147483647) ``` Do you have thoughts on the best way to handle that?
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin iainsmith/main:iainsmith/main
git checkout iainsmith/main
Sign in to join this conversation.
No description provided.