Compile a specific file always

How can I define that a specific file will be always compiled, even if there is no build all command executed ?

make a new env:

[env:only_one_file_env]
build_src_filter = "+<the_file_to_compile_for_this_env.cpp>"`

then build it with pio run -e only_one_file_env

1 Like

Thank you for your answer, but it seems that this is not what I am looking for.
Let me explain it in more detail:

I have a project with several files and normally only these files are compiled which are modified.
Now I want one file to compile always, even it is not modified.
The reason behind is as follows:
I want to implement the compilation date and time into my program and use the DATE and TIME . But this will be only updated if the file is compiled again.

What can I do ?

best regards

1 Like

I have the same problem.

one solution is to delete (automatically) the .o file for the corresponding code where DATE and TIME is used. this forces the compiler to re-compile the code with updated defines.

here is what chatgpt gave me (haven’t tested it, but seems legit):

  1. Create a Python script (let’s call it pre_compile.py) in the root of your PlatformIO project.
  2. In pre_compile.py, you can use Python’s standard library to remove the .o file. Here’s a sample script:

pythonCopy code

import os

def pre_compile_action(env):
    build_dir = ".pio/build/YOUR_BUILD_ENVIRONMENT/"
    object_file = build_dir + "src/your_file.o"
    
    if os.path.exists(object_file):
        os.remove(object_file)
        print("Removed object file to force recompilation: " + object_file)

# If you need to do this for multiple build environments, you can repeat these lines for each.
Export("pre_compile_action")

Replace YOUR_BUILD_ENVIRONMENT with your PlatformIO environment name and your_file.o with the name of the object file you want to force recompilation for.

  1. In your platformio.ini file, add the pre:script directive to call this script before compiling:

iniCopy code

[env:your_environment]
platform = ...
board = ...
framework = ...
extra_scripts = pre:pre_compile.py

This way, the script will be executed before each compilation process, effectively removing the .o file and forcing a recompilation of your source file.

Note: The script and the platformio.ini file are just examples. You will need to adapt them to fit your specific project layout and requirements.