Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ament_cmake_python_install() handle symlink installs. #325

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
155 changes: 109 additions & 46 deletions ament_cmake_python/cmake/ament_python_install_package.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
macro(ament_python_install_package)
_ament_cmake_python_register_environment_hook()
_ament_cmake_python_install_package(${ARGN})
_ament_cmake_python_install_package_hook()
endmacro()

function(_ament_cmake_python_install_package package_name)
Expand Down Expand Up @@ -71,7 +72,6 @@ function(_ament_cmake_python_install_package package_name)
endif()

set(build_dir "${CMAKE_CURRENT_BINARY_DIR}/ament_cmake_python/${package_name}")
file(RELATIVE_PATH source_dir "${build_dir}" "${ARG_PACKAGE_DIR}")

if(ARG_NO_DATA)
string(CONFIGURE "\
Expand All @@ -83,9 +83,7 @@ setup(
name='${package_name}',
version='${ARG_VERSION}',
packages=find_packages(
where=os.path.normpath('${source_dir}/..'),
include=('${package_name}', '${package_name}.*')),
package_dir={'${package_name}': '${source_dir}'},
)
" setup_py_content)
else()
Expand All @@ -100,11 +98,8 @@ setup(
name='${package_name}',
version='${ARG_VERSION}',
packages=find_packages(
where=os.path.normpath('${source_dir}/..'),
include=('${package_name}', '${package_name}.*')),
package_dir={'${package_name}': '${source_dir}'},
package_data=find_packages_data(
where=os.path.normpath('${source_dir}/..'),
include=('${package_name}', '${package_name}.*'))
)
" setup_py_content)
Expand All @@ -116,51 +111,107 @@ setup(
)

