tools: Update pw build script

- Enable the fpga build target if icestorm tools are available.
- Add log file output for nextpnr and yosys.
- Remove double .elf.elf extension for stm32f730r8tx_arduino

Change-Id: Ie54c089e94be99cec91b661a11d2eb3c49c9e628
Reviewed-on: https://pigweed-review.googlesource.com/c/gonk/+/185550
Reviewed-by: Armando Montanez <amontanez@google.com>
Commit-Queue: Anthony DiGirolamo <tonymd@google.com>
diff --git a/README.md b/README.md
index 3ba5179..b3d5d7c 100644
--- a/README.md
+++ b/README.md
@@ -52,11 +52,23 @@
 
 ```sh
 pw build
-ninja -C out/gn fpga
 ```
 
 The bitstream files will be written with the `.bin` extenson under
-`./out/gn/obj/fpga/*/*.bin`.
+`./out/gn/obj/fpga/*/*.bin` along with log output files.
+
+For example:
+
+```sh
+$ ls ./out/gn/obj/fpga/toplevel/
+nextpnr-log.txt
+toplevel.asc
+toplevel.bin
+toplevel.json
+toplevel_timing_report.json
+toplevel_timing_report.txt
+yosys-log.txt
+```
 
 ## Gonk `fpga_config` Example
 
diff --git a/fpga/fpga_image.gni b/fpga/fpga_image.gni
index 6cbf00d..fd8cee0 100644
--- a/fpga/fpga_image.gni
+++ b/fpga/fpga_image.gni
@@ -54,6 +54,9 @@
   _json_file = "$target_out_dir/$target_name/${_image_name}.json"
   _asc_file = "$target_out_dir/$target_name/${_image_name}.asc"
   _bin_file = "$target_out_dir/$target_name/${_image_name}.bin"
+
+  _yosys_log = "$target_out_dir/$target_name/yosys-log.txt"
+  _nextpnr_log = "$target_out_dir/$target_name/nextpnr-log.txt"
   _timing_report_text =
       "$target_out_dir/$target_name/${_image_name}_timing_report.txt"
   _timing_report_json =
@@ -69,10 +72,15 @@
 
   pw_exec("${target_name}._json") {
     inputs = _verilog_files
-    outputs = [ _json_file ]
+    outputs = [
+      _json_file,
+      _yosys_log,
+    ]
 
     program = "yosys"
     args = [
+      "-l",
+      rebase_path(_yosys_log, root_build_dir),
       "-q",
       "-p",
       "synth_ice40 -json " + rebase_path(_json_file, root_build_dir),
@@ -82,12 +90,17 @@
 
   pw_exec("${target_name}._asc") {
     inputs = [ _json_file ]
-    outputs = [ _asc_file ]
+    outputs = [
+      _asc_file,
+      _nextpnr_log,
+    ]
 
     deps = [ ":${invoker.target_name}._json" ]
 
     program = "nextpnr-ice40"
     args = [
+      "-l",
+      rebase_path(_nextpnr_log, root_build_dir),
       "--freq",
       "75",
       "--hx8k",
diff --git a/targets/stm32f730r8tx_arduino/target_toolchains.gni b/targets/stm32f730r8tx_arduino/target_toolchains.gni
index 57528f2..0522cd0 100644
--- a/targets/stm32f730r8tx_arduino/target_toolchains.gni
+++ b/targets/stm32f730r8tx_arduino/target_toolchains.gni
@@ -63,7 +63,6 @@
 }
 
 _toolchain_properties = {
-  final_binary_extension = ".elf"
 }
 
 _target_default_configs = pigweed_default_configs + [
diff --git a/tools/gonk_tools/build_project.py b/tools/gonk_tools/build_project.py
index 3447962..1b1c874 100644
--- a/tools/gonk_tools/build_project.py
+++ b/tools/gonk_tools/build_project.py
@@ -15,6 +15,7 @@
 
 import logging
 from pathlib import Path
+import shutil
 import sys
 
 from pw_build.build_recipe import (
@@ -29,6 +30,9 @@
 
 _LOG = logging.getLogger('pw_build')
 
+_ENABLE_FPGA_BUILD = (shutil.which('nextpnr-ice40') and shutil.which('yosys')
+                      and shutil.which('icepack') and shutil.which('icetime'))
+
 
 def build_project(force_pw_watch: bool = False) -> int:
     """Use pw_build to compile Gonk.
@@ -59,20 +63,30 @@
 
     default_gn_gen_command.append(gn_args(**default_gn_args))
 
+    default_targets = ['default']
+    if _ENABLE_FPGA_BUILD:
+        default_targets += ['fpga']
+    else:
+        _LOG.warning(
+            'FPGA build disabled. The following tools were not found: '
+            'icepack, icetime, nextpnr-ice40, yosys.')
+
+    default_build_steps = [
+        BuildCommand(
+            run_if=should_gn_gen_with_args(default_gn_args),
+            command=default_gn_gen_command,
+        ),
+        BuildCommand(
+            build_system_command='ninja',
+            targets=default_targets,
+        ),
+    ]
+
     build_recipes = [
         BuildRecipe(
             build_dir=Path('out/gn'),
             title='default',
-            steps=[
-                BuildCommand(
-                    run_if=should_gn_gen_with_args(default_gn_args),
-                    command=default_gn_gen_command,
-                ),
-                BuildCommand(
-                    build_system_command='ninja',
-                    targets=['default'],
-                ),
-            ],
+            steps=default_build_steps,
         ),
     ]