An example project that shows how to check if a certain macro is active in a file.

Overview

PlatformIO Check Compiler Flags Example

Description

Demonstrates the usage of an extra script and a special compilter invocation to get the active macros in a file.

E.g., if you have a file project_config.h

#ifndef _PROJECT_CONFIG_H_
#define _PROJECT_CONFIG_H_

// select driver here
#define DRIVER_ILI9341 1
//#define DRIVER_ST7789 1

#endif /* _PROJECT_CONFIG_H_ */

And you want to check in an extra script which macro was active or not, this repo shows a way to do that.

Inner workings

The script uses primarily env.Execute() to execute the C++ compiler (stored in env.["CXX"]) with the regular build flags given by the environment and saves the output to a file.

xtensa-esp32-elf-g++ -DPLATFORMIO=50202 [..more macros..] -w -dM -E -x c++ "C:\Users\Max\temp\check_flags\src\project_config.h" > flags.txt

The file then contains all compiler-builtin macros and explicitly enabled macros.

#define __DBL_MIN_EXP__ (-1021)
#define __UINT_LEAST16_MAX__ 0xffff
#define __ATOMIC_ACQUIRE 2
#define __FLT_MIN__ 1.1754943508222875e-38F
#define __GCC_IEC_559_COMPLEX 0
#define PLATFORMIO 50202
[..]
#define DRIVER_ILI9341 1
#define __DEC128_MIN__ 1E-6143DL
[...]

The file is then read out line-by-line and put into a Python dictionary, the key being the macro name and the value being the macro name

The resulting dictionary can then be checked for the existance of a certain key, in the standard if "KEY_NAME" in macros: way.

Limitations

The built-up command does not include any -I flags to the e.g. Arduino core, so the target header file musn't #include <Arduino.h>. There is commented-out code in the script to fix that in a brute-force way, that is, adds all items from env["CPPPATH"] as -I flags. However, this probably misses out on library includes if additional libararies are used.

It's best to keep the configuration header file as simple as possible, only defining the configuration macros in the most minimal way.

Expected output

With the unmodified source code, one should get

check_for_flags(["buildprog"], [".pio\build\esp32dev\firmware.bin"])
AFTER build!!
xtensa-esp32-elf-g++ -DPLATFORMIO=50202 -DARDUINO_ESP32_DEV -DESP32 -DESP_PLATFORM -DF_CPU=\""240000000L\"" -DHAVE_CONFIG_H -DMBEDTLS_CONFIG_FILE=\""mbedtls/esp_config.h\"" -DARDUINO=10805 -DARDUINO_ARCH_ESP32 -DARDUINO_VARIANT=\""esp32\"" -DARDUINO_BOARD=\""Espressif ESP32 Dev Module\"" -w -dM -E -x c++ "C:\Users\Max\temp\check_flags\src\project_config.h" > flags.txt
Parsed a total of 239 defines (explicit and implicitly set).
DRIVER_ILI9341 was defined!! With value: 1
echo Super special command here....
Super special command here....

At the end, showcasing that it was successfully detected that the DRIVER_ILI9341 macro was defined (and also to which value it was defined).

Usage in a different project

Copy the check_flags.py into the project and add it to the extra_scripts expression of your platformio.ini.

Adapt the logic in check_flags.py regarding the read-out file and the reaction to it accordingly.

extra_scripts =
   check_flags.py
You might also like...
A tool for RaceRoom Racing Experience which shows you launch data

R3E Launch Tool A tool for RaceRoom Racing Experience which shows you launch data. Usage Run the tool, change the Stop Speed to whatever you want, and

Python Example Project Structure

Python Example Project Structure Example of statuses that can be in readme: Visit my docs for the full documentation, examples and guides. With this p

Certipy is a Python tool to enumerate and abuse misconfigurations in Active Directory Certificate Services (AD CS).