if(ARG_SETUP_CFG)
add_custom_command(
OUTPUT "${build_dir}/setup.cfg"
COMMAND ${CMAKE_COMMAND} -E copy "${ARG_SETUP_CFG}" "${build_dir}/setup.cfg"
MAIN_DEPENDENCY "${ARG_SETUP_CFG}"
)
add_custom_target(${package_name}_setup ALL
DEPENDS "${build_dir}/setup.cfg"
add_custom_target(
ament_cmake_python_symlink_${package_name}_setup ALL
COMMAND ${CMAKE_COMMAND} -E create_symlink
"${ARG_SETUP_CFG}" "${build_dir}/setup.cfg"
)
endif()

if(NOT ARG_SKIP_COMPILE)
set(extra_install_args --compile)
add_custom_target(
ament_cmake_python_symlink_${package_name} ALL
COMMAND ${CMAKE_COMMAND} -E create_symlink
"${ARG_PACKAGE_DIR}" "${build_dir}/${package_name}"
)

set(install_dir "${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_DIR}")

if(NOT AMENT_CMAKE_SYMLINK_INSTALL)
# Install as flat Python egg to mimic https://github.com/colcon/colcon-core
# handling of pure Python packages for non-symlink installs.
if(NOT ARG_SKIP_COMPILE)
set(extra_install_args --compile)
else()
set(extra_install_args --no-compile)
endif()

# NOTE(hidmic): Allow setup.py install to build, as there is no way to
# determine the Python package's source dependencies for proper build
# invalidation.
install(CODE
"set(install_dir ${install_dir})
set(extra_install_args ${extra_install_args})
if(DEFINED ENV{DESTDIR} AND NOT \"\$ENV{DESTDIR}\" STREQUAL \"\")
list(APPEND extra_install_args --root \$ENV{DESTDIR})
file(TO_CMAKE_PATH \"\$ENV{DESTDIR}/\${install_dir}\" install_dir)
endif()
message(STATUS
\"Installing: ${package_name} as flat Python egg under \${install_dir}\")
file(TO_NATIVE_PATH \"${CMAKE_INSTALL_PREFIX}\" install_prefix)
file(TO_NATIVE_PATH \"${CMAKE_INSTALL_PREFIX}/bin\" install_scripts_dir)
execute_process(
COMMAND
\"${PYTHON_EXECUTABLE}\" setup.py install
--single-version-externally-managed
--install-scripts \${install_scripts_dir}
--prefix \${install_prefix}
--record install.log
\${extra_install_args}
WORKING_DIRECTORY \"${build_dir}\"
OUTPUT_QUIET
)"
)
else()
set(extra_install_args --no-compile)
endif()
# NOTE(hidmic): Make sure build directory makes it into the PYTHONPATH.
# See https://github.com/colcon/colcon-core/pull/163 for further reference.
file(TO_NATIVE_PATH "${build_dir}" build_prefix)

if(NOT "${package_name}" STREQUAL "${PROJECT_NAME}")
set(hook_name "${package_name}_python_develop")
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
else()
set(hook_name "python_develop")
endif()

# Install as flat Python .egg to mimic https://github.com/colcon/colcon-core
# handling of pure Python packages.

# NOTE(hidmic): Allow setup.py install to build, as there is no way to
# determine the Python package's source dependencies for proper build
# invalidation.
install(CODE
"set(extra_install_args ${extra_install_args})
set(install_dir \"${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_DIR}\")
if(DEFINED ENV{DESTDIR} AND NOT \"\$ENV{DESTDIR}\" STREQUAL \"\")
list(APPEND extra_install_args --root \$ENV{DESTDIR})
file(TO_CMAKE_PATH \"\$ENV{DESTDIR}/\${install_dir}\" install_dir)
endif()
message(STATUS
\"Installing: ${package_name} as flat Python egg under \${install_dir}\")
file(TO_NATIVE_PATH \"${CMAKE_INSTALL_PREFIX}\" install_prefix)
file(TO_NATIVE_PATH \"${CMAKE_INSTALL_PREFIX}/bin\" scripts_install_dir)
execute_process(
COMMAND
\"${PYTHON_EXECUTABLE}\" setup.py install
--single-version-externally-managed
--install-scripts \${scripts_install_dir}
--prefix \${install_prefix}
--record install.log
\${extra_install_args}
WORKING_DIRECTORY \"${build_dir}\"
OUTPUT_QUIET
)"
)
if(WIN32)
set(_ext "bat")
else()
set(_ext "sh")
endif()

configure_file(
"${ament_cmake_python_DIR}/environment_hooks/${hook_name}.${_ext}.in"
"${CMAKE_CURRENT_BINARY_DIR}/ament_cmake_python/environment_hooks/${hook_name}.${_ext}"
@ONLY
)
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved

# register information for .dsv generation
set(_AMENT_CMAKE_PYTHON_INSTALL_HOOK_DESC
"prepend-non-duplicate;PYTHONPATH;${build_prefix}"
PARENT_SCOPE)

set(_AMENT_CMAKE_PYTHON_INSTALL_HOOK
"${CMAKE_CURRENT_BINARY_DIR}/ament_cmake_python/environment_hooks/${hook_name}.${_ext}"
PARENT_SCOPE)

# Install as Python egg link to mimic https://github.com/colcon/colcon-core
# handling of pure Python packages for symlink installs.
install(CODE
"message(STATUS
\"Symlinking: ${package_name} as flat Python egg under ${install_dir}\")
file(TO_NATIVE_PATH \"${CMAKE_INSTALL_PREFIX}\" install_prefix)
file(TO_NATIVE_PATH \"${CMAKE_INSTALL_PREFIX}/bin\" install_scripts_dir)
execute_process(
COMMAND
\"${PYTHON_EXECUTABLE}\" setup.py develop
--prefix \${install_prefix}
--script-dir \${install_scripts_dir}
--editable --no-deps
--build-directory build
WORKING_DIRECTORY \"${build_dir}\"
OUTPUT_QUIET
)"
)
endif()

if(package_name IN_LIST AMENT_CMAKE_PYTHON_INSTALL_INSTALLED_NAMES)
message(FATAL_ERROR
Expand All @@ -171,3 +222,15 @@ setup(
set(AMENT_CMAKE_PYTHON_INSTALL_INSTALLED_NAMES
"${AMENT_CMAKE_PYTHON_INSTALL_INSTALLED_NAMES}" PARENT_SCOPE)
endfunction()

macro(_ament_cmake_python_install_package_hook)
if(_AMENT_CMAKE_PYTHON_INSTALL_HOOK)
get_filename_component(hook_name "${_AMENT_CMAKE_PYTHON_INSTALL_HOOK}" NAME_WE)
set(AMENT_CMAKE_ENVIRONMENT_HOOKS_DESC_${hook_name}
"${_AMENT_CMAKE_PYTHON_INSTALL_HOOK_DESC}")
ament_environment_hooks("${_AMENT_CMAKE_PYTHON_INSTALL_HOOK}")

unset(_AMENT_CMAKE_PYTHON_INSTALL_HOOK_DESC)
unset(_AMENT_CMAKE_PYTHON_INSTALL_HOOK)
endif()
endmacro()
39 changes: 39 additions & 0 deletions ament_cmake_python/cmake/environment_hooks/python_develop.bat.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
:: configured from ament_cmake_python/cmake/environment_hooks/python_develop.bat.in

@echo off

call:ament_prepend_unique_value PYTHONPATH @build_prefix@

goto:eof


:: Prepend non-duplicate values to environment variables
:: using semicolons as separators and avoiding trailing separators.
:: first argument: the name of the result variable
:: second argument: the value
:ament_prepend_unique_value
setlocal enabledelayedexpansion
:: arguments
set "listname=%~1"
set "value=%~2"
:: expand the list variable
set "list=!%listname%!"
:: check if the list contains the value
set "is_duplicate="
if "%list%" NEQ "" (
for %%v in ("%list:;=";"%") do (
if "%%~v" == "%value%" set "is_duplicate=1"
)
)
:: if it is not a duplicate prepend it
if "%is_duplicate%" == "" (
:: if not empty, prepend a semi-colon
if "!list!" NEQ "" set "list=;!list!"
:: prepend the value
set "list=%value%!list!"
)
endlocal & (
:: set result variable in parent scope
set "%~1=%list%"
)
goto:eof
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# configured from ament_cmake_python/cmake/environment_hooks/python_develop.sh.in

ament_prepend_unique_value PYTHONPATH @build_prefix@