Building to a directory external to the project source files and workspace

In platformio.ini I’d like to have something such as:-

[platformio]
build_dir = c:\builds\pio\$PROJECT_NAME

For project named Blink this would become c:\builds\pio\Blink.

Doing it this way means if I copy a project and give it a new name, or rename an existing project, I don’t manually have to modify build_dir.
Also I want to keep generated files outside my project folders to save space.

But looking through symbols via “pio run --target envdump” I can’t see a convenient symbol for the project name which I can use. Maybe it can be extracted from another symbol.

Am I missing something, or is there some other way of doing the same thing?

You can effect env["BUILD_DIR"] arbitrarily using advanced scripting where you can e.g. infer the project folder name from env["PROJECT_DIR"] and running os.path.basename on it.

1 Like

Thanks, I’ll try this.

Thanks for the hints from maxgerhardt, I did this:-

Define an OS environment variable PIO_BUILD, e.g. “C:\builds\pio”.
Put script set_env.py in the directory containing the platformio projects.

Then in the platformio.ini files for each project:

extra_scripts = pre:..\set_env.py

In script set_env.py:-

import os
Import("env")
env["PROJECT_BUILD_DIR"] = os.path.join(os.environ["PIO_BUILD"], 
os.path.basename(env["PROJECT_DIR"]))

Now a project can be copied and renamed, or the projects directory can be moved, without manually changing the output directory for building into.
(A PROJECT_NAME variable would be useful in a future release.! But this is a handy template for doing some other tweeks to the configuration.)

1 Like