platformio / platformio-core

Your Gateway to Embedded Software Development Excellence :alien:
https://platformio.org
Apache License 2.0
7.95k stars 792 forks source link

Feature request: provide some default flags if setting option for that in the platformio.ini file #4505

Open jerabaul29 opened 1 year ago

jerabaul29 commented 1 year ago

I often end up having snippets like this in my platformio.ini files:

build_flags =
    !echo "-DREPO_GIT_BRANCH="$(git branch --show-current) ; this is non portable, unix only; would need a python "pre platformio script" to get this to work portably...
    !echo "-DREPO_COMMIT_ID="$(git log | head -1 | cut -c8-)  ; this is non portable, unix only; would need a python "pre platformio script" to get this to work portably...
    !echo "-DCOMPILING_HOST_NAME="$(hostname) ; this is non portable, unix only; would need a python "pre platformio script" to get this to work portably...
    !echo "-DCOMPILING_USER_NAME="$(whoami) ; this is non portable, unix only; would need a python "pre platformio script" to get this to work portably...

This works perfectly fine for me. The only "issue" is that, as you can see, I am on linux, and I have set this in a way that will not work in windows / possibly mac. I do not want to myself do some work to make things platform agnostic (I do not have access to a windows machine to test for example), but still it would be really convenient for my users if things were platform agnostic.

Hence my question: would it be possible to provide, from platformio-core "natively", some sets of "pre_defined_flags" that could be enabled to provide some typical, often needed, build flags?

For example something like:

pre_defined_flags =
    COMMIT_ID  ; provide as platform agnostic flag the git commit id from which compile if a git repo, "NONE" else
    GIT_BRANCH  ; provide as platform agnostic flag the git branch from which compile if a git repo, "NONE" else
    HOSTNAME  ; provide as platform agnostic flag the hostname on which compiling
    USERNAME  ; provide as platform agnostic flag the username who compiles
    ; COMPILE_UNIX_TIMESTAMP  ; provide as platform agnostic flag the unix timestamp of compilation time; now disabled

I guess this would mean:

I.e. when building such a project, the flag COMMIT_ID containing the commit id would be visible, but the flag COMPILE_UNIX_TIMESTAMP would not.

As a small note / to help macro n00bs like me, it can be useful in documentation to remind that, to use all any of these flags, what is needed in the program is then:


// the usual "macro stringification magics"
#define XSTR(a) #a
#define STRINGIFY_CONTENT(a) XSTR(a)

// getting the content to a variable and then printing the variable
static constexpr char commit_id[] {STRINGIFY_CONTENT(REPO_COMMIT_ID)};
Serial.println(F(commit_id));

// printing directly
Serial.println(STRINGIFY_CONTENT(COMPILING_USER_NAME))
ivankravets commented 1 year ago

You can solve this issue with:

Also, the list of built-in variables, where you can find $UNIX_TIME. Please note that adding $UNIX_TIME to the build flags will trigger PlatformIO Build System to recompile the project from scratch. The value changes every second...

jerabaul29 commented 1 year ago

Many thanks for the pointers @ivankravets :) . Yes, I remembered about these resources (or some older version of them), and I was aware that this can be used to solve the need :) .

I guess my point would be that it would be easier on the user to provide a list of default, readily implemented, turn key flags that are commonly used, so that the user does not need to set it up themselves but can just write a few words in the ini file. At least for some of the users I work with, who have no background in embedded from before / maybe know a tiny bit of C but no python / are not used to automation, it would be very helpful :) .

jerabaul29 commented 1 year ago

(and even for advanced users, it would be faster than reading the full doc, writing and testing the script, etc :) ).

ivankravets commented 1 year ago

We agree with you. This is the reason why we marked this issue as a feature request. The flags/values which you requests can't be implemented as build variables because it will need to provide the initial computation of these vars. It will increase building time. But! It is possible to call functions within the SCons template. For example

[env]
build_flags =
   '-D GIT_BRANCH=${fetch_git_branch()}'

In this case, we will fetch the value by demand.