Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sandialabs/SpecUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohns committed Jan 7, 2025
2 parents 9e00e5a + e6773e6 commit f1e9570
Show file tree
Hide file tree
Showing 25 changed files with 1,944 additions and 17,009 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/pip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# From https://github.com/wjakob/nanobind_example/blob/5ce5ce57143a469e92d9ebaee9c47949ee77caa8/.github/workflows/pip.yml
name: Pip

on:
workflow_dispatch:
pull_request:
push:
branches:
- master

jobs:
build:
name: Build with Pip
runs-on: ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
platform: [windows-latest, macos-latest, ubuntu-latest]
#python-version: ["3.9", "3.12"]
python-version: ["3.12"]

steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Set min macOS version
if: runner.os == 'macOS'
run: |
echo "MACOSX_DEPLOYMENT_TARGET=10.14" >> $GITHUB_ENV
- name: Build and install
run: |
python -m pip install pytest
pip install --verbose ./bindings/python
- name: Test
run: python -m unittest discover ./bindings/python/tests
91 changes: 91 additions & 0 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# From https://github.com/wjakob/nanobind_example/blob/5ce5ce57143a469e92d9ebaee9c47949ee77caa8/.github/workflows/wheels.yml

name: Wheels

on:
workflow_dispatch:
pull_request:
push:
branches:
- master
release:
types:
- published

jobs:
build_sdist:
name: Build SDist
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true

- name: Build SDist
run: pipx run build --sdist ./bindings/python

