Skip to content

Commit

Permalink
Added updated scikit-build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrettDMorrison committed Dec 8, 2023
1 parent 8dcdc9b commit 730ffcf
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 60 deletions.
21 changes: 9 additions & 12 deletions python/scikit-build-cmake/FindCython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,22 @@

# Use the Cython executable that lives next to the Python executable
# if it is a local installation.
if(Python_EXECUTABLE)
get_filename_component(_python_path ${Python_EXECUTABLE} PATH)
elseif(Python3_EXECUTABLE)
get_filename_component(_python_path ${Python3_EXECUTABLE} PATH)
elseif(DEFINED PYTHON_EXECUTABLE)
get_filename_component(_python_path ${PYTHON_EXECUTABLE} PATH)
endif()

cmake_policy(SET CMP0148 OLD)

find_package(PythonInterp)
if(PYTHONINTERP_FOUND)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from os import path; import cython; print(path.dirname(cython.__file__))"
OUTPUT_VARIABLE CYTHON_PATH
ERROR_VARIABLE CYTHON_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE)

if(DEFINED _python_path)
find_program(CYTHON_EXECUTABLE
NAMES cython cython.bat cython3
HINTS ${CYTHON_PATH}
HINTS ${_python_path}
DOC "path to the cython executable")
else()
find_program(CYTHON_EXECUTABLE
NAMES cython cython.bat cython3
HINTS ${CYTHON_PATH}
DOC "path to the cython executable")
endif()

Expand Down
37 changes: 33 additions & 4 deletions python/scikit-build-cmake/FindNumPy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@
#
# ``NumPy_INCLUDE_DIR``
#

cmake_policy(SET CMP0148 OLD)
# .. note::
#
# To support NumPy < v0.15.0 where ``from-template`` and ``conv-template`` are not declared as entry points,
# the module emulates the behavior of standalone executables by setting the corresponding variables with the
# path the the python interpreter and the path to the associated script. For example:
# ::
#
# set(NumPy_CONV_TEMPLATE_EXECUTABLE /path/to/python /path/to/site-packages/numpy/distutils/conv_template.py CACHE STRING "Command executing conv-template program" FORCE)
#
# set(NumPy_FROM_TEMPLATE_EXECUTABLE /path/to/python /path/to/site-packages/numpy/distutils/from_template.py CACHE STRING "Command executing from-template program" FORCE)
#

if(NOT NumPy_FOUND)
set(_find_extra_args)
Expand All @@ -31,8 +40,6 @@ if(NOT NumPy_FOUND)
if(NumPy_FIND_QUIET)
list(APPEND _find_extra_args QUIET)
endif()
find_package(PythonInterp ${_find_extra_args})
find_package(PythonLibs ${_find_extra_args})

find_program(NumPy_CONV_TEMPLATE_EXECUTABLE NAMES conv-template)
find_program(NumPy_FROM_TEMPLATE_EXECUTABLE NAMES from-template)
Expand All @@ -50,6 +57,28 @@ if(NOT NumPy_FOUND)
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)

