cmake: make the use of split stack configureable

Add a CMake option GOLLVM_USE_SPLIT_STACK, default to true, to
control whether to use split stacks. This applies to both C and
Go compilations for the standard library.

llvm-goc's default is still split stack, regardless of this
setting. One needs to pass -fno-split-stack for user Go code
compilations.

Caveat: when using cgo, cmd/go may still by default pass
-fsplit-stack to the C compiler. Maybe a custom CC or CFLAGS
setting is needed to fully disable split stacks.

Change-Id: I6fd0471135b8061c4a2ff5331a7af263daceea84
Reviewed-on: https://go-review.googlesource.com/c/141678
Reviewed-by: Than McIntosh <thanm@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 86109ec..41ae721 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,6 +22,8 @@
 # So that we can issue "make -jN" cmds in externalproject_add
 processorcount(PROCESSOR_COUNT)
 
+set(GOLLVM_USE_SPLIT_STACK ON CACHE BOOL "use split stack by default")
+
 set(EXTINSTALLDIR ${CMAKE_CURRENT_BINARY_DIR}/external/install)
 set(EXTLIBDIR "${EXTINSTALLDIR}/lib")
 set(EXTINCLUDEDIR "${EXTINSTALLDIR}/include")
diff --git a/cmake/modules/ConfigSetup.cmake b/cmake/modules/ConfigSetup.cmake
index 6763c1a..e6874f6 100644
--- a/cmake/modules/ConfigSetup.cmake
+++ b/cmake/modules/ConfigSetup.cmake
@@ -135,9 +135,11 @@
 # Issue an error if the C compiler doesn't support -fsplit-stack
 # (in theory you can build libgo without it, so I suppose this could
 # be changed to a warning).
-check_c_compiler_flag("-fsplit-stack" C_SUPPORTS_SPLIT_STACK)
-if(NOT C_SUPPORTS_SPLIT_STACK)
-  message(SEND_ERROR "C compiler does not support -fsplit-stack")
+if(GOLLVM_USE_SPLIT_STACK)
+  check_c_compiler_flag("-fsplit-stack" C_SUPPORTS_SPLIT_STACK)
+  if(NOT C_SUPPORTS_SPLIT_STACK)
+    message(SEND_ERROR "C compiler does not support -fsplit-stack")
+  endif()
+  set(USING_SPLIT_STACK 1)
 endif()
-set(USING_SPLIT_STACK 1)
 set(USE_LIBFFI 1)
diff --git a/cmake/modules/LibbacktraceUtils.cmake b/cmake/modules/LibbacktraceUtils.cmake
index 5489fc8..228f61b 100644
--- a/cmake/modules/LibbacktraceUtils.cmake
+++ b/cmake/modules/LibbacktraceUtils.cmake
@@ -61,7 +61,11 @@
     "${libbacktrace_srcroot}/sort.c"
     "${libbacktrace_srcroot}/state.c")
 
-  set(libbacktraceflags "-fsplit-stack")
+  if(GOLLVM_USE_SPLIT_STACK)
+    set(libbacktraceflags "-fsplit-stack")
+  else()
+    set(libbacktraceflags "")
+  endif()
 
   # Object libraries built from libbacktrace sources.
   # Note: this build uses -fsplit-stack, whereas in the gccgo
diff --git a/gotools/CMakeLists.txt b/gotools/CMakeLists.txt
index 1d947dd..b7b9fd8 100644
--- a/gotools/CMakeLists.txt
+++ b/gotools/CMakeLists.txt
@@ -14,6 +14,9 @@
 # Pick up any extra Go compiler flags specified via
 # "cmake -DGOLLVM_EXTRA_GOCFLAGS=..."
 set(gotools_extra_gocflags ${GOLLVM_EXTRA_GOCFLAGS})
+if(NOT GOLLVM_USE_SPLIT_STACK)
+  list(APPEND gotools_extra_gocflags "-fno-split-stack")
+endif()
 
 # Driver for compiling *.go files.
 get_target_property(driverdir llvm-goc RUNTIME_OUTPUT_DIRECTORY)
diff --git a/libgo/CMakeLists.txt b/libgo/CMakeLists.txt
index 9945d9c..821d171 100644
--- a/libgo/CMakeLists.txt
+++ b/libgo/CMakeLists.txt
@@ -58,9 +58,6 @@
 file(COPY "${llvmconfigdir}/config.h" DESTINATION "llvm/Config")
 file(COPY "${llvmconfigdir}/llvm-config.h" DESTINATION "llvm/Config")
 
-set(USING_SPLIT_STACK 1)
-set(USE_LIBFFI 1)
-
 # Generate config.h, included by various C files in libgo/go/runtime
 # and also used to emit gen-sysinfo.go.
 file(MAKE_DIRECTORY "${libgo_binroot}/runtime")
@@ -82,6 +79,9 @@
 # Pick up any extra Go compiler flags specified via
 # "cmake -DGOLLVM_EXTRA_GOCFLAGS=..."
 set(libgo_extra_gocflags ${GOLLVM_EXTRA_GOCFLAGS})
+if(NOT GOLLVM_USE_SPLIT_STACK)
+  list(APPEND libgo_extra_gocflags "-fno-split-stack")
+endif()
 
 # Read in list of all libgo packages
 file(STRINGS "${libgo_srcroot}/libgo-packages.txt" libpackages)
@@ -453,7 +453,10 @@
 endforeach()
 
 # Compiler flags for C files in the runtime.
-set(baseopts "-Wno-zero-length-array -fsplit-stack ")
+set(baseopts "-Wno-zero-length-array ")
+if(GOLLVM_USE_SPLIT_STACK)
+  string(APPEND baseopts "-fsplit-stack ")
+endif()
 foreach(def ${basedefines})
   string(APPEND baseopts "${def} ")
 endforeach()
@@ -500,7 +503,11 @@
 set_target_properties(libgo_shared PROPERTIES
   LIBRARY_OUTPUT_DIRECTORY ${libgo_binroot})
 set_target_properties(libgo_shared PROPERTIES OUTPUT_NAME "go")
-target_link_libraries(libgo_shared PUBLIC "-fsplit-stack;-lpthread;-lm")
+set(linkopts "-lpthread;-lm")
+if(GOLLVM_USE_SPLIT_STACK)
+  string(APPEND linkopts ";-fsplit-stack")
+endif()
+target_link_libraries(libgo_shared PUBLIC "${linkopts}")
 
 # Sources for libgobegin.a
 set(libgobegincfiles