- name: Check metadata
run: pipx run twine check ./bindings/python/dist/*

- uses: actions/upload-artifact@v4
with:
name: dist-sdist
path: dist/*.tar.gz


build_wheels:
name: Wheels on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-13, macos-14, macos-latest, windows-latest]

steps:
- uses: actions/checkout@v4
with:
submodules: true

- uses: pypa/[email protected]
env:
CIBW_ARCHS_MACOS: "universal2"
with:
package-dir: ./bindings/python

- name: Verify clean directory
run: git diff --exit-code
shell: bash

- name: Upload wheels
uses: actions/upload-artifact@v4
with:
path: wheelhouse/*.whl
name: dist-${{ matrix.os }}

upload_all:
name: Upload if release
needs: [build_wheels, build_sdist]
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/SandiaSpecUtils
permissions:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
if: github.event_name == 'release' && github.event.action == 'published'

steps:
- uses: actions/setup-python@v5
- uses: actions/download-artifact@v4
with:
pattern: dist-*
merge-multiple: true
- shell: bash
run: |
pwd
ls
- uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: /home/runner/work/SpecUtils/SpecUtils
user: __token__
password: ${{ secrets.pypi_password }}
48 changes: 7 additions & 41 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,13 @@ option( SpecUtils_BUILD_UNIT_TESTS "Builds unit tests" OFF )
option( SpecUtils_BUILD_FUZZING_TESTS "Builds fuzzing tests, requires clang" OFF )
option( SpecUtils_BUILD_REGRESSION_TEST "Creates executable to perform interactive regression test" OFF )
option( SpecUtils_BUILD_EXAMPLE "Builds example SpecUtil applications" OFF )
option( SpecUtils_PYTHON_BINDINGS "Creates python bindings to the c++ code" OFF )
option( SpecUtils_JAVA_SWIG "Creates swig/java bindings to the c++ code" OFF )
option( SpecUtils_C_BINDINGS "Creates C bindings to the c++ code" OFF )
option( SpecUtils_INJA_TEMPLATES "Creates inja template interface" OFF )
option( SpecUtils_USE_SIMD "Use SIMD operations; i386/x64 only right now, and very alpha, and extremely minimally used" OFF )
option( SpecUtils_ENABLE_EQUALITY_CHECKS "Enables the equal_enough(...) functions for comparing two spectrum files." OFF ) #code size is only reason to default to off, I think
option( PERFORM_DEVELOPER_CHECKS "Performs additional computationally expensive tests during execution (requires linking to boost)" OFF )


if( SpecUtils_PYTHON_BINDINGS )
option( SpecUtils_SHARED_LIB "Whether to compile a shared, or static library" ON )
list( APPEND sources bindings/python/SpecFile_py.cpp )
else()
#default to building a static lib
option( SpecUtils_SHARED_LIB "Whether to compile a shared, or static library" OFF )
endif()
option( SpecUtils_SHARED_LIB "Whether to compile a shared, or static library" OFF )


set( SpecUtils_FLT_PARSE_METHOD "default_value" CACHE STRING [[How to parse lists of numbers.
Expand Down Expand Up @@ -82,7 +73,7 @@ endif()


# If we are building this on Windows, not as a sub-project, lets enable selecting C++ runtime
if( MSVC AND (SpecUtils_PYTHON_BINDINGS OR SpecUtils_BUILD_UNIT_TESTS) )
if( MSVC AND SpecUtils_BUILD_UNIT_TESTS )
get_directory_property(hasParent PARENT_DIRECTORY)
if(NOT hasParent)
option(${PROJECT_NAME}_USE_MSVC_MultiThreadDLL "Use dynamically-link runtime library." OFF)
Expand All @@ -96,7 +87,7 @@ if( MSVC AND (SpecUtils_PYTHON_BINDINGS OR SpecUtils_BUILD_UNIT_TESTS) )
endif()
message(STATUS "Using CMAKE_MSVC_RUNTIME_LIBRARY ${CMAKE_MSVC_RUNTIME_LIBRARY}")
endif(NOT hasParent)
endif( MSVC AND (SpecUtils_PYTHON_BINDINGS OR SpecUtils_BUILD_UNIT_TESTS) )
endif( MSVC AND SpecUtils_BUILD_UNIT_TESTS )


if( SpecUtils_BUILD_EXAMPLE )
Expand All @@ -121,14 +112,6 @@ endif( SpecUtils_JAVA_SWIG )

set( THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty" )

if( SpecUtils_PYTHON_BINDINGS )
FIND_PACKAGE(Python3 COMPONENTS Development )
IF(NOT Python3_FOUND)
MESSAGE(FATAL_ERROR "Unable to find PythonLibs.")
ENDIF()
message( "Python include dirs: ${Python3_INCLUDE_DIRS}")
endif()

set( sources
src/SpecFile.cpp
src/SpecFile_pcf.cpp
Expand Down Expand Up @@ -255,10 +238,6 @@ if( SpecUtils_ENABLE_URI_SPECTRA )
find_package( ZLIB REQUIRED )
endif( SpecUtils_ENABLE_URI_SPECTRA )

if( SpecUtils_PYTHON_BINDINGS )
list( APPEND sources bindings/python/SpecFile_py.cpp )
endif( SpecUtils_PYTHON_BINDINGS )

if( SpecUtils_JAVA_SWIG )
list( APPEND sources bindings/swig/SpecUtils.i )
endif( SpecUtils_JAVA_SWIG )
Expand All @@ -277,7 +256,6 @@ endif( SpecUtils_SHARED_LIB )
add_library( SpecUtils ${SpecUtils_LIB_TYPE} ${sources} ${headers} ${OTHER_SUPPORT_FILES} )
set_target_properties( SpecUtils PROPERTIES PREFIX "lib" OUTPUT_NAME "SpecUtils" )


set( SpecUtils_USE_FAST_FLOAT OFF ) # Uses https://github.com/fastfloat/fast_float . If fast_float.h isnt found, will be fetched
set( SpecUtils_USE_FROM_CHARS OFF ) # Supported by MSVC >= 2019, and gcc >= 12. Not supported by Apple clang. In MSVC, about 50% slower than boost::spirit
set( SpecUtils_USE_BOOST_SPIRIT OFF ) # Uses boost::spirit, and fasted method (maybe a hair faster than fastfloat)
Expand Down Expand Up @@ -335,28 +313,22 @@ if( SpecUtils_USE_FAST_FLOAT )
endif( NOT FAST_FLOAT_FILE_PATH )
endif( SpecUtils_USE_FAST_FLOAT )

# We only need boost libraries if we are using boost to parse floats, if we are performing developer
# checks, or we are creating Python bindings
if( SpecUtils_USE_BOOST_SPIRIT OR PERFORM_DEVELOPER_CHECKS OR SpecUtils_PYTHON_BINDINGS )
# We only need boost libraries if we are using boost to parse floats, if we are performing developer checks
if( SpecUtils_USE_BOOST_SPIRIT OR PERFORM_DEVELOPER_CHECKS )

if( DEFINED boost_SOURCE_DIR )
# We used CMake Fetch to get boost (e.g., in InterSpec)
target_link_libraries( SpecUtils PUBLIC Boost::system Boost::spirit Boost::fusion Boost::algorithm Boost::functional Boost::crc )

else( DEFINED boost_SOURCE_DIR )

if( SpecUtils_PYTHON_BINDINGS )
list( APPEND boost_libs_required python )
endif( SpecUtils_PYTHON_BINDINGS )

find_package( Boost REQUIRED COMPONENTS system ${boost_libs_required} )
if( NOT Boost_FOUND )
message(FATAL_ERROR "Couldnt Find Boost")
endif( NOT Boost_FOUND )

target_link_libraries( SpecUtils PUBLIC Boost::system )
endif( DEFINED boost_SOURCE_DIR )
endif( SpecUtils_USE_BOOST_SPIRIT OR PERFORM_DEVELOPER_CHECKS OR SpecUtils_PYTHON_BINDINGS )
endif( SpecUtils_USE_BOOST_SPIRIT OR PERFORM_DEVELOPER_CHECKS )

if( SpecUtils_USE_FROM_CHARS )
if( NOT (MSVC AND (MSVC_VERSION GREATER_EQUAL 1920)) )
Expand Down Expand Up @@ -402,17 +374,11 @@ elseif( NOT SpecUtils_USING_NO_THREADING )
endif( SpecUtils_USE_WT_THREADPOOL )



if( SpecUtils_ENABLE_URI_SPECTRA )
target_link_libraries( SpecUtils PUBLIC ZLIB::ZLIB )
endif( SpecUtils_ENABLE_URI_SPECTRA )


if( SpecUtils_PYTHON_BINDINGS )
target_link_libraries( SpecUtils PUBLIC Boost::python ${Boost_PYTHON_LIBRARY} ${Python3_LIBRARIES} )
endif( SpecUtils_PYTHON_BINDINGS )


if( SpecUtils_JAVA_SWIG )
target_link_libraries( SpecUtils PUBLIC ${JAVA_LIBRARIES} )
endif( SpecUtils_JAVA_SWIG )
Expand All @@ -428,7 +394,7 @@ elseif( WIN32 )
endif( MINGW )

target_include_directories( SpecUtils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}
PRIVATE ${THIRD_PARTY_DIR} ${Python3_INCLUDE_DIRS} )
PRIVATE ${THIRD_PARTY_DIR} )

if( SpecUtils_BUILD_REGRESSION_TEST )
if( NOT SpecUtils_ENABLE_EQUALITY_CHECKS )
Expand Down
87 changes: 87 additions & 0 deletions bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
cmake_minimum_required(VERSION 3.15...3.26)

project(SpecUtils_py LANGUAGES CXX)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

set( SpecUtils_ENABLE_D3_CHART ON CACHE BOOL "" )
set( SpecUtils_D3_SUPPORT_FILE_STATIC ON CACHE BOOL "" )
set( SpecUtils_ENABLE_URI_SPECTRA OFF CACHE BOOL "" ) #Lets not worry about linking to zlib right now
set( SpecUtils_PYTHON_BINDINGS ON CACHE BOOL "Build the python bindings" )
set( SpecUtils_ENABLE_EQUALITY_CHECKS OFF CACHE BOOL "" )
set( PERFORM_DEVELOPER_CHECKS OFF CACHE BOOL "" )
set( SpecUtils_SHARED_LIB OFF CACHE BOOL "" ) #we'll statically link SpecUtils into the lib we create here
set( SpecUtils_FLT_PARSE_METHOD "strtod" CACHE STRING "Float parsing method" )

add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/LibSpecUtils )

if (NOT SKBUILD)
message(FATAL_ERROR "\
This CMake file is must be executed using 'scikit-build'.
If you are a user trying to install this package, please use the command
below, which will install all necessary build dependencies, compile
the package in an isolated environment, and then install it.
=====================================================================
$ pip install .
=====================================================================
If you are a software developer, and this is your own package, then
it is usually much more efficient to install the build dependencies
in your environment once and use the following command that avoids
a costly creation of a new virtual environment at every compilation:
=====================================================================
$ pip install nanobind scikit-build-core
$ pip install --no-build-isolation -ve .
=====================================================================
You may optionally add -Ceditable.rebuild=true to auto-rebuild when
the package is imported. Otherwise, you need to re-run the above
after editing C++ files.")
endif()

# Try to import all Python components potentially needed by nanobind
find_package(Python 3.8
REQUIRED COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)

# Import nanobind through CMake's find_package mechanism
find_package(nanobind CONFIG REQUIRED)

# We are now ready to compile the actual extension module
nanobind_add_module(
# Name of the extension
PySpecUtils

# Target the stable ABI for Python 3.12+, which reduces
# the number of binary wheels that must be built. This
# does nothing on older Python versions
STABLE_ABI

# Build libnanobind statically and merge it into the
# extension (which itself remains a shared library)
#
# If your project builds multiple extensions, you can
# replace this flag by NB_SHARED to conserve space by
# reusing a shared libnanobind across libraries
NB_STATIC

#Perform link time optimization.
#LTO


# Source code goes here
SpecFile_py.cpp
)

set_target_properties( PySpecUtils PROPERTIES OUTPUT_NAME "SpecUtils" )
target_link_libraries( PySpecUtils PUBLIC SpecUtils )

# Install directive for scikit-build-core
install(TARGETS PySpecUtils LIBRARY DESTINATION SpecUtils)
install(FILES __init__.py DESTINATION SpecUtils)

nanobind_add_stub(
SpecUtils_stub
MODULE SpecUtils
OUTPUT SpecUtils.pyi
PYTHON_PATH $<TARGET_FILE_DIR:PySpecUtils>
DEPENDS PySpecUtils
)
Binary file removed bindings/python/Ex_pyconverted.chn
Binary file not shown.
Loading

0 comments on commit f1e9570

Please sign in to comment.