Skip to content

Commit

Permalink
feat(python): port to nanobind, add actions for building package
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Nov 5, 2024
1 parent f15e4f5 commit 56e2848
Show file tree
Hide file tree
Showing 18 changed files with 482 additions and 216 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Create & Deploy Docs
uses: DenverCoder1/doxygen-github-pages-action@v1.3.1
uses: DenverCoder1/doxygen-github-pages-action@v2.0.0
with:
github_token: ${{secrets.GITHUB_TOKEN}}
branch: docs
102 changes: 102 additions & 0 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
name: Build Wheels
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
inputs:
release:
type: boolean
required: true
default: false
description: 'Push the wheels to PyPI'
release:
types: [published]
jobs:
build_sdist:
name: Build SDist
runs-on: ubuntu-latest
defaults:
run:
working-directory: '${{github.workspace}}/lang/python'
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
submodules: recursive

- name: Build SDist
run: |
pipx run build --sdist
- name: Check Metadata
run: |
pipx run twine check dist/*
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: dist-sdist
path: ${{github.workspace}}/lang/python/dist/*.tar.gz

# Linux build fails because the compiler's not new enough
# macOS build fails because it keeps trying to build a universal binary
#
# build_wheels:
# strategy:
# fail-fast: false
# matrix:
# os: [windows-latest, ubuntu-latest, macos-latest]
# name: Build Wheels For ${{matrix.os}}
# runs-on: ${{matrix.os}}
# steps:
# - name: Checkout Repository
# uses: actions/checkout@v4
# with:
# submodules: true
#
# - name: Build Wheels
# uses: pypa/[email protected]
# with:
# package-dir: '${{github.workspace}}/lang/python'
# output-dir: '${{github.workspace}}/lang/python/wheelhouse'
#
# - name: Verify Clean Directory
# shell: bash
# run: |
# git diff --exit-code
#
# - name: Upload Wheels
# uses: actions/upload-artifact@v4
# with:
# path: ${{github.workspace}}/lang/python/wheelhouse/*.whl
# name: dist-${{matrix.os}}

merge_wheels:
name: Merge Wheels
runs-on: ubuntu-latest
needs:
- build_sdist
# - build_wheels
steps:
- name: Merge Artifacts
uses: actions/upload-artifact/merge@v4
with:
name: dist
pattern: dist-*

upload_release:
name: Upload a Release
if: (github.event_name == 'release' && github.event.action == 'published') || (github.event_name == 'workflow_dispatch' && inputs.release)
needs: [merge_wheels]
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v5
- uses: actions/download-artifact@v4
with:
path: dist
- uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{secrets.PYPI_TOKEN}}
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ cmake-build-*/
out/

# Generated
__pycache__/
docs/html/
test/res/
test/Helpers.h

# Python
.mypy_cache/
__pycache__/
*.pyd
*.pyi
*.typed
*.whl
58 changes: 47 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.25 FATAL_ERROR)

# Set defaults before project call
if(PROJECT_IS_TOP_LEVEL)
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE INTERNAL "" FORCE)
endif()


Expand Down Expand Up @@ -101,15 +101,16 @@ endif()
# Python bindings, part 1
if(SOURCEPP_BUILD_PYTHON_WRAPPERS)
set(SOURCEPP_PYTHON_NAME "${PROJECT_NAME}_python")
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(Python REQUIRED
COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)
FetchContent_Declare(
pybind11
GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
GIT_TAG "v2.13.6")
FetchContent_MakeAvailable(pybind11)
nanobind
GIT_REPOSITORY "https://github.com/wjakob/nanobind.git"
GIT_TAG "origin/master")
FetchContent_MakeAvailable(nanobind)
set(${SOURCEPP_PYTHON_NAME}_SOURCES "")
set(${SOURCEPP_PYTHON_NAME}_DEFINES "")
list(APPEND ${SOURCEPP_PYTHON_NAME}_DEPS pybind11::headers)
endif()


Expand Down Expand Up @@ -143,7 +144,6 @@ endif()
# Benchmarks
if(SOURCEPP_BUILD_BENCHMARKS)
set(SOURCEPP_BENCH_NAME "${PROJECT_NAME}_bench")
include(FetchContent)
FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
Expand Down Expand Up @@ -182,16 +182,52 @@ endif()

