Add Global CMake Build and OSS Tests (#1217)

Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1217

This updates the CMake build present for being able to share options, fixing up flags, etc. A GTest build is added as well, along with a script and VSCode debug target so that OSS contributors can very easily run and debug tests on any OS.

Note that this isn't completely done (need to revise Windows, Mac, documentation), but should be finished enough otherwise for review.

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D42406686

fbshipit-source-id: 95e7ba5e4751c496a171785490e85cf0097fa839
This commit is contained in:
Nick Gerleman
2023-01-16 07:56:11 -08:00
committed by Facebook GitHub Bot
parent 53872e2521
commit 83c6997f29
21 changed files with 380 additions and 119 deletions

View File

@@ -0,0 +1,14 @@
name: Install Ninja
runs:
using: "composite"
steps:
- name: Install ninja (Linux)
if: ${{ runner.os == 'Linux' }}
shell: bash
run: sudo apt-get install -y ninja-build
- name: Install ninja (Windows)
if: ${{ runner.os == 'Windows' }}
shell: powershell
run: choco install ninja

12
.github/actions/setup-cpp/action.yml vendored Normal file
View File

@@ -0,0 +1,12 @@
name: Setup C++ envirionment
runs:
using: "composite"
steps:
- name: Install Ninja
if: ${{ runner.os != 'Windows' }}
uses: ./.github/actions/install-ninja
- name: Setup VS Developer Command Prompt
if: ${{ runner.os == 'Windows' }}
uses: ilammy/msvc-dev-cmd@v1

View File

@@ -11,15 +11,8 @@ runs:
- name: Install emsdk
uses: ./.github/actions/install-emsdk
- name: Install ninja (Linux)
if: ${{ runner.os == 'Linux' }}
shell: bash
run: sudo apt-get install -y ninja-build
- name: Install ninja (Windows)
if: ${{ runner.os == 'Windows' }}
shell: powershell
run: choco install ninja
- name: Install Ninja
uses: ./.github/actions/install-ninja
- name: yarn install
shell: bash

View File

@@ -8,6 +8,46 @@ on:
workflow_dispatch:
jobs:
test:
name: Build and Test [${{ matrix.os }}][${{ matrix.mode }}]
runs-on: ${{ matrix.os }}
strategy:
matrix:
mode: [Debug, Release]
os: [ubuntu-latest] # TODO: fix issues building GTest Binary with MSVC in GitHub Actions
steps:
- uses: actions/checkout@v3
- name: Setup
uses: ./.github/actions/setup-cpp
- name: Unit tests
run: ./unit_tests ${{ matrix.mode }}
benchmark:
name: Benchmark [${{ matrix.os }}]
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v3
- name: Setup
uses: ./.github/actions/setup-cpp
- name: Build benchmark
run: |
cmake -S . -B build -G Ninja -D CMAKE_BUILD_TYPE=Release
cmake --build build
working-directory: benchmark
- name: Run benchmark
run: ./build/benchmark
working-directory: benchmark
clang-format:
name: Format
runs-on: ubuntu-latest

3
.gitignore vendored
View File

@@ -12,7 +12,8 @@
/_site/
# Visual studio code
.vscode
!.vscode
*.pdb
*.tlog
*.obj

3
.gitmodules vendored
View File

@@ -1,3 +0,0 @@
[submodule "lib/gtest/googletest"]
path = lib/gtest/googletest
url = https://github.com/google/googletest.git

7
.vscode/extensions.json vendored Normal file
View File

@@ -0,0 +1,7 @@
{
"recommendations": [
"ms-vscode.cpptools",
"EditorConfig.EditorConfig",
"dbaeumer.vscode-eslint",
]
}

25
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,25 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug C++ Unit tests (lldb)",
"type": "cppdbg",
"MIMode": "lldb",
"request": "launch",
"program": "${workspaceFolder}/tests/build/yogatests",
"cwd": "${workspaceFolder}",
"preLaunchTask": "Build Unit Tests"
},
{
"name": "Debug C++ Unit tests (vsdbg)",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/tests/build/yogatests",
"cwd": "${workspaceFolder}",
"preLaunchTask": "Build Unit Tests"
}
]
}

20
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,20 @@
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.format.enable": true,
"eslint.packageManager": "yarn",
"eslint.enable": true,
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"eslint.workingDirectories": [
{
"mode": "auto"
}
],
}

