Skip to content

Commit

Permalink
Update python bindings build instructions (#37)
Browse files Browse the repository at this point in the history
* updates to instructions

* don't need boost include and lib dirs

* add note

* update

* Add helpful script to download, build and install boost

* update README

* update README
  • Loading branch information
hbivens authored Sep 18, 2024
1 parent f18580a commit a25002f
Show file tree
Hide file tree
Showing 13 changed files with 72,578 additions and 30 deletions.
11 changes: 6 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ endif( SpecUtils_JAVA_SWIG )
set( THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty" )

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

set( sources
src/SpecFile.cpp
Expand Down Expand Up @@ -399,7 +400,7 @@ endif( SpecUtils_ENABLE_URI_SPECTRA )


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


Expand All @@ -418,7 +419,7 @@ elseif( WIN32 )
endif( MINGW )

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

if( SpecUtils_BUILD_REGRESSION_TEST )
if( NOT SpecUtils_ENABLE_EQUALITY_CHECKS )
Expand Down
Binary file added bindings/python/Ex_pyconverted.chn
Binary file not shown.
15,487 changes: 15,487 additions & 0 deletions bindings/python/Ex_pyconverted.n42

Large diffs are not rendered by default.

Binary file added bindings/python/Ex_pyconverted.pcf
Binary file not shown.
Binary file added bindings/python/Ex_pyconverted_writeToFile.chn
Binary file not shown.
Binary file added bindings/python/Ex_pyconverted_writeToStream.chn
Binary file not shown.
32 changes: 8 additions & 24 deletions bindings/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,13 @@ python3 setup.py bdist_wheel
python3 -m pip install --user --force dist/SpecUtils-0.0.1-cp39-cp39-macosx_12_0_x86_64.whl
```


## Windows build instructions
Assumes you already compiled boost with Python support
```bash
cd SpecUtils
mkdir build_python
cd build_python
cmake -DSpecUtils_PYTHON_BINDINGS=ON -DCMAKE_PREFIX_PATH=/path/to/boost ..
cmake --build . --config Release -j8
Assumes you already compiled boost with Python support. The script `get-boost.ps1` will help you with this.

mkdir SpecUtils
cp .\Release\libSpecUtils.dll .\SpecUtils\SpecUtils.pyd
cp ..\bindings\python\__init__.py .\SpecUtils\
cp ..\bindings\python\setup.py .
pip install pip setuptools wheel
# We should be able to run the following command to make a package:
# python.exe -m build --wheel
# But I ran into an issue with the "Tag" wasnt compatible with the current
# Python, so needed to use:
python.exe setup.py bdist_wheel --plat-name=win_amd64
# You can list tags compatible with your install `python.exe -m pip debug --verbose`
# And you may be able.
# If we ever get around to only using the stable API, we could use the command:
# python.exe setup.py bdist_wheel --plat-name=win_amd64 --python-tag=cp35 --py-limited-api=cp35
python.exe -m pip install --user --force .\dist\SpecUtils-0.0.1-cp312-cp312-win32.whl
```
Run these powershell scripts to build SpecUtils with python bindings and install the bindings into your python environment. Tested with python 3.11.

```ps1
# Run these in a Visual Studio Development Powershell Console
PS C:\Projects\code\SpecUtils\bindings\python> .\build-4-python.ps1
PS C:\Projects\code\SpecUtils\bindings\python> .\make-wheel.ps1
```
19 changes: 19 additions & 0 deletions bindings/python/build-4-python.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
$build_dir="c:\temp\build-with-python-bindings\"
$project_dir = Resolve-Path ".\..\..\"

$boost_root="C:\Tools\boost_1_84_install"

mkdir -f $build_dir

pushd $build_dir

if (test-path CMakeCache.txt )
{
rm -force CMakeCache.txt
}

cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DBoost_DEBUG=1 -DSpecUtils_PYTHON_BINDINGS=ON -DBOOST_ROOT="$boost_root" $project_dir

ninja

popd
Binary file added bindings/python/ex_output.pcf
Binary file not shown.
158 changes: 158 additions & 0 deletions bindings/python/get-boost.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<#
.SYNOPSIS
This script downloads and builds the Boost library and installs it to a specified directory.
Based on dependency download script written by [email protected]
.DESCRIPTION
This script downloads, verifies, extracts, builds, and installs the Boost library.
It is intended to be run from within the "x64 Native Tools Command Prompt for VS 2022"
terminal (although b2 may invoke it for you).
7-zip is required: https://7-zip.org/download.html
Boost source is from https://archives.boost.io/release/
.PARAMETER buildPath
The temporary directory where the Boost library will be built.
Default: "c:\temp\boost-build"
.PARAMETER installPath
The directory where the Boost library will be installed.
Default: "c:\boost-install"
.PARAMETER boostUrl
The URL from which the Boost library source archive will be downloaded.
Default: "https://archives.boost.io/release/1.86.0/source/boost_1_86_0.7z"
.PARAMETER boostRequiredSha256
The expected SHA256 hash of the downloaded Boost library source archive.
Default: "413ee9d5754d0ac5994a3bf70c3b5606b10f33824fdd56cf04d425f2fc6bb8ce"
.PARAMETER sevenZipPath
The path to the 7-Zip executable (7z.exe).
Default: "C:\Program Files\7-Zip\7z.exe"
.EXAMPLE
.\get-boost.ps1 -buildPath "c:\temp\boost-build" -installPath "c:\boost-install"
.EXAMPLE
.\get-boost.ps1 -buildPath "c:\temp\boost-build" -installPath "c:\boost-install" `
-boostUrl "https://example.com/boost_1_86_0.7z" `
-boostRequiredSha256 "your_sha256_hash_here"
.EXAMPLE
.\get-boost.ps1 -buildPath "c:\temp\boost-build" -installPath "c:\boost-install" `
-boostUrl "https://example.com/boost_1_86_0.7z" `
-boostRequiredSha256 "your_sha256_hash_here" `
-sevenZipPath "C:\Path\To\7z.exe"
#>
param (
[string]$buildPath = "c:\temp\boost-build",
[string]$installPath = "c:\boost-install",
[string]$boostUrl = "https://archives.boost.io/release/1.86.0/source/boost_1_86_0.7z",
[string]$boostRequiredSha256 = "413ee9d5754d0ac5994a3bf70c3b5606b10f33824fdd56cf04d425f2fc6bb8ce",
[string]$sevenZipPath = "C:\Program Files\7-Zip\7z.exe"
)

if (-not $buildPath -or -not $installPath) {
Write-Host "Usage: .\get-boost.ps1 [build path] [install path] [boost URL] [boost SHA256]"
exit 1
}

if (-not (Test-Path -Path $sevenZipPath)) {
Write-Host "7-Zip executable not found at $sevenZipPath. You can download it here: https://7-zip.org/download.html"
exit 1
}

$ErrorActionPreference = "Stop"

function Create-Directory {
param (
[string]$path
)
if (-not (Test-Path -Path $path)) {
New-Item -ItemType Directory -Path $path | Out-Null
Write-Host "Created directory $path"
} else {
Write-Host "Directory $path already exists."
}
}

$origDir = Get-Location

Create-Directory -path $buildPath
$buildDir = Resolve-Path -Path $buildPath

Create-Directory -path $installPath
$installDir = Resolve-Path -Path $installPath

Write-Host "Will build in $buildDir"
Write-Host "Will install to $installDir"

Set-Location -Path $buildDir

$boostFile = [System.IO.Path]::GetFileName($boostUrl)
$boostDir = [System.IO.Path]::GetFileNameWithoutExtension($boostFile)
$boostBuiltFile = "built_$boostDir"

if (-not (Test-Path -Path $boostBuiltFile)) {
if (-not (Test-Path -Path $boostFile)) {
Invoke-WebRequest -Uri $boostUrl -OutFile $boostFile
Write-Host "Downloaded Boost"
} else {
Write-Host "$boostFile already downloaded"
}

$boostSha256 = (Get-FileHash -Path $boostFile -Algorithm SHA256).Hash

if ($boostSha256 -ne $boostRequiredSha256) {
Write-Host "Invalid hash of boost. Expected $boostRequiredSha256 and got $boostSha256"
Set-Location -Path $origDir
exit 2
}

if (-not (Test-Path -Path $boostDir)) {
& "C:\Program Files\7-Zip\7z.exe" x $boostFile -o"$buildDir"
Write-Host "Unzipped $boostFile"
} else {
Write-Host "Boost was already unzipped"
}

Set-Location -Path $boostDir

Write-Host "Running boost bootstrap.bat"
& cmd /c .\bootstrap.bat
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to run bootstrap.bat"
Set-Location -Path $origDir
exit 2
}

Write-Host "Building boost"
& .\b2.exe --build-type=minimal --with-system --with-python runtime-link=static link=static threading=multi variant=release address-model=64 architecture=x86 --prefix=$installDir --build-dir=win_build -j8 install
# if you want everything
#& .\b2.exe --build-type=complete runtime-link=static link=static threading=multi address-model=64 architecture=x86 --prefix=$installDir --build-dir=win_build -j8 install
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to build boost"
Set-Location -Path $origDir
exit 2
}
#Remove-Item -Recurse -Force win_build

Write-Host "Built boost!"

Set-Location -Path $buildDir

# Keep around in case we want to re-build with different options?
#Remove-Item -Recurse -Force $boostDir
#Write-Host "Removed $boostDir directory"

New-Item -ItemType File -Path $boostBuiltFile | Out-Null
} else {
Write-Host "Boost was already built ($boostBuiltFile existed)"
}

Write-Host "Completed Successfully"
Set-Location -Path $origDir

exit 0
17 changes: 17 additions & 0 deletions bindings/python/make-wheel.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$build_dir="c:\temp\build-with-python-bindings\"
$py_bind_dir = pwd

mkdir -f $build_dir/SpecUtils
pushd $build_dir

cp .\libSpecUtils.dll .\SpecUtils\SpecUtils.pyd
cp $py_bind_dir\__init__.py .\SpecUtils\
cp $py_bind_dir\setup.py .\
cp $py_bind_dir\README.md .\
pip install pip setuptools wheel
python.exe setup.py bdist_wheel --plat-name=win_amd64

# version is 0.0.2 as of this writing
python.exe -m pip install --user --force .\dist\SpecUtils-0.0.2-cp311-cp311-win_amd64.whl

popd
Loading

0 comments on commit a25002f

Please sign in to comment.