RELNOTES: Fix FDO_STAMP_MACRO to only be set when fdoBuildStamp is not null.

PiperOrigin-RevId: 184734801
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkstampCompileHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkstampCompileHelper.java
index 9b7ee60..dad73d3 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkstampCompileHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkstampCompileHelper.java
@@ -87,7 +87,6 @@
         ImmutableList.<String>builder()
             .add("GPLATFORM=\"" + cppConfiguration + "\"")
             .add("BUILD_COVERAGE_ENABLED=" + (codeCoverageEnabled ? "1" : "0"))
-            .add(CppConfiguration.FDO_STAMP_MACRO + "=\"" + fdoBuildStamp + "\"")
             // G3_VERSION_INFO and G3_TARGET_NAME are C string literals that normally
             // contain the label of the target being linked.  However, they are set
             // differently when using shared native deps. In that case, a single .so file
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkstampCompileHelperTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkstampCompileHelperTest.java
index 8082a7f..04014e8 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkstampCompileHelperTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkstampCompileHelperTest.java
@@ -79,6 +79,10 @@
     assertThat(Iterables.tryFind(arguments, (arg) -> arg.matches(correctG3BuildTargetPattern)))
         .named("in " + arguments + " flag matching " + correctG3BuildTargetPattern)
         .isPresent();
+    String fdoStampPattern = "-D" + CppConfiguration.FDO_STAMP_MACRO + "=\".*\"";
+    assertThat(Iterables.tryFind(arguments, (arg) -> arg.matches(fdoStampPattern)))
+        .named("in " + arguments + " flag matching " + fdoStampPattern)
+        .isAbsent();
   }
 
   /** Tests that linkstamp compilation applies expected command line options. */
@@ -144,4 +148,31 @@
         (CppCompileAction) getGeneratingAction(compiledLinkstamp);
     assertThat(linkstampCompileAction.getArguments()).contains("-fPIC");
   }
+
+  @Test
+  public void testLinkstampRespectsFdoFromConfiguration() throws Exception {
+    useConfiguration("--fdo_instrument=foo");
+    scratch.file(
+        "x/BUILD",
+        "cc_binary(",
+        "  name = 'foo',",
+        "  deps = ['a'],",
+        ")",
+        "cc_library(",
+        "  name = 'a',",
+        "  srcs = [ 'a.cc' ],",
+        "  linkstamp = 'ls.cc',",
+        ")");
+    ConfiguredTarget target = getConfiguredTarget("//x:foo");
+    Artifact executable = getExecutable(target);
+    CppLinkAction generatingAction = (CppLinkAction) getGeneratingAction(executable);
+    Artifact compiledLinkstamp =
+        ActionsTestUtil.getFirstArtifactEndingWith(generatingAction.getInputs(), "ls.o");
+    assertThat(generatingAction.getInputs()).contains(compiledLinkstamp);
+
+    CppCompileAction linkstampCompileAction =
+        (CppCompileAction) getGeneratingAction(compiledLinkstamp);
+    assertThat(linkstampCompileAction.getArguments())
+        .contains("-D" + CppConfiguration.FDO_STAMP_MACRO + "=\"FDO\"");
+  }
 }