Skip to content

Commit

Permalink
Use PYIMATH_OVERRIDE_PYTHON_INSTALL_DIR to specify destination python…
Browse files Browse the repository at this point in the history
… modules (#200)

The previous PYIMATH_OVERRIDE_PYTHON2_INSTALL_DIR and
PYIMATH_OVERRIDE_PYTHON3_INSTALL_DIR options were left over from when
python2 and python3 could be built simultaneously, and even worse,
they were ignored. There's no need for two variables now. This takes
effect only if it is provided.

Also, INSTALL.md properly mentions PYTHON, USE_PYTHON2, and
PYIMATH_OVERRIDE_PYTHON_INSTALL_DIR.

Signed-off-by: Cary Phillips <[email protected]>
  • Loading branch information
cary-ilm committed Sep 1, 2021
1 parent a682224 commit 0b9f732
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 59 deletions.
35 changes: 23 additions & 12 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,6 @@ install`` installs the header files in ``/usr/local/include``, the
object libraries in ``/usr/local/lib``, and the executable programs in
``/usr/local/bin``.

## Python Bindings

If you wish to install the optional python bindings included in this
repository, this must first be toggled on by appending -DPYTHON=ON to the
CMake command before compiling.

% cmake $source_directory -DPYTHON=ON

From here forward PyImath will be compiled until it is toggled back. Using:

% cmake $source_directory -DPYTHON=OFF

## Windows Quick Start

Under Windows, if you are using a command line-based setup, such as
Expand All @@ -81,6 +69,15 @@ can specify a local install directory to CMake via the

% cmake .. -DCMAKE_INSTALL_PREFIX=$install_directory

## Python Bindings

To build and install the optional Python bindings for Imath, set the
CMake option ``PYTHON=ON``.

The Python bindings require that ``boost_python`` is installed. By
default, the bindings build for Python 3. To build with python 2, set
the CMake option ``USE_PYTHON2=ON``.

## Library Names

By default the installed libraries follow a pattern for how they are
Expand Down Expand Up @@ -256,6 +253,20 @@ ways:
``IMATH_OUTPUT_SUBDIR``
Destination sub-folder of the include path for install. Default is ``Imath``.

``PYTHON``
Build the optional Imath python bindings. Default is ``OFF``.

The Python bindings require that ``boost_python`` is installed.

``USE_PYTHON2`` If ``ON`` and ``PYTHON`` is also ``ON``, build the
bindings for Python 2. Default is ``OFF``, implying that the default
bindings are built for Python 3.

``PYIMATH_OVERRIDE_PYTHON_INSTALL_DIR``
Custom destination for installatation of ``imath.so`` and
``imathnumpy.so`` modules. By default, they go into either
``site-packages`` or ``dist-packages`.

To enable half-to-float conversion using the F16C SSE instruction set
for g++ and clang when installing Imath, add the ``-mf16c`` compiler
option:
Expand Down
93 changes: 49 additions & 44 deletions src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ find_package(Imath ${CMAKE_PROJECT_VERSION} REQUIRED CONFIG)
# first make sure we find *some* python
find_package(Python COMPONENTS Interpreter Development)
if(NOT TARGET Python::Interpreter AND NOT TARGET Python::Python)
message(WARNING ": Unable to find any python interpreter or libraries, disabling PyImath")
message(WARNING "Unable to find any python interpreter or libraries, disabling PyImath")
return()
endif()

Expand All @@ -43,13 +43,13 @@ option(USE_PYTHON2 "Whether to use Python 2.x or 3.x" OFF)
if(USE_PYTHON2)
find_package(Python2 COMPONENTS Interpreter Development)
if(Python2_FOUND)
message(STATUS ": Found Python ${Python2_VERSION}")
message(STATUS "Found Python ${Python2_VERSION}")
elseif(Python2::Python)
message(WARNING ": Found Python ${Python2_VERSION} development libraries, but no interpreter")
message(WARNING "Found Python ${Python2_VERSION} development libraries, but no interpreter")
elseif(Python2::Interpreter)
message(WARNING ": Found Python ${Python2_VERSION} interpreter, but no development libraries")
message(WARNING "Found Python ${Python2_VERSION} interpreter, but no development libraries")
else()
message(WARNING ": Unable to find Python2 interpreter or development libraries")
message(WARNING "Unable to find Python2 interpreter or development libraries")
endif()
set(PY_MAJOR_VERSION ${Python2_VERSION_MAJOR})
set(Python_VERSION_MAJOR ${Python2_VERSION_MAJOR})
Expand All @@ -59,13 +59,13 @@ if(USE_PYTHON2)
else()
find_package(Python3 COMPONENTS Interpreter Development)
if(Python3_FOUND)
message(STATUS ": Found Python ${Python3_VERSION}")
message(STATUS "Found Python ${Python3_VERSION}")
elseif(Python3::Python)
message(WARNING ": Found Python ${Python3_VERSION} development libraries, but no interpreter")
message(WARNING "Found Python ${Python3_VERSION} development libraries, but no interpreter")
elseif(Python3::Interpreter)
message(WARNING ": Found Python ${Python3_VERSION} interpreter, but no development libraries")
message(WARNING "Found Python ${Python3_VERSION} interpreter, but no development libraries")
else()
message(WARNING ": Unable to find Python3 interpreter or development libraries")
message(WARNING "Unable to find Python3 interpreter or development libraries")
endif()
set(PY_MAJOR_VERSION ${Python3_VERSION_MAJOR})
set(Python_VERSION_MAJOR ${Python3_VERSION_MAJOR})
Expand All @@ -75,7 +75,7 @@ else()
endif()

if (NOT Python2_FOUND AND NOT Python3_FOUND)
message(WARNING ": Disabling PyImath")
message(WARNING "Disabling PyImath")
return()
endif()

Expand All @@ -84,41 +84,46 @@ endif()
# Boost Python has some .. annoyances in that the python module
# has version names attached to it
function(PYIMATH_EXTRACT_REL_SITEARCH varname pyver pyexe pysitearch)
get_filename_component(_exedir ${pyexe} DIRECTORY)
# we do this such that cmake will canonicalize the slashes
# so the directory search will work under windows and unix
# consistently
get_filename_component(_basedir ${pysitearch} DIRECTORY)
get_filename_component(_basename ${pysitearch} NAME)
set(_basedir "${_basedir}/${_basename}")
string(FIND ${_basedir} ${_exedir} _findloc)
string(LENGTH ${_exedir} _elen)
while(_findloc EQUAL -1 AND _elen GREATER 0)
get_filename_component(_nexedir ${_exedir} DIRECTORY)
string(FIND ${_basedir} ${_nexedir} _findloc)
if (_nexedir STREQUAL _exedir)
message(WARNING "Unable to get parent directory for ${_exedir}, using absolute python site arch folder ${pysitearch}")
set(_elen -1)
break()
else()
set(_exedir ${_nexedir})
endif()
if(PYIMATH_OVERRIDE_PYTHON_INSTALL_DIR)
set(${varname} ${PYIMATH_OVERRIDE_PYTHON_INSTALL_DIR} CACHE STRING "Destination sub-folder (relative) for the python ${pyver} modules")
message(STATUS "Will install to: ${PYIMATH_OVERRIDE_PYTHON_INSTALL_DIR}")
else()
get_filename_component(_exedir ${pyexe} DIRECTORY)
# we do this such that cmake will canonicalize the slashes
# so the directory search will work under windows and unix
# consistently
get_filename_component(_basedir ${pysitearch} DIRECTORY)
get_filename_component(_basename ${pysitearch} NAME)
set(_basedir "${_basedir}/${_basename}")
string(FIND ${_basedir} ${_exedir} _findloc)
string(LENGTH ${_exedir} _elen)
endwhile()
math(EXPR _elen "${_elen}+1")
string(SUBSTRING ${_basedir} ${_elen} -1 _reldir)
if(APPLE)
# on macOS, set install path to user's python package directory
# so that elevated privileges are not necessary
execute_process(
COMMAND "${pyexe}" -c "if True:
import sysconfig
print(sysconfig.get_path('platlib', 'posix_user'))"
OUTPUT_VARIABLE _reldir
OUTPUT_STRIP_TRAILING_WHITESPACE)
while(_findloc EQUAL -1 AND _elen GREATER 0)
get_filename_component(_nexedir ${_exedir} DIRECTORY)
string(FIND ${_basedir} ${_nexedir} _findloc)
if (_nexedir STREQUAL _exedir)
message(WARNING "Unable to get parent directory for ${_exedir}, using absolute python site arch folder ${pysitearch}")
set(_elen -1)
break()
else()
set(_exedir ${_nexedir})
endif()
string(LENGTH ${_exedir} _elen)
endwhile()
math(EXPR _elen "${_elen}+1")
string(SUBSTRING ${_basedir} ${_elen} -1 _reldir)
if(APPLE)
# on macOS, set install path to user's python package directory
# so that elevated privileges are not necessary
execute_process(
COMMAND "${pyexe}" -c "if True:
import sysconfig
print(sysconfig.get_path('platlib', 'posix_user'))"
OUTPUT_VARIABLE _reldir
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
set(${varname} ${_reldir} CACHE STRING "Destination sub-folder (relative) for the python ${pyver} modules")
message(STATUS "Will install to: ${_reldir}")
endif()
set(${varname} ${_reldir} CACHE STRING "Destination sub-folder (relative) for the python ${pyver} modules")
message(STATUS " -> Will install to: ${_reldir}")
endfunction()

set(PYIMATH_BOOST_PY_COMPONENT "python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}")
Expand Down Expand Up @@ -174,7 +179,7 @@ elseif(NOT _pyimath_have_perver_boost)
return()
else()
if(TARGET Boost::${PYIMATH_BOOST_PY_COMPONENT})
message(STATUS " -> Found Python ${Python_VERSION_MAJOR} boost: Boost::${PYIMATH_BOOST_PY_COMPONENT}")
message(STATUS "Found Python ${Python_VERSION_MAJOR} boost: Boost::${PYIMATH_BOOST_PY_COMPONENT}")
elseif(Boost_PYTHON_FOUND OR Boost_${PYIMATH_PY_UPPER}_FOUND)
message(WARNING "Found boost for python ${Python_VERSION_MAJOR}, but FindBoost did not create an import library. If you believe this is wrong, check the cmake documentation and see if you need to set Boost_ROOT or Boost_NO_BOOST_CMAKE")
return()
Expand Down
4 changes: 1 addition & 3 deletions src/python/config/PyImathSetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ include(GNUInstallDirs)
########################
## Target configuration

# TODO: Right now, we support compiling for multiple pythons at once
set(PYIMATH_OVERRIDE_PYTHON2_INSTALL_DIR "" CACHE STRING "Override the install location for any python 2.x modules compiled")
set(PYIMATH_OVERRIDE_PYTHON3_INSTALL_DIR "" CACHE STRING "Override the install location for any python 3.x modules compiled")
set(PYIMATH_OVERRIDE_PYTHON_INSTALL_DIR "" CACHE STRING "Override the install location for imath.so and imathnumpy.so modules")

########################
## Build related options
Expand Down

0 comments on commit 0b9f732

Please sign in to comment.