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_test.cmake.
16
17include(CMakeParseArguments)
18include(cmake/ruy_include_directories.cmake)
19
20# ruy_cc_test()
21#
22# CMake function to imitate Bazel's cc_test rule.
23function(ruy_cc_test)
24  cmake_parse_arguments(
25    _RULE
26    ""
27    "NAME"
28    "SRCS;COPTS;LINKOPTS;DEPS;TAGS"
29    ${ARGN}
30  )
31
32  if(RUY_MINIMAL_BUILD)
33    return()
34  endif()
35
36  set(_NAME "${_RULE_NAME}")
37
38  add_executable(${_NAME} "")
39  target_sources(${_NAME}
40    PRIVATE
41      ${_RULE_SRCS}
42  )
43  set_target_properties(${_NAME} PROPERTIES OUTPUT_NAME "${_RULE_NAME}")
44  ruy_include_directories(${_NAME} "${_RULE_DEPS}")
45  target_compile_options(${_NAME}
46    PRIVATE
47      ${_RULE_COPTS}
48  )
49  target_link_options(${_NAME}
50    PRIVATE
51      ${_RULE_LINKOPTS}
52  )
53  target_link_libraries(${_NAME}
54    PUBLIC
55      ${_RULE_DEPS}
56  )
57  if(ANDROID)
58    add_test(
59      NAME
60        ${_NAME}
61      COMMAND
62        "${CMAKE_SOURCE_DIR}/cmake/run_android_test.sh"
63        "$<TARGET_FILE:${_NAME}>"
64    )
65  else()
66    add_test(
67        NAME
68          ${_NAME}
69        COMMAND
70          "$<TARGET_FILE:${_NAME}>"
71        )
72  endif()
73  if (_RULE_TAGS)
74    set_property(TEST ${_NAME} PROPERTY LABELS ${_RULE_TAGS})
75  endif()
76endfunction()
77