[CMake] Refactor subdirectory inclusion code to take a project name.
authorChris Bieneman <beanz@apple.com>
Tue, 20 Oct 2015 16:42:58 +0000 (16:42 +0000)
committerChris Bieneman <beanz@apple.com>
Tue, 20 Oct 2015 16:42:58 +0000 (16:42 +0000)
Summary:
This refactoring makes some of the code used to control including subdirectories parameterized so it can be re-used elsewhere.

Specifically I want to re-use this code in clang to be able to turn off specific tool subdirectories.

Reviewers: chapuni, filcab, bogner, Bigcheese

Subscribers: emaste, llvm-commits

Differential Revision: http://reviews.llvm.org/D13783

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@250835 91177308-0d34-0410-b5e6-96231b3b80d8

cmake/modules/AddLLVM.cmake

index 752e74a00e4f2bf4654e08b64f9e82199d31f45c..84f3216d6bc45cd73ef59827da28472f056c6a16 100644 (file)
@@ -730,12 +730,10 @@ function(canonicalize_tool_name name output)
   set(${output} "${nameUPPER}" PARENT_SCOPE)
 endfunction(canonicalize_tool_name)
 
   set(${output} "${nameUPPER}" PARENT_SCOPE)
 endfunction(canonicalize_tool_name)
 
-# Add external project that may want to be built as part of llvm such as Clang,
-# lld, and Polly. This adds two options. One for the source directory of the
-# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
-# enable or disable building it with everything else.
-# Additional parameter can be specified as the name of directory.
-macro(add_llvm_external_project name)
+# Custom add_subdirectory wrapper
+# Takes in a project name (i.e. LLVM), the the subdirectory name, and an
+# and an optional path if it differs from the name.
+macro(add_llvm_subdirectory project type name)
   set(add_llvm_external_dir "${ARGN}")
   if("${add_llvm_external_dir}" STREQUAL "")
     set(add_llvm_external_dir ${name})
   set(add_llvm_external_dir "${ARGN}")
   if("${add_llvm_external_dir}" STREQUAL "")
     set(add_llvm_external_dir ${name})
@@ -743,39 +741,48 @@ macro(add_llvm_external_project name)
   canonicalize_tool_name(${name} nameUPPER)
   if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
     # Treat it as in-tree subproject.
   canonicalize_tool_name(${name} nameUPPER)
   if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt)
     # Treat it as in-tree subproject.
-    option(LLVM_TOOL_${nameUPPER}_BUILD
-           "Whether to build ${name} as part of LLVM" On)
-    mark_as_advanced(LLVM_TOOL_${name}_BUILD)
-    if(LLVM_TOOL_${nameUPPER}_BUILD)
+    option(${project}_${type}_${nameUPPER}_BUILD
+           "Whether to build ${name} as part of ${project}" On)
+    mark_as_advanced(${project}_${type}_${name}_BUILD)
+    if(${project}_${type}_${nameUPPER}_BUILD)
       add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
       # Don't process it in add_llvm_implicit_projects().
       add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir})
       # Don't process it in add_llvm_implicit_projects().
-      set(LLVM_TOOL_${nameUPPER}_BUILD OFF)
+      set(${project}_${type}_${nameUPPER}_BUILD OFF)
     endif()
   else()
     set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
       "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
       CACHE PATH "Path to ${name} source directory")
     endif()
   else()
     set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR
       "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}"
       CACHE PATH "Path to ${name} source directory")
-    set(LLVM_TOOL_${nameUPPER}_BUILD_DEFAULT ON)
+    set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT ON)
     if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
     if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
-      set(LLVM_TOOL_${nameUPPER}_BUILD_DEFAULT OFF)
+      set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
     endif()
     if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
     endif()
     if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF")
-      set(LLVM_TOOL_${nameUPPER}_BUILD_DEFAULT OFF)
+      set(${project}_${type}_${nameUPPER}_BUILD_DEFAULT OFF)
     endif()
     endif()
-    option(LLVM_TOOL_${nameUPPER}_BUILD
+    option(${project}_${type}_${nameUPPER}_BUILD
       "Whether to build ${name} as part of LLVM"
       "Whether to build ${name} as part of LLVM"
-      ${LLVM_TOOL_${nameUPPER}_BUILD_DEFAULT})
-    if (LLVM_TOOL_${nameUPPER}_BUILD)
+      ${${project}_${type}_${nameUPPER}_BUILD_DEFAULT})
+    if (${project}_${type}_${nameUPPER}_BUILD)
       if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
         add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
       elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
         message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
       endif()
       # FIXME: It'd be redundant.
       if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR})
         add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
       elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "")
         message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}")
       endif()
       # FIXME: It'd be redundant.
-      set(LLVM_TOOL_${nameUPPER}_BUILD Off)
+      set(${project}_${type}_${nameUPPER}_BUILD Off)
     endif()
   endif()
     endif()
   endif()
-endmacro(add_llvm_external_project)
+endmacro()
+
+# Add external project that may want to be built as part of llvm such as Clang,
+# lld, and Polly. This adds two options. One for the source directory of the
+# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
+# enable or disable building it with everything else.
+# Additional parameter can be specified as the name of directory.
+macro(add_llvm_external_project name)
+  add_llvm_subdirectory(LLVM TOOL ${name})
+endmacro()
 
 macro(add_llvm_tool_subdirectory name)
   add_llvm_external_project(${name})
 
 macro(add_llvm_tool_subdirectory name)
   add_llvm_external_project(${name})
@@ -791,16 +798,20 @@ function(get_project_name_from_src_var var output)
   endif()
 endfunction()
 
   endif()
 endfunction()
 
-function(create_llvm_tool_options)
+function(create_subdirectory_options project type)
   file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
   foreach(dir ${sub-dirs})
     if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
       canonicalize_tool_name(${dir} name)
   file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
   foreach(dir ${sub-dirs})
     if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt")
       canonicalize_tool_name(${dir} name)
-      option(LLVM_TOOL_${name}_BUILD
-           "Whether to build ${name} as part of LLVM" On)
-      mark_as_advanced(LLVM_TOOL_${name}_BUILD)
+      option(${project}_${type}_${name}_BUILD
+           "Whether to build ${name} as part of ${project}" On)
+      mark_as_advanced(${project}_${type}_${name}_BUILD)
     endif()
   endforeach()
     endif()
   endforeach()
+endfunction(create_subdirectory_options)
+
+function(create_llvm_tool_options)
+  create_subdirectory_options(LLVM TOOL)
 endfunction(create_llvm_tool_options)
 
 function(add_llvm_implicit_projects)
 endfunction(create_llvm_tool_options)
 
 function(add_llvm_implicit_projects)