23
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,23 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build Unit Tests",
"type": "shell",
"command": "(which ninja && cmake -B build -S . -D CMAKE_BUILD_TYPE=Debug -G Ninja || cmake -B build -S . -D CMAKE_BUILD_TYPE=Debug) && cmake --build build",
"windows": {
"command": "(where ninja && cmake -B build -S . -D CMAKE_BUILD_TYPE=Debug -G Ninja || cmake -B build -S . -D CMAKE_BUILD_TYPE=Debug) && cmake --build build",
},
"group": "build",
"options": {
"cwd": "tests"
},
"presentation": {
"reveal": "always",
"panel": "new"
}
}
]
}

View File

@@ -3,32 +3,14 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
cmake_minimum_required(VERSION 3.4.1)
cmake_minimum_required(VERSION 3.13)
project(yoga-all)
set(CMAKE_VERBOSE_MAKEFILE on)
add_compile_options(
-fno-omit-frame-pointer
-fexceptions
-fvisibility=hidden
-ffunction-sections
-fdata-sections
-Wall
-std=c++14)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/project-defaults.cmake)
file(GLOB_RECURSE yogacore_SRC yoga/*.cpp)
add_library(yogacore STATIC ${yogacore_SRC})
target_include_directories(yogacore
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include/yoga>)
if (ANDROID)
target_link_libraries(yogacore android log)
endif()
set_target_properties(yogacore PROPERTIES CXX_STANDARD 14)
add_subdirectory(yoga)
add_subdirectory(tests)
# cmake install config
include(GNUInstallDirs)
@@ -58,7 +40,7 @@ install(EXPORT yoga-targets
# generate config file
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/scripts/yoga-config.cmake.in
${CMAKE_CURRENT_SOURCE_DIR}/cmake/yoga-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/yogaConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/yoga
)

View File

@@ -1,28 +1,24 @@
# Yoga [![Support Ukraine](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB)](https://opensource.fb.com/support-ukraine) [![CocoaPods](https://img.shields.io/cocoapods/v/YogaKit.svg)](http://cocoapods.org/pods/YogaKit) [![npm](https://img.shields.io/npm/v/yoga-layout.svg)](https://www.npmjs.com/package/yoga-layout) [![bintray](https://img.shields.io/bintray/v/facebook/maven/com.facebook.yoga:yoga.svg)](https://bintray.com/facebook/maven/com.facebook.yoga%3Ayoga/_latestVersion) [![NuGet](https://img.shields.io/nuget/v/Facebook.Yoga.svg)](https://www.nuget.org/packages/Facebook.Yoga)
# Yoga [![Support Ukraine](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB)](https://opensource.fb.com/support-ukraine) [![CocoaPods](https://img.shields.io/cocoapods/v/YogaKit.svg)](http://cocoapods.org/pods/YogaKit) [![npm](https://img.shields.io/npm/v/yoga-layout.svg)](https://www.npmjs.com/package/yoga-layout) [![Maven Central](https://img.shields.io/maven-central/v/com.facebook.yoga/yoga)](https://search.maven.org/artifact/com.facebook.yoga/yoga)
Yoga is an embeddable and performant flexbox layout engine with bindings for multiple languages.
## Building
Yoga builds with [buck](https://buckbuild.com). Make sure you install buck before contributing to Yoga. Yoga's main implementation is in C++, with bindings to supported languages and frameworks. When making changes to Yoga please ensure the changes are also propagated to these bindings when applicable.
Alternatively, you can build and install Yoga using [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
Yoga's main implementation targets C++ 14 with accompanying build logic in CMake. A wrapper is provided to build the main library and run unit tests.
```sh
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install yoga
./unit_tests <Debug|Release>
```
The Yoga port in vcpkg is kept up to date by Microsoft team members and community contributors.
While not required, this script will use [ninja](https://ninja-build.org/) if it is installed for faster builds.
If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
Yoga is additionally part of the [vcpkg](https://github.com/Microsoft/vcpkg/) collection of ports maintained by Microsoft and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
## Testing
For testing we rely on [gtest](https://github.com/google/googletest) as a submodule. After cloning Yoga run `git submodule init` followed by `git submodule update`.
For any changes you make you should ensure that all the tests are passing. In case you make any fixes or additions to the library please also add tests for that change to ensure we don't break anything in the future. Tests are located in the `tests` directory. Run the tests by executing `buck test //:yoga`.
Instead of manually writing a test which ensures parity with web implementations of Flexbox you can run `gentest/gentest.rb` to generate a test for you. You can write html which you want to verify in Yoga, in `gentest/fixtures` folder, such as the following.
## Adding Tests
Many of Yoga's tests are automatically generated, using HTML fixtures describing node structure. These are rendered in Chrome to generate an expected layout result for the tree. New fixtures can be added to `gentest/fixtures`.
```html
<div id="my_test" style="width: 100px; height: 100px; align-items: center;">
@@ -30,41 +26,11 @@ Instead of manually writing a test which ensures parity with web implementations
</div>
```
Run `gentest/gentest.rb` to generate test code and re-run `buck test //:yoga` to validate the behavior. One test case will be generated for every root `div` in the input html.
To generate new tests from added fixtures:
1. Run `bundle install` in the `gentest` directory to install dependencies of the test generator.
2. Run `ruby gentest.rb` in the `gentest` directory.
You should run `bundle install` in the `gentest` directory to install dependencies for the `gentest/gentest.rb` Ruby script.
### .NET
.NET testing is not integrated in buck yet, you might need to set up .NET testing environment. We have a script which to launch C# test on macOS, `csharp/tests/Facebook.Yoga/test_macos.sh`.
## Debugging
## Benchmarks
Benchmarks are located in `benchmark/YGBenchmark.c` and can be run with `buck run //benchmark:benchmark`. If you think your change has affected performance please run this before and after your change to validate that nothing has regressed. Benchmarks are run on every commit in CI.
### JavaScript
Installing through NPM
```sh
npm install yoga-layout
```
By default this will install the library and try to build for all platforms (node, browser asm, and standalone webpack). You may receive errors if you do not have the required platform development tools already installed. To preset the platform you'd like to build for you can set a .npmrc property first.
```sh
npm config set yoga-layout:platform standalone
```
This will now only run the standalone webpack build upon install.
## Build Platforms
| name | description |
|----------------|-------------------------------------------------|
| all (default) | Builds all of these platforms. |
| browser | Builds asm js browser version. |
| node | Builds node js version. |
| standalone | Runs webpack. |
| none | Does nothing. You can use the prepackaged libs. |
## Maintainer Release Guide
Release artifacts are published automatically when a new GitHub release is
created. The publishing workflows may also be executed manually, given a Git
Tag, to re-attempt publish.
NPM and NuGet packages are not currently published.
Yoga provides a VSCode "launch.json" configuration which allows debugging unit tests. Simply add your breakpoints, and run "Debug C++ Unit tests (lldb)" (or "Debug C++ Unit tests (vsdbg)" on Windows).

19
benchmark/CMakeLists.txt Normal file
View File

@@ -0,0 +1,19 @@
# 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.
cmake_minimum_required(VERSION 3.13)
project(benchmark)
set(CMAKE_VERBOSE_MAKEFILE on)
set(YOGA_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
include(${YOGA_ROOT}/cmake/project-defaults.cmake)
add_subdirectory(${YOGA_ROOT}/yoga ${CMAKE_CURRENT_BINARY_DIR}/yoga)
file(GLOB SOURCES CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/*.c)
add_executable(benchmark ${SOURCES})
target_link_libraries(benchmark yogacore)

View File

@@ -0,0 +1,50 @@
# 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.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_compile_definitions($<$<CONFIG:DEBUG>:DEBUG>)
if(MSVC)
add_compile_options(
# Don't omit frame pointers (e.g. for crash dumps)
/Oy-
# "Standard C++ exception handling" (C++ stack unwinding including extern c)
/EHsc
# Enable warnings and warnings as errors
/W3
/WX
# Disable RTTI
$<$<COMPILE_LANGUAGE:CXX>:/GR->
# Use /O2 (Maximize Speed)
$<$<CONFIG:RELEASE>:/O2>)
else()
add_compile_options(
# Don't omit frame pointers (e.g. for crash dumps)
-fno-omit-frame-pointer
# Enable exception handling
-fexceptions
# Enable warnings and warnings as errors
-Wall
-Werror
# Disable RTTI
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
# Use -O2 (prioritize speed)
$<$<CONFIG:RELEASE>:-O2>
# Enable separate sections per function/data item
$<$<CONFIG:RELEASE>:-ffunction-sections>
$<$<CONFIG:RELEASE>:-fdata-sections>)
add_link_options(
# Discard unused sections
$<$<CONFIG:RELEASE>:$<$<CXX_COMPILER_ID:Clang,GNU>:-Wl,--gc-sections>>
$<$<CONFIG:RELEASE>:$<$<CXX_COMPILER_ID:AppleClang>:-Wl,-dead_strip>>)
endif()

View File

@@ -3,38 +3,20 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
cmake_minimum_required(VERSION 3.4.1)
cmake_minimum_required(VERSION 3.13)
project(yogajni)
set(CMAKE_VERBOSE_MAKEFILE on)
# configure import libs
set(yogacore_DIR ${CMAKE_SOURCE_DIR}/..)
set(build_DIR ${CMAKE_SOURCE_DIR}/build)
set(YOGA_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
include(${YOGA_ROOT}/cmake/project-defaults.cmake)
set(yogacore_build_DIR ${build_DIR}/yogacore/${ANDROID_ABI})
add_subdirectory(${YOGA_ROOT}/yoga ${CMAKE_CURRENT_BINARY_DIR}/yoga)
file(MAKE_DIRECTORY ${build_DIR})
file(GLOB SOURCES CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/jni/*.cpp)
file(GLOB VERSION_SCRIPT CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/yogajni.version)
add_subdirectory(${yogacore_DIR} ${yogacore_build_DIR})
add_compile_options(
-fno-omit-frame-pointer
-fexceptions
-fvisibility=hidden
-ffunction-sections
-fdata-sections
-Wall
-std=c++14)
file(GLOB jni_SRC
jni/*.cpp)
file(GLOB yogajni_version_script
yogajni.version)
add_library(yoga SHARED ${jni_SRC})
target_include_directories(yoga PRIVATE
${yogacore_DIR})
target_link_libraries(yoga -Wl,--gc-sections,--version-script=${yogajni_version_script} yogacore)
add_library(yoga SHARED ${SOURCES})
target_link_libraries(yoga yogacore)
target_link_options(yoga PRIVATE -Wl,--version-script=${VERSION_SCRIPT})

View File

@@ -7,14 +7,14 @@ cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)
project(yoga)
file(GLOB SOURCES
file(GLOB SOURCES CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/../yoga/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../yoga/**/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src_native/*.cc)
include_directories(..)
set(CXX_STANDARD, 14)
set(CMAKE_CXX_STANDARD 14)
add_compile_definitions(
EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0)

36
tests/CMakeLists.txt Normal file
View File

@@ -0,0 +1,36 @@
# 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.
cmake_minimum_required(VERSION 3.13)
project(tests)
set(CMAKE_VERBOSE_MAKEFILE on)
include(FetchContent)
include(GoogleTest)
set(YOGA_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
include(${YOGA_ROOT}/cmake/project-defaults.cmake)
# Fetch GTest
cmake_policy(SET CMP0135 NEW)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.zip
)
FetchContent_MakeAvailable(googletest)
add_subdirectory(${YOGA_ROOT}/yoga ${CMAKE_CURRENT_BINARY_DIR}/yoga)
file(GLOB SOURCES CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/**/*.cpp)
enable_testing()
add_executable(yogatests ${SOURCES})
target_link_libraries(yogatests yogacore GTest::gtest_main)
gtest_discover_tests(yogatests)

