117 lines
4.0 KiB
CMake
117 lines
4.0 KiB
CMake
cmake_minimum_required(VERSION 3.8)
|
||
project(robot_speaker)
|
||
|
||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||
add_compile_options(-Wall -Wextra -Wpedantic)
|
||
endif()
|
||
|
||
find_package(ament_cmake REQUIRED)
|
||
find_package(ament_cmake_python REQUIRED)
|
||
find_package(interfaces REQUIRED)
|
||
|
||
# 确保使用系统 Python(而不是 conda/miniconda 的 Python)
|
||
find_program(PYTHON3_CMD python3 PATHS /usr/bin /usr/local/bin NO_DEFAULT_PATH)
|
||
if(NOT PYTHON3_CMD)
|
||
find_program(PYTHON3_CMD python3)
|
||
endif()
|
||
if(PYTHON3_CMD)
|
||
set(Python3_EXECUTABLE ${PYTHON3_CMD} CACHE FILEPATH "Python 3 executable" FORCE)
|
||
set(PYTHON_EXECUTABLE ${PYTHON3_CMD} CACHE FILEPATH "Python executable" FORCE)
|
||
endif()
|
||
|
||
install(CODE "
|
||
execute_process(
|
||
COMMAND ${PYTHON3_CMD} -m pip install --prefix=${CMAKE_INSTALL_PREFIX} --no-deps ${CMAKE_CURRENT_SOURCE_DIR}
|
||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||
RESULT_VARIABLE install_result
|
||
OUTPUT_VARIABLE install_output
|
||
ERROR_VARIABLE install_error
|
||
)
|
||
if(NOT install_result EQUAL 0)
|
||
message(FATAL_ERROR \"Failed to install Python package. Output: ${install_output} Error: ${install_error}\")
|
||
endif()
|
||
|
||
execute_process(
|
||
COMMAND ${PYTHON3_CMD} -c \"
|
||
import os
|
||
import shutil
|
||
import glob
|
||
import sysconfig
|
||
|
||
install_prefix = '${CMAKE_INSTALL_PREFIX}'
|
||
build_dir = '${CMAKE_CURRENT_BINARY_DIR}'
|
||
python_version = f'{sysconfig.get_python_version()}'
|
||
|
||
# ROS2 期望的 Python 包位置
|
||
ros2_site_packages = os.path.join(install_prefix, 'lib', f'python{python_version}', 'site-packages')
|
||
os.makedirs(ros2_site_packages, exist_ok=True)
|
||
|
||
# pip install --prefix 可能将包安装到不同位置(系统环境通常是 local/lib/pythonX/dist-packages)
|
||
pip_locations = [
|
||
os.path.join(install_prefix, 'local', 'lib', f'python{python_version}', 'dist-packages'),
|
||
os.path.join(install_prefix, 'lib', f'python{python_version}', 'site-packages'),
|
||
os.path.join(install_prefix, 'local', 'lib', f'python{python_version}', 'site-packages'),
|
||
]
|
||
|
||
# 查找并复制 robot_speaker 包到 ROS2 期望的位置
|
||
robot_speaker_src = None
|
||
for location in pip_locations:
|
||
candidate = os.path.join(location, 'robot_speaker')
|
||
if os.path.exists(candidate) and os.path.isdir(candidate):
|
||
robot_speaker_src = candidate
|
||
break
|
||
|
||
if robot_speaker_src:
|
||
robot_speaker_dest = os.path.join(ros2_site_packages, 'robot_speaker')
|
||
if os.path.exists(robot_speaker_dest):
|
||
shutil.rmtree(robot_speaker_dest)
|
||
if robot_speaker_src != robot_speaker_dest:
|
||
shutil.copytree(robot_speaker_src, robot_speaker_dest)
|
||
print(f'Copied robot_speaker from {robot_speaker_src} to {ros2_site_packages}')
|
||
else:
|
||
print(f'robot_speaker already in correct location')
|
||
|
||
# 处理 entry_points 脚本
|
||
lib_dir = os.path.join(install_prefix, 'lib', 'robot_speaker')
|
||
os.makedirs(lib_dir, exist_ok=True)
|
||
|
||
# 脚本可能在 local/bin 或 bin 中
|
||
for bin_dir in [os.path.join(install_prefix, 'local', 'bin'), os.path.join(install_prefix, 'bin')]:
|
||
if os.path.exists(bin_dir):
|
||
scripts = glob.glob(os.path.join(bin_dir, '*_node'))
|
||
for script in scripts:
|
||
script_name = os.path.basename(script)
|
||
dest = os.path.join(lib_dir, script_name)
|
||
if script != dest:
|
||
shutil.copy2(script, dest)
|
||
os.chmod(dest, 0o755)
|
||
print(f'Copied {script_name} to {lib_dir}')
|
||
\"
|
||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||
RESULT_VARIABLE python_result
|
||
OUTPUT_VARIABLE python_output
|
||
)
|
||
if(python_result EQUAL 0)
|
||
message(STATUS \"${python_output}\")
|
||
else()
|
||
message(WARNING \"Failed to setup Python package: ${python_output}\")
|
||
endif()
|
||
")
|
||
|
||
install(DIRECTORY launch/
|
||
DESTINATION share/${PROJECT_NAME}/launch
|
||
FILES_MATCHING PATTERN "*.launch.py"
|
||
)
|
||
|
||
install(DIRECTORY config/
|
||
DESTINATION share/${PROJECT_NAME}/config
|
||
FILES_MATCHING PATTERN "*.yaml" PATTERN "*.json"
|
||
)
|
||
|
||
if(BUILD_TESTING)
|
||
find_package(ament_lint_auto REQUIRED)
|
||
ament_lint_auto_find_test_dependencies()
|
||
endif()
|
||
|
||
ament_package()
|