Skip to content

Commit 56e2848

Browse files
feat(python): port to nanobind, add actions for building package
1 parent f15e4f5 commit 56e2848

File tree

18 files changed

+482
-216
lines changed

18 files changed

+482
-216
lines changed

.github/workflows/deploy_docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Create & Deploy Docs
14-
uses: DenverCoder1/doxygen-github-pages-action@v1.3.1
14+
uses: DenverCoder1/doxygen-github-pages-action@v2.0.0
1515
with:
1616
github_token: ${{secrets.GITHUB_TOKEN}}
1717
branch: docs

.github/workflows/wheels.yml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
name: Build Wheels
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
branches: [main]
7+
workflow_dispatch:
8+
inputs:
9+
release:
10+
type: boolean
11+
required: true
12+
default: false
13+
description: 'Push the wheels to PyPI'
14+
release:
15+
types: [published]
16+
jobs:
17+
build_sdist:
18+
name: Build SDist
19+
runs-on: ubuntu-latest
20+
defaults:
21+
run:
22+
working-directory: '${{github.workspace}}/lang/python'
23+
steps:
24+
- name: Checkout Repository
25+
uses: actions/checkout@v4
26+
with:
27+
submodules: recursive
28+
29+
- name: Build SDist
30+
run: |
31+
pipx run build --sdist
32+
33+
- name: Check Metadata
34+
run: |
35+
pipx run twine check dist/*
36+
37+
- name: Upload Artifact
38+
uses: actions/upload-artifact@v4
39+
with:
40+
name: dist-sdist
41+
path: ${{github.workspace}}/lang/python/dist/*.tar.gz
42+
43+
# Linux build fails because the compiler's not new enough
44+
# macOS build fails because it keeps trying to build a universal binary
45+
#
46+
# build_wheels:
47+
# strategy:
48+
# fail-fast: false
49+
# matrix:
50+
# os: [windows-latest, ubuntu-latest, macos-latest]
51+
# name: Build Wheels For ${{matrix.os}}
52+
# runs-on: ${{matrix.os}}
53+
# steps:
54+
# - name: Checkout Repository
55+
# uses: actions/checkout@v4
56+
# with:
57+
# submodules: true
58+
#
59+
# - name: Build Wheels
60+
# uses: pypa/cibuildwheel@v2.19
61+
# with:
62+
# package-dir: '${{github.workspace}}/lang/python'
63+
# output-dir: '${{github.workspace}}/lang/python/wheelhouse'
64+
#
65+
# - name: Verify Clean Directory
66+
# shell: bash
67+
# run: |
68+
# git diff --exit-code
69+
#
70+
# - name: Upload Wheels
71+
# uses: actions/upload-artifact@v4
72+
# with:
73+
# path: ${{github.workspace}}/lang/python/wheelhouse/*.whl
74+
# name: dist-${{matrix.os}}
75+
76+
merge_wheels:
77+
name: Merge Wheels
78+
runs-on: ubuntu-latest
79+
needs:
80+
- build_sdist
81+
# - build_wheels
82+
steps:
83+
- name: Merge Artifacts
84+
uses: actions/upload-artifact/merge@v4
85+
with:
86+
name: dist
87+
pattern: dist-*
88+
89+
upload_release:
90+
name: Upload a Release
91+
if: (github.event_name == 'release' && github.event.action == 'published') || (github.event_name == 'workflow_dispatch' && inputs.release)
92+
needs: [merge_wheels]
93+
runs-on: ubuntu-latest
94+
steps:
95+
- uses: actions/setup-python@v5
96+
- uses: actions/download-artifact@v4
97+
with:
98+
path: dist
99+
- uses: pypa/gh-action-pypi-publish@release/v1
100+
with:
101+
user: __token__
102+
password: ${{secrets.PYPI_TOKEN}}

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ cmake-build-*/
1010
out/
1111

1212
# Generated
13-
__pycache__/
1413
docs/html/
1514
test/res/
1615
test/Helpers.h
16+
17+
# Python
18+
.mypy_cache/
19+
__pycache__/
20+
*.pyd
21+
*.pyi
22+
*.typed
23+
*.whl

CMakeLists.txt

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.25 FATAL_ERROR)
33

44
# Set defaults before project call
55
if(PROJECT_IS_TOP_LEVEL)
6-
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
6+
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE INTERNAL "" FORCE)
77
endif()
88