Certipy Certipy is a Python tool to enumerate and abuse misconfigurations in Active Directory Certificate Services (AD CS). Based on the C# variant Ce

Python implementation for Active Directory certificate abuse

Certipy is a Python tool to enumerate and abuse misconfigurations in Active Directory Certificate Services (AD CS). Based on the C# variant Ce

A simply program to find active jackbox.tv game codes

PeepingJack A simply program to find active jackbox.tv game codes How does this work? It uses a threadpool to loop through all possible codes in a ran

Active Transport Analytics Model: A new strategic transport modelling and data visualization framework

{ATAM} Active Transport Analytics Model Active Transport Analytics Model (“ATAM”

User management system (UMS), has the primary purpose of connecting to an Active Directory (AD)
User management system (UMS), has the primary purpose of connecting to an Active Directory (AD)

💿 Sistema de Gerenciamento de Usuário (SGU) 📚 Sobre o projeto Sistema de gerenciamento de usuários (SGU), tem o objetivo primário de se conectar a u

MiniJVM is simple java virtual machine written by python language, it can load class file from file system and run it.

MiniJVM MiniJVM是一款使用python编写的简易JVM,能够从本地加载class文件并且执行绝大多数指令。 支持的功能 1.从本地磁盘加载class并解析 2.支持绝大多数指令集的执行 3.支持虚拟机内存分区以及对象的创建 4.支持方法的调用和参数传递 5.支持静态代码块的初始化 不支

A Python3 script to decode an encoded VBScript file, often seen with a .vbe file extension

vbe-decoder.py Decode one or multiple encoded VBScript files, often seen with a .vbe file extension. Usage usage: vbe-decoder.py [-h] [-o output] file

Owner
Maximilian Gerhardt
Firmware developer and security specialist.
Maximilian Gerhardt
Sheet2export - FreeCAD macro to export spreadsheet

Description This is FreeCAD macro to export spreadsheet to file.

Darek L 3 Jul 9, 2022
Collection of tools to be more productive in your work environment and to avoid certain repetitive tasks. 💛💙💚

Collection of tools to be more productive in your work environment and to avoid certain repetitive tasks. ??????

Raja Rakotonirina 2 Jan 10, 2022
edgetest is a tox-inspired python library that will loop through your project's dependencies, and check if your project is compatible with the latest version of each dependency

Bleeding edge dependency testing Full Documentation edgetest is a tox-inspired python library that will loop through your project's dependencies, and

Capital One 16 Dec 7, 2022
A example project's description is a high-level overview of why you’re doing a project.

A example project's description is a high-level overview of why you’re doing a project.

Nikita Matyukhin 12 Mar 23, 2022
Birthday program - A program that lookups a birthday txt file and compares to the current date to check for birthdays

Birthday Program This is a program that lookups a birthday txt file and compares

Daquiver 4 Feb 2, 2022
An example file showing a simple endpoints like a login/logout function and maybe some others.

Flask API Example An example project showing a simple endpoints like a login/logout function and maybe some others. How to use: Open up your IDE (or u

Kevin 1 Oct 27, 2021
IDA Pro plugin that shows the comments in a database

ShowComments A Simple IDA Pro plugin that shows the comments in a database Installation Copy the file showcomments.py to the plugins folder under IDA

Fernando Mercês 32 Dec 10, 2022
A simple script that shows important photography times. written in python.

A simple script that shows important photography times. written in python.

John Evans 13 Oct 16, 2022
Shows VRML team stats of all players in your pubs

VRML Team Stat Searcher Displays Team Name, Team Rank (Worldwide), and tier of all the players in your pubs. GUI WIP: Only username search works (for

Hamish Burke 2 Dec 22, 2022
Cisco IOS-XE Operations Program. Shows operational data using restconf and yang

XE-Ops View operational and config data from devices running Cisco IOS-XE software. NoteS The build folder is the latest build. All other files are fo

null 18 Jul 23, 2022