Robotics - ROS2 Colcon Mixins

Group colcon build arguments under a single name

Posted by Rico's Nerd Cluster on November 30, 2024

What is a Mixin?

A mixin is a reusable piece of functionality that you can “mix into” something else.

In Python, a class mixin adds behavior to a class:

1
2
3
4
5
6
class LoggerMixin:
    def log(self, msg):
        print(msg)

class MyClass(LoggerMixin):
    pass

Similarly, a colcon mixin adds a named group of arguments to a colcon command:

  • Class mixin → adds behavior to code
  • Colcon mixin → adds options to a command

Colcon Mixins allow you to apply a group of arguments under one name. For example:

1
colcon build --mixin debug clang

This might expand to flags such as:

1
--cmake-args -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++

Note: Mixins are applied in the order listed. If two mixins set the same option, the later one overrides the earlier one.

Installation

1
sudo apt install python3-colcon-mixin

How To Use Mixins

1. Define YAML Files

File structure:

1
2
3
4
my_mixins/
├── index.yaml
├── debug.yaml
└── clang.yaml

index.yaml — maps mixin names to their files:

1
2
3
build:
  debug: debug.yaml
  clang: clang.yaml

debug.yaml:

1
2
3
arguments:
    cmake-args:
        - -DCMAKE_BUILD_TYPE=Debug

clang.yaml:

1
2
3
4
arguments:
    cmake-args:
        - -DCMAKE_C_COMPILER=clang
        - -DCMAKE_CXX_COMPILER=clang++

2. Register the Mixins

1
2
colcon mixin add my_mixins /path/to/my_mixins/index.yaml
colcon mixin update

3. Verify

1
colcon mixin show

4. Use

1
colcon build --mixin debug clang

Useful Commands

Remove build artifacts for specific packages:

1
2
3
4
5
# Remove a specific package
colcon clean packages -y --packages-select <package_name>

# Remove a package and all packages that depend on it
colcon clean packages -y --packages-up-to <package_name>

The --packages-up-to flag also works with mixins.