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
Changes from all 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
153 changes: 106 additions & 47 deletions ament_cmake_python/cmake/ament_python_install_package.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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 +82,10 @@ 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}'},
data_files=[] # for setup.py install_data to be a no-op
# if no data_files were specified in the
# setup.cfg
)
" setup_py_content)
else()
Expand All @@ -100,12 +100,12 @@ 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}.*'))
include=('${package_name}', '${package_name}.*')),
data_files=[] # for setup.py install_data to be a no-op
# if no data_files were specified in the
# setup.cfg
)
" setup_py_content)
endif()
Expand All @@ -116,52 +116,111 @@ 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_prefix "${CMAKE_INSTALL_PREFIX}")
set(scripts_dir "${install_prefix}/bin")
set(install_dir "${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 \"${install_prefix}\" install_prefix)
file(TO_NATIVE_PATH \"${scripts_dir}\" scripts_dir)
execute_process(
COMMAND
\"${PYTHON_EXECUTABLE}\" setup.py install
--single-version-externally-managed
--install-scripts \${scripts_dir}
--prefix \${install_prefix}
--record install_manifest.txt
\${extra_install_args}
WORKING_DIRECTORY \"${build_dir}\"
RESULT_VARIABLE result
OUTPUT_QUIET
)
if(NOT result EQUAL \"0\")
message(FATAL_ERROR \"Failed to install ${package_name} flat Python egg\")
endif()"
)
else()
set(extra_install_args --no-compile)
# Install 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}\")
# NOTE(hidmic): recent setuptools versions expect this directory to exist
file(MAKE_DIRECTORY \"${install_dir}\")
file(TO_NATIVE_PATH \"${install_dir}\" install_dir)
file(TO_NATIVE_PATH \"${scripts_dir}\" scripts_dir)
file(TO_NATIVE_PATH \"${install_prefix}\" install_prefix)
execute_process(
COMMAND
\"${PYTHON_EXECUTABLE}\" setup.py
develop
--install-dir \${install_dir}
--script-dir \${scripts_dir}
--editable --no-deps
--build-directory build
install_data
--install-dir \${install_prefix}
WORKING_DIRECTORY \"${build_dir}\"
RESULT_VARIABLE result
OUTPUT_QUIET
)
if(NOT result EQUAL \"0\")
message(FATAL_ERROR \"Failed to build ${package_name} \"
\"flat Python egg for development\")
endif()"
)
# Ensure package can be found in and imported from the install space
ament_cmake_symlink_install_directory(
DIRECTORY "${ARG_PACKAGE_DIR}/"
DESTINATION "${PYTHON_INSTALL_DIR}/${package_name}"
PATTERN "*.pyc" EXCLUDE
PATTERN "__pycache__" EXCLUDE
)
if(NOT ARG_SKIP_COMPILE)
# Compile Python files
install(CODE
"execute_process(
COMMAND
\"${PYTHON_EXECUTABLE}\" -m compileall
\"${install_dir}/${package_name}\"
)"
)
endif()
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(package_name IN_LIST AMENT_CMAKE_PYTHON_INSTALL_INSTALLED_NAMES)
message(FATAL_ERROR
"ament_python_install_package() a Python module file or package with "
Expand Down