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
Owner
Maximilian Gerhardt
Firmware developer and security specialist.
Maximilian Gerhardt
Machine Learning powered app to decide whether a photo is food or not.

Food Not Food dot app ( 🍔 🚫 🍔 ) Code for building a machine Learning powered app to decide whether a photo is of food or not. See it working live a

Daniel Bourke 48 Dec 28, 2022
Companion Web site for Fluent Python, Second Edition

Fluent Python, the site Source code and content for fluentpython.com. The site complements Fluent Python, Second Edition with extra content that did n

Fluent Python 49 Dec 08, 2022
Scripts to convert the Ted-MDB corpora into the formats for DISRPT shared task and the converted corpora

Scripts to convert the Ted-MDB corpora into the formats for DISRPT shared task and the converted corpora.

1 Feb 08, 2022
Let’s Play with Python3

Python3-FirstEdition a bunch of python programs and stuff Super Important Notice THIS IS LICENSED UNDER GNU PUBLIC LICENSE V3 also, refer to Contribut

Jym Patel 2 Nov 24, 2022
Absolute solvation free energy calculations with OpenFF and OpenMM

ABsolute SOLVantion Free Energy Calculations The absolv framework aims to offer a simple API for computing the change in free energy when transferring

7 Dec 07, 2022
→ Plantilla de registro para Python

🔧 Pasos Necesarios CMD 🖥️ SOCKETS pip install sockets 🎨 COLORAMA pip install colorama 💻 Código register-by-inputs from turtle import color # Impor

Panda.xyz 4 Mar 12, 2022
Encode stuff with ducks!

Duckify Encoder Usage Download main.py and run it. main.py has an encoded version in encoded_main.py.txt. As A Module Download the duckify folder (or

Jeremiah 2 Nov 15, 2021
RCCで開催する『バックエンド勉強会』の資料

RCC バックエンド勉強会 開発環境 Python 3.9 Pipenv 使い方 1. インストール pipenv install 2. アプリケーションを起動 pipenv run start 本コマンドを実行するとlocalhost:8000へアクセスできるようになります。 3. テストを実行

Averak 7 Nov 14, 2021
Generic NDJSON importer for hashlookup server

Generic NDJSON importer for hashlookup server Usage usage: hashlookup-json-importer.py [-h] [-v] [-s SOURCE] [-p PARENT] [--parent-meta PARENT_META [P

hashlookup 2 Jan 19, 2022
Project issue to website data transformation toolkit

braintransform Project issue to website data transformation toolkit. Introduction The purpose of these scripts is to be able to dynamically generate t

Brainhack 1 Nov 19, 2021
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
Advent of Code 2021 challenges

Data analysis Document here the project: AoC21 Description: Project Description Data Source: Type of analysis: Please document the project the better

Daniel Wendel 1 Jan 07, 2022
Badge-Link-Creater 'For more beautiful profiles.'

Badge-Link-Creater 'For more beautiful profiles.' Ready Badges Prepares the codes of the previously prepared badges for you. Note Click here for more

Mücahit Gündüz 9 Oct 19, 2022
Tutorials for on-ramping to StarkNet

Full-Stack StarkNet Repo containing the code for a short tutorial series I wrote while diving into StarkNet and learning Cairo. Aims to onramp existin

Sam Barnes 71 Dec 07, 2022
Python Programmma DarkMap.py

DarkMap Python Programmma DarkMap.py O'rganish va rasmlarni ko'riosh https://drive.google.com/drive/folders/1l1zybs_0Zy9z_trZYz5R72WrwsE6mFOh?usp=shar

Og'abek 0 May 06, 2022
Bookmarkarchiver - Python script that archives all of your bookmarks on the Internet Archive

bookmarkarchiver Python script that archives all of your bookmarks on the Internet Archive. Supports all major browsers. bookmarkarchiver uses the off

Anthony Chen 3 Oct 09, 2022
Acesse seus investimentos da NuInvest pelo Python (Experimental)

Acesse seus investimentos da NuInvest pelo Python (Experimental)

André Roggeri Campos 5 Dec 06, 2022
An After Effects render queue for ShotGrid Toolkit.

AEQueue An After Effects render queue for ShotGrid Toolkit. Features Render multiple comps to locations defined by templates in your Toolkit config. C

Brand New School 5 Nov 20, 2022
Alfred 4 Workflow to search through your maintained/watched/starred GitHub repositories.

Alfred 4 Workflow to search through your maintained/watched/starred GitHub repositories. Setup This workflow requires a number of Python modules. Thes

Bᴇʀɴᴅ Sᴄʜᴏʀɢᴇʀs 1 Oct 14, 2022
A simple app that helps to train quick calculations.

qtcounter A simple app that helps to train quick calculations. Usage Manual Clone the repo in a folder using git clone https://github.com/Froloket64/q

0 Nov 27, 2021