25
unit_tests Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/sh
# 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.
cd "$(dirname "$0")/tests"
if [ "$#" -eq 0 ]; then
build_type="Debug"
else
build_type="$1"
fi
if which ninja; then
set -e
cmake -B build -S . -D CMAKE_BUILD_TYPE="$build_type" -G Ninja
else
set -e
cmake -B build -S . -D CMAKE_BUILD_TYPE="$build_type"
fi
cmake --build build
./build/yogatests

28
unit_tests.bat Executable file
View File

@@ -0,0 +1,28 @@
@REM Copyright (c) Meta Platforms, Inc. and affiliates.
@REM
@REM This source code is licensed under the MIT license found in the
@REM LICENSE file in the root directory of this source tree.
@echo off
SETLOCAL
cd "%0\..\tests"
if "%1" == "" (
set build_type="Debug"
) else (
set build_type="%1"
)
where ninja
if errorlevel 1 (
cmake -B build -S . -D CMAKE_BUILD_TYPE="%build_type%"
) else (
cmake -B build -S . -D CMAKE_BUILD_TYPE="%build_type%" -G Ninja
)
if %errorlevel% neq 0 exit /b %errorlevel%
cmake --build build
if %errorlevel% neq 0 exit /b %errorlevel%
.\build\yogatests.exe

41
yoga/CMakeLists.txt Normal file
View File

@@ -0,0 +1,41 @@
# 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.
cmake_minimum_required(VERSION 3.13)
project(yogacore)
set(CMAKE_VERBOSE_MAKEFILE on)
if(TARGET yogacore)
return()
endif()
include(CheckIPOSupported)
set(YOGA_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
include(${YOGA_ROOT}/cmake/project-defaults.cmake)
file(GLOB SOURCES CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/**/*.cpp)
add_library(yogacore STATIC ${SOURCES})
# Yoga conditionally uses <android/log> when building for Android
if (ANDROID)
target_link_libraries(yogacore log)
endif()
check_ipo_supported(RESULT result)
if(result)
set_target_properties(yogacore PROPERTIES
CMAKE_INTERPROCEDURAL_OPTIMIZATION true)
endif()
target_include_directories(yogacore
PUBLIC
$<BUILD_INTERFACE:${YOGA_ROOT}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include/yoga>)