diff options
author | Petr Hosek <phosek@google.com> | 2022-05-25 20:37:54 +0300 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-05-25 20:38:30 +0300 |
commit | cd19e0e1b0fd24cd3d785beff1b50583e572dae0 (patch) | |
tree | 9b151216f3063a9ae270ba1524b197822e4484fa | |
parent | efd639c7848791506e4e4c868f66863422730310 (diff) |
Support install rules in the CMake build
This allows using ruy from other projects through find_package.
Closes https://github.com/google/ruy/pull/309
COPYBARA_INTEGRATE_REVIEW=https://github.com/google/ruy/pull/309 from petrhosek:cmake-install b6168af3fe06ec794f5b5253b5cc626ffac21916
PiperOrigin-RevId: 450959295
-rw-r--r-- | CMakeLists.txt | 85 | ||||
-rwxr-xr-x | cmake/bazel_to_cmake.py | 2 | ||||
-rw-r--r-- | cmake/ruyConfig.cmake.in | 9 | ||||
-rw-r--r-- | cmake/ruy_cc_library.cmake | 23 | ||||
-rw-r--r-- | cmake/ruy_include_directories.cmake | 20 | ||||
-rw-r--r-- | ruy/CMakeLists.txt | 10 |
6 files changed, 93 insertions, 56 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 98d480d..e085ed0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,45 +35,57 @@ endif() option(RUY_PROFILER "Enable ruy's built-in profiler (harms performance)" OFF) +option(RUY_ENABLE_INSTALL "Enable install rule" ${RUY_IS_TOPLEVEL}) + include(cmake/ruy_add_all_subdirs.cmake) include(cmake/ruy_cc_library.cmake) include(cmake/ruy_cc_binary.cmake) include(cmake/ruy_cc_test.cmake) +option(RUY_FIND_CPUINFO "Use find_package to find cpuinfo" OFF) + # Skip cpuinfo if it was already generated, which can happen when ruy is # a subdirectory in a wider project that already uses cpuinfo. -if (NOT TARGET cpuinfo) - # Test if the third_party/cpuinfo submodule was checked out before - # adding that subdirectory, so we can do more helpful things below in the - # else() block when it's not. - set(RUY_CPUINFO_CMAKELISTS_FILE "${CMAKE_CURRENT_SOURCE_DIR}/third_party/cpuinfo/CMakeLists.txt") - if (EXISTS "${RUY_CPUINFO_CMAKELISTS_FILE}") - # Disabling cpuinfo's tests and benchmarks to prevent a copy of its - # googletest dependency getting downloaded into a 'deps' directory in the - # source tree! - set(CPUINFO_BUILD_BENCHMARKS OFF CACHE BOOL "" FORCE) - set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE BOOL "" FORCE) - set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE BOOL "" FORCE) - add_subdirectory("third_party/cpuinfo" EXCLUDE_FROM_ALL) +if (NOT TARGET cpuinfo AND NOT TARGET cpuinfo::cpuinfo) + if (RUY_FIND_CPUINFO) + find_package(cpuinfo REQUIRED) else() - # third_party/cpuinfo is not checked out. That could be intentional when - # ruy is a subdirectory in a wider project that is already providing - # the cpuinfo target. Maybe that wider project's CMakeLists is ordered - # in such a way that cpuinfo gets generated after ruy. In that case, - # it's helpful that we continue silently. In the worst case if the cpuinfo - # target never gets defined, ruy will fail to compile. - # On the other hand, if ruy is the top-level project here (not part of a - # wider project) then nothing will define the cpuinfo target for us, - # so we will definitely fail to compile, so we may as well fail right here. - if (RUY_IS_TOPLEVEL) - message(FATAL_ERROR "This file does not exist:\n${RUY_CPUINFO_CMAKELISTS_FILE}\n" - "That typically means that the git submodules of the ruy " - "repository haven't been checked out. Try this in the ruy " - "git directory:\n git submodule update --init") + # Test if the third_party/cpuinfo submodule was checked out before + # adding that subdirectory, so we can do more helpful things below in the + # else() block when it's not. + set(RUY_CPUINFO_CMAKELISTS_FILE "${CMAKE_CURRENT_SOURCE_DIR}/third_party/cpuinfo/CMakeLists.txt") + if (EXISTS "${RUY_CPUINFO_CMAKELISTS_FILE}") + # Disabling cpuinfo's tests and benchmarks to prevent a copy of its + # googletest dependency getting downloaded into a 'deps' directory in the + # source tree! + set(CPUINFO_BUILD_BENCHMARKS OFF CACHE BOOL "" FORCE) + set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE BOOL "" FORCE) + set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE BOOL "" FORCE) + add_subdirectory("third_party/cpuinfo" EXCLUDE_FROM_ALL) + else() + # third_party/cpuinfo is not checked out. That could be intentional when + # ruy is a subdirectory in a wider project that is already providing + # the cpuinfo target. Maybe that wider project's CMakeLists is ordered + # in such a way that cpuinfo gets generated after ruy. In that case, + # it's helpful that we continue silently. In the worst case if the cpuinfo + # target never gets defined, ruy will fail to compile. + # On the other hand, if ruy is the top-level project here (not part of a + # wider project) then nothing will define the cpuinfo target for us, + # so we will definitely fail to compile, so we may as well fail right here. + if (RUY_IS_TOPLEVEL) + message(FATAL_ERROR "This file does not exist:\n${RUY_CPUINFO_CMAKELISTS_FILE}\n" + "That typically means that the git submodules of the ruy " + "repository haven't been checked out. Try this in the ruy " + "git directory:\n git submodule update --init") + endif() endif() endif() endif() +if (TARGET cpuinfo AND NOT TARGET cpuinfo::cpuinfo) + add_library(cpuinfo::cpuinfo ALIAS cpuinfo) +endif() + # googletest is only needed for tests. Projects embedding ruy as a subdirectory # and not needing to build ruy tests may proceed without a local checkout of # third_party/googletest. @@ -88,3 +100,22 @@ add_subdirectory("ruy") if (NOT RUY_MINIMAL_BUILD) add_subdirectory("example") endif() + +if (RUY_ENABLE_INSTALL) + install(EXPORT ${PROJECT_NAME}Targets + NAMESPACE ${PROJECT_NAME}:: + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" + ) + + include(CMakePackageConfigHelpers) + + configure_package_config_file( + "cmake/${PROJECT_NAME}Config.cmake.in" + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" + ) + + install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" + ) +endif() diff --git a/cmake/bazel_to_cmake.py b/cmake/bazel_to_cmake.py index 8f972ba..caf9cbf 100755 --- a/cmake/bazel_to_cmake.py +++ b/cmake/bazel_to_cmake.py @@ -49,7 +49,7 @@ replacements = [ ['selects.config_setting_group', 'config_setting_group'], ['@com_google_googletest//:gtest', 'gtest'], ['@com_google_googletest//:gtest_main', 'gtest_main'], - ['@cpuinfo', 'cpuinfo'], + ['@cpuinfo', 'cpuinfo::cpuinfo'], ] diff --git a/cmake/ruyConfig.cmake.in b/cmake/ruyConfig.cmake.in new file mode 100644 index 0000000..0f3a4f1 --- /dev/null +++ b/cmake/ruyConfig.cmake.in @@ -0,0 +1,9 @@ +# ruy CMake configuration file. + +include(CMakeFindDependencyMacro) + +find_dependency(cpuinfo) + +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") diff --git a/cmake/ruy_cc_library.cmake b/cmake/ruy_cc_library.cmake index 38accc5..999842f 100644 --- a/cmake/ruy_cc_library.cmake +++ b/cmake/ruy_cc_library.cmake @@ -42,12 +42,16 @@ function(ruy_cc_library) set(_RULE_IS_INTERFACE 0) endif() + file(RELATIVE_PATH _SUBDIR ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_LIST_DIR}) + if(_RULE_IS_INTERFACE) # Generating a header-only library. add_library(${_NAME} INTERFACE) + set_target_properties(${_NAME} PROPERTIES PUBLIC_HEADER "${_RULE_HDRS}") target_include_directories(${_NAME} INTERFACE - "${PROJECT_SOURCE_DIR}" + "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>" + "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" ) target_link_libraries(${_NAME} INTERFACE @@ -60,12 +64,8 @@ function(ruy_cc_library) ) else() # Generating a static binary library. - add_library(${_NAME} STATIC "") - target_sources(${_NAME} - PRIVATE - ${_RULE_SRCS} - ${_RULE_HDRS} - ) + add_library(${_NAME} STATIC ${_RULE_SRCS} ${_RULE_HDRS}) + set_target_properties(${_NAME} PROPERTIES PUBLIC_HEADER "${_RULE_HDRS}") ruy_include_directories(${_NAME} "${_RULE_DEPS}") target_compile_options(${_NAME} PRIVATE @@ -82,4 +82,13 @@ function(ruy_cc_library) ${_RULE_DEFINES} ) endif() + + if(NOT _RULE_TESTONLY) + install( + TARGETS ${_NAME} + EXPORT ruyTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${_SUBDIR} + ) + endif() endfunction() diff --git a/cmake/ruy_include_directories.cmake b/cmake/ruy_include_directories.cmake index e9b50a9..a90ab61 100644 --- a/cmake/ruy_include_directories.cmake +++ b/cmake/ruy_include_directories.cmake @@ -14,20 +14,8 @@ function(ruy_include_directories NAME DEPS) target_include_directories(${NAME} - PUBLIC - "${PROJECT_SOURCE_DIR}" + PUBLIC + "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>" + "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" ) - if (cpuinfo IN_LIST DEPS) - target_include_directories(${NAME} - PRIVATE - "${PROJECT_SOURCE_DIR}/third_party/cpuinfo/include" - ) - endif() - if ((gtest IN_LIST DEPS) OR - (gtest_main IN_LIST DEPS)) - target_include_directories(${NAME} - PRIVATE - "${PROJECT_SOURCE_DIR}/third_party/googletest/googletest" - ) - endif() -endfunction()
\ No newline at end of file +endfunction() diff --git a/ruy/CMakeLists.txt b/ruy/CMakeLists.txt index cab0091..67f0f54 100644 --- a/ruy/CMakeLists.txt +++ b/ruy/CMakeLists.txt @@ -410,13 +410,13 @@ else() endif() if(CMAKE_SYSTEM_PROCESSOR STREQUAL ppc64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL ppc64le) - set(ruy_6_cpuinfo "") + set(ruy_6_cpuinfo_cpuinfo "") elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL s390 OR CMAKE_SYSTEM_PROCESSOR STREQUAL s390x) - set(ruy_6_cpuinfo "") + set(ruy_6_cpuinfo_cpuinfo "") elseif(CMAKE_SYSTEM_NAME STREQUAL Fuchsia) - set(ruy_6_cpuinfo "") + set(ruy_6_cpuinfo_cpuinfo "") else() - set(ruy_6_cpuinfo "cpuinfo") + set(ruy_6_cpuinfo_cpuinfo "cpuinfo::cpuinfo") endif() ruy_cc_library( @@ -436,7 +436,7 @@ ruy_cc_library( ruy_platform ruy_check_macros ruy_cpu_cache_params - ${ruy_6_cpuinfo} + ${ruy_6_cpuinfo_cpuinfo} ) ruy_cc_library( |