# Linux Kernel Module Cheat

##### 38.13.3.1. gem5 worktree

`--gem5-build-id` goes a long way, but if you want to seamlessly switch between two gem5 tress without checking out multiple times, then `--gem5-worktree` is for you.

    # Build gem5 at the revision in the gem5 submodule.
    ./build-gem5

    # Create a branch at the same revision as the gem5 submodule.
    ./build-gem5 --gem5-worktree my-new-feature
    cd "$(./getvar --gem5-worktree my-new-feature)"
    vim create-bugs
    git add .
    git commit -m 'Created a bug'
    cd -
    ./build-gem5 --gem5-worktree my-new-feature

    # Run the submodule.
    ./run --emulator gem5 --run-id 0 &>/dev/null &

    # Run the branch the need to check out anything.
    # With --gem5-worktree, we can do both runs at the same time!
    ./run --emulator gem5 --gem5-worktree my-new-feature --run-id 1 &>/dev/null &

`--gem5-worktree <worktree-id>` automatically creates:

- a Git worktree of gem5 if one didn’t exit yet for `<worktree-id>`

- a separate build directory, exactly like `--gem5-build-id my-new-feature` would

We promise that the scripts sill never touch that worktree again once it has been created: it is now up to you to manage the code manually.

`--gem5-worktree` is required if you want to do multiple simultaneous runs of different gem5 versions, because each gem5 build needs to use the matching Python scripts inside the source tree.

The difference between `--gem5-build-id` and `--gem5-worktree` is that `--gem5-build-id` specifies only the gem5 build output directory, while `--gem5-worktree` specifies the source input directory.

Each Git worktree needs a branch name, and we append the `wt/` prefix to the `--gem5-worktree` value, where `wt` stands for `WorkTree`. This is done to allow us to checkout to a test `some-branch` branch under `submodules/gem5` and still use `--gem5-worktree some-branch`, without conflict for the worktree branch, which can only be checked out once.