xref: /aosp_15_r20/external/ruy/cmake/ruy_cc_library.cmake (revision bb86c7ed5fb1b98a7eac808e443a46cc8b90dfc0)
1# Copyright 2019 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
15# Forked from IREE's iree_cc_library.cmake.
16
17include(CMakeParseArguments)
18include(cmake/ruy_include_directories.cmake)
19
20# ruy_cc_library()
21#
22# CMake function to imitate Bazel's cc_library rule.
23function(ruy_cc_library)
24  cmake_parse_arguments(
25    _RULE
26    "PUBLIC;TESTONLY"
27    "NAME"
28    "HDRS;SRCS;COPTS;DEFINES;LINKOPTS;DEPS"
29    ${ARGN}
30  )
31
32  if(_RULE_TESTONLY AND RUY_MINIMAL_BUILD)
33    return()
34  endif()
35
36  set(_NAME "${_RULE_NAME}")
37
38  # Check if this is a header-only library.
39  if("${_RULE_SRCS}" STREQUAL "")
40    set(_RULE_IS_INTERFACE 1)
41  else()
42    set(_RULE_IS_INTERFACE 0)
43  endif()
44
45  file(RELATIVE_PATH _SUBDIR ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_LIST_DIR})
46
47  if(_RULE_IS_INTERFACE)
48    # Generating a header-only library.
49    add_library(${_NAME} INTERFACE)
50    set_target_properties(${_NAME} PROPERTIES PUBLIC_HEADER "${_RULE_HDRS}")
51    target_include_directories(${_NAME}
52      INTERFACE
53        "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"
54        "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
55    )
56    target_link_libraries(${_NAME}
57      INTERFACE
58        ${_RULE_DEPS}
59        ${_RULE_LINKOPTS}
60    )
61    target_compile_definitions(${_NAME}
62      INTERFACE
63        ${_RULE_DEFINES}
64    )
65  else()
66    # Generating a static binary library.
67    add_library(${_NAME} STATIC ${_RULE_SRCS} ${_RULE_HDRS})
68    set_target_properties(${_NAME} PROPERTIES PUBLIC_HEADER "${_RULE_HDRS}")
69    ruy_include_directories(${_NAME} "${_RULE_DEPS}")
70    target_compile_options(${_NAME}
71      PRIVATE
72        ${_RULE_COPTS}
73    )
74    target_link_libraries(${_NAME}
75      PUBLIC
76        ${_RULE_DEPS}
77      PRIVATE
78        ${_RULE_LINKOPTS}
79    )
80    target_compile_definitions(${_NAME}
81      PUBLIC
82        ${_RULE_DEFINES}
83    )
84  endif()
85
86  add_library(${PROJECT_NAME}::${_NAME} ALIAS ${_NAME})
87
88  if(NOT _RULE_TESTONLY)
89    install(
90      TARGETS ${_NAME}
91      EXPORT ruyTargets
92      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
93      PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${_SUBDIR}
94    )
95  endif()
96endfunction()
97