colcon / colcon-ros

Extension for colcon to support ROS packages
http://colcon.readthedocs.io
Apache License 2.0
13 stars 26 forks source link

ament_python packages building C extensions need path to source directory #91

Closed sloretz closed 4 years ago

sloretz commented 4 years ago

This is a feature request to have some way for ament_python packages to reference the source directory when building CPython extensions.

import os
from setuptools import setup, Extension

ext_modules = [
    Extension(
        'ape_needs_src',
        [
            'src/asdf.cpp',
        ],
        language='c++'
    ),
]

setup(
    name='destructible_capsule',
    version='0.8.3',
    author='Shane Loretz',
    author_email='sloretz@openrobotics.org',
    ext_modules=ext_modules,
    zip_safe=False,
)

When built with colcon, the build fails complaining the file does not exist.

$ colcon build --packages-select ape_needs_src
Starting >>> ape_needs_src
[1.754s] WARNING:colcon.colcon_ros.task.ament_python.build:Package 'ape_needs_src' doesn't explicitly install the 'package.xml' file (colcon-ros currently does it implicitly but that fallback will be removed in the future)
--- stderr: ape_needs_src                   
x86_64-linux-gnu-gcc: error: src/asdf.cpp: No such file or directory
x86_64-linux-gnu-gcc: fatal error: no input files
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
---
Failed   <<< ape_needs_src  [ Exited with code 1 ]

I am able to build this package using python3 setup.py build. I tried replacing 'src/asdf.cpp' with a full path relative to the setup.py.

            os.path.abspath(os.path.join(os.path.dirname(__file__), 'src/asdf.cpp')),

However, when I build with colcon the error indicates the setup.py file is located in the build directory.

x86_64-linux-gnu-gcc: error: /home/sloretz/ws/ros2/build/ape_needs_src/src/asdf.cpp: No such file or directory

The file src/asdf.cpp needs to be found elsewhere, but I have no way of specifying that.

/home/sloretz/ws/ros2/src/ros2/rclpy/ape_needs_src/src/asdf.cpp
dirk-thomas commented 4 years ago

colcon passes a lot of arguments to the setup.py file in order to do an out-of-source build: https://github.com/colcon/colcon-core/blob/e0b6abdb8d6f5a14b041b3ee8b6153d559524d43/colcon_core/task/python/build.py#L63-L74

So the question is how to achieve the same with a Python package containing a C extension?

dirk-thomas commented 4 years ago

If Python doesn't provide a way to do that we could implement something similar as for the develop command (which doesn't support the out-of-source-build either): https://github.com/colcon/colcon-core/blob/e0b6abdb8d6f5a14b041b3ee8b6153d559524d43/colcon_core/task/python/build.py#L82

Basically symlink all required files from source into the build directory.

dirk-thomas commented 4 years ago

So the question is how to achieve the same with a Python package containing a C extension?

@sloretz Friendly ping.

dirk-thomas commented 4 years ago

@sloretz Friendly ping.

dirk-thomas commented 4 years ago

Without feedback how to achieve this for a vanilla Python package I am going to close this. Please feel free to comment with the requested information and the ticket can be reopened.