Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

feat(cpp): laya-cpp CMake skeleton and CI lane

Adeshina Lasisif66c4d224608
diff --git a/.github/workflows/cpp.yml b/.github/workflows/cpp.yml
new file mode 100644
index 00000000..91b97312
--- /dev/null
+++ b/.github/workflows/cpp.yml
@@ -0,0 +1,99 @@
+name: C++
+
+# Build lane for laya-cpp. Like the Go, .NET and Java lanes it runs on every push and pull request
+# with no path filter. This first change carries a skeleton only (no library code, no goldens, no
+# ONNX Runtime), so there is no parity against the Python reference yet; what the lane proves is
+# that the toolchain builds, tests run, and the numeric flags are in force.
+#
+# The build job is ADVISORY (`continue-on-error`) for the reason the other ports are: the Python
+# package is the product and the port follows it.
+#
+# Two things hold the lane honest, each there because a green run could otherwise mean nothing:
+#
+# * Fail on skip, and a floor: ctest reports a skipped test as success. Each cell writes JUnit
+# XML and laya-cpp/scripts/check_test_xml.py counts instead of trusting the exit status: any
+# skip, any failure and any shortfall below the floor fails the step.
+# * The flags are tested: the `compile_flags` ctest fails if -ffp-contract=off or
+# -fno-fast-math is missing from a laya-cpp translation unit.
+#
+# The security job is NOT advisory, as in go.yml.
+#
+# TODO(security): CodeQL for C++ is not added here. go.yml has no CodeQL step; CodeQL lives in the
+# `codeql` matrix of security.yml, and a `cpp` cell there needs a manual build step and is a
+# follow-up for the maintainer to agree to, not a pin invented in this change.
+
+on:
+ pull_request:
+ push:
+ workflow_dispatch:
+
+permissions:
+ contents: read
+
+concurrency:
+ group: cpp-${{ github.event_name == 'pull_request' && github.ref || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+
+env:
+ # The test floor: the number of ctest tests laya-cpp registers (the GoogleTest cases plus
+ # compile_flags). Raise it with every test added.
+ MIN_TESTS: "2"
+
+jobs:
+ build:
+ name: build (${{ matrix.compiler }}, ${{ matrix.preset }})
+ runs-on: ubuntu-latest
+ continue-on-error: true # advisory: see the header
+ timeout-minutes: 20
+ strategy:
+ fail-fast: false
+ matrix:
+ compiler: [g++, clang++]
+ preset: [debug, asan-ubsan]
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
+ with:
+ python-version: "3.12"
+
+ - name: Checker tests
+ run: python laya-cpp/scripts/test_check_test_xml.py
+
+ - name: Configure
+ working-directory: laya-cpp
+ run: cmake --preset "${{ matrix.preset }}" -DCMAKE_CXX_COMPILER="${{ matrix.compiler }}"
+
+ - name: Build
+ working-directory: laya-cpp
+ run: cmake --build --preset "${{ matrix.preset }}"
+
+ - name: Test
+ working-directory: laya-cpp
+ run: ctest --preset "${{ matrix.preset }}" --output-junit "$RUNNER_TEMP/ctest.xml"
+
+ - name: Count tests (fail on skip, enforce the floor)
+ if: ${{ !cancelled() }}
+ run: python laya-cpp/scripts/check_test_xml.py "$RUNNER_TEMP/ctest.xml" --min-tests "$MIN_TESTS"
+
+ security:
+ name: cpp security scan
+ runs-on: ubuntu-latest
+ timeout-minutes: 15
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
+ with:
+ python-version: "3.12"
+
+ # Standard library only, so no install step.
+ - name: No workflow folds a comment into a run block
+ run: python laya-java/scripts/check_workflow_folding.py
+
+ # FetchContent runs code from the fetched repository at build time, so the commit it pins
+ # must be the one the upstream tag names, checked on every run and not once by hand.
+ - name: GoogleTest pin matches the upstream tag
+ run: |
+ pinned=$(sed -n 's/^ *GIT_TAG \([0-9a-f]\{40\}\))*$/\1/p' laya-cpp/CMakeLists.txt)
+ tagged=$(git ls-remote https://github.com/google/googletest refs/tags/v1.18.0 | cut -f1)
+ echo "pinned=$pinned tagged=$tagged"
+ test -n "$pinned" && test "$pinned" = "$tagged"
diff --git a/laya-cpp/.gitignore b/laya-cpp/.gitignore
new file mode 100644
index 00000000..567609b1
--- /dev/null
+++ b/laya-cpp/.gitignore
@@ -0,0 +1 @@
+build/
diff --git a/laya-cpp/CMakeLists.txt b/laya-cpp/CMakeLists.txt
new file mode 100644
index 00000000..82f0c393
--- /dev/null
+++ b/laya-cpp/CMakeLists.txt
@@ -0,0 +1,66 @@
+cmake_minimum_required(VERSION 3.20)
+project(laya_cpp VERSION 0.0.0 LANGUAGES CXX)
+
+option(LAYA_BUILD_TESTS "Build the GoogleTest suite" ON)
+option(LAYA_ONNX "Build the ONNX Runtime backend (not implemented yet)" OFF)
+
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+set(CMAKE_CXX_EXTENSIONS OFF)
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+
+# Floating point must match the Python reference bit for bit: no fused multiply-add, no fast math.
+if(MSVC)
+ set(LAYA_FP_FLAGS /fp:precise)
+ set(LAYA_WARN_FLAGS /W4 /WX)
+else()
+ set(LAYA_FP_FLAGS -ffp-contract=off -fno-fast-math)
+ set(LAYA_WARN_FLAGS -Wall -Wextra -Wpedantic -Werror)
+endif()
+
+function(laya_target_flags target)
+ target_compile_options(${target} PRIVATE ${LAYA_FP_FLAGS} ${LAYA_WARN_FLAGS})
+endfunction()
+
+add_library(laya src/version.cpp)
+target_include_directories(laya PUBLIC include)
+laya_target_flags(laya)
+
+if(LAYA_ONNX)
+ message(FATAL_ERROR "LAYA_ONNX is not implemented yet")
+endif()
+
+if(LAYA_BUILD_TESTS)
+ include(FetchContent)
+ # Pinned by commit, not tag: tags move. This is the commit of googletest v1.18.0.
+ FetchContent_Declare(googletest
+ GIT_REPOSITORY https://github.com/google/googletest.git
+ GIT_TAG 063de7e9578f82b369302001269680b4b1553359)
+ set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
+ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
+ FetchContent_MakeAvailable(googletest)
+
+ enable_testing()
+ add_executable(laya_tests tests/version_test.cpp)
+ target_link_libraries(laya_tests PRIVATE laya GTest::gtest_main)
+ laya_target_flags(laya_tests)
+
+ include(GoogleTest)
+ gtest_discover_tests(laya_tests)
+
+ # compile_commands.json is the record of what the compiler was really given, so this fails if
+ # the floating point flags are dropped from either target.
+ # Spelled out here, not derived from LAYA_FP_FLAGS, so dropping a flag above cannot also drop the
+ # requirement.
+ if(MSVC)
+ set(LAYA_FP_FLAGS_ARG "/fp:precise")
+ else()
+ set(LAYA_FP_FLAGS_ARG "-ffp-contract=off|-fno-fast-math")
+ endif()
+ add_test(NAME compile_flags
+ COMMAND ${CMAKE_COMMAND}
+ -DCOMPILE_COMMANDS=${CMAKE_BINARY_DIR}/compile_commands.json
+ -DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}
+ -DREQUIRED_FLAGS=${LAYA_FP_FLAGS_ARG}
+ -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_compile_flags.cmake)
+endif()
diff --git a/laya-cpp/CMakePresets.json b/laya-cpp/CMakePresets.json
new file mode 100644
index 00000000..de7e9175
--- /dev/null
+++ b/laya-cpp/CMakePresets.json
@@ -0,0 +1,30 @@
+{
+ "version": 6,
+ "cmakeMinimumRequired": {"major": 3, "minor": 25, "patch": 0},
+ "configurePresets": [
+ {"name": "base", "hidden": true, "binaryDir": "${sourceDir}/build/${presetName}",
+ "cacheVariables": {"LAYA_BUILD_TESTS": "ON"}},
+ {"name": "debug", "inherits": "base", "cacheVariables": {"CMAKE_BUILD_TYPE": "Debug"}},
+ {"name": "asan-ubsan", "inherits": "base",
+ "cacheVariables": {"CMAKE_BUILD_TYPE": "Debug",
+ "CMAKE_CXX_FLAGS": "-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer",
+ "CMAKE_EXE_LINKER_FLAGS": "-fsanitize=address,undefined"}},
+ {"name": "tsan", "inherits": "base",
+ "cacheVariables": {"CMAKE_BUILD_TYPE": "Debug",
+ "CMAKE_CXX_FLAGS": "-fsanitize=thread",
+ "CMAKE_EXE_LINKER_FLAGS": "-fsanitize=thread"}},
+ {"name": "release", "inherits": "base", "cacheVariables": {"CMAKE_BUILD_TYPE": "Release"}}
+ ],
+ "buildPresets": [
+ {"name": "debug", "configurePreset": "debug"},
+ {"name": "asan-ubsan", "configurePreset": "asan-ubsan"},
+ {"name": "tsan", "configurePreset": "tsan"},
+ {"name": "release", "configurePreset": "release"}
+ ],
+ "testPresets": [
+ {"name": "debug", "configurePreset": "debug", "output": {"outputOnFailure": true}},
+ {"name": "asan-ubsan", "configurePreset": "asan-ubsan", "output": {"outputOnFailure": true}},
+ {"name": "tsan", "configurePreset": "tsan", "output": {"outputOnFailure": true}},
+ {"name": "release", "configurePreset": "release", "output": {"outputOnFailure": true}}
+ ]
+}
diff --git a/laya-cpp/README.md b/laya-cpp/README.md
new file mode 100644
index 00000000..a5393d44
--- /dev/null
+++ b/laya-cpp/README.md
@@ -0,0 +1,37 @@
+# laya-cpp
+
+A C++17 port of [laya](../README.md), following the same plan as [laya-go](../laya-go/README.md):
+the Python package is the reference and the port is checked against goldens the reference records.
+
+**Status: skeleton only.** There is no library code yet beyond `laya::version()`. This directory
+holds the build, the presets, the CI lane and the test-count checker, so the toolchain can be
+reviewed before anything is ported. ONNX Runtime support (`-DLAYA_ONNX=ON`) is not implemented
+and is refused at configure time.
+
+## Build and test
+
+Needs CMake 3.25+ and a C++17 compiler (gcc 13, clang 18 and Apple clang are exercised). The first
+configure fetches GoogleTest, pinned by commit.
+
+```sh
+cd laya-cpp
+cmake --preset debug && cmake --build --preset debug && ctest --preset debug
+# also: asan-ubsan, tsan, release
+```
+
+Options: `LAYA_BUILD_TESTS` (default ON), `LAYA_ONNX` (default OFF).
+
+Floating point is compiled with `-ffp-contract=off -fno-fast-math` (`/fp:precise` on MSVC) and
+warnings are errors. The `compile_flags` test fails if either flag is missing from a laya-cpp
+translation unit.
+
+## Test-count gate
+
+`scripts/check_test_xml.py` reads JUnit XML and fails on any skipped or failed test and below
+`--min-tests`; `scripts/test_check_test_xml.py` proves it can fail:
+
+```sh
+ctest --preset debug --output-junit "$PWD/build/debug/junit.xml"
+python3 scripts/check_test_xml.py build/debug/junit.xml --min-tests 2
+python3 scripts/test_check_test_xml.py
+```
diff --git a/laya-cpp/cmake/check_compile_flags.cmake b/laya-cpp/cmake/check_compile_flags.cmake
new file mode 100644
index 00000000..d880155e
--- /dev/null
+++ b/laya-cpp/cmake/check_compile_flags.cmake
@@ -0,0 +1,32 @@
+cmake_minimum_required(VERSION 3.20) # script mode: enables IN_LIST (CMP0057) on older CMake
+# Fail unless every laya-cpp translation unit was compiled with every flag in REQUIRED_FLAGS
+# ("|" separated). Reads the compile_commands.json CMake wrote.
+foreach(var COMPILE_COMMANDS SOURCE_DIR REQUIRED_FLAGS)
+ if(NOT DEFINED ${var})
+ message(FATAL_ERROR "check_compile_flags: ${var} is not set")
+ endif()
+endforeach()
+file(READ "${COMPILE_COMMANDS}" json)
+string(JSON count LENGTH "${json}")
+string(REPLACE "|" ";" required "${REQUIRED_FLAGS}")
+set(checked 0)
+math(EXPR last "${count} - 1")
+foreach(i RANGE ${last})
+ string(JSON file GET "${json}" ${i} file)
+ # Only our own sources; fetched code (googletest) lives under the build tree.
+ if(NOT file MATCHES "^${SOURCE_DIR}/(src|tests)/")
+ continue()
+ endif()
+ string(JSON command GET "${json}" ${i} command)
+ separate_arguments(args UNIX_COMMAND "${command}")
+ foreach(flag IN LISTS required)
+ if(NOT flag IN_LIST args)
+ message(FATAL_ERROR "${file} was compiled without ${flag}:\n${command}")
+ endif()
+ endforeach()
+ math(EXPR checked "${checked} + 1")
+endforeach()
+if(checked LESS 2)
+ message(FATAL_ERROR "expected at least 2 laya-cpp translation units, found ${checked}")
+endif()
+message(STATUS "compile flags ok on ${checked} translation units")
diff --git a/laya-cpp/include/laya/version.hpp b/laya-cpp/include/laya/version.hpp
new file mode 100644
index 00000000..98be21d6
--- /dev/null
+++ b/laya-cpp/include/laya/version.hpp
@@ -0,0 +1,8 @@
+#pragma once
+
+namespace laya {
+
+// Library version, "major.minor.patch".
+const char* version() noexcept;
+
+} // namespace laya
diff --git a/laya-cpp/scripts/check_test_xml.py b/laya-cpp/scripts/check_test_xml.py
new file mode 100644
index 00000000..4dba26aa
--- /dev/null
+++ b/laya-cpp/scripts/check_test_xml.py
@@ -0,0 +1,74 @@
+#!/usr/bin/env python3
+"""Fail a JUnit XML test report in which any test skipped, failed, or went missing.
+
+A lane that exits 0 having skipped its tests proves nothing, and GoogleTest and CTest both treat a
+skip as success. This reads the report (GoogleTest `--gtest_output=xml:` or `ctest --output-junit`)
+and counts instead of trusting the exit status.
+
+ ctest --test-dir build/debug --output-junit junit.xml
+ python laya-cpp/scripts/check_test_xml.py junit.xml --min-tests 2
+
+Exit 0 only when no test skipped, none failed or errored, and at least --min-tests ran. A disabled
+test (`status="notrun"`) counts as skipped. Several report files may be given; counts add up.
+"""
+import argparse
+import os
+import sys
+# XXE and entity bombs need a DTD, and summarise() refuses any report that has one, so the stdlib
+# parser is safe here and the checker keeps to the standard library.
+import xml.etree.ElementTree as ET # nosemgrep: use-defused-xml
+
+
+def summarise(path):
+ """(tests, problems) for one report file. Raises ValueError on a malformed report."""
+ try:
+ with open(path, "rb") as handle:
+ data = handle.read()
+ if b"<!DOCTYPE" in data or b"<!ENTITY" in data:
+ raise ValueError("%s: a DTD is not allowed in a test report" % path)
+ root = ET.fromstring(data)
+ except (ET.ParseError, OSError) as err:
+ raise ValueError("%s is not a readable XML report: %s" % (path, err))
+ if root.tag not in ("testsuites", "testsuite"):
+ raise ValueError("%s: unexpected root element <%s>" % (path, root.tag))
+ tests, problems = 0, []
+ for case in root.iter("testcase"):
+ tests += 1
+ name = "%s.%s" % (case.get("classname") or "", case.get("name") or "?")
+ children = {child.tag for child in case}
+ if "skipped" in children or case.get("status") == "notrun":
+ problems.append("skipped: " + name)
+ if "failure" in children or "error" in children or case.get("status") == "fail":
+ problems.append("failed: " + name)
+ return tests, problems
+
+
+def main(argv=None):
+ parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
+ parser.add_argument("reports", nargs="+", help="JUnit XML report file(s)")
+ parser.add_argument("--min-tests", type=int, default=1,
+ help="fail when fewer tests than this ran")
+ args = parser.parse_args(argv)
+ total, problems = 0, []
+ for path in args.reports:
+ if not os.path.isfile(path):
+ print("check_test_xml: %s does not exist: the step that writes it did not run or "
+ "failed before producing it" % path, file=sys.stderr)
+ return 1
+ try:
+ tests, found = summarise(path)
+ except ValueError as err:
+ print("check_test_xml: %s" % err, file=sys.stderr)
+ return 1
+ total += tests
+ problems += found
+ if total < args.min_tests:
+ problems.append("only %d tests ran, floor is %d" % (total, args.min_tests))
+ for problem in problems:
+ print("check_test_xml: " + problem, file=sys.stderr)
+ print("check_test_xml: %d tests, %d problems" % (total, len(problems)))
+ return 1 if problems else 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/laya-cpp/scripts/test_check_test_xml.py b/laya-cpp/scripts/test_check_test_xml.py
new file mode 100644
index 00000000..a3da107b
--- /dev/null
+++ b/laya-cpp/scripts/test_check_test_xml.py
@@ -0,0 +1,83 @@
+#!/usr/bin/env python3
+"""The checker has to be able to fail, or it is decoration: prove each way it can."""
+import os
+import subprocess
+import sys
+import tempfile
+import unittest
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+CHECKER = os.path.join(HERE, "check_test_xml.py")
+
+
+def report(*cases):
+ return '<?xml version="1.0"?><testsuites><testsuite name="s">%s</testsuite></testsuites>' % "".join(cases)
+
+
+PASS = '<testcase name="%s" classname="S" status="run"/>'
+SKIP = '<testcase name="%s" classname="S" status="run"><skipped message="x"/></testcase>'
+FAIL = '<testcase name="%s" classname="S" status="run"><failure message="x"/></testcase>'
+NOTRUN = '<testcase name="%s" classname="S" status="notrun"/>'
+
+
+def run(text, *extra):
+ with tempfile.NamedTemporaryFile("w", suffix=".xml", delete=False) as handle:
+ handle.write(text)
+ try:
+ done = subprocess.run([sys.executable, CHECKER, handle.name, *extra],
+ capture_output=True, text=True)
+ finally:
+ os.unlink(handle.name)
+ return done.returncode, done.stdout + done.stderr
+
+
+class CheckerTest(unittest.TestCase):
+ def test_all_pass_meets_floor_exactly(self):
+ code, out = run(report(PASS % "a", PASS % "b"), "--min-tests", "2")
+ self.assertEqual(code, 0, out)
+
+ def test_one_below_floor_fails(self):
+ code, out = run(report(PASS % "a", PASS % "b"), "--min-tests", "3")
+ self.assertEqual(code, 1)
+ self.assertIn("floor is 3", out)
+
+ def test_skip_fails(self):
+ code, out = run(report(PASS % "a", SKIP % "b"))
+ self.assertEqual(code, 1)
+ self.assertIn("skipped: S.b", out)
+
+ def test_disabled_test_counts_as_skip(self):
+ code, _ = run(report(PASS % "a", NOTRUN % "b"))
+ self.assertEqual(code, 1)
+
+ def test_failure_fails(self):
+ code, out = run(report(FAIL % "a"))
+ self.assertEqual(code, 1)
+ self.assertIn("failed: S.a", out)
+
+ def test_malformed_xml_fails(self):
+ code, out = run("<testsuites><testsuite>")
+ self.assertEqual(code, 1)
+ self.assertIn("not a readable XML report", out)
+
+ def test_dtd_is_refused(self):
+ code, out = run('<!DOCTYPE x [<!ENTITY a "b">]>' + report(PASS % "a"))
+ self.assertEqual(code, 1)
+ self.assertIn("DTD", out)
+
+ def test_wrong_root_fails(self):
+ code, _ = run("<html/>")
+ self.assertEqual(code, 1)
+
+ def test_missing_file_fails(self):
+ done = subprocess.run([sys.executable, CHECKER, "/nonexistent/x.xml"],
+ capture_output=True, text=True)
+ self.assertEqual(done.returncode, 1)
+
+ def test_empty_report_fails_default_floor(self):
+ code, _ = run(report())
+ self.assertEqual(code, 1)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/laya-cpp/src/version.cpp b/laya-cpp/src/version.cpp
new file mode 100644
index 00000000..3e7f9aca
--- /dev/null
+++ b/laya-cpp/src/version.cpp
@@ -0,0 +1,7 @@
+#include "laya/version.hpp"
+
+namespace laya {
+
+const char* version() noexcept { return "0.0.0"; }
+
+} // namespace laya
diff --git a/laya-cpp/tests/version_test.cpp b/laya-cpp/tests/version_test.cpp
new file mode 100644
index 00000000..4186a1be
--- /dev/null
+++ b/laya-cpp/tests/version_test.cpp
@@ -0,0 +1,10 @@
+#include <gtest/gtest.h>
+
+#include <string>
+
+#include "laya/version.hpp"
+
+TEST(Version, IsSemver) {
+ const std::string v = laya::version();
+ EXPECT_EQ(v, "0.0.0");
+}