99

@@ -101,15 +101,16 @@ endif()
101101
# Python bindings, part 1
102102
if(SOURCEPP_BUILD_PYTHON_WRAPPERS)
103103
set(SOURCEPP_PYTHON_NAME "${PROJECT_NAME}_python")
104-
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
104+
find_package(Python REQUIRED
105+
COMPONENTS Interpreter Development.Module
106+
OPTIONAL_COMPONENTS Development.SABIModule)
105107
FetchContent_Declare(
106-
pybind11
107-
GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
108-
GIT_TAG "v2.13.6")
109-
FetchContent_MakeAvailable(pybind11)
108+
nanobind
109+
GIT_REPOSITORY "https://github.com/wjakob/nanobind.git"
110+
GIT_TAG "origin/master")
111+
FetchContent_MakeAvailable(nanobind)
110112
set(${SOURCEPP_PYTHON_NAME}_SOURCES "")
111113
set(${SOURCEPP_PYTHON_NAME}_DEFINES "")
112-
list(APPEND ${SOURCEPP_PYTHON_NAME}_DEPS pybind11::headers)
113114
endif()
114115

115116

@@ -143,7 +144,6 @@ endif()
143144
# Benchmarks
144145
if(SOURCEPP_BUILD_BENCHMARKS)
145146
set(SOURCEPP_BENCH_NAME "${PROJECT_NAME}_bench")
146-
include(FetchContent)
147147
FetchContent_Declare(
148148
benchmark
149149
GIT_REPOSITORY https://github.com/google/benchmark.git
@@ -182,16 +182,52 @@ endif()
182182

183183
# Python bindings, part 2
184184
if(SOURCEPP_BUILD_PYTHON_WRAPPERS)
185-
python_add_library(${SOURCEPP_PYTHON_NAME} MODULE "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/sourcepp.cpp" ${${SOURCEPP_PYTHON_NAME}_SOURCES} WITH_SOABI)
186-
set_target_properties(${SOURCEPP_PYTHON_NAME} PROPERTIES PREFIX "_" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/dist/sourcepp")
185+
nanobind_add_module(${SOURCEPP_PYTHON_NAME} NB_STATIC STABLE_ABI LTO
186+
"${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/sourcepp.cpp"
187+
${${SOURCEPP_PYTHON_NAME}_SOURCES})
188+
set_target_properties(${SOURCEPP_PYTHON_NAME} PROPERTIES
189+
OUTPUT_NAME "_${PROJECT_NAME}_impl"
190+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/sourcepp"
191+
LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/sourcepp"
192+
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/sourcepp")
187193
target_compile_definitions(${SOURCEPP_PYTHON_NAME} PRIVATE ${${SOURCEPP_PYTHON_NAME}_DEFINES})
188194
target_link_libraries(${SOURCEPP_PYTHON_NAME} PRIVATE ${${SOURCEPP_PYTHON_NAME}_DEPS})
195+
196+
add_custom_target(${SOURCEPP_PYTHON_NAME}_all)
197+
add_dependencies(${SOURCEPP_PYTHON_NAME}_all ${SOURCEPP_PYTHON_NAME})
198+
199+
# We need to manually write out each module :(
200+
set(${SOURCEPP_PYTHON_NAME}_MODULES
201+
"sourcepp"
202+
"sourcepp._sourcepp_impl"
203+
"sourcepp._sourcepp_impl.gamepp"
204+
"sourcepp._sourcepp_impl.sourcepp"
205+
"sourcepp._sourcepp_impl.sourcepp.math"
206+
"sourcepp._sourcepp_impl.steampp"
207+
"sourcepp._sourcepp_impl.vcryptpp"
208+
"sourcepp._sourcepp_impl.vcryptpp.VFONT"
209+
"sourcepp._sourcepp_impl.vcryptpp.VICE"
210+
"sourcepp._sourcepp_impl.vtfpp"
211+
"sourcepp._sourcepp_impl.vtfpp.ImageFormatDetails"
212+
"sourcepp._sourcepp_impl.vtfpp.ImageDimensions"
213+
"sourcepp._sourcepp_impl.vtfpp.ImageConversion")
214+
foreach(MODULE ${${SOURCEPP_PYTHON_NAME}_MODULES})
215+
string(REPLACE "." "/" MODULE_DIR "${MODULE}")
216+
string(REPLACE "." "_" MODULE_NAME_NORMALIZED "${MODULE}")
217+
set(MODULE_NAME_NORMALIZED "${MODULE_NAME_NORMALIZED}_stub")
218+
nanobind_add_stub("${SOURCEPP_PYTHON_NAME}_stub_${MODULE_NAME_NORMALIZED}"
219+
DEPENDS ${SOURCEPP_PYTHON_NAME}
220+
MODULE "${MODULE}"
221+
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/${MODULE_DIR}.pyi"
222+
PYTHON_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src")
223+
add_dependencies(${SOURCEPP_PYTHON_NAME}_all ${SOURCEPP_PYTHON_NAME}_stub_${MODULE_NAME_NORMALIZED})
224+
endforeach()
189225
endif()
190226

191227

192228
# Print options
193229
print_options(OPTIONS
194230
USE_BSPPP USE_DMXPP USE_GAMEPP USE_KVPP USE_MDLPP USE_STEAMPP USE_TOOLPP USE_VCRYPTPP USE_VPKPP USE_VTFPP
195-
BUILD_BENCHMARKS BUILD_C_WRAPPERS BUILD_PYTHON_WRAPPERS BUILD_WITH_OPENCL BUILD_WITH_TBB BUILD_WITH_THREADS BUILD_TESTS BUILD_WIN7_COMPAT
231+
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
196232
LINK_STATIC_MSVC_RUNTIME
197233
VPKPP_SUPPORT_VPK_V54)

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
2222
</tr>
2323
<tr><!-- empty row to disable github striped bg color --></tr>
2424
<tr>
25-
<td rowspan="1"><code>bsppp</code></td>
25+
<td rowspan="1"><code>bsppp</code><sup>*</sup></td>
2626
<td><a href="https://developer.valvesoftware.com/wiki/BSP_(Source)">BSP</a> v17-27</td>
2727
<td align="center">✅</td>
2828
<td align="center">✅</td>
2929
<td rowspan="1" align="center"></td>
3030
</tr>
3131
<tr><!-- empty row to disable github striped bg color --></tr>
3232
<tr>
33-
<td rowspan="1"><code>dmxpp</code></td>
33+
<td rowspan="1"><code>dmxpp</code><sup>*</sup></td>
3434
<td><a href="https://developer.valvesoftware.com/wiki/DMX">DMX</a> Binary v1-5</td>
3535
<td align="center">✅</td>
3636
<td align="center">❌</td>
@@ -53,15 +53,15 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
5353
<tr><!-- empty row to disable github striped bg color --></tr>
5454
<tr>
5555
<td rowspan="1"><code>kvpp</code></td>
56-
<td><a href="https://developer.valvesoftware.com/wiki/KeyValues">KeyValues</a> v1<sup>*</sup></td>
56+
<td><a href="https://developer.valvesoftware.com/wiki/KeyValues">KeyValues</a> Text v1<sup>&dagger;</sup></td>
5757
<td align="center">✅</td>
5858
<td align="center">✅</td>
5959
<td rowspan="1" align="center"></td>
6060
</tr>
6161
<tr><!-- empty row to disable github striped bg color --></tr>
6262
<tr>
63-
<td rowspan="5"><code>mdlpp</code></td>
64-
<td><a href="https://developer.valvesoftware.com/wiki/MDL_(Source)">MDL</a> v44-49<sup>&dagger;</sup></td>
63+
<td rowspan="5"><code>mdlpp</code><sup>*</sup></td>
64+
<td><a href="https://developer.valvesoftware.com/wiki/MDL_(Source)">MDL</a> v44-49</td>
6565
<td align="center">✅</td>
6666
<td align="center">❌</td>
6767
<td rowspan="5" align="center"></td>
@@ -290,9 +290,16 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
290290
</tr>
291291
</table>
292292

293-
(\*) 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)).
293+
(\*) These libraries are incomplete and still in development. Their interfaces are unstable and will likely change in the future.
294+
Libraries not starred should be considered stable, and their existing interfaces will not change much if at all. Note that wrappers
295+
only exist for stable libraries.
294296

295-
(&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.
297+
(&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)).
298+
299+
## Wrappers
300+
301+
Wrappers for libraries considered complete exist for C, C#, and/or Python, depending on the library. The Python wrappers can be
302+
found on PyPI in the `sourcepp` package.
296303

297304
## Special Thanks
298305

THIRDPARTY_LEGAL_NOTICES.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,37 @@ freely, subject to the following restrictions:
222222
3. This notice may not be removed or altered from any source distribution.
223223

224224

225+
--------------- nanobind ---------------
226+
227+
Copyright (c) 2022 Wenzel Jakob <wenzel.jakob@epfl.ch>.
228+
All rights reserved.
229+
230+
Redistribution and use in source and binary forms, with or without
231+
modification, are permitted provided that the following conditions are met:
232+
233+
* Redistributions of source code must retain the above copyright notice, this
234+
list of conditions and the following disclaimer.
235+
236+
* Redistributions in binary form must reproduce the above copyright notice,
237+
this list of conditions and the following disclaimer in the documentation
238+
and/or other materials provided with the distribution.
239+
240+
* Neither the name of the copyright holder nor the names of its
241+
contributors may be used to endorse or promote products derived from
242+
this software without specific prior written permission.
243+
244+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
245+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
246+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
247+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
248+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
249+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
250+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
251+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
253+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
254+
255+
225256
--------------- stb ---------------
226257

227258
Copyright (c) 2017 Sean Barrett

docs/index.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
2222
<th>Wrappers</th>
2323
</tr>
2424
<tr>
25-
<td rowspan="1"><code>bsppp</code></td>
25+
<td rowspan="1"><code>bsppp</code><sup>*</sup></td>
2626
<td><a href="https://developer.valvesoftware.com/wiki/BSP_(Source)">BSP</a> v17-27</td>
2727
<td align="center">✅</td>
2828
<td align="center">✅</td>
2929
<td rowspan="1" align="center"></td>
3030
</tr>
3131
<tr style="background: none;">
32-
<td rowspan="1"><code>dmxpp</code></td>
32+
<td rowspan="1"><code>dmxpp</code><sup>*</sup></td>
3333
<td><a href="https://developer.valvesoftware.com/wiki/DMX">DMX</a> Binary v1-5</td>
3434
<td align="center">✅</td>
3535
<td align="center">❌</td>
@@ -49,14 +49,14 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
4949
</tr>
5050
<tr>
5151
<td rowspan="1"><code>kvpp</code></td>
52-
<td><a href="https://developer.valvesoftware.com/wiki/KeyValues">KeyValues</a> v1<sup>*</sup></td>
52+
<td><a href="https://developer.valvesoftware.com/wiki/KeyValues">KeyValues</a> Text v1<sup>&dagger;</sup></td>
5353
<td align="center">✅</td>
5454
<td align="center">✅</td>
5555
<td rowspan="1" align="center"></td>
5656
</tr>
5757
<tr>
58-
<td rowspan="3"><code>mdlpp</code></td>
59-
<td><a href="https://developer.valvesoftware.com/wiki/MDL_(Source)">MDL</a> v44-49<sup>&dagger;</sup></td>
58+
<td rowspan="3"><code>mdlpp</code><sup>*</sup></td>
59+
<td><a href="https://developer.valvesoftware.com/wiki/MDL_(Source)">MDL</a> v44-49</td>
6060
<td align="center">✅</td>
6161
<td align="center">❌</td>
6262
<td rowspan="3" align="center"></td>
@@ -253,9 +253,16 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
253253
</table>
254254
\endhtmlonly
255255

256-
(\*) 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)).
256+
(\*) These libraries are incomplete and still in development. Their interfaces are unstable and will likely change in the future.
257+
Libraries not starred should be considered stable, and their existing interfaces will not change much if at all. Note that wrappers
258+
only exist for stable libraries.
257259

258-
(&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.
260+
(&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)).
261+
262+
## Wrappers
263+
264+
Wrappers for libraries considered complete exist for C, C#, and/or Python, depending on the library. The Python wrappers can be
265+
found on PyPI in the `sourcepp` package.
259266

260267
## Special Thanks
261268

lang/python/.gitignore

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Build Files
2-
__pycache__/
3-
dist/sourcepp/*
4-
!dist/sourcepp/__init__.py
1+
# Keep most Python-specific stuff in the root .gitignore
2+
# because scikit-build-core will use it to ignore everything
3+
# you want to ship!!!!!!!! This took like an hour to figure
4+
# out, thanks folks :D
5+
6+
# Anyway
7+
8+
wheelhouse/

0 commit comments

Comments
 (0)