"""
    Build script for lattice ice40 FPGAs
    latticeice40-builder.py
"""

from os.path import join
from SCons.Script import AlwaysBuild, Builder, Default, DefaultEnvironment, Glob

env = DefaultEnvironment()
env.Replace(PROGNAME="firmware")

# -- Get all the source files
src_dir = env.subst('$PROJECTSRC_DIR')
total = join(src_dir, '*.v')
src_files = Glob(total)

PCFs = join(src_dir, '*.pcf')
PCF = Glob(PCFs)
print("PCF: {}".format(PCF[0]))

# -- Builder 1 (.v --> .blif)
synth = Builder(action='yosys -p \"synth_ice40 -blif $TARGET\" $SOURCES',
                suffix='.blif',
                src_suffix='.v')

# -- Builder 2 (.blif --> .asc)
pnr = Builder(action='arachne-pnr -d 1k -o $TARGET -p {} $SOURCE'.format(PCF[0]),
              suffix='.asc',
              src_suffix='.blif')

bitstream = Builder(action='icepack $SOURCE $TARGET',
                    suffix='.bin',
                    src_suffix='.asc')

env.Append(BUILDERS={'Synth': synth, 'PnR': pnr, 'Bin': bitstream})

blif = env.Synth(src_files)
asc = env.PnR([blif, PCF[0]])
binf = env.Bin(asc)

upload = env.Alias('upload', binf, 'iceprog $SOURCE')
AlwaysBuild(upload)

@@ -442,16 +442,81 @@
 F%5B0%5D))%0A%0A
+bin_dir = join('$PIOPACKAGES_DIR', 'toolchain-icestorm', 'bin')%0A%0A
 # -- Bui
@@ -564,14 +564,34 @@
 ion=
-'yosys
+join(bin_dir, 'yosys') + '
  -p 
@@ -750,16 +750,30 @@
 (action=
+join(bin_dir, 
 'arachne
@@ -776,16 +776,22 @@
 chne-pnr
+') + '
  -d 1k -
@@ -926,16 +926,30 @@
 ion=
+join(bin_dir, 
 'icepack
  $SO
@@ -944,16 +944,22 @@
 'icepack
+') + '
  $SOURCE
@@ -1226,16 +1226,30 @@
 ', binf,
+ join(bin_dir,
  'icepro
@@ -1249,16 +1249,22 @@
 'iceprog
+') + '
  $SOURCE

Use toolchain-icestorm