[PATCH] D51561: [CMake] Add support for unittests that have input files

Zachary Turner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 31 15:35:49 PDT 2018


zturner created this revision.
zturner added reviewers: beanz, chandlerc, rnk.
Herald added subscribers: hiraditya, mgorny.

This patch allows a target to call a function `add_llvm_unittest_inputs()` with a list of files.  This list of files will be copied from the source directory to the binary directory.  Then, it adds a function to SupportHelpers that computes the input directory folder, which a unittest can then use to construct a path to the inputs folder.

LLDB has had this for a while, but now I need it in LLVM.  Since we have more than one user now, having this in LLVM makes sense, and as a followup we can port LLDB's uses over to the new LLVM code.


https://reviews.llvm.org/D51561

Files:
  llvm/cmake/modules/AddLLVM.cmake
  llvm/include/llvm/Testing/Support/SupportHelpers.h
  llvm/lib/Testing/Support/CMakeLists.txt
  llvm/lib/Testing/Support/SupportHelpers.cpp


Index: llvm/lib/Testing/Support/SupportHelpers.cpp
===================================================================
--- /dev/null
+++ llvm/lib/Testing/Support/SupportHelpers.cpp
@@ -0,0 +1,19 @@
+
+#include "llvm/Testing/Support/SupportHelpers.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+
+using namespace llvm;
+using namespace llvm::unittest;
+
+extern const char *TestMainArgv0;
+
+SmallString<128> llvm::unittest::getInputFileDirectory() {
+  llvm::SmallString<128> Result = llvm::sys::path::parent_path(TestMainArgv0);
+  llvm::sys::fs::make_absolute(Result);
+  llvm::sys::path::append(Result, "Inputs");
+  return Result;
+}
Index: llvm/lib/Testing/Support/CMakeLists.txt
===================================================================
--- llvm/lib/Testing/Support/CMakeLists.txt
+++ llvm/lib/Testing/Support/CMakeLists.txt
@@ -3,6 +3,7 @@
 
 add_llvm_library(LLVMTestingSupport
   Error.cpp
+  SupportHelpers.cpp
 
   BUILDTREE_ONLY
   
Index: llvm/include/llvm/Testing/Support/SupportHelpers.h
===================================================================
--- llvm/include/llvm/Testing/Support/SupportHelpers.h
+++ llvm/include/llvm/Testing/Support/SupportHelpers.h
@@ -10,10 +10,12 @@
 #ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
 #define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H
 
-#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/Support/Error.h"
 #include "gtest/gtest-printers.h"
 
+#include <string>
+
 namespace llvm {
 namespace detail {
 struct ErrorHolder {
@@ -52,6 +54,10 @@
   }
 }
 } // namespace detail
+
+namespace unittest {
+SmallString<128> getInputFileDirectory();
+}
 } // namespace llvm
 
 #endif
Index: llvm/cmake/modules/AddLLVM.cmake
===================================================================
--- llvm/cmake/modules/AddLLVM.cmake
+++ llvm/cmake/modules/AddLLVM.cmake
@@ -1120,6 +1120,24 @@
   endif ()
 endfunction()
 
+function(add_llvm_unittest_inputs test_name inputs)
+  add_custom_command(
+    TARGET ${test_name}
+    POST_BUILD
+    COMMAND "${CMAKE_COMMAND}" -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/Inputs
+    COMMENT "Making Inputs directory"
+    )
+
+  foreach (INPUT ${inputs})
+    add_custom_command(
+      TARGET ${test_name}
+      POST_BUILD
+      COMMAND "${CMAKE_COMMAND}" -E copy ${CMAKE_CURRENT_SOURCE_DIR}/Inputs/${INPUT} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/Inputs
+      COMMENT "Copying ${INPUT} to binary directory.")
+  endforeach()
+endfunction()
+
+
 # Generic support for adding a benchmark.
 function(add_benchmark benchmark_name)
   if( NOT LLVM_BUILD_BENCHMARKS )


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51561.163601.patch
Type: text/x-patch
Size: 2723 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180831/734dda2f/attachment.bin>


More information about the llvm-commits mailing list