From 03ff187676c405d5b6b4e50ccaf347f7d6f48ffd Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 10 Jul 2018 13:00:17 +0000 Subject: [CMake] Add compiler-rt header files to the list of sources for targets when building with an IDE so that header files show up in the UI. This massively improves the development workflow in IDEs. To implement this a new function `compiler_rt_process_sources(...)` has been added that adds header files to the list of sources when the generator is an IDE. For non-IDE generators (e.g. Ninja/Makefile) no changes are made to the list of source files. The function can be passed a list of headers via the `ADDITIONAL_HEADERS` argument. For each runtime library a list of explicit header files has been added and passed via `ADDITIONAL_HEADERS`. For `tsan` and `sanitizer_common` a list of headers was already present but it was stale and has been updated to reflect the current state of the source tree. The original version of this patch used file globbing (`*.{h,inc,def}`) to find the headers but the approach was changed due to this being a CMake anti-pattern (if the list of headers changes CMake won't automatically re-generate if globbing is used). The LLVM repo contains a similar function named `llvm_process_sources()` but we don't use it here for several reasons: * It depends on the `LLVM_ENABLE_OPTION` cache variable which is not set in standalone compiler-rt builds. * We would have to `include(LLVMProcessSources)` which I'd like to avoid because it would include a bunch of stuff we don't need. Differential Revision: https://reviews.llvm.org/D48422 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@336663 91177308-0d34-0410-b5e6-96231b3b80d8 --- cmake/Modules/AddCompilerRT.cmake | 30 ++++++++++++++++++++++++++---- cmake/Modules/CompilerRTUtils.cmake | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) (limited to 'cmake/Modules') diff --git a/cmake/Modules/AddCompilerRT.cmake b/cmake/Modules/AddCompilerRT.cmake index e7ec4bd28..cd4c704fc 100644 --- a/cmake/Modules/AddCompilerRT.cmake +++ b/cmake/Modules/AddCompilerRT.cmake @@ -32,9 +32,11 @@ endfunction() # SOURCES # CFLAGS # DEFS -# DEPS ) +# DEPS +# ADDITIONAL_HEADERS
) function(add_compiler_rt_object_libraries name) - cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS;DEPS" ${ARGN}) + cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS;DEPS;ADDITIONAL_HEADERS" + ${ARGN}) set(libnames) if(APPLE) foreach(os ${LIB_OS}) @@ -55,6 +57,13 @@ function(add_compiler_rt_object_libraries name) endforeach() endif() + # Add headers to LIB_SOURCES for IDEs + compiler_rt_process_sources(LIB_SOURCES + ${LIB_SOURCES} + ADDITIONAL_HEADERS + ${LIB_ADDITIONAL_HEADERS} + ) + foreach(libname ${libnames}) add_library(${libname} OBJECT ${LIB_SOURCES}) if(LIB_DEPS) @@ -132,7 +141,8 @@ endmacro() # DEFS # LINK_LIBS (only for shared library) # OBJECT_LIBS -# PARENT_TARGET ) +# PARENT_TARGET +# ADDITIONAL_HEADERS
) function(add_compiler_rt_runtime name type) if(NOT type MATCHES "^(STATIC|SHARED)$") message(FATAL_ERROR "type argument must be STATIC or SHARED") @@ -141,7 +151,7 @@ function(add_compiler_rt_runtime name type) cmake_parse_arguments(LIB "" "PARENT_TARGET" - "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;LINK_LIBS;OBJECT_LIBS" + "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;LINK_LIBS;OBJECT_LIBS;ADDITIONAL_HEADERS" ${ARGN}) set(libnames) # Until we support this some other way, build compiler-rt runtime without LTO @@ -152,6 +162,18 @@ function(add_compiler_rt_runtime name type) set(NO_LTO_FLAGS "") endif() + list(LENGTH LIB_SOURCES LIB_SOURCES_LENGTH) + if (${LIB_SOURCES_LENGTH} GREATER 0) + # Add headers to LIB_SOURCES for IDEs. It doesn't make sense to + # do this for a runtime library that only consists of OBJECT + # libraries, so only add the headers when source files are present. + compiler_rt_process_sources(LIB_SOURCES + ${LIB_SOURCES} + ADDITIONAL_HEADERS + ${LIB_ADDITIONAL_HEADERS} + ) + endif() + if(APPLE) foreach(os ${LIB_OS}) # Strip out -msse3 if this isn't macOS. diff --git a/cmake/Modules/CompilerRTUtils.cmake b/cmake/Modules/CompilerRTUtils.cmake index ea332d37d..e5651718f 100644 --- a/cmake/Modules/CompilerRTUtils.cmake +++ b/cmake/Modules/CompilerRTUtils.cmake @@ -344,3 +344,39 @@ function(get_compiler_rt_output_dir arch output_dir) set(${output_dir} ${COMPILER_RT_LIBRARY_OUTPUT_DIR} PARENT_SCOPE) endif() endfunction() + +# compiler_rt_process_sources( +# +# ... +# [ADDITIONAL_HEADERS
...] +# ) +# +# Process the provided sources and write the list of new sources +# into ``. +# +# ADDITIONAL_HEADERS - Adds the supplied header to list of sources for IDEs. +# +# This function is very similar to `llvm_process_sources()` but exists here +# because we need to support standalone builds of compiler-rt. +function(compiler_rt_process_sources OUTPUT_VAR) + cmake_parse_arguments( + ARG + "" + "" + "ADDITIONAL_HEADERS" + ${ARGN} + ) + set(sources ${ARG_UNPARSED_ARGUMENTS}) + set(headers "") + if (XCODE OR MSVC_IDE OR CMAKE_EXTRA_GENERATOR) + # For IDEs we need to tell CMake about header files. + # Otherwise they won't show up in UI. + set(headers ${ARG_ADDITIONAL_HEADERS}) + list(LENGTH headers headers_length) + if (${headers_length} GREATER 0) + set_source_files_properties(${headers} + PROPERTIES HEADER_FILE_ONLY ON) + endif() + endif() + set("${OUTPUT_VAR}" ${sources} ${headers} PARENT_SCOPE) +endfunction() -- cgit v1.2.3