Skip to content

Commit

Permalink
Simplify since FindPython3 can be found multiple times
Browse files Browse the repository at this point in the history
Signed-off-by: Shane Loretz <[email protected]>
  • Loading branch information
sloretz authored and clalancette committed Nov 3, 2023
1 parent 536c24c commit e29e74f
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions cmake/Modules/FindPythonExtra.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,10 @@
# find_package(PythonExtra MODULE)
# # use PythonExtra::Interpreter
#
# Note on Python3
# This module requires that `find_package(Python3 REQUIRED COMPONENTS Interpreter)` be called,
# and will call it if not present.
# If more components from FindPython3.cmake are needed, then find it manually before finding
# this module.
# Example:
#
# find_package(python_cmake_module REQUIRED)
# find_package(Python3 REQUIRED COMPONENTS Interpreter Development NumPy)
# find_package(PythonExtra MODULE)
# # use PythonExtra::Interpreter
# Note on FindPython3
# This module will `find_package(Python3 REQUIRED COMPONENTS Interpreter)`
# If more components from FindPython3.cmake are needed, then find them manually before
# or after finding this module.
#
###############################################################################

Expand All @@ -55,47 +48,45 @@ if(PythonExtra_FOUND)
return()
endif()

if(Python3_FOUND AND NOT TARGET Python3::Interpreter)
message(FATAL_ERROR "PythonExtra requires Python3 to be found with the Interpreter component")
elseif(NOT Python3_FOUND)
find_package(Python3 REQUIRED COMPONENTS Interpreter)
endif()
find_package(Python3 REQUIRED COMPONENTS Interpreter)

get_target_property(PythonExtra_EXECUTABLE Python3::Interpreter IMPORTED_LOCATION)
get_target_property(PythonExtra_EXECUTABLE Python3::Interpreter LOCATION)

add_executable(PythonExtra::Interpreter IMPORTED)
set_property(TARGET PythonExtra::Interpreter
PROPERTY IMPORTED_LOCATION "${PythonExtra_EXECUTABLE}")

# Set the location to the debug interpretter on Windows if it exists
# Set the location to the debug interpreter on Windows if it exists
if(WIN32)
get_target_property(PythonExtra_EXECUTABLE PythonExtra::Interpreter IMPORTED_LOCATION)
get_target_property(PythonExtra_EXECUTABLE PythonExtra::Interpreter LOCATION)
get_filename_component(_python_executable_dir "${PythonExtra_EXECUTABLE}" DIRECTORY)
get_filename_component(_python_executable_name "${PythonExtra_EXECUTABLE}" NAME_WE)
get_filename_component(_python_executable_ext "${PythonExtra_EXECUTABLE}" EXT)
set(PythonExtra_EXECUTABLE_DEBUG "${_python_executable_dir}/${_python_executable_name}_d${_python_executable_ext}")
if(EXISTS "${PythonExtra_EXECUTABLE_DEBUG}")
set_target_property(PythonExtra::Interpreter IMPORTED_LOCATION_Debug "${PythonExtra_EXECUTABLE_DEBUG}")
set(PythonExtra_EXECUTABLE_DEBUG "${PythonExtra_EXECUTABLE_DEBUG}")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
message(FATAL_ERROR "${PythonExtra_EXECUTABLE_DEBUG} doesn't exist and Debug build required")
message(WARNING "${PythonExtra_EXECUTABLE_DEBUG} doesn't exist but a Windows Debug build requires it")
unset(PythonExtra_EXECUTABLE_DEBUG)
endif()
unset(_python_executable_dir)
unset(_python_executable_name)
unset(_python_executable_ext)
endif()

include(FindPackageHandleStandardArgs)
set(_required_vars
PythonExtra_EXECUTABLE)
if(WIN32 AND "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
list(APPEND _required_vars PythonExra_EXECUTABLE_DEBUG)
endif()
# Use PythonExtra::Interpreter instead of these variables
# Downstream users should use PythonExtra::Interpreter instead of these variables
mark_as_advanced(
PythonExtra_EXECUTABLE
PythonExtra_EXECUTABLE_DEBUG)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PythonExtra
REQUIRED_VARS ${_required_vars}
)

unset(_required_vars)

0 comments on commit e29e74f

Please sign in to comment.