# XXX This is required to support NumPy < v0.15.0. See note in module documentation above.
if(NOT NumPy_CONV_TEMPLATE_EXECUTABLE)
execute_process(COMMAND "${PYTHON_EXECUTABLE}"
-c "from numpy.distutils import conv_template; print(conv_template.__file__)"
OUTPUT_VARIABLE _numpy_conv_template_file
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
set(NumPy_CONV_TEMPLATE_EXECUTABLE "${PYTHON_EXECUTABLE}" "${_numpy_conv_template_file}" CACHE STRING "Command executing conv-template program" FORCE)
endif()

# XXX This is required to support NumPy < v0.15.0. See note in module documentation above.
if(NOT NumPy_FROM_TEMPLATE_EXECUTABLE)
execute_process(COMMAND "${PYTHON_EXECUTABLE}"
-c "from numpy.distutils import from_template; print(from_template.__file__)"
OUTPUT_VARIABLE _numpy_from_template_file
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
set(NumPy_FROM_TEMPLATE_EXECUTABLE "${PYTHON_EXECUTABLE}" "${_numpy_from_template_file}" CACHE STRING "Command executing from-template program" FORCE)
endif()
endif()
endif()

Expand Down
51 changes: 37 additions & 14 deletions python/scikit-build-cmake/FindPythonExtensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@
#
# .. code-block:: cmake
#
# find_package(PythonInterp)
# find_package(PythonLibs)
# find_package(PythonExtensions)
# find_package(Cython)
# find_package(Boost COMPONENTS python)
Expand Down Expand Up @@ -244,14 +242,19 @@
# limitations under the License.
#=============================================================================

cmake_policy(SET CMP0148 OLD)

find_package(PythonInterp REQUIRED)
find_package(PythonLibs)
if(SKBUILD AND NOT PYTHON_LIBRARY)
set(PYTHON_LIBRARY "no-library-required")
find_package(PythonLibs)
unset(PYTHON_LIBRARY)
unset(PYTHON_LIBRARIES)
else()
find_package(PythonLibs)
endif()
include(targetLinkLibrariesWithDynamicLookup)

set(_command "
import sysconfig
import distutils.sysconfig
import itertools
import os
import os.path
Expand All @@ -263,7 +266,7 @@ rel_result = None
candidate_lists = []
try:
candidate_lists.append((sysconfig.get_paths()['purelib'],))
candidate_lists.append((distutils.sysconfig.get_python_lib(),))
except AttributeError: pass
try:
Expand All @@ -290,7 +293,7 @@ sys.stdout.write(\";\".join((
sys.prefix,
result,
rel_result,
sysconfig.get_config_var('EXT_SUFFIX')
distutils.sysconfig.get_config_var('EXT_SUFFIX')
)))
")

Expand Down Expand Up @@ -329,20 +332,38 @@ function(_set_python_extension_symbol_visibility _target)
else()
set(_modinit_prefix "init")
endif()
message("_modinit_prefix:${_modinit_prefix}")
if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
set_target_properties(${_target} PROPERTIES LINK_FLAGS
"/EXPORT:${_modinit_prefix}${_target}"
)
elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# Option to not run version script. See https://github.com/scikit-build/scikit-build/issues/668
if(NOT DEFINED SKBUILD_GNU_SKIP_LOCAL_SYMBOL_EXPORT_OVERRIDE)
set(SKBUILD_GNU_SKIP_LOCAL_SYMBOL_EXPORT_OVERRIDE FALSE)
endif()
set(_script_path
${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${_target}-version-script.map
)
file(WRITE ${_script_path}
"{global: ${_modinit_prefix}${_target}; local: *; };"
)
set_property(TARGET ${_target} APPEND_STRING PROPERTY LINK_FLAGS
# Export all symbols. See https://github.com/scikit-build/scikit-build/issues/668
if(SKBUILD_GNU_SKIP_LOCAL_SYMBOL_EXPORT_OVERRIDE)
file(WRITE ${_script_path}
"{global: ${_modinit_prefix}${_target};};"
)
else()
file(WRITE ${_script_path}
"{global: ${_modinit_prefix}${_target}; local: *;};"
)
endif()
if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
set_property(TARGET ${_target} APPEND_STRING PROPERTY LINK_FLAGS
" -Wl,--version-script=\"${_script_path}\""
)
)
else()
set_property(TARGET ${_target} APPEND_STRING PROPERTY LINK_FLAGS
" -Wl,-M \"${_script_path}\""
)
endif()
endif()
endfunction()

Expand Down Expand Up @@ -431,7 +452,7 @@ endfunction()

function(python_standalone_executable _target)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(${_target} ${PYTHON_LIBRARIES})
target_link_libraries(${_target} ${SKBUILD_LINK_LIBRARIES_KEYWORD} ${PYTHON_LIBRARIES})
endfunction()

function(python_modules_header _name)
Expand Down Expand Up @@ -572,3 +593,5 @@ function(python_modules_header _name)
endif()
set(${_include_dirs_var} ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)
endfunction()

include(UsePythonExtensions)
20 changes: 7 additions & 13 deletions python/scikit-build-cmake/UseCython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@
# limitations under the License.
#=============================================================================

cmake_policy(SET CMP0148 OLD)

# Configuration options.
set(CYTHON_ANNOTATE OFF
CACHE BOOL "Create an annotated .html file when compiling *.pyx.")
Expand All @@ -104,8 +102,6 @@ set(CYTHON_FLAGS "" CACHE STRING
"Extra flags to the cython compiler.")
mark_as_advanced(CYTHON_ANNOTATE CYTHON_FLAGS)

find_package(PythonLibs REQUIRED)

set(CYTHON_CXX_EXTENSION "cxx")
set(CYTHON_C_EXTENSION "c")

Expand Down Expand Up @@ -147,12 +143,6 @@ function(add_cython_target _name)
message(FATAL_ERROR "Either C or CXX must be enabled to use Cython")
endif()

if("${PYTHONLIBS_VERSION_STRING}" MATCHES "^2.")
set(_input_syntax "PY2")
else()
set(_input_syntax "PY3")
endif()

if(_args_EMBED_MAIN)
set(_embed_main TRUE)
endif()
Expand All @@ -165,6 +155,10 @@ function(add_cython_target _name)
set(_output_syntax "CXX")
endif()

# Doesn't select an input syntax - Cython
# defaults to 2 for Cython 2 and 3 for Cython 3
set(_input_syntax "default")

if(_args_PY2)
set(_input_syntax "PY2")
endif()
Expand Down Expand Up @@ -210,15 +204,15 @@ function(add_cython_target _name)
set(c_header_dependencies "")

# Get the include directories.
get_source_file_property(pyx_location ${_source_file} LOCATION)
get_filename_component(pyx_path ${pyx_location} PATH)
get_directory_property(cmake_include_directories
DIRECTORY ${pyx_path}
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
INCLUDE_DIRECTORIES)
list(APPEND cython_include_directories ${cmake_include_directories})

# Determine dependencies.
# Add the pxd file with the same basename as the given pyx file.
get_source_file_property(pyx_location ${_source_file} LOCATION)
get_filename_component(pyx_path ${pyx_location} PATH)
get_filename_component(pyx_file_basename ${_source_file} NAME_WE)
unset(corresponding_pxd_file CACHE)
find_file(corresponding_pxd_file ${pyx_file_basename}.pxd
Expand Down
Loading

0 comments on commit 730ffcf

Please sign in to comment.