Skip to content

Commit

Permalink
Extend Python Package Helpers (#9)
Browse files Browse the repository at this point in the history
* Allow python packages located not at ${CMAKE_CURRENT_SOURCE_DIR}
* Add parameter `PACKAGE_SOURCE_DIRECTORY`
* Fix: Set `INSTALL_PREFIX` as `oneValueArg` instead of `multiValueArg`

Signed-off-by: Andreas Heinrich <[email protected]>
  • Loading branch information
andistorm authored Aug 8, 2024
1 parent 4805f09 commit dbc00d4
Showing 1 changed file with 62 additions and 13 deletions.
75 changes: 62 additions & 13 deletions python-package-helpers.cmake
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
function (ev_create_pip_install_dist_target)
set(oneValueArgs
PACKAGE_NAME
PACKAGE_SOURCE_DIRECTORY
)
set(multiValueArgs
DEPENDS
)
cmake_parse_arguments(
"EV_CREATE_PIP_INSTALL_DIST_TARGET"
""
"PACKAGE_NAME"
"DEPENDS"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if ("${EV_CREATE_PIP_INSTALL_DIST_TARGET_PACKAGE_NAME}" STREQUAL "")
message(FATAL_ERROR "PACKAGE_NAME is required")
endif()
if ("${EV_CREATE_PIP_INSTALL_DIST_TARGET_PACKAGE_SOURCE_DIRECTORY}" STREQUAL "")
set(EV_CREATE_PIP_INSTALL_DIST_TARGET_PACKAGE_SOURCE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif()

set(CHECK_DONE_FILE "${CMAKE_BINARY_DIR}/${EV_CREATE_PIP_INSTALL_DIST_TARGET_PACKAGE_NAME}_pip_install_dist_installed")
add_custom_command(
Expand All @@ -14,7 +27,7 @@ function (ev_create_pip_install_dist_target)
COMMENT
"Installing ${EV_CREATE_PIP_INSTALL_DIST_TARGET_PACKAGE_NAME} from distribution"
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
${EV_CREATE_PIP_INSTALL_DIST_TARGET_PACKAGE_SOURCE_DIRECTORY}
# Remove build dir from pip
COMMAND
${CMAKE_COMMAND} -E remove_directory build
Expand All @@ -33,13 +46,26 @@ function (ev_create_pip_install_dist_target)
endfunction()

function (ev_create_pip_install_local_target)
set(oneValueArgs
PACKAGE_NAME
PACKAGE_SOURCE_DIRECTORY
)
set(multiValueArgs
DEPENDS
)
cmake_parse_arguments(
"EV_CREATE_PIP_INSTALL_LOCAL_TARGET"
""
"PACKAGE_NAME"
"DEPENDS"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if ("${EV_CREATE_PIP_INSTALL_LOCAL_TARGET_PACKAGE_NAME}" STREQUAL "")
message(FATAL_ERROR "PACKAGE_NAME is required")
endif()
if ("${EV_CREATE_PIP_INSTALL_LOCAL_TARGET_PACKAGE_SOURCE_DIRECTORY}" STREQUAL "")
set(EV_CREATE_PIP_INSTALL_LOCAL_TARGET_PACKAGE_SOURCE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif()

add_custom_target(${EV_CREATE_PIP_INSTALL_LOCAL_TARGET_PACKAGE_NAME}_pip_install_local
# Remove build dir from pip
Expand All @@ -48,7 +74,7 @@ function (ev_create_pip_install_local_target)
COMMAND
${Python3_EXECUTABLE} -m pip install --force-reinstall -e .
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
${EV_CREATE_PIP_INSTALL_LOCAL_TARGET_PACKAGE_SOURCE_DIRECTORY}
DEPENDS
${EV_CREATE_PIP_INSTALL_LOCAL_TARGET_DEPENDS}
COMMENT
Expand All @@ -57,32 +83,55 @@ function (ev_create_pip_install_local_target)
endfunction()

function(ev_create_pip_install_targets)
set(oneValueArgs
PACKAGE_NAME
PACKAGE_SOURCE_DIRECTORY
)
set(multiValueArgs
DIST_DEPENDS
LOCAL_DEPENDS
)
cmake_parse_arguments(
"EV_CREATE_PIP_INSTALL_TARGETS"
""
"PACKAGE_NAME"
"DIST_DEPENDS;LOCAL_DEPENDS"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
ev_create_pip_install_dist_target(
PACKAGE_NAME ${EV_CREATE_PIP_INSTALL_TARGETS_PACKAGE_NAME}
PACKAGE_SOURCE_DIRECTORY ${EV_CREATE_PIP_INSTALL_TARGETS_PACKAGE_SOURCE_DIRECTORY}
DEPENDS ${EV_CREATE_PIP_INSTALL_TARGETS_DIST_DEPENDS}
)
ev_create_pip_install_local_target(
PACKAGE_NAME ${EV_CREATE_PIP_INSTALL_TARGETS_PACKAGE_NAME}
PACKAGE_SOURCE_DIRECTORY ${EV_CREATE_PIP_INSTALL_TARGETS_PACKAGE_SOURCE_DIRECTORY}
DEPENDS ${EV_CREATE_PIP_INSTALL_TARGETS_LOCAL_DEPENDS}
)
endfunction()

function (ev_create_python_wheel_targets)
set(oneValueArgs
PACKAGE_NAME
PACKAGE_SOURCE_DIRECTORY
INSTALL_PREFIX
)
set(multiValueArgs
DEPENDS
)
cmake_parse_arguments(
"EV_CREATE_PYTHON_WHEEL_TARGETS"
""
"PACKAGE_NAME"
"DEPENDS;INSTALL_PREFIX"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)

if ("${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME}" STREQUAL "")
message(FATAL_ERROR "PACKAGE_NAME is required")
endif()
if ("${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_SOURCE_DIRECTORY}" STREQUAL "")
set(EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_SOURCE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif()
if (NOT DEFINED ${EV_CREATE_PYTHON_WHEEL_TARGETS_INSTALL_PREFIX})
if ("${${PROJECT_NAME}_WHEEL_INSTALL_PREFIX}" STREQUAL "")
message(FATAL_ERROR
Expand All @@ -93,15 +142,15 @@ function (ev_create_python_wheel_targets)
endif()
endif()

set(WHEEL_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/dist)
set(WHEEL_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/dist_${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME})
add_custom_target(${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_NAME}_build_wheel
# Remove build dir from pip
COMMAND
${CMAKE_COMMAND} -E remove_directory build
COMMAND
${Python3_EXECUTABLE} -m build --wheel --outdir ${WHEEL_OUTDIR} .
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
${EV_CREATE_PYTHON_WHEEL_TARGETS_PACKAGE_SOURCE_DIRECTORY}
DEPENDS
${EV_CREATE_PYTHON_WHEEL_TARGETS_DEPENDS}
COMMENT
Expand Down

0 comments on commit dbc00d4

Please sign in to comment.