# Python bindings, part 2
if(SOURCEPP_BUILD_PYTHON_WRAPPERS)
python_add_library(${SOURCEPP_PYTHON_NAME} MODULE "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/sourcepp.cpp" ${${SOURCEPP_PYTHON_NAME}_SOURCES} WITH_SOABI)
set_target_properties(${SOURCEPP_PYTHON_NAME} PROPERTIES PREFIX "_" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/dist/sourcepp")
nanobind_add_module(${SOURCEPP_PYTHON_NAME} NB_STATIC STABLE_ABI LTO
"${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/sourcepp.cpp"
${${SOURCEPP_PYTHON_NAME}_SOURCES})
set_target_properties(${SOURCEPP_PYTHON_NAME} PROPERTIES
OUTPUT_NAME "_${PROJECT_NAME}_impl"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/sourcepp"
LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/sourcepp"
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/sourcepp")
target_compile_definitions(${SOURCEPP_PYTHON_NAME} PRIVATE ${${SOURCEPP_PYTHON_NAME}_DEFINES})
target_link_libraries(${SOURCEPP_PYTHON_NAME} PRIVATE ${${SOURCEPP_PYTHON_NAME}_DEPS})

add_custom_target(${SOURCEPP_PYTHON_NAME}_all)
add_dependencies(${SOURCEPP_PYTHON_NAME}_all ${SOURCEPP_PYTHON_NAME})

# We need to manually write out each module :(
set(${SOURCEPP_PYTHON_NAME}_MODULES
"sourcepp"
"sourcepp._sourcepp_impl"
"sourcepp._sourcepp_impl.gamepp"
"sourcepp._sourcepp_impl.sourcepp"
"sourcepp._sourcepp_impl.sourcepp.math"
"sourcepp._sourcepp_impl.steampp"
"sourcepp._sourcepp_impl.vcryptpp"
"sourcepp._sourcepp_impl.vcryptpp.VFONT"
"sourcepp._sourcepp_impl.vcryptpp.VICE"
"sourcepp._sourcepp_impl.vtfpp"
"sourcepp._sourcepp_impl.vtfpp.ImageFormatDetails"
"sourcepp._sourcepp_impl.vtfpp.ImageDimensions"
"sourcepp._sourcepp_impl.vtfpp.ImageConversion")
foreach(MODULE ${${SOURCEPP_PYTHON_NAME}_MODULES})
string(REPLACE "." "/" MODULE_DIR "${MODULE}")
string(REPLACE "." "_" MODULE_NAME_NORMALIZED "${MODULE}")
set(MODULE_NAME_NORMALIZED "${MODULE_NAME_NORMALIZED}_stub")
nanobind_add_stub("${SOURCEPP_PYTHON_NAME}_stub_${MODULE_NAME_NORMALIZED}"
DEPENDS ${SOURCEPP_PYTHON_NAME}
MODULE "${MODULE}"
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/${MODULE_DIR}.pyi"
PYTHON_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src")
add_dependencies(${SOURCEPP_PYTHON_NAME}_all ${SOURCEPP_PYTHON_NAME}_stub_${MODULE_NAME_NORMALIZED})
endforeach()
endif()


# Print options
print_options(OPTIONS
USE_BSPPP USE_DMXPP USE_GAMEPP USE_KVPP USE_MDLPP USE_STEAMPP USE_TOOLPP USE_VCRYPTPP USE_VPKPP USE_VTFPP
BUILD_BENCHMARKS BUILD_C_WRAPPERS BUILD_PYTHON_WRAPPERS BUILD_WITH_OPENCL BUILD_WITH_TBB BUILD_WITH_THREADS BUILD_TESTS BUILD_WIN7_COMPAT
BUILD_BENCHMARKS BUILD_C_WRAPPERS BUILD_CSHARP_WRAPPERS BUILD_PYTHON_WRAPPERS BUILD_WITH_OPENCL BUILD_WITH_TBB BUILD_WITH_THREADS BUILD_TESTS BUILD_WIN7_COMPAT
LINK_STATIC_MSVC_RUNTIME
VPKPP_SUPPORT_VPK_V54)
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
</tr>
<tr><!-- empty row to disable github striped bg color --></tr>
<tr>
<td rowspan="1"><code>bsppp</code></td>
<td rowspan="1"><code>bsppp</code><sup>*</sup></td>
<td><a href="https://developer.valvesoftware.com/wiki/BSP_(Source)">BSP</a> v17-27</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td rowspan="1" align="center"></td>
</tr>
<tr><!-- empty row to disable github striped bg color --></tr>
<tr>
<td rowspan="1"><code>dmxpp</code></td>
<td rowspan="1"><code>dmxpp</code><sup>*</sup></td>
<td><a href="https://developer.valvesoftware.com/wiki/DMX">DMX</a> Binary v1-5</td>
<td align="center">✅</td>
<td align="center">❌</td>
Expand All @@ -53,15 +53,15 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
<tr><!-- empty row to disable github striped bg color --></tr>
<tr>
<td rowspan="1"><code>kvpp</code></td>
<td><a href="https://developer.valvesoftware.com/wiki/KeyValues">KeyValues</a> v1<sup>*</sup></td>
<td><a href="https://developer.valvesoftware.com/wiki/KeyValues">KeyValues</a> Text v1<sup>&dagger;</sup></td>
<td align="center">✅</td>
<td align="center">✅</td>
<td rowspan="1" align="center"></td>
</tr>
<tr><!-- empty row to disable github striped bg color --></tr>
<tr>
<td rowspan="5"><code>mdlpp</code></td>
<td><a href="https://developer.valvesoftware.com/wiki/MDL_(Source)">MDL</a> v44-49<sup>&dagger;</sup></td>
<td rowspan="5"><code>mdlpp</code><sup>*</sup></td>
<td><a href="https://developer.valvesoftware.com/wiki/MDL_(Source)">MDL</a> v44-49</td>
<td align="center">✅</td>
<td align="center">❌</td>
<td rowspan="5" align="center"></td>
Expand Down Expand Up @@ -290,9 +290,16 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
</tr>
</table>

(\*) Many text-based formats in Source are close to (if not identical to) KeyValues v1, such as [VDF](https://developer.valvesoftware.com/wiki/VDF), [VMT](https://developer.valvesoftware.com/wiki/VMT), and [VMF](https://developer.valvesoftware.com/wiki/VMF_(Valve_Map_Format)).
(\*) These libraries are incomplete and still in development. Their interfaces are unstable and will likely change in the future.
Libraries not starred should be considered stable, and their existing interfaces will not change much if at all. Note that wrappers
only exist for stable libraries.

(&dagger;) The MDL parser is not complete. It is usable in its current state, but it does not currently parse more complex components like animations. This parser is still in development.
(&dagger;) Many text-based formats in Source are close to (if not identical to) KeyValues v1, such as [VMT](https://developer.valvesoftware.com/wiki/VMT) and [VMF](https://developer.valvesoftware.com/wiki/VMF_(Valve_Map_Format)).

## Wrappers

Wrappers for libraries considered complete exist for C, C#, and/or Python, depending on the library. The Python wrappers can be
found on PyPI in the `sourcepp` package.

## Special Thanks

Expand Down
31 changes: 31 additions & 0 deletions THIRDPARTY_LEGAL_NOTICES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,37 @@ freely, subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution.


--------------- nanobind ---------------

Copyright (c) 2022 Wenzel Jakob <[email protected]>.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------- stb ---------------

Copyright (c) 2017 Sean Barrett
Expand Down
21 changes: 14 additions & 7 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
<th>Wrappers</th>
</tr>
<tr>
<td rowspan="1"><code>bsppp</code></td>
<td rowspan="1"><code>bsppp</code><sup>*</sup></td>
<td><a href="https://developer.valvesoftware.com/wiki/BSP_(Source)">BSP</a> v17-27</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td rowspan="1" align="center"></td>
</tr>
<tr style="background: none;">
<td rowspan="1"><code>dmxpp</code></td>
<td rowspan="1"><code>dmxpp</code><sup>*</sup></td>
<td><a href="https://developer.valvesoftware.com/wiki/DMX">DMX</a> Binary v1-5</td>
<td align="center">✅</td>
<td align="center">❌</td>
Expand All @@ -49,14 +49,14 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
</tr>
<tr>
<td rowspan="1"><code>kvpp</code></td>
<td><a href="https://developer.valvesoftware.com/wiki/KeyValues">KeyValues</a> v1<sup>*</sup></td>
<td><a href="https://developer.valvesoftware.com/wiki/KeyValues">KeyValues</a> Text v1<sup>&dagger;</sup></td>
<td align="center">✅</td>
<td align="center">✅</td>
<td rowspan="1" align="center"></td>
</tr>
<tr>
<td rowspan="3"><code>mdlpp</code></td>
<td><a href="https://developer.valvesoftware.com/wiki/MDL_(Source)">MDL</a> v44-49<sup>&dagger;</sup></td>
<td rowspan="3"><code>mdlpp</code><sup>*</sup></td>
<td><a href="https://developer.valvesoftware.com/wiki/MDL_(Source)">MDL</a> v44-49</td>
<td align="center">✅</td>
<td align="center">❌</td>
<td rowspan="3" align="center"></td>
Expand Down Expand Up @@ -253,9 +253,16 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
</table>
\endhtmlonly

(\*) Many text-based formats in Source are close to (if not identical to) KeyValues v1, such as [VDF](https://developer.valvesoftware.com/wiki/VDF), [VMT](https://developer.valvesoftware.com/wiki/VMT), and [VMF](https://developer.valvesoftware.com/wiki/VMF_(Valve_Map_Format)).
(\*) These libraries are incomplete and still in development. Their interfaces are unstable and will likely change in the future.
Libraries not starred should be considered stable, and their existing interfaces will not change much if at all. Note that wrappers
only exist for stable libraries.

(&dagger;) The MDL parser is not complete. It is usable in its current state, but it does not currently parse more complex components like animations. This parser is still in development.
(&dagger;) Many text-based formats in Source are close to (if not identical to) KeyValues v1, such as [VMT](https://developer.valvesoftware.com/wiki/VMT) and [VMF](https://developer.valvesoftware.com/wiki/VMF_(Valve_Map_Format)).

## Wrappers

Wrappers for libraries considered complete exist for C, C#, and/or Python, depending on the library. The Python wrappers can be
found on PyPI in the `sourcepp` package.

## Special Thanks

Expand Down
12 changes: 8 additions & 4 deletions lang/python/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Build Files
__pycache__/
dist/sourcepp/*
!dist/sourcepp/__init__.py
# Keep most Python-specific stuff in the root .gitignore
# because scikit-build-core will use it to ignore everything
# you want to ship!!!!!!!! This took like an hour to figure
# out, thanks folks :D

# Anyway

wheelhouse/
Loading

0 comments on commit 56e2848

Please sign in to comment.