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