|
| 1 | +# Copyright 2018, OpenCensus Authors |
| 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 | +# http://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 | +# Prepends opencensus_ to all deps that aren't in a :: namespace. |
| 16 | +function(prepend_opencensus OUT DEPS) |
| 17 | + set(_DEPS "") |
| 18 | + foreach(dep ${DEPS}) |
| 19 | + if("${dep}" MATCHES "::") |
| 20 | + list(APPEND _DEPS "${dep}") |
| 21 | + else() |
| 22 | + list(APPEND _DEPS "opencensus_${dep}") |
| 23 | + endif() |
| 24 | + endforeach() |
| 25 | + set(${OUT} ${_DEPS} PARENT_SCOPE) |
| 26 | +endfunction() |
| 27 | + |
| 28 | +# Helper function like bazel's cc_test. Usage: |
| 29 | +# |
| 30 | +# opencensus_test(trace_some_test internal/some_test.cc dep1 dep2...) |
| 31 | +function(opencensus_test NAME SRC) |
| 32 | + if(BUILD_TESTING) |
| 33 | + set(_NAME "opencensus_${NAME}") |
| 34 | + add_executable(${_NAME} ${SRC}) |
| 35 | + prepend_opencensus(DEPS "${ARGN}") |
| 36 | + target_link_libraries(${_NAME} "${DEPS}" gmock gtest_main) |
| 37 | + add_test(NAME ${_NAME} COMMAND ${_NAME}) |
| 38 | + endif() |
| 39 | +endfunction() |
| 40 | + |
| 41 | +# Helper function like bazel's cc_library. Libraries are namespaced as |
| 42 | +# opencensus_* and public libraries are also aliased as opencensus-cpp::*. |
| 43 | +function(opencensus_lib NAME) |
| 44 | + cmake_parse_arguments(ARG "PUBLIC" "" "SRCS;DEPS" ${ARGN}) |
| 45 | + set(_NAME "opencensus_${NAME}") |
| 46 | + prepend_opencensus(ARG_DEPS "${ARG_DEPS}") |
| 47 | + if(ARG_SRCS) |
| 48 | + add_library(${_NAME} ${ARG_SRCS}) |
| 49 | + target_link_libraries(${_NAME} PUBLIC ${ARG_DEPS}) |
| 50 | + target_include_directories(${_NAME} PUBLIC ${OPENCENSUS_INCLUDE_DIR}) |
| 51 | + else() |
| 52 | + add_library(${_NAME} INTERFACE) |
| 53 | + target_link_libraries(${_NAME} INTERFACE ${ARG_DEPS}) |
| 54 | + target_include_directories(${_NAME} INTERFACE ${OPENCENSUS_INCLUDE_DIR}) |
| 55 | + endif() |
| 56 | + if(ARG_PUBLIC) |
| 57 | + add_library(${PROJECT_NAME}::${NAME} ALIAS ${_NAME}) |
| 58 | + endif() |
| 59 | +endfunction() |
0 commit comments