fpga_rom: make TfLM linker args conditional on 'tflm' feature

This lets us build hps-factory (which turns off the 'tflm' feature)
without a RISC-V C++ compiler available.

BUG=b:206034105
TEST=scripts/build-fpga-rom
TEST=scripts/build-hps-factory-static

Change-Id: Id79dc28b3629859e6454143557239be668447ffe
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/hps-firmware/+/3452523
Tested-by: Dan Callaghan <dcallagh@chromium.org>
Reviewed-by: David Lattimore <dml@chromium.org>
Commit-Queue: Dan Callaghan <dcallagh@chromium.org>
diff --git a/rust/riscv/.cargo/config.toml b/rust/riscv/.cargo/config.toml
index 7a68170..1e065f1 100644
--- a/rust/riscv/.cargo/config.toml
+++ b/rust/riscv/.cargo/config.toml
@@ -4,7 +4,6 @@
   "-C", "link-arg=-Tlinker_discard.x",
   "-C", "link-arg=-Tmemory.x",
   "-C", "link-arg=-Tlink.x",
-  "-C", "link-arg=-Ttflm_inference.x",
 ]
 
 [build]
diff --git a/rust/riscv/fpga_rom/build.rs b/rust/riscv/fpga_rom/build.rs
index bdd0a45..9297ba4 100644
--- a/rust/riscv/fpga_rom/build.rs
+++ b/rust/riscv/fpga_rom/build.rs
@@ -22,6 +22,9 @@
 
     let out_dir = std::path::PathBuf::from(std::env::var_os("OUT_DIR").expect("OUT_DIR not set"));
 
+    // Also tell linker to search in out_dir, so it finds the linker scripts we generate below.
+    println!("cargo:rustc-link-search={}", out_dir.display());
+
     write_file(
         &out_dir.join("linker_discard.x"),
         "
@@ -36,42 +39,43 @@
     }",
     );
 
-    // The symbol `end` is used by "sbrk" from libnosys. It's unlikely that sbrk
-    // is actually used from any reachable code, but linking fails if we don't
-    // define `end`.
-    //
-    // The arena is a 256KB section region containing the TfLM tensor arena.
-    // Since the standard link script does not know about the arena, We need to
-    // explicitly place arena sections in the arena region.
-    write_file(
-        &out_dir.join("tflm_inference.x"),
-        "
-    PROVIDE( end = . );
-    SECTIONS {
-        .arena (NOLOAD) :
-        {
-                _farena = .;
-                *(.arena)
-                _earena = .;
-        } > arena
-    }
-    ",
-    );
+    if cfg!(feature = "tflm") {
+        // The symbol `end` is used by "sbrk" from libnosys. It's unlikely that sbrk
+        // is actually used from any reachable code, but linking fails if we don't
+        // define `end`.
+        //
+        // The arena is a 256KB section region containing the TfLM tensor arena.
+        // Since the standard link script does not know about the arena, We need to
+        // explicitly place arena sections in the arena region.
+        write_file(
+            &out_dir.join("tflm_inference.x"),
+            "
+        PROVIDE( end = . );
+        SECTIONS {
+            .arena (NOLOAD) :
+            {
+                    _farena = .;
+                    *(.arena)
+                    _earena = .;
+            } > arena
+        }
+        ",
+        );
+        println!("cargo:rustc-link-arg=-Ttflm_inference.x");
 
-    // For tflm_inference.x.
-    println!("cargo:rustc-link-search={}", out_dir.display());
-    // For libc.a, libm.a and libstdc++.a.
-    println!(
-        "cargo:rustc-link-search={}",
-        find_builtin_lib("libc.a")?.display()
-    );
-    // For libgcc.a.
-    println!(
-        "cargo:rustc-link-search={}",
-        find_builtin_lib("libgcc.a")?.display()
-    );
-    // For libtensorflow-microlite.a.
-    println!("cargo:rustc-link-search=../../build/riscv-gcc");
+        // For libc.a, libm.a and libstdc++.a.
+        println!(
+            "cargo:rustc-link-search={}",
+            find_builtin_lib("libc.a")?.display()
+        );
+        // For libgcc.a.
+        println!(
+            "cargo:rustc-link-search={}",
+            find_builtin_lib("libgcc.a")?.display()
+        );
+        // For libtensorflow-microlite.a.
+        println!("cargo:rustc-link-search=../../build/riscv-gcc");
+    }
 
     Ok(())
 }