# Direct CMake

The steps below add heliaDSP to a CMake project with `add_subdirectory()`, build and run a 256-point real FFT on the host, and build the same library for Cortex-M55. The last section lists the configure-time checks and the messages they print.

Placeholders on this page:

- `<commit>`: the qualified CMSIS-DSP commit, `commit` in the `[cmsis_dsp_dependency]` table of [`PROVENANCE.toml`](https://github.com/AmbiqAI/helia-dsp/blob/4a7b7f054eafd96ae50c12050ad3ef3ec1ebaea1/PROVENANCE.toml).
- `<CMSIS_6>`: a checkout of [ARM-software/CMSIS_6](https://github.com/ARM-software/CMSIS_6); qualification uses tag v6.2.0.

CMSIS-DSP is Arm's C library of signal-processing functions for Cortex-M (`arm_*` functions, declared in `arm_math.h`). The CMake target that builds it, `CMSISDSP`, is called the provider. heliaDSP builds CMSIS-DSP from the qualified CMSIS-DSP commit, one commit of Ambiq's upstream-tracking fork [AmbiqAI/CMSIS-DSP](https://github.com/AmbiqAI/CMSIS-DSP), checked out as the submodule `third_party/CMSIS-DSP`.

## 1. Get the source

From an empty project directory:

```sh
mkdir rfft_demo && cd rfft_demo
git clone --recurse-submodules https://github.com/AmbiqAI/helia-dsp
```

The last line of output is:

```text
Submodule path 'third_party/CMSIS-DSP': checked out '<commit>'
```

To check an existing clone:

```sh
git -C helia-dsp submodule status
```

The line starts with a space followed by `<commit>` and `third_party/CMSIS-DSP`. A leading `-` means the submodule is not checked out (run `git -C helia-dsp submodule update --init`); a leading `+` means a different commit is checked out, which the configure step will reject.

A release archive (`helia-dsp-v<version>.tar.gz`) extracts to `helia-dsp-v<version>/` with the submodule's files in place; rename the directory to `helia-dsp` to follow the rest of this page.

## 2. Build and run on the host

Create `CMakeLists.txt` beside the `helia-dsp` directory:

```cmake
cmake_minimum_required(VERSION 3.20)
project(rfft_demo LANGUAGES C)

add_subdirectory(helia-dsp)

add_executable(rfft_demo main.c)
target_link_libraries(rfft_demo PRIVATE helia_dsp m)
```

and `main.c`:

```c
#include <math.h>
#include <stdio.h>
#include "arm_math.h"

#define N 256

int main(void)
{
    static float32_t x[N], X[N];
    arm_rfft_fast_instance_f32 S;

    for (int n = 0; n < N; n++) {
        x[n] = cosf(2.0f * PI * 8.0f * (float32_t)n / N);   /* bin 8 */
    }
    if (arm_rfft_fast_init_256_f32(&S) != ARM_MATH_SUCCESS) {
        return 1;
    }
    arm_rfft_fast_f32(&S, x, X, 0);   /* forward; x is overwritten */

    /* X[0] = DC, X[1] = Nyquist, then re/im pairs for bins 1..N/2-1 */
    printf("bin 8: re=%.3f im=%.3f\n", X[16], X[17]);
    return 0;
}
```

Configure, build and run:

```sh
cmake -S . -B build -DHOST=ON
cmake --build build
./build/rfft_demo
```

The configure step ends with `-- Build files have been written to: .../rfft_demo/build`, the build with `[100%] Built target rfft_demo`, and the program prints the following and exits with status 0:

```text
bin 8: re=128.000 im=0.000
```

The forward transform is unnormalized, so a unit-amplitude cosine at bin 8 of a 256-point transform gives 256/2 = 128 at that bin. `HOST=ON` builds CMSIS-DSP's portable C kernels for the build machine and needs no CMSIS-Core headers. A host build is for development and testing on a computer; it is not a device configuration, and the compatibility statements for Ambiq SoCs do not cover it. `m` is the C maths library, needed here for `cosf`.

`helia_dsp` is the target an application links. It links `CMSISDSP`, the library target, which carries Arm's include directories and the header-affecting definitions as usage requirements (settings CMake applies to every target that links it), so `arm_math.h` is found and is read with the same definitions the library was compiled with. `nsx::helia_dsp` is an alias of `helia_dsp`. Add heliaDSP once, from the top-level project; every library in the build that needs CMSIS-DSP links `helia_dsp`, and the build contains one `libCMSISDSP.a`.

The call sequence is CMSIS-DSP's instance, initialize, process pattern: the caller owns the instance and every buffer, the initializer fills the instance, and the process function works on one block. `arm_rfft_fast_init_256_f32` links the tables for 256 points only; the generic `arm_rfft_fast_init_f32(&S, 256)` links the tables of every length (see [Contracts](https://ambiqai.github.io/helia-dsp/guide/contracts/)).

## 3. Build for Cortex-M55

A Cortex-M build needs the CPU, FPU and float-ABI flags of the target, a compiler from the [toolchain policy](https://ambiqai.github.io/helia-dsp/guide/toolchains/), and the CMSIS-Core headers. Create `cortex-m55-gcc.cmake`:

```cmake
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_C_FLAGS_INIT "-mcpu=cortex-m55 -mthumb -mfloat-abi=hard")
```

Configure and build the library target (linking `rfft_demo` for a board needs that board's startup code and linker script, which this example does not have):

```sh
cmake -S . -B build-m55 -DCMAKE_TOOLCHAIN_FILE=cortex-m55-gcc.cmake \
  -DCMSISCORE=<CMSIS_6>/CMSIS/Core -DHELIUM=ON
cmake --build build-m55 --target CMSISDSP
```

With `arm-none-eabi-gcc` 14.2.1 the configure step prints:

```text
-- helia-dsp: gcc 14.2.1 < 15 cannot compile the f16 MVE kernels (ICE); defaulting DISABLEFLOAT16=ON. Pass -DDISABLEFLOAT16=OFF to override.
```

and the build ends with `[100%] Built target CMSISDSP`. To confirm that the Helium kernels were compiled, look for the Helium-only biquad initializer:

```sh
arm-none-eabi-nm build-m55/helia-dsp/cmsis-dsp/libCMSISDSP.a | grep ' T arm_biquad_cascade_df1_mve_init_f32'
```

Expected: one line ending in `T arm_biquad_cascade_df1_mve_init_f32`. In a host or portable-kernel build the command prints nothing.

The Helium kernels are selected by the compiler target, not by a CMake option: when the compiler defines `__ARM_FEATURE_MVE`, CMSIS-DSP's headers select them unless `AUTOVECTORIZE=ON`. `HELIUM=ON` adds the compiler options those sources need (`-flax-vector-conversions` under GCC); the NSX module sets it for MVE-capable boards.

For ATfE 22.1.0, use this toolchain file instead, with `<ATfE>` the installation directory:

```cmake
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_C_COMPILER <ATfE>/bin/clang)
set(CMAKE_AR <ATfE>/bin/llvm-ar)
set(CMAKE_RANLIB <ATfE>/bin/llvm-ranlib)
set(CMAKE_C_FLAGS_INIT "--target=arm-none-eabihf -mcpu=cortex-m55 -mthumb -mfloat-abi=hard")
```

The configure step prints no `helia-dsp:` notice, because ATfE compiles the f16 kernels and `DISABLEFLOAT16` keeps its default `OFF`; the same `nm` check, run with `<ATfE>/bin/llvm-nm`, prints the initializer.

## Build options

CMSIS-DSP's own options apply unchanged; heliaDSP adds none.

| Option | Effect |
| --- | --- |
| `HOST` | Build for the build machine with portable kernels; no CMSIS-Core needed. |
| `CMSISCORE` | Path to `CMSIS/Core` for Cortex-M builds outside NSX. |
| `HELIUM` | Compiler options for the Helium sources. |
| `AUTOVECTORIZE` | Use the portable C kernels on a Helium target. Changes contracts; see [Contracts](https://ambiqai.github.io/helia-dsp/guide/contracts/). |
| `DISABLEFLOAT16` | Omit the f16 functions and their declarations. Defaults to `ON` for GCC below 15 in cross builds. |
| `MVEFLOAT16` | Use the f16 Helium intrinsics. |

`AUTOVECTORIZE`, `MVEFLOAT16`, `DISABLEFLOAT16` and the Neon options change what the headers declare; they are propagated to every target that links `CMSISDSP`.

A target that links `nsx::helia_dsp` is compiled with `HELIA_DSP_VERSION_MAJOR`, `HELIA_DSP_VERSION_MINOR` and `HELIA_DSP_VERSION_PATCH`, the three components of `VERSION`; they are not options.

## Configure-time checks and their messages

### The CMSIS-DSP source is not the qualified CMSIS-DSP commit

heliaDSP computes a SHA-256 digest over the CMSIS-DSP files that affect the build and compares it with the digest recorded for the qualified CMSIS-DSP commit in `cmake/HeliaDspPin.cmake`. The digest covers the files named `*.c`, `*.h`, `*.hpp`, `*.cmake`, `*.in`, `CMakeLists.txt`, `Kconfig` and `module.yml` under the build-input directories, with each CRLF read as LF, and leaves out CMake build trees (directories holding a `CMakeCache.txt` and a `CMakeFiles/` subdirectory, and no `CMakeLists.txt`). A checkout with CRLF line endings (Git for Windows with `core.autocrlf=true`), a build directory, object files or editor backups inside the dependency therefore do not stop the configure step. A local edit to one of those files, an added or missing one, or a different commit stops it. A directory that holds both `CMakeCache.txt` and `CMakeFiles/` and no `CMakeLists.txt` is excluded wherever it is; excluding one that holds files of the qualified commit changes the digest, but files added inside such a directory are not seen, so the check guards against accidental changes, not deliberate tampering:

```text
CMake Error at helia-dsp/cmake/HeliaDspDependencyIdentity.cmake:103 (message):
  helia-dsp: the bundled CMSIS-DSP provider at

    .../rfft_demo/helia-dsp/third_party/CMSIS-DSP

  is not the pinned dependency.

    expected build-input digest <digest recorded for the qualified commit>
    (AmbiqAI/CMSIS-DSP commit <commit>)
    found                       <digest of the tree on disk>

  helia-dsp is qualified against one CMSIS-DSP revision and does not build or
  link against another.  The digest covers its source, header, CMake and
  Zephyr module files, with CRLF read as LF; other files and CMake build trees
  are left out.
```

Line endings and files other than sources, headers, CMake and Zephyr module files do not cause this message. `git -C helia-dsp submodule update --init --force` checks out the pinned commit and restores edited and missing files; it does not remove added files, which `git -C helia-dsp/third_party/CMSIS-DSP status --untracked-files=all --ignored` lists. If the submodule is not checked out at all, the message instead says that the provider `has no Source/CMakeLists.txt` and names the same command.

### No CMSIS-Core headers

A Cortex-M build without `CMSISCORE`, without the NSX target `nsx::cmsis_core`, and without `HOST=ON` stops with:

```text
helia-dsp requires nsx::cmsis_core, CMSISCORE=/path/to/CMSIS/Core, or HOST=ON.
```

### A second CMSIS-DSP library

If the enclosing project already defines a `CMSISDSP` target before adding heliaDSP, heliaDSP reuses it only if it can verify it; it never adds a second CMSIS-DSP library. The checks run when heliaDSP is added and again at the end of the top-level directory, after the other calls deferred to it, so a definition, source or option applied to `CMSISDSP` after `add_subdirectory(<helia-dsp>)`, by an ordinary command or by a deferred call, is checked as well. Each failed condition stops the configure step with a message beginning `helia-dsp: the existing CMSISDSP target cannot be used as the CMSIS-DSP provider:`, or, at the end of the top-level directory, `helia-dsp: the existing CMSISDSP target, as changed after helia-dsp was added, cannot be used as the CMSIS-DSP provider:`, followed by the cause:

| Cause in the message | Meaning |
| --- | --- |
| `it is an imported or non-static target` | A prebuilt or imported library cannot be checked. Build it from the source of the qualified CMSIS-DSP commit in this build tree, or remove it to use the bundled one. |
| `is not the pinned dependency` | Its source tree has a different digest. |
| `source ... is outside the verified tree` | It compiles a file that is not part of the qualified CMSIS-DSP commit. |
| `... changes the public headers but is not both compiled into the library and propagated to its consumers` | A header-affecting definition reaches the library or its consumers, not both. A definition counts with or without a value (`ARM_MATH_AUTOVECTORIZE=0` included, since the headers test `defined()`), from the target's compile definitions or `-D` and `/D` options (`-D NAME` as two options included), the provider directory's compile definitions, or the usage requirements of a target it links. |
| `ARM_DSP_CUSTOM_CONFIG is defined for the library or its consumers` | `ARM_DSP_CUSTOM_CONFIG` makes `arm_math_types.h` include an `arm_dsp_config.h` ahead of its feature tests. That header can define macros that change structure layouts, and the check cannot see them. Remove the target and let heliaDSP build CMSIS-DSP from its pinned dependency. |
| `'...' is a generator expression that can add a definition changing the public headers` | A header-affecting definition, a linked target that carries one, or a `$<TARGET_PROPERTY:...>` lookup is under a generator expression, which configure-time checks cannot evaluate. |
| `the option '...' undefines ...` | A `-U` or `/U` option removes a header-affecting definition; its effect depends on the order of the compiler's arguments, which the configure step does not see. |
| `the source ... is compiled with ... through its own source properties` | A header-affecting definition is set on individual library sources (`COMPILE_DEFINITIONS`, `COMPILE_OPTIONS` or `COMPILE_FLAGS` source properties), so those objects differ from the rest of the library and from its consumers. |
| `helia-dsp is configured with <option>=ON but the provider is built without ...`, or the reverse | Its CMSIS-DSP options differ from those in the scope that adds heliaDSP. |
| `it does not link <target> PUBLIC` | It does not carry the board's CPU, FPU and float-ABI flags to its consumers (NSX builds). |
| `machine option '...' is private to the provider` | A `-mcpu`, `-mfpu` or `-mfloat-abi` option applies to the library and not to its consumers. |
| `CMAKE_C_FLAGS differs between the provider ... and helia-dsp`, or `CMAKE_C_FLAGS_<CONFIG>` | The two directories compile with different global flags, compared for each configuration the build can compile (each of `CMAKE_CONFIGURATION_TYPES` with a multi-configuration generator). |

If a call deferred to the end of the top-level directory defers itself again whenever other calls are pending, the check cannot run after it; after 100 deferrals the configure step stops with `helia-dsp: the check of the existing CMSISDSP target cannot run after the other calls deferred to the end of the top-level directory`.

The header-affecting definitions are `ARM_MATH_AUTOVECTORIZE`, `ARM_MATH_MVE_FLOAT16`, `DISABLEFLOAT16`, `ARM_MATH_NEON` and `ARM_MATH_NEON_EXPERIMENTAL`, which must also match the options in the scope that adds heliaDSP, and `ARM_MATH_HELIUM`, `ARM_MATH_MVEF`, `ARM_MATH_MVEI`, `ARM_MATH_NEON_FLOAT16`, `ARM_MATH_MATRIX_CHECK`, `ARM_MFCC_CFFT_BASED`, `ARM_MFCC_USE_CFFT`, `ARM_MATH_DSP`, `ARM_FLOAT16_SUPPORTED` and `__GNUC_PYTHON__`, which heliaDSP has no option for and which must reach both the library and its consumers or neither. CMSIS-DSP's `MATRIXCHECK=ON` compiles `ARM_MATH_MATRIX_CHECK` into the library only, so an external provider built with it is rejected unless the definition also reaches its consumers; heliaDSP's bundled provider propagates it.

When every check passes, the configure step prints `helia-dsp: using the external CMSISDSP target at ...`. The compiler workarounds are in the qualified CMSIS-DSP sources and apply to that target as well; heliaDSP's CMake policy, the `DISABLEFLOAT16` default for GCC below 15, is not applied to it. Per-source properties are inspected for header-affecting definitions only; other per-file compile options are not inspected.

There is no installed `find_package(helia_dsp)` package.

## Next

- [Contracts](https://ambiqai.github.io/helia-dsp/guide/contracts/): what changes for a caller in a Helium build.
- [Toolchains](https://ambiqai.github.io/helia-dsp/guide/toolchains/)
- [neuralSPOT-X](https://ambiqai.github.io/helia-dsp/getting-started/neuralspot-x/) and [Zephyr](https://ambiqai.github.io/helia-dsp/getting-started/zephyr/)
