Add files to be compiled in EXTRA_SCRIPT

I’ve got some files that I’m auto generating into the build directory, but I can’t work out how to get them into the SCons environment to actually be compiled, what have I missed?

Cheers

Matthew.

Could you provide an example to reproduce?

In my platformio.ini file I have

[env:water]
extra_script = preHook.py
platform = atmelavr
framework = arduino
board = sensee
upload_protocol = stk500
upload_flags = -P/dev/cu.usbserial -e
upload_port = /dev/cu.usbserial
build_flags = -DWATERFLOW_SAMPLER -DBUILD -I./src -I./src/firmware-common/RadioHead -DHARDWARE_SPI -DULONG_ADDRESS

where in preHook.py I have

Import("env")
from SCons.Script import (File)
import json

projDir = env.subst('$PROJECT_DIR')
buildDir = env.subst('$BUILD_DIR')

samplerFactoryCpp =  open(buildDir + "/samplerFactory.cpp", 'w')
samplerFactoryH =  open(buildDir + "/samplerFactory.h", 'w')

def createKeyDefintions(sensorDef):
   # Would normally do stuff with the JSON structure and create #defines out of it
   # But here we just have something that needs compiling. 
   samplerFactoryH.write("int randomInRange(int a);")
   samplerFactoryCpp.write("int randomInRange(int a) { return rand()%a; }")

with open(projDir+"/defs.json") as data_file:
        data = json.load(data_file)
        createKeyDefintions(data)

In the real solution, the json file describes a whole bunch of stuff that gets autogenerated into some #defines, and some factory methods (that need to be compiled).

So I need to add sampleFactory.cpp to the list of files to be compiled.

Is anything else needed, or is that enough?

You don’t need to use advanced extra script. Take a look at Dynamic Build Flags where you can generate custom flags and modify headers before compiling.

I’ve had a read on those flags, but it isn’t just headers I’m generating, my script is creating both cpp and header files which need to be added to the list of sources to be compiled, and I would preferably keep the generated files out of the /src tree and in the build folder (don’t need to be committed)

Why you can’t generate them to ./lib/MyCustomSources? PIO will automatically build them and check for modifications.

Thanks, I’ll try that this evening (so in around 12 hours!)

Do you understand what do I mean? You can generate “dynamic” library, then use it in src as a generic library.