colcon-library-path

0.2.1 · maintenance · verified Fri Apr 17

colcon-library-path is an extension for the colcon build system (commonly used in ROS 2 projects) that automatically adds the build and install directories of built libraries to the COLCON_LIBRARY_PATH environment variable. This helps downstream packages locate dependencies. It is currently at version 0.2.1 and appears to be in a maintenance phase, with a stable feature set and infrequent updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the effect of `colcon-library-path` by programmatically (via `subprocess`) running a `colcon build` command with the `--add-build-paths` option. It then simulates sourcing the generated `setup.bash` file and prints the `COLCON_LIBRARY_PATH` environment variable, showing how the extension contributes library paths. Note that `colcon-library-path` is primarily a CLI extension for `colcon` and is not typically used for direct programmatic Python imports.

import subprocess
import os
import shutil

# --- Setup: Create a dummy colcon workspace and package ---
# This part simulates typical shell commands for setting up a colcon project.
workspace_path = "my_colcon_workspace"
os.makedirs(f"{workspace_path}/src/my_library", exist_ok=True)
with open(f"{workspace_path}/src/my_library/CMakeLists.txt", "w") as f:
    f.write("project(my_library NONE)\n")
    f.write("add_library(my_library SHARED my_library.cpp)\n")
with open(f"{workspace_path}/src/my_library/my_library.cpp", "w") as f:
    f.write("// dummy C++ file\nint main() { return 0; }")

print(f"Created dummy package in {workspace_path}/src/my_library")

# --- Step 1: Run colcon build with --add-build-paths ---
# This activates the colcon-library-path extension.
try:
    print("\nRunning colcon build with --add-build-paths...")
    # Ensure colcon is in PATH, otherwise specify full path to colcon executable
    result = subprocess.run(
        ['colcon', 'build', '--cmake-args', '-DBUILD_SHARED_LIBS=ON',
         '--packages-select', 'my_library', '--add-build-paths'],
        cwd=workspace_path,
        check=True,
        capture_output=True,
        text=True
    )
    print("colcon build stdout:\n" + result.stdout)
    if result.stderr:
        print("colcon build stderr:\n" + result.stderr)

except FileNotFoundError:
    print("Error: 'colcon' command not found. Please install colcon (pip install colcon-common-extensions).")
    # Cleanup before exiting
    shutil.rmtree(workspace_path)
    exit(1)
except subprocess.CalledProcessError as e:
    print(f"Error during colcon build:\n{e.stderr}")
    # Cleanup before exiting
    shutil.rmtree(workspace_path)
    exit(1)

# --- Step 2: Source the generated setup file and check COLCON_LIBRARY_PATH ---
# This demonstrates the effect of colcon-library-path on the environment.
setup_file_path = os.path.join(workspace_path, "install", "setup.bash")
if os.path.exists(setup_file_path):
    print(f"\nSourcing {setup_file_path} and checking COLCON_LIBRARY_PATH...")
    # Simulate sourcing in a new shell to get the environment variable
    command = f"source {setup_file_path} && echo $COLCON_LIBRARY_PATH"
    result_env = subprocess.run(
        ['bash', '-c', command],
        check=True,
        capture_output=True,
        text=True
    )
    colcon_lib_path = result_env.stdout.strip()
    print(f"COLCON_LIBRARY_PATH after setup: {colcon_lib_path}")
    if f"{workspace_path}/install/my_library/lib" in colcon_lib_path or \
       f"{workspace_path}/build/my_library" in colcon_lib_path:
        print("\u2705 colcon-library-path successfully contributed to COLCON_LIBRARY_PATH.")
    else:
        print("\u274c COLCON_LIBRARY_PATH does not contain expected library paths.")
else:
    print(f"\u274c Error: {setup_file_path} not found. Build might have failed or not produced a setup file.")

# --- Cleanup ---
print(f"\nCleaning up workspace: {workspace_path}")
shutil.rmtree(workspace_path)

view raw JSON →