Skip to content

Commit

Permalink
add first attempt at python library (#349)
Browse files Browse the repository at this point in the history
Fix some of the CI builds and update coverage job.
Add a python package through nanobind and workflows that build and add to pypi
---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
phlptp and pre-commit-ci[bot] authored Dec 21, 2024
1 parent 25a1601 commit b8bf817
Show file tree
Hide file tree
Showing 20 changed files with 1,615 additions and 195 deletions.
12 changes: 9 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ aliases:
jobs:
unitsTSan:
machine:
image: ubuntu-2204:2024.01.2
docker:
- image: helics/buildenv:sanitizers-14
environment:
Expand All @@ -49,8 +51,10 @@ jobs:
- run: *run_units

unitsASan:
machine:
image: ubuntu-2204:2024.11.1
docker:
- image: helics/buildenv:sanitizers-14
- image: helics/buildenv:sanitizers-19
environment:
CMAKE_FLAGS: '-DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_FLAGS="-fsanitize=undefined,address -lc++ -lc++abi -fsanitize-address-use-after-scope -Wno-unused-command-line-argument -fno-omit-frame-pointer -g -O1 -fsanitize-blacklist=/root/project/.circleci/asan_blacklist.txt" '
LSAN_OPTIONS: "suppressions=/root/project/.circleci/leak_suppression.txt"
Expand All @@ -62,6 +66,8 @@ jobs:
- run: *run_units

unitsMSan:
machine:
image: ubuntu-2204:2022.10.2
docker:
- image: helics/buildenv:sanitizers-14
environment:
Expand Down Expand Up @@ -110,9 +116,9 @@ workflows:
version: 2
units_test:
jobs:
- unitsMSan
# - unitsMSan
- unitsASan
- unitsTSan
# - unitsTSan
- unitsInstall
- unitsInstall-alt
- unitsFuzz
14 changes: 7 additions & 7 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ concurrency:
jobs:
coverage:
name: Coverage
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
strategy:
matrix:
std: ["11", "14", "17", "20"]
Expand All @@ -25,11 +25,11 @@ jobs:

- name: Get LCov
run: |
sudo apt-get install ca-certificates
wget https://github.com/linux-test-project/lcov/releases/download/v1.16/lcov-1.16.tar.gz
tar -xzf lcov-1.16.tar.gz
cd lcov-1.16
sudo make install
sudo apt-get install ca-certificates lcov
#wget https://github.com/linux-test-project/lcov/releases/download/v2.2/lcov-2.2.tar.gz
#tar -xzf lcov-2.2.tar.gz
#cd lcov-2.2
#sudo make install
- name: Configure
run: |
Expand All @@ -54,7 +54,7 @@ jobs:
lcov --list coverage.info
working-directory: build

- uses: codecov/codecov-action@v4
- uses: codecov/codecov-action@v5
with:
files: build/coverage.info
fail_ci_if_error: true
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/pip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Pip

on:
workflow_dispatch:
pull_request:
push:
branches:
- main

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.10", "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=11.0" >> $GITHUB_ENV
- name: Build and install
run: |
python -m pip install pytest
pip install -vv .
- name: Test
run: python -m pytest ./test/python
80 changes: 80 additions & 0 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Wheels

on:
workflow_dispatch:
pull_request:
push:
branches:
- main
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

- name: Check metadata
run: pipx run twine check 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-14, windows-latest]

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

- uses: pypa/cibuildwheel@v2.22

- 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 }}

publish-to-testpypi:
name: Upload if release
needs: [build_wheels, build_sdist]
runs-on: ubuntu-latest
#if: github.event_name == 'release' && github.event.action == 'published'
environment:
name: testpypi
url: https://test.pypi.org/p/units_llnl
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
steps:
- uses: actions/setup-python@v5
- uses: actions/download-artifact@v4
with:
pattern: dist-*
merge-multiple: true
path: dist/

- uses: pypa/gh-action-pypi-publish@release/v1
with:
verbose: true
repository-url: https://test.pypi.org/legacy/
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
build*/
~$.xlsx
~$*.xlsx
nanobind_test.egg-info
_skbuild
/dist
__pycache__
build
*.pyd
*.egg-info
.vscode
.vs
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ repos:
- id: end-of-file-fixer
- id: check-shebang-scripts-are-executable
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v19.1.3
rev: v19.1.4
hooks:
- id: clang-format
types_or: [c++, c]
Expand Down
57 changes: 51 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
# SPDX-License-Identifier: BSD-3-Clause
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Make sure users don't get warnings on a tested (3.0 to 3.27) version of CMake. For
# Make sure users don't get warnings on a tested (3.0 to 3.31) version of CMake. For
# most of the policies, the new version is better (hence the change). We don't use the
# 3.0...3.17 syntax because of a bug in an older MSVC's built-in and modified CMake 3.11
if(${CMAKE_VERSION} VERSION_GREATER 3.20)
cmake_minimum_required(VERSION 3.20...3.30)
if(${CMAKE_VERSION} VERSION_GREATER 3.22)
cmake_minimum_required(VERSION 3.22...3.31)
else()
cmake_minimum_required(VERSION 3.0)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
Expand All @@ -23,7 +23,7 @@ endif()
project(
${UNITS_CMAKE_PROJECT_NAME}
LANGUAGES C CXX
VERSION 0.9.2
VERSION 0.10.0
)
include(CMakeDependentOption)
include(CTest)
Expand Down Expand Up @@ -108,6 +108,11 @@ cmake_dependent_option(
UNITS_CLANG_TIDY "Look for and use Clang-Tidy" OFF
"CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME;NOT CMAKE_VERSION VERSION_LESS 3.6" OFF
)

cmake_dependent_option(
UNITS_BUILD_PYTHON_LIBRARY "Build the simplified python library" OFF
"CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME" OFF
)
set(UNITS_CLANG_TIDY_OPTIONS
""
CACHE STRING "Clang tidy options, such as -fix, semicolon separated"
Expand Down Expand Up @@ -219,12 +224,52 @@ elseif(UNITS_ENABLE_TESTS)
message(WARNING "UNITS unit tests only supported under cmake 3.13 or greater")
endif()

if(NOT UNITS_HEADER_ONLY AND NOT UNITS_BUILD_FUZZ_TARGETS)
if(NOT UNITS_HEADER_ONLY
AND NOT UNITS_BUILD_FUZZ_TARGETS
AND NOT SKBUILD
)
add_subdirectory(webserver)
add_subdirectory(converter)
endif()

if(UNITS_INSTALL)
if(NOT UNITS_HEADER_ONLY AND UNITS_BUILD_PYTHON_LIBRARY)
message(STATUS "building the python library")

find_package(
Python 3.10 REQUIRED
COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule
)

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE
Release
CACHE STRING "Choose the type of build." FORCE
)
set_property(
CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel"
"RelWithDebInfo"
)
endif()

# Detect the installed nanobind package and import it into CMake
execute_process(
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE nanobind_ROOT
)
find_package(nanobind CONFIG REQUIRED)

nanobind_add_module(units_llnl_ext NB_STATIC STABLE_ABI python/units_python.cpp)

target_link_libraries(units_llnl_ext PRIVATE units::units)

# Install directive for scikit-build-core
install(TARGETS units_llnl_ext LIBRARY DESTINATION units_llnl)

endif()

if(UNITS_INSTALL AND NOT SKBUILD)
if(UNITS_BUILD_STATIC_LIBRARY)
install(TARGETS compile_flags_target ${UNITS_LIBRARY_EXPORT_COMMAND})
endif()
Expand Down
Loading

0 comments on commit b8bf817

Please sign in to comment.