Skip to content

Commit

Permalink
Determine up front which projects are enabled.
Browse files Browse the repository at this point in the history
Some projects need to add conditional dependencies on other projects.
compiler-rt is already doing this, and I attempted to add this to
debuginfo-tests when I ran into the ordering problem, that you can't
conditionally add a dependency unless that dependency's CMakeLists.txt
has already been run (which would allow you to say if (TARGET foo).

The solution to this seems to be to determine very early on the entire
set of projects which is enabled. This is complicated by the fact that
there are multiple ways to enable projects, and different tree layouts
(e.g. mono-repo, out of -tree, external, etc). This patch attempts to
centralize all of this into one place, and then updates compiler-rt to
demonstrate as a proof of concept how this can simplify code.

Differential Revision: https://reviews.llvm.org/D37637

llvm-svn: 313091
  • Loading branch information
Zachary Turner committed Sep 12, 2017
1 parent ce2da1e commit c7fc939
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
30 changes: 9 additions & 21 deletions compiler-rt/CMakeLists.txt
Expand Up @@ -225,7 +225,7 @@ append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 SANITIZER_COMMON_CFLAGS)
# always respect the optimization flags set by CMAKE_BUILD_TYPE instead.
if (NOT MSVC)

# Build with optimization, unless we're in debug mode.
# Build with optimization, unless we're in debug mode.
if(COMPILER_RT_DEBUG)
list(APPEND SANITIZER_COMMON_CFLAGS -O0)
else()
Expand Down Expand Up @@ -315,28 +315,16 @@ endif()

add_subdirectory(include)

set(COMPILER_RT_LIBCXX_PATH ${LLVM_MAIN_SRC_DIR}/projects/libcxx)
if(EXISTS ${COMPILER_RT_LIBCXX_PATH}/)
set(COMPILER_RT_HAS_LIBCXX_SOURCES TRUE)
else()
set(COMPILER_RT_LIBCXX_PATH ${LLVM_MAIN_SRC_DIR}/../libcxx)
if(EXISTS ${COMPILER_RT_LIBCXX_PATH}/)
set(COMPILER_RT_HAS_LIBCXX_SOURCES TRUE)
else()
set(COMPILER_RT_HAS_LIBCXX_SOURCES FALSE)
endif()
set(COMPILER_RT_HAS_LIBCXX_SOURCES ${LLVM_PROJECT_LIBCXX_ENABLED})
if (LLVM_PROJECT_LIBCXX_ENABLED)
set(COMPILER_RT_LIBCXX_PATH ${LLVM_PROJECT_LIBCXX_SOURCE_DIR})
message("compiler-rt libcxx enabled at ${COMPILER_RT_LIBCXX_PATH}")
endif()

set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/tools/lld)
if(EXISTS ${COMPILER_RT_LLD_PATH}/ AND LLVM_TOOL_LLD_BUILD)
set(COMPILER_RT_HAS_LLD TRUE)
else()
set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/../lld)
if(EXISTS ${COMPILER_RT_LLD_PATH}/ AND LLVM_TOOL_LLD_BUILD)
set(COMPILER_RT_HAS_LLD TRUE)
else()
set(COMPILER_RT_HAS_LLD FALSE)
endif()
set(COMPILER_RT_HAS_LLD ${LLVM_PROJECT_LLD_ENABLED})
if (LLVM_PROJECT_LLD_ENABLED)
set(COMPILER_RT_LLD_PATH ${LLVM_PROJECT_LLD_SOURCE_DIR})
message("compiler-rt lld enabled at ${COMPILER_RT_LLD_PATH}")
endif()
pythonize_bool(COMPILER_RT_HAS_LLD)

Expand Down
19 changes: 19 additions & 0 deletions llvm/CMakeLists.txt
Expand Up @@ -823,6 +823,25 @@ endif()
include(AddLLVM)
include(TableGen)


# Find all subprojects which are either enabled in a side-by-side layout, or
# cloned into a non-side-by-side layout. Do this before adding any
# subdirectories so that any project can check for the existence of any other
# project. Each call takes priority over the next call, so any project which
# is enabled via LLVM_ENABLE_PROJECTS will not have its location or enabled
# status overwritten via a subsequent call.

# First look for all projects explicitly enabled at the root.
find_llvm_enabled_projects("${LLVM_SOURCE_DIR}/.." "${LLVM_ENABLE_PROJECTS}")

# Then pick up any projects explicitly cloned into llvm/projects or llvm/runtimes
find_llvm_enabled_projects("${LLVM_SOURCE_DIR}/runtimes")
find_llvm_enabled_projects("${LLVM_SOURCE_DIR}/projects")

# Then pick up a few specific projects which can be explicit cloned into llvm/tools
find_llvm_enabled_projects("${LLVM_SOURCE_DIR}/tools" "clang;lldb;lld")


if( MINGW )
# People report that -O3 is unreliable on MinGW. The traditional
# build also uses -O2 for that reason:
Expand Down
35 changes: 33 additions & 2 deletions llvm/cmake/modules/AddLLVM.cmake
Expand Up @@ -687,7 +687,7 @@ macro(add_llvm_executable name)
# it forces Xcode to properly link the static library.
list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp")
endif()

if( EXCLUDE_FROM_ALL )
add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
else()
Expand Down Expand Up @@ -920,6 +920,37 @@ function(canonicalize_tool_name name output)
set(${output} "${nameUPPER}" PARENT_SCOPE)
endfunction(canonicalize_tool_name)

macro(find_llvm_enabled_projects base_dir)
set(specific_project_list "${ARGN}")
if("${specific_project_list}" STREQUAL "")
file(GLOB entries "${base_dir}/*")
set(list_of_project_dirs "")
foreach(entry ${entries})
if(IS_DIRECTORY ${entry} AND EXISTS ${entry}/CMakeLists.txt)
get_filename_component(filename "${entry}" NAME)
list(APPEND specific_project_list "${filename}")
endif()
endforeach(entry)
endif()

foreach(proj ${specific_project_list})
canonicalize_tool_name(${proj} projUPPER)

if (${LLVM_PROJECT_${projUPPER}_ENABLED})
if (EXISTS "${base_dir}/${proj}")
message(WARNING "Project ${projUPPER} in ${base_dir}/${proj} previously found in ${LLVM_PROJECT_${projUPPER}_SOURCE_DIR}")
endif()
continue()
elseif(EXISTS "${LLVM_EXTERNAL_${projUPPER}_SOURCE_DIR}")
set(LLVM_PROJECT_${projUPPER}_ENABLED ON)
set(LLVM_PROJECT_${projUPPER}_SOURCE_DIR "${LLVM_EXTERNAL_${projUPPER}_SOURCE_DIR}")
elseif(EXISTS "${base_dir}/${proj}")
set(LLVM_PROJECT_${projUPPER}_ENABLED ON)
set(LLVM_PROJECT_${projUPPER}_SOURCE_DIR "${base_dir}/${proj}")
endif()
endforeach()
endmacro()

# Custom add_subdirectory wrapper
# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional
# path if it differs from the name.
Expand Down Expand Up @@ -1350,7 +1381,7 @@ function(add_llvm_tool_symlink link_name target)
# magic. First we grab one of the types, and a type-specific path. Then from
# the type-specific path we find the last occurrence of the type in the path,
# and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type
# agnostic again.
# agnostic again.
if(NOT ARG_OUTPUT_DIR)
# If you're not overriding the OUTPUT_DIR, we can make the link relative in
# the same directory.
Expand Down

0 comments on commit c7fc939

Please sign in to comment.