Add option to pass static link library lines

Some commands a user might want to run via fargo cargo need
the static link libraries passed via RUSTFLAGS, while some
will fail if such are in the flags. Make it an option on
fargo cargo.

Change-Id: I43785f0450078d3efb911fabea2fff061731f8f1
Reviewed-on: https://fuchsia-review.googlesource.com/c/fargo/+/511127
Reviewed-by: Yilong Li <liyl@google.com>
diff --git a/src/command_line.rs b/src/command_line.rs
index eab1eac..59ea71b 100644
--- a/src/command_line.rs
+++ b/src/command_line.rs
@@ -122,6 +122,9 @@
 
 #[derive(Debug, StructOpt)]
 struct Cargo {
+    /// Pass options only needed for linking to cargo via RUSTFLAGS.
+    #[structopt(long)]
+    link: bool,
     subcommand: String,
     cargo_params: Vec<String>,
 }
@@ -432,6 +435,7 @@
             return autotest(
                 &run_cargo_options
                     .release(autotest_opts.release)
+                    .linking(true)
                     .manifest_path(opt.manifest_path)
                     .cmx_path(autotest_opts.cmx_path)
                     .app_dir(&Some(&autotest_opts.app_dir))
@@ -444,7 +448,10 @@
         FargoCommand::Build(build_opts) => {
             let params = build_params(&build_opts);
             build_binary(
-                &run_cargo_options.release(build_opts.release).manifest_path(opt.manifest_path),
+                &run_cargo_options
+                    .release(build_opts.release)
+                    .manifest_path(opt.manifest_path)
+                    .linking(true),
                 &target_options,
                 &params,
             )?;
@@ -463,6 +470,7 @@
                     fargo_manifest,
                     verbose,
                     release: false,
+                    linking: cargo_opts.link,
                     nocapture: false,
                     run_mode: RunMode::Run,
                     story_name: None,
@@ -579,6 +587,7 @@
                 &run_cargo_options
                     .release(run.release)
                     .run_mode(run_mode)
+                    .linking(true)
                     .app_dir(&Some(&run.app_dir))
                     .app_name(&Some(&run.app_name))
                     .manifest_path(opt.manifest_path)
@@ -698,6 +707,7 @@
                     .app_name(&Some(&test_opts.app_name))
                     .run_mode(run_mode)
                     .release(test_opts.release)
+                    .linking(true)
                     .manifest_path(opt.manifest_path)
                     .nocapture(test_opts.nocapture),
                 test_opts.no_run,
diff --git a/src/lib.rs b/src/lib.rs
index 3705a26..e64602f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -263,6 +263,7 @@
     pub fargo_manifest: Manifest,
     pub verbose: bool,
     pub release: bool,
+    pub linking: bool,
     pub run_mode: RunMode,
     pub story_name: Option<String>,
     pub mod_name: Option<String>,
@@ -287,6 +288,10 @@
         Self { release, ..self.clone() }
     }
 
+    pub fn linking(&self, linking: bool) -> RunCargoOptions {
+        Self { linking, ..self.clone() }
+    }
+
     pub fn nocapture(&self, nocapture: bool) -> RunCargoOptions {
         Self { nocapture, ..self.clone() }
     }
@@ -578,11 +583,6 @@
         println!("options = {:?}", options);
     }
 
-    let linking = match subcommand {
-        RUN | TEST | BUILD => true,
-        _ => false,
-    };
-
     let target_triple_uc = format!("{}_fuchsia", triple_cpu).to_uppercase();
 
     let fargo_command = make_fargo_command(
@@ -611,7 +611,7 @@
 
     let runner_env_name = format!("CARGO_TARGET_{}_RUNNER", target_triple_uc);
     let rustflags_env_name = format!("CARGO_TARGET_{}_RUSTFLAGS", target_triple_uc);
-    let rustflags = get_rustflags(options, target_options, &sysroot_as_path, linking)?;
+    let rustflags = get_rustflags(options, target_options, &sysroot_as_path, options.linking)?;
 
     if options.verbose {
         println!("runner_env_name: {:?}", runner_env_name);