colcon-defaults

0.2.9 · active · verified Fri Apr 17

colcon-defaults is an extension for the colcon build tool, primarily used in the ROS 2 ecosystem. It allows users to define default arguments and package sets for colcon commands (like `build`, `test`) in a YAML configuration file, such as `defaults.yaml`. This helps streamline complex build workflows and ensure consistent build parameters across projects or team members. The current stable version is 0.2.9, and it maintains a consistent release cadence aligned with colcon updates, focusing on stability and integration.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `colcon-defaults` by creating a `defaults.yaml` file that specifies build arguments and target packages. It then executes a `colcon build` command, explicitly passing the `--defaults-file` option. The `colcon` tool, with `colcon-defaults` installed, will automatically parse this file and apply the specified defaults to the build process.

# 1. Create a dummy colcon package (e.g., 'my_package/package.xml')
# This minimal package.xml makes 'my_package' discoverable by colcon
# <package format="3">
#   <name>my_package</name>
#   <version>0.0.0</version>
#   <description>A minimal dummy package</description>
#   <maintainer email="user@example.com">Your Name</maintainer>
#   <license>Apache-2.0</license>
#   <buildtool_depend>ament_cmake</buildtool_depend>
#   <buildtool_depend>ament_cmake_auto</buildtool_depend>
# </package>

# 2. Create a defaults.yaml file in your workspace root
# This file applies custom arguments and selects packages for 'build' command.
with open('defaults.yaml', 'w') as f:
    f.write('''
build:
  args: [ --cmake-args -DCMAKE_BUILD_TYPE=Release ]
  packages: [ my_package ]
''')

# 3. Create a dummy package directory and package.xml for 'my_package'
import os
os.makedirs('my_package', exist_ok=True)
with open('my_package/package.xml', 'w') as f:
    f.write('''
<package format="3">
  <name>my_package</name>
  <version>0.0.0</version>
  <description>A minimal dummy package</description>
  <maintainer email="user@example.com">Your Name</maintainer>
  <license>Apache-2.0</license>
  <buildtool_depend>ament_cmake</buildtool_depend>
  <buildtool_depend>ament_cmake_auto</buildtool_depend>
</package>
''')

# 4. Run colcon build with the defaults file
# This command will build 'my_package' with CMAKE_BUILD_TYPE=Release
# If colcon-defaults is installed, this will apply the defaults.
import subprocess
try:
    print("\n--- Running colcon build with defaults.yaml ---")
    subprocess.run(['colcon', 'build', '--defaults-file', 'defaults.yaml'], check=True)
    print("\n--- colcon build completed. Check build arguments in logs. ---")
except FileNotFoundError:
    print("Error: 'colcon' command not found. Please ensure colcon-core is installed and in your PATH.")
except subprocess.CalledProcessError as e:
    print(f"Error during colcon build: {e}")

# Cleanup (optional)
os.remove('defaults.yaml')
os.remove('my_package/package.xml')
os.rmdir('my_package')

view raw JSON →