Skip to content

Commit

Permalink
First attempt at nanobind code
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 authored and Matt M committed Sep 17, 2024
1 parent 24fb6af commit e5cf605
Show file tree
Hide file tree
Showing 18 changed files with 460 additions and 26 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Setup Node.js
uses: actions/setup-node@v4
with:
Expand All @@ -46,6 +48,7 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'
- name: Fetch tags
run: git fetch --tags --force
- name: Install RoboRIO Toolchain
Expand Down Expand Up @@ -82,6 +85,7 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'
- name: Fetch tags
run: git fetch --tags --force
- name: Install Java 17
Expand Down Expand Up @@ -115,6 +119,7 @@ jobs:
- uses: actions/setup-python@v5
with:
python-version: '3.11'
submodules: 'recursive'
- name: Install dependencies
working-directory: docs
run: |
Expand Down Expand Up @@ -151,6 +156,7 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'
- name: Install Java 17
uses: actions/setup-java@v4
with:
Expand Down Expand Up @@ -195,6 +201,7 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'
- name: Config Git
run: |
git config --global --add safe.directory /__w/photonvision/photonvision
Expand Down Expand Up @@ -225,6 +232,7 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'
- run: git fetch --tags --force
# download all maven-* artifacts to outputs/
- uses: actions/download-artifact@v4
Expand Down Expand Up @@ -277,6 +285,7 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'
- name: Install Java 17
uses: actions/setup-java@v4
with:
Expand Down Expand Up @@ -448,6 +457,7 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'
- uses: actions/download-artifact@v4
with:
name: jar-${{ matrix.artifact-name }}
Expand Down
131 changes: 119 additions & 12 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,155 @@ on:

jobs:
buildAndDeploy:
runs-on: ubuntu-latest

runs-on: ${{ matrix.os }}
strategy:
max-parallel: 1
# fail-fast: true
matrix:
os: ["ubuntu-22.04", "macos-12", "windows-2022"]
python_version:
# - '3.8'
- '3.9'
- '3.10'
# - '3.11'
# - '3.12'


steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.11
python-version: ${{ matrix.python_version }}

