From ef5784aca647a68343f5409e379519827edb86a9 Mon Sep 17 00:00:00 2001 From: "Lvv.me" Date: Thu, 10 Aug 2023 17:40:10 -0700 Subject: [PATCH] Add SwiftPM support (#1339) Summary: Using yoga in Swift ```swift import UIKit import yoga let YGUndefined = Float.nan UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: 1), true, 0) let scale = UIGraphicsGetCurrentContext()?.ctm.a ?? 1 UIGraphicsEndImageContext() let config = YGConfigNew() YGConfigSetUseWebDefaults(config, true) YGConfigSetPointScaleFactor(config, Float(scale)) let root = YGNodeNewWithConfig(config) YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow) YGNodeStyleSetFlexWrap(root, YGWrapWrap) YGNodeStyleSetWidth(root, 200) for i in 0..<4 { let leaf = YGNodeNewWithConfig(config) YGNodeStyleSetAspectRatio(leaf, 1) YGNodeStyleSetWidthPercent(leaf, 50) YGNodeInsertChild(root, leaf, UInt32(i)) } YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGNodeStyleGetDirection(root)) for i in 0..<4 { let leaf = YGNodeGetChild(root, UInt32(i)) let left = YGNodeLayoutGetLeft(leaf) let top = YGNodeLayoutGetTop(leaf) let width = YGNodeLayoutGetWidth(leaf) let height = YGNodeLayoutGetHeight(leaf) print("leaf \(i): origin: \(left), \(top), size: \(width), \(height)") } let width = YGNodeLayoutGetWidth(root) let height = YGNodeLayoutGetHeight(root) print("root size: \(width), \(height)") YGNodeFreeRecursive(root) YGConfigFree(config) ``` Pull Request resolved: https://github.com/facebook/yoga/pull/1339 Reviewed By: cipolleschi Differential Revision: D48166958 Pulled By: NickGerleman fbshipit-source-id: 72fef99729d01c7ba291b11f047cf75fd39f7630 --- .github/workflows/validate-swiftpm.yml | 22 ++++++++++++++++++ Package.swift | 32 ++++++++++++++++++++++++++ yoga/module.modulemap | 16 +++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 .github/workflows/validate-swiftpm.yml create mode 100644 Package.swift create mode 100644 yoga/module.modulemap diff --git a/.github/workflows/validate-swiftpm.yml b/.github/workflows/validate-swiftpm.yml new file mode 100644 index 00000000..da582708 --- /dev/null +++ b/.github/workflows/validate-swiftpm.yml @@ -0,0 +1,22 @@ +name: Swift + +on: + pull_request: + push: + branches: + - main + workflow_dispatch: + +jobs: + test: + name: Build + runs-on: macos-latest + + steps: + - uses: actions/checkout@v3 + + - name: Build Debug + run: swift build -c debug + + - name: Build Release + run: swift build -c release diff --git a/Package.swift b/Package.swift new file mode 100644 index 00000000..4c8d846f --- /dev/null +++ b/Package.swift @@ -0,0 +1,32 @@ +// swift-tools-version:5.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + + +import PackageDescription + +let package = Package( + name: "yoga", + products: [ + .library(name: "yoga", targets: [ "core" ]) + ], + targets: [ + .target( + name: "core", + path: ".", + sources: [ + "yoga" + ], + publicHeadersPath: ".", + cxxSettings: [ + .headerSearchPath(".") + ] + ) + ], + cxxLanguageStandard: CXXLanguageStandard(rawValue: "c++17") +) diff --git a/yoga/module.modulemap b/yoga/module.modulemap new file mode 100644 index 00000000..91c6db29 --- /dev/null +++ b/yoga/module.modulemap @@ -0,0 +1,16 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +module yoga [system] { + module core { + header "YGEnums.h" + header "YGMacros.h" + header "YGValue.h" + header "Yoga.h" + export * + } +}