diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -598,6 +598,14 @@ endif() endfunction() +# This macro is a wrapper around llvm_add_library used only in the +# benchmark subproject. It needs to be a macro instead of a function +# so as not to add additional scope and interfere with set() commands +# within llvm_add_library that specify PARENT_SCOPE. +macro(benchmark_add_library) + llvm_add_library(${ARGN}) +endmacro() + function(add_llvm_install_targets target) cmake_parse_arguments(ARG "" "COMPONENT;PREFIX" "DEPENDS" ${ARGN}) if(ARG_COMPONENT) diff --git a/llvm/utils/benchmark/CMakeLists.txt b/llvm/utils/benchmark/CMakeLists.txt --- a/llvm/utils/benchmark/CMakeLists.txt +++ b/llvm/utils/benchmark/CMakeLists.txt @@ -1,22 +1,35 @@ +# If we are not building as a part of another project, e.g., LLVM, +# build Benchmark as an standalone project. +if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR ) cmake_minimum_required (VERSION 2.8.12) -# Tell cmake 3.0+ that it's safe to clear the PROJECT_VERSION variable in the -# call to project() below. -if(POLICY CMP0048) - cmake_policy(SET CMP0048 NEW) -endif() + # Tell cmake 3.0+ that it's safe to clear the PROJECT_VERSION variable in the + # call to project() below. + if(POLICY CMP0048) + cmake_policy(SET CMP0048 NEW) + endif() -project (benchmark) + project (benchmark) -foreach(p - CMP0054 # CMake 3.1 - CMP0056 # export EXE_LINKER_FLAGS to try_run - CMP0057 # Support no if() IN_LIST operator - ) - if(POLICY ${p}) - cmake_policy(SET ${p} NEW) - endif() -endforeach() + foreach(p + CMP0054 # CMake 3.1 + CMP0056 # export EXE_LINKER_FLAGS to try_run + CMP0057 # Support no if() IN_LIST operator + ) + if(POLICY ${p}) + cmake_policy(SET ${p} NEW) + endif() + endforeach() +endif() + +# Define this wrapper unless it is already defined, +# e.g. because benchmark is included as a subproject, +# and the parent project has provided it's own wrapper. +if(NOT COMMAND benchmark_add_library) + macro(benchmark_add_library) + add_library(${ARGN}) + endfunction() +endif() option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON) option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON) diff --git a/llvm/utils/benchmark/src/CMakeLists.txt b/llvm/utils/benchmark/src/CMakeLists.txt --- a/llvm/utils/benchmark/src/CMakeLists.txt +++ b/llvm/utils/benchmark/src/CMakeLists.txt @@ -16,7 +16,7 @@ list(REMOVE_ITEM SOURCE_FILES "${item}") endforeach() -add_library(benchmark ${SOURCE_FILES}) +benchmark_add_library(benchmark ${SOURCE_FILES}) set_target_properties(benchmark PROPERTIES OUTPUT_NAME "benchmark" VERSION ${GENERIC_LIB_VERSION} @@ -45,7 +45,7 @@ endif() # Benchmark main library -add_library(benchmark_main "benchmark_main.cc") +benchmark_add_library(benchmark_main "benchmark_main.cc") set_target_properties(benchmark_main PROPERTIES OUTPUT_NAME "benchmark_main" VERSION ${GENERIC_LIB_VERSION} @@ -77,8 +77,8 @@ "${version_config}" VERSION ${GIT_VERSION} COMPATIBILITY SameMajorVersion ) -configure_file("${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in" "${project_config}" @ONLY) -configure_file("${PROJECT_SOURCE_DIR}/cmake/benchmark.pc.in" "${pkg_config}" @ONLY) +configure_file("${CMAKE_CURRENT_LIST_DIR}/../cmake/Config.cmake.in" "${project_config}" @ONLY) +configure_file("${CMAKE_CURRENT_LIST_DIR}/../cmake/benchmark.pc.in" "${pkg_config}" @ONLY) if (BENCHMARK_ENABLE_INSTALL) # Install target (will install the library to specified CMAKE_INSTALL_PREFIX variable)