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  if(_RULE_IS_INTERFACE)
46    # Generating a header-only library.
47    add_library(${_NAME} INTERFACE)
48    target_include_directories(${_NAME}
49      INTERFACE
50        "${PROJECT_SOURCE_DIR}"
51    )
52    target_link_libraries(${_NAME}
53      INTERFACE
54        ${_RULE_DEPS}
55        ${_RULE_LINKOPTS}
56    )
57    target_compile_definitions(${_NAME}
58      INTERFACE
59        ${_RULE_DEFINES}
60    )
61  else()
62    # Generating a static binary library.
63    add_library(${_NAME} STATIC "")
64    target_sources(${_NAME}
65      PRIVATE
66        ${_RULE_SRCS}
67        ${_RULE_HDRS}
68    )
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()
85endfunction()
86