xref: /aosp_15_r20/external/ruy/CMakeLists.txt (revision bb86c7ed5fb1b98a7eac808e443a46cc8b90dfc0)
1# Copyright 2021 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15cmake_policy(SET CMP0012 NEW)
16cmake_policy(SET CMP0048 NEW)
17project(ruy CXX)
18cmake_minimum_required(VERSION 3.13)  # Copied from IREE
19set(CMAKE_CXX_STANDARD 14)
20
21include(GNUInstallDirs)
22
23if (PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
24  set(RUY_IS_TOPLEVEL TRUE)
25  set(RUY_MINIMAL_BUILD_DEFAULT_VALUE OFF)
26else()
27  set(RUY_IS_TOPLEVEL FALSE)
28  set(RUY_MINIMAL_BUILD_DEFAULT_VALUE ON)
29endif()
30
31option(RUY_MINIMAL_BUILD "Disable ruy's tests, examples, etc. Build only ruy public libraries." ${RUY_MINIMAL_BUILD_DEFAULT_VALUE})
32if (NOT RUY_MINIMAL_BUILD)
33  enable_testing()
34endif()
35
36option(RUY_PROFILER "Enable ruy's built-in profiler (harms performance)" OFF)
37
38option(RUY_ENABLE_INSTALL "Enable install rule" ${RUY_IS_TOPLEVEL})
39
40include(cmake/ruy_add_all_subdirs.cmake)
41include(cmake/ruy_cc_library.cmake)
42include(cmake/ruy_cc_binary.cmake)
43include(cmake/ruy_cc_test.cmake)
44
45option(RUY_FIND_CPUINFO "Use find_package to find cpuinfo" OFF)
46
47# Skip cpuinfo if it was already generated, which can happen when ruy is
48# a subdirectory in a wider project that already uses cpuinfo.
49if (NOT TARGET cpuinfo::cpuinfo)
50  if (RUY_FIND_CPUINFO)
51    find_package(cpuinfo REQUIRED)
52  else()
53    # Test if the third_party/cpuinfo submodule was checked out before
54    # adding that subdirectory, so we can do more helpful things below in the
55    # else() block when it's not.
56    set(RUY_CPUINFO_CMAKELISTS_FILE "${CMAKE_CURRENT_SOURCE_DIR}/third_party/cpuinfo/CMakeLists.txt")
57    if (EXISTS "${RUY_CPUINFO_CMAKELISTS_FILE}")
58      # Disabling cpuinfo's tests and benchmarks to prevent a copy of its
59      # googletest dependency getting downloaded into a 'deps' directory in the
60      # source tree!
61      set(CPUINFO_BUILD_BENCHMARKS OFF CACHE BOOL "" FORCE)
62      set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE BOOL "" FORCE)
63      set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE BOOL "" FORCE)
64      add_subdirectory("third_party/cpuinfo" EXCLUDE_FROM_ALL)
65    else()
66      # third_party/cpuinfo is not checked out. That could be intentional when
67      # ruy is a subdirectory in a wider project that is already providing
68      # the cpuinfo target. Maybe that wider project's CMakeLists is ordered
69      # in such a way that cpuinfo gets generated after ruy. In that case,
70      # it's helpful that we continue silently. In the worst case if the cpuinfo
71      # target never gets defined, ruy will fail to compile.
72      # On the other hand, if ruy is the top-level project here (not part of a
73      # wider project) then nothing will define the cpuinfo target for us,
74      # so we will definitely fail to compile, so we may as well fail right here.
75      if (RUY_IS_TOPLEVEL)
76        message(FATAL_ERROR "This file does not exist:\n${RUY_CPUINFO_CMAKELISTS_FILE}\n"
77                      "That typically means that the git submodules of the ruy "
78                      "repository haven't been checked out. Try this in the ruy "
79                      "git directory:\n  git submodule update --init")
80      endif()
81    endif()
82  endif()
83endif()
84
85# googletest is only needed for tests. Projects embedding ruy as a subdirectory
86# and not needing to build ruy tests may proceed without a local checkout of
87# third_party/googletest.
88if (NOT RUY_MINIMAL_BUILD
89    AND NOT TARGET gtest
90    AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/googletest/CMakeLists.txt")
91  add_subdirectory("third_party/googletest" EXCLUDE_FROM_ALL)
92endif()
93
94add_subdirectory("ruy")
95
96if (NOT RUY_MINIMAL_BUILD)
97  add_subdirectory("example")
98endif()
99
100if (RUY_ENABLE_INSTALL)
101  install(EXPORT ${PROJECT_NAME}Targets
102    NAMESPACE ${PROJECT_NAME}::
103    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
104  )
105
106  include(CMakePackageConfigHelpers)
107
108  configure_package_config_file(
109    "cmake/${PROJECT_NAME}Config.cmake.in"
110    "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
111    INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
112  )
113
114  install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
115    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
116  )
117endif()
118