- name: Install Java 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin
architecture: ${{ matrix.architecture }}
- run: git fetch --tags --force
- run: |
chmod +x gradlew
./gradlew photon-targeting:build photon-lib:build -i -PpythonExecutable=python${{ matrix.python_version }}
./gradlew photon-lib:installPhotonlibpyNative -PpythonExecutable=python${{ matrix.python_version }}
- name: Install dependencies
working-directory: ./photon-lib/py
run: |
python -m pip install --upgrade pip
pip install setuptools wheel pytest
python -m pip install -r build_requirements.txt
- name: Build wheel
working-directory: ./photon-lib/py
# disable isolation so we can run stubgen (ew but w/e)
run: |
python setup.py sdist bdist_wheel
python -m build -swn
- name: Run Unit Tests
working-directory: ./photon-lib/py
run: |
pip install --no-cache-dir dist/*.whl
pytest
- name: Upload artifacts
uses: actions/upload-artifact@master
with:
name: dist
name: "dist-${{ runner.os }}-${{ matrix.python_version }}"
path: ./photon-lib/py/dist/

- name: Publish package distributions to TestPyPI
# Only upload on tags
if: startsWith(github.ref, 'refs/tags/v')
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages_dir: ./photon-lib/py/dist/
# - name: Publish package distributions to TestPyPI
# # Only upload on tags
# if: startsWith(github.ref, 'refs/tags/v')
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# packages_dir: ./photon-lib/py/dist/

permissions:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing

cross-build:
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
os:
- container: wpilib/roborio-cross-ubuntu:2024-22.04-py312
name: roborio
arch-override: linuxathena

# - container: wpilib/raspbian-cross-ubuntu:bullseye-22.04-py38
# name: raspbian-py38
# - container: wpilib/raspbian-cross-ubuntu:bullseye-22.04-py39
# name: raspbian-py39
# - container: wpilib/raspbian-cross-ubuntu:bullseye-22.04-py310
# name: raspbian-py310
# - container: wpilib/raspbian-cross-ubuntu:bullseye-22.04-py311
# name: raspbian-py311
- container: wpilib/raspbian-cross-ubuntu:bullseye-22.04-py312
name: raspbian-py312
arch-override: linuxarm64

# - container: wpilib/aarch64-cross-ubuntu:bullseye-22.04-py38
# name: raspbian-aarch64-py38
# - container: wpilib/aarch64-cross-ubuntu:bullseye-22.04-py39
# name: raspbian-aarch64-py39
- container: wpilib/aarch64-cross-ubuntu:bullseye-22.04-py310
name: raspbian-aarch64-py310
arch-override: linuxarm64
# - container: wpilib/aarch64-cross-ubuntu:bullseye-22.04-py311
# name: raspbian-aarch64-py311
# - container: wpilib/aarch64-cross-ubuntu:bullseye-22.04-py312
# name: raspbian-aarch64-py312

container:
image: "${{ matrix.os.container }}"

steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
fetch-depth: 0

- name: Install Java 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin

- name: Install setuptools + wheel
run: |
/build/venv/bin/build-pip --disable-pip-version-check install -U "setuptools==63.4.3; python_version < '3.12'"
/build/venv/bin/build-pip --disable-pip-version-check install -U wheel==0.41.2
/build/venv/bin/cross-pip --disable-pip-version-check install -U "setuptools==63.4.3; python_version < '3.12'"
/build/venv/bin/cross-pip --disable-pip-version-check install -U wheel==0.41.2
# See https://github.com/pypa/setuptools_scm/issues/784
- name: Set git directory as safe to allow setuptools-scm to work
shell: bash
run: |
pwd
/usr/bin/git config --global --add safe.directory $(pwd)
- name: Install deps
shell: bash
run: |
/build/venv/bin/cross-pip --disable-pip-version-check install -r photon-lib/py/build_requirements.txt
- run: git fetch --tags --force
- run: |
chmod +x gradlew
./gradlew photon-targeting:build photon-lib:build -i -PpythonExecutable=/build/venv/bin/cross-python -PArchOverride=${{ matrix.os.arch-override }}
./gradlew photon-lib:installPhotonlibpyNative -PpythonExecutable=/build/venv/bin/cross-python -PArchOverride=${{ matrix.os.arch-override }}
- name: Build wheel
working-directory: ./photon-lib/py
# disable isolation so we can run stubgen (ew but w/e)
run: |
/build/venv/bin/cross-python -m build -swn
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,5 @@ venv

.venv/*
.venv
meme/*.so
meme/*.so.4.8
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "photon-lib/nanobind"]
path = photon-lib/nanobind
url = https://github.com/wjakob/nanobind
3 changes: 3 additions & 0 deletions photon-lib/.styleguide
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
generatedFileExclude {
nanobind/.*
}
95 changes: 90 additions & 5 deletions photon-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ nativeUtils {
}
}

def getPythonIncludePath = {
def pythonexe = project.findProperty('pythonExecutable') ?: 'python3.10'
def stdout = new ByteArrayOutputStream()
exec {
commandLine pythonexe, '-c', "from sysconfig import get_paths as gp; print(gp()[\"include\"])"
standardOutput = stdout
}
def ret = stdout.toString().trim();
println("using python include path: " + ret)
return ret
}

def getPythonLibrarySuffix = {
def pythonexe = project.findProperty('pythonExecutable') ?: 'python3.10'
def stdout = new ByteArrayOutputStream()
exec {
commandLine pythonexe, '-c', "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))"
standardOutput = stdout
}
def ret = stdout.toString().trim();
println("using python module suffix: " + ret)
return ret
}

model {
components {
"${nativeName}"(NativeLibrarySpec) {
Expand All @@ -66,13 +90,36 @@ model {
if(project.hasProperty('includePhotonTargeting')) {
lib project: ':photon-targeting', library: 'photontargeting', linkage: 'shared'
}

nativeUtils.useRequiredLibrary(it, "wpilib_shared")
nativeUtils.useRequiredLibrary(it, "apriltag_shared")
nativeUtils.useRequiredLibrary(it, "opencv_shared")
nativeUtils.useRequiredLibrary(it, "cscore_shared")
nativeUtils.useRequiredLibrary(it, "cameraserver_shared")
}

nativeUtils.useRequiredLibrary(it, "wpilib_shared")
nativeUtils.useRequiredLibrary(it, "apriltag_shared")
nativeUtils.useRequiredLibrary(it, "opencv_shared")
nativeUtils.useRequiredLibrary(it, "cscore_shared")
nativeUtils.useRequiredLibrary(it, "cameraserver_shared")
}
photonlibpy(NativeLibrarySpec) {
sources {
cpp {
source {
srcDirs 'src/main/pybindings/cpp'
include '**/*.cpp', '**/*.cc'
}
exportedHeaders {
srcDirs 'nanobind/include', 'nanobind/src', 'nanobind/ext/robin_map/include', getPythonIncludePath()
include "**/*.h"
include "**/*.hpp"
}
}
}

binaries.all {
lib project: ':photon-targeting', library: 'photontargeting', linkage: 'shared'
lib library: nativeName

nativeUtils.useRequiredLibrary(it, "wpilib_shared")
}
}
}
testSuites {
Expand Down Expand Up @@ -142,6 +189,16 @@ model {
}
}
}

binaries {
withType(NativeBinarySpec).all {
if((it.component.baseName == "photonlibpy" || it.component.baseName == nativeName) && it.toolChain instanceof GccCompatibleToolChain) {
println(it)
it.cppCompiler.args << "-Wno-pedantic"
it.linker.args '-Wl,-rpath,\'$ORIGIN\''
}
}
}
}

apply from: "${rootDir}/shared/javacpp/publish.gradle"
Expand Down Expand Up @@ -193,6 +250,34 @@ task publishVendorJsonToLocalOutputs(type: Copy) {
publish.dependsOn it
}

task installPhotonlibpyNative(type: Copy) {
into("$projectDir/py/photonlibpy")

from "$projectDir/build/libs/photonlib/shared/$jniPlatform/release/libphotonlib.so"
for (lib in [
"libcameraserver.so",
"libcscore.so",
"libopencv_core.so.4.8",
"libopencv_calib3d.so.4.8",
"libopencv_features2d.so.4.8",
"libopencv_imgcodecs.so.4.8",
"libopencv_flann.so.4.8",
"libopencv_imgproc.so.4.8",
"libphotontargeting.so",
]) {
from "$projectDir/build/install/photonlibTest/$jniPlatform/release/lib/$lib"
}

from("$projectDir/build/libs/photonlibpy/shared/$jniPlatform/release/libphotonlibpy.so") {
// Basically split
rename "(?:lib)?(.*)\\.(.*)", ('_$1' + getPythonLibrarySuffix())
}

// // TODO - make generic
// it.dependsOn "installPhotonlibTestLinuxx86-64ReleaseGoogleTestExe"
// it.dependsOn "photonlibpyLinuxx86-64ReleaseSharedLibrary"
}

task writeCurrentVersion {
def versionFileIn = file("${rootDir}/shared/PhotonVersion.java.in")
writePhotonVersionFile(versionFileIn, Path.of("$projectDir", "src", "main", "java", "org", "photonvision", "PhotonVersion.java"),
Expand Down
1 change: 1 addition & 0 deletions photon-lib/nanobind
Submodule nanobind added at 9641bb
3 changes: 3 additions & 0 deletions photon-lib/py/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ dist/
build/
.eggs/
photonlibpy/version.py
photonlibpy/*.so*
photonlibpy/*.dylib*
photonlibpy/*.dll*
9 changes: 9 additions & 0 deletions photon-lib/py/build_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
nanobind~=2.1.0
typing-extensions
wpilib~=2024.3.2.1
robotpy-wpimath~=2024.3.2.1
robotpy-apriltag~=2024.3.2.1
pyntcore~=2024.3.2.1
build
setuptools
wheel
Loading

0 comments on commit e5cf605

Please sign in to comment.