gollvm: update libgo cmake rules to auto-generate linknames

This patch brings the gollvm cmake rules for libgo into sync with
recent gofrontend changes to the way //go:linkname directives are
emitted for various runtime packages (the corresponding gofrontend
change is in CL 265125). We now run an awk script over selected
packages to emit linknames for things mentioned in gen-sysinfo.go.

Change-Id: Iedf1c8b6d78033aa1e0b0009c6a74ef30aa99e72
Reviewed-on: https://go-review.googlesource.com/c/gollvm/+/267479
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/libgo/CMakeLists.txt b/libgo/CMakeLists.txt
index 08b9f2c..76b2aea 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_sysinfo.go" "sigtab.go")
+set(runtime_extra_go_files "runtime_linknames.go" "runtime_sysinfo.go" "sigtab.go")
 set(cmd_internal_objabi_extra_go_files "objabi.go")
 set(internal_goroot_extra_go_files "zstdpkglist.go")
 set(internal_cpu_extra_go_files "cpugen.go")
@@ -108,7 +108,9 @@
 set(cmd_go_internal_cfg_extra_go_files "zdefaultcc.go")
 set(runtime_internal_sys_extra_go_files "version.go")
 set(go_types_extra_go_files "gccgosizes.go")
-set(syscall_extra_go_files "libcalls.go" "sysinfo.go" "syscall_arch.go")
+set(syscall_extra_go_files "libcalls.go" "sysinfo.go" "syscall_arch.go" "syscall_linknames.go")
+set(os_extra_go_files "os_linknames.go")
+set(os_user_extra_go_files "os_user_linknames.go")
 if(${goos} STREQUAL "linux")
   list(APPEND syscall_extra_go_files "epoll.go")
 endif()
@@ -335,6 +337,33 @@
 mkgcpugen(${goarch} ${gcpugentmp} ${libgo_scriptroot})
 copy_if_different(${gcpugentmp} ${gcpugendotgo})
 
+# Generate *_linknames.go files for various packages. For the syscall
+# package, generate syscall_linknames.go by examining libcalls.go; for
+# { runtime, os, os/user } packages, examine the package source files.
+set(lnpkgs "runtime" "os" "os/user" "syscall")
+set(lnawkfile "${libgo_scriptroot}/mklinknames.awk")
+set(mklinknamessh "${CMAKE_CURRENT_SOURCE_DIR}/mklinknames.sh")
+foreach( pack ${lnpkgs})
+  string(REPLACE "/" "_" ptarget2 "${pack}")
+  string(REPLACE "." "_" ptarget "${ptarget2}")
+  set(pkgofiles "${libgo_binroot}/${ptarget}.lnbasefiles")
+  set(deps "")
+  if(${pack} STREQUAL "syscall")
+    set(deps "${libcallsdotgo}")
+  else()
+    collect_package_inputs(${pack})
+    string(REPLACE ";" " " deps "${basepacksrcs}")
+  endif()
+  file(WRITE "${pkgofiles}" "${deps}\n")
+  set(pklinknamestmp "${libgo_binroot}/tmp-${ptarget}_linknames.go")
+  set(pklinknamesdotgo "${libgo_binroot}/${ptarget}_linknames.go")
+  get_filename_component(pkbase "${pack}" NAME)
+  generate_go_from_script(${pklinknamesdotgo} ${mklinknamessh} ${goos} ${goarch}
+                          ${libgo_binroot} DEP ${awkfile} ${pkgofiles}
+			  ${libcallsdotgo} SCRIPTARGS ${awk} ${lnawkfile}
+			  ${pkgofiles} ${pklinknamestmp} ${pkbase})
+endforeach()
+
 #........................................................................
 
 message(STATUS "Libgo: creating stdlib package targets")
diff --git a/libgo/mklinknames.sh b/libgo/mklinknames.sh
new file mode 100644
index 0000000..ae01c8c
--- /dev/null
+++ b/libgo/mklinknames.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+set -e
+AWK=$1
+AWKFILE=$2
+FILESLIST=$3
+OUTFILE=$4
+PACKAGE=$5
+if [ -z "$AWK" ]; then
+  echo "error: no awk version specified"
+  exit 1
+fi
+if [ ! -x "$AWK" ]; then
+  echo "error: awk $AWK not executable"
+  exit 1
+fi
+if [ -z "$AWKFILE" ]; then
+  echo "error: no awkfile specified"
+  exit 1
+fi
+if [ -z "$FILESLIST" ]; then
+  echo "error: no filelist specified"
+  exit 1
+fi
+if [ ! -r "$FILESLIST" ]; then
+  echo "error: fileslist file $FILESLIST not readable"
+  exit 1
+fi
+if [ -z "$OUTFILE" ]; then
+  echo "error: no outfile specified"
+  exit 1
+fi
+INFILES=`cat $FILESLIST`
+exec ${AWK} -v package=$PACKAGE -f ${AWKFILE} ${INFILES} > ${OUTFILE}