# Running ESP-IDF Applications on Host

[中文]

Note

Running ESP-IDF applications on host is currently still an experimental feature, thus there is no guarantee for API stability. However, user feedback via the ESP-IDF GitHub repository or the ESP32 forum is highly welcome, and may help influence the future of design of the ESP-IDF host-based applications.

This document provides an overview of the methods to run ESP-IDF applications on Linux, and what type of ESP-IDF applications can typically be run on Linux.

## Introduction

Typically, an ESP-IDF application is built (cross-compiled) on a host machine, uploaded (i.e., flashed) to an ESP chip for execution, and monitored by the host machine via a UART/USB port. However, execution of an ESP-IDF application on an ESP chip can be limiting in various development/usage/testing scenarios.

Therefore, it is possible for an ESP-IDF application to be built and executed entirely within the same Linux host machine (henceforth referred to as "running on host"). Running ESP-IDF applications on host has several advantages:

- No need to upload to a target.

- Faster execution on a host machine, compared to running on an ESP chip.

- No requirements for any specific hardware, except the host machine itself.

- Easier automation and setup for software testing.

- Large number of tools for code and runtime analysis, e.g., Valgrind.

A large number of ESP-IDF components depend on chip-specific hardware. These hardware dependencies must be mocked or simulated when running on host. ESP-IDF currently supports the following mocking and simulation approaches:

1. Using the FreeRTOS POSIX/Linux simulator that simulates FreeRTOS scheduling. On top of this simulation, other APIs are also simulated or implemented when running on host.

2. Using CMock to mock all dependencies and run the code in complete isolation.

In principle, it is possible to mix both approaches (POSIX/Linux simulator and mocking using CMock), but this has not been done yet in ESP-IDF. Note that despite the name, the FreeRTOS POSIX/Linux simulator currently also works on macOS. Running ESP-IDF applications on host machines is often used for testing. However, simulating the environment and mocking dependencies does not fully represent the target device. Thus, testing on the target device is still necessary, though with a different focus that usually puts more weight on integration and system testing.

Note

Another possibility to run applications on the host is to use the QEMU simulator. However, QEMU development for ESP-IDF applications is still a work in progress and has not been documented yet.

### CMock-Based Approach

This approach uses the CMock framework to solve the problem of missing hardware and software dependencies. CMock-based applications running on the host machine have the added advantage that they usually only compile the necessary code, i.e., the (mostly mocked) dependencies instead of the entire system. For a general introduction to Mocks and how to configure and use them in ESP-IDF, please refer to Mocks.

### POSIX/Linux Simulator Approach

The FreeRTOS POSIX/Linux simulator is available on ESP-IDF as a preview target already. This simulator allows ESP-IDF components to be implemented on the host, making them accessible to ESP-IDF applications when running on host. Currently, only a limited number of components are ready to be built on Linux. Furthermore, the functionality of each component ported to Linux may also be limited or different compared to the functionality when building that component for a chip target. For more information about whether the desired components are supported on Linux, please refer to Component Linux/Mock Support Overview.

## Requirements for Using Mocks

- Installed ESP-IDF including all ESP-IDF requirements

- System package requirements (`libbsd`, `libbsd-dev`)

- A recent enough Linux or macOS version and GCC compiler

- All components the application depends on must be either supported on the Linux target (Linux/POSIX simulator) or mock-able

An application that runs on the Linux target has to set the `COMPONENTS` variable to `main` in the CMakeLists.txt of the application's root directory:

    set(COMPONENTS main)

This prevents the automatic inclusion of all components from ESP-IDF to the build process which is otherwise done for convenience.

If any mocks are used, then `Ruby` is required, too.

## Build and Run

To build the application on Linux, the target has to be set to `linux` and then it can be built and run:

    idf.py --preview set-target linux
    idf.py build
    idf.py monitor

## Component Linux/Mock Support Overview

Note that any "Yes" here does not necessarily mean a full implementation or mocking. It can also mean a partial implementation or mocking of functionality. Usually, the implementation or mocking is done to a point where enough functionality is provided to build and run a test application.

Component | Mock | Simulation
--- | --- | ---
cmock | No | Yes
driver | Yes | No
esp_common | No | Yes
esp_event | Yes | Yes
esp_http_client | No | Yes
esp_http_server | No | Yes
esp_https_server | No | Yes
esp_hw_support | Yes | Yes
esp_netif | Yes | Yes
esp_netif_stack | No | Yes
esp_partition | Yes | Yes
esp_rom | No | Yes
esp_system | No | Yes
esp_timer | Yes | No
esp_tls | Yes | Yes
fatfs | No | Yes
freertos | Yes | Yes
hal | No | Yes
heap | No | Yes
http_parser | Yes | Yes
json | No | Yes
linux | No | Yes
log | No | Yes
lwip | Yes | Yes
mbedtls | No | Yes
mqtt | No | Yes
nvs_flash | No | Yes
partition_table | No | Yes
protobuf-c | No | Yes
pthread | No | Yes
soc | No | Yes
spiffs | No | Yes
spi_flash | Yes | No
tcp_transport | Yes | No
unity | No | Yes

Provide feedback about this document