gollvm: revise recipe for setting defaultGOROOT

Relocate the setting of defaultGOROOT, to be consistent with the
corresponding change in gofrontend CL 351313.

Change-Id: I84cc830ed675245513352f215bbb9b6202e18761
Reviewed-on: https://go-review.googlesource.com/c/gollvm/+/351330
Reviewed-by: Cherry Mui <cherryyz@google.com>
Trust: Than McIntosh <thanm@google.com>
diff --git a/cmake/modules/AutoGenGo.cmake b/cmake/modules/AutoGenGo.cmake
index 5872d79..05c7de9 100644
--- a/cmake/modules/AutoGenGo.cmake
+++ b/cmake/modules/AutoGenGo.cmake
@@ -285,7 +285,7 @@
 
 
 #----------------------------------------------------------------------
-# Emit 'objabi.go', containing default settings for various GO* vars.
+# Emit 'objabi.go', containing the stack guard multiplier constant.
 #
 # Unnamed parameters:
 #
@@ -302,6 +302,23 @@
 endfunction()
 
 #----------------------------------------------------------------------
+# Emit 'goroot.go', containing the default GOROOT setting.
+#
+# Unnamed parameters:
+#
+#   * output file to target
+#   * libgo cmake binary directory
+#   * libgo source code root directory
+#
+function(mkgoroot outfile binroot srcroot)
+
+  file(REMOVE ${outfile})
+  file(WRITE ${outfile} "package runtime\n\n")
+  file(APPEND ${outfile} "var defaultGOROOT = \"${GOLLVM_INSTALL_DIR}\"\n")
+
+endfunction()
+
+#----------------------------------------------------------------------
 # Emit 'zstdpkglist.go', containing a map with all of the std Go packages.
 #
 # Unnamed parameters:
diff --git a/libgo/CMakeLists.txt b/libgo/CMakeLists.txt
index b86b1f8..78df6cf 100644
--- a/libgo/CMakeLists.txt
+++ b/libgo/CMakeLists.txt
@@ -100,7 +100,7 @@
 
 # Certain packages need extra Go source files. The convention here is
 # that <pkg>_extra_go_files holds the additional sources for <pkg>.
-set(runtime_extra_go_files "runtime_linknames.go" "runtime_sysinfo.go" "sigtab.go")
+set(runtime_extra_go_files "runtime_linknames.go" "runtime_sysinfo.go" "sigtab.go" "goroot.go")
 set(cmd_internal_objabi_extra_go_files "objabi.go")
 set(internal_buildcfg_extra_go_files "buildcfg.go")
 set(internal_goroot_extra_go_files "zstdpkglist.go")
@@ -229,6 +229,12 @@
 mkobjabi(${objabitmp} ${libgo_binroot} ${libgo_gosrcroot})
 copy_if_different(${objabitmp} ${objabidotgo})
 
+# Rules for goroot.go
+set(gorootdotgo "${libgo_binroot}/goroot.go")
+set(goroottmp "${libgo_binroot}/goroot.go.tmp")
+mkgoroot(${goroottmp} ${libgo_binroot} ${libgo_gosrcroot})
+copy_if_different(${goroottmp} ${gorootdotgo})
+
 # Rules for buildcfg.go
 set(buildcfgdotgo "${libgo_binroot}/buildcfg.go")
 set(buildcfgtmp "${libgo_binroot}/buildcfg.go.tmp")