gollvm: driver code reorganization

Split the existing driver code into two subdirectories, one with a
driver utilities library and one with the driver executable itself
(which links against the utilities library). This is intended to make
it possible write unit tests for code in the utilities library. Add a
stub driver utilities unit test (contents to be added later).

Change-Id: I9e3841b85bae10c918552cb3ef70297243ae970d
Reviewed-on: https://go-review.googlesource.com/110616
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e392952..ded072f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -74,19 +74,25 @@
 # Location of bridge source code.
 set(BRIDGE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/bridge)
 
+# Location of driver utilities source code.
+set(DRIVER_UTILS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/driver)
+
 # Subdirectory for the middle layer that translates Backend method
 # calls into LLVM IR.
 add_subdirectory(bridge)
 
-# Subdirectory for compiler driver executable.
+# Subdirectory for compiler driver utilities library.
 add_subdirectory(driver)
 
+# Subdirectory for compiler driver executable.
+add_subdirectory(driver-main)
+
 # Go standard library
 add_subdirectory(libgo)
 
 # Go tools (go, gofmt, etc)
 add_subdirectory(gotools)
 
-# Subdir for unit tests that test the middle layer
+# Subdir for unit tests
 add_subdirectory(unittests)
 
diff --git a/driver-main/CMakeLists.txt b/driver-main/CMakeLists.txt
new file mode 100644
index 0000000..66b4ea1
--- /dev/null
+++ b/driver-main/CMakeLists.txt
@@ -0,0 +1,43 @@
+
+# Rules for building llvm-goc executable.
+
+#----------------------------------------------------------------------
+
+# Libraries that we need to link into llvm-goc
+set(LLVM_LINK_COMPONENTS
+  DriverUtils
+  CppGoFrontEnd
+  ${LLVM_TARGETS_TO_BUILD}
+  CodeGen
+  Core
+  IRReader
+  MC
+  Support
+  Target
+  Object
+  Option
+  Passes
+  Support
+  )
+
+# Include directories needed by llvm-goc
+include_directories(${GOFRONTEND_SOURCE_DIR})
+include_directories(${BRIDGE_SOURCE_DIR})
+include_directories(${DRIVER_UTILS_SOURCE_DIR})
+include_directories("${gollvm_binroot}/driver")
+
+# Gofrontend headers use headers from these packages.
+include_directories(${EXTINSTALLDIR}/include)
+
+# The llvm-goc executable itself
+add_llvm_tool(llvm-goc
+  llvm-goc.cpp)
+
+# Record the fact that llvm-goc depends on these libs
+add_dependencies(llvm-goc libmpfr libmpc libgmp)
+
+# Add in the libraries for the llvm-goc dependencies.
+target_link_libraries(llvm-goc
+  PRIVATE
+  "-L${EXTLIBDIR}" "-lmpc" "-lmpfr" "-lgmp"
+  )
diff --git a/driver/llvm-goc.cpp b/driver-main/llvm-goc.cpp
similarity index 100%
rename from driver/llvm-goc.cpp
rename to driver-main/llvm-goc.cpp
diff --git a/driver/CMakeLists.txt b/driver/CMakeLists.txt
index e9a3c61..c54b7fd 100644
--- a/driver/CMakeLists.txt
+++ b/driver/CMakeLists.txt
@@ -1,5 +1,5 @@
 
-# Rules for building llvm-goc executable.
+# Rules for building driver utils library
 
 #----------------------------------------------------------------------
 
@@ -7,42 +7,17 @@
 tablegen(LLVM GollvmOptions.inc -gen-opt-parser-defs)
 add_public_tablegen_target(GollvmDriverOptions)
 
-# Libraries that we need to link into llvm-goc
-set(LLVM_LINK_COMPONENTS
-  ${LLVM_TARGETS_TO_BUILD}
-  CppGoFrontEnd
-  CodeGen
-  Core
-  IRReader
-  MC
-  Support
-  Target
-  Object
-  Option
-  Passes
-  Support
-  )
-
-# Include directories needed by llvm-goc
+# Include directories needed for this lib.
 include_directories(${GOFRONTEND_SOURCE_DIR})
 include_directories(${BRIDGE_SOURCE_DIR})
 
 # Gofrontend headers use headers from these packages.
 include_directories(${EXTINSTALLDIR}/include)
 
-# The llvm-goc executable itself
-add_llvm_tool(llvm-goc
-  llvm-goc.cpp
+# A library containing driver utility code.
+add_llvm_library(LLVMDriverUtils
   GollvmOptions.cpp
   DEPENDS
   GollvmDriverOptions
   )
 
-# Record the fact that llvm-goc depends on these libs
-add_dependencies(llvm-goc libmpfr libmpc libgmp)
-
-# Add in the libraries for the llvm-goc dependencies.
-target_link_libraries(llvm-goc
-  PRIVATE
-  "-L${EXTLIBDIR}" "-lmpc" "-lmpfr" "-lgmp"
-  )
diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt
index 734d640..fb9c3ab 100644
--- a/unittests/CMakeLists.txt
+++ b/unittests/CMakeLists.txt
@@ -25,4 +25,5 @@
 include_directories(${BRIDGE_SOURCE_DIR})
 include_directories(${GOFRONTEND_SOURCE_DIR})
 
+add_subdirectory(DriverUtils)
 add_subdirectory(BackendCore)
diff --git a/unittests/DriverUtils/CMakeLists.txt b/unittests/DriverUtils/CMakeLists.txt
new file mode 100644
index 0000000..426e09f
--- /dev/null
+++ b/unittests/DriverUtils/CMakeLists.txt
@@ -0,0 +1,24 @@
+
+set(LLVM_LINK_COMPONENTS
+  DriverUtils
+  CodeGen
+  Core
+  Option
+  Support)
+
+set(DriverUtilsTestSources
+  DriverUtilsTests.cpp)
+
+add_gobackend_unittest(DriverUtilsTests
+  ${DriverUtilsTestSources})
+
+set(driver_src_dir "${GOLLVM_SOURCE_DIR}/driver")
+
+include_directories(${unittest_testutils_src})
+include_directories(${driver_src_dir})
+include_directories("${gollvm_binroot}/driver")
+
+target_link_libraries(DriverUtilsTests
+  PRIVATE
+  GoUnitTestUtils)
+
diff --git a/unittests/DriverUtils/DriverUtilsTests.cpp b/unittests/DriverUtils/DriverUtilsTests.cpp
new file mode 100644
index 0000000..6baefd2
--- /dev/null
+++ b/unittests/DriverUtils/DriverUtilsTests.cpp
@@ -0,0 +1,30 @@
+//===---- DriverUtilsTests.cpp --------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <iostream>
+#include <map>
+#include <set>
+
+// nothing here yet (stub)
+
+#include "gtest/gtest.h"
+
+#include "DiffUtils.h"
+
+using namespace goBackendUnitTests;
+
+namespace {
+
+TEST(DriverUtilsTests, Empty) {
+
+  // To be filled in later.
+
+}
+
+} // namespace