|
| 1 | +# Copyright (c) Facebook, Inc. and its affiliates. |
| 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 | +include_guard(GLOBAL) |
| 15 | +function(axiom_get_rpath_origin VAR) |
| 16 | + if(APPLE) |
| 17 | + set(_origin @loader_path) |
| 18 | + else() |
| 19 | + set(_origin "\$ORIGIN") |
| 20 | + endif() |
| 21 | + set(${VAR} |
| 22 | + ${_origin} |
| 23 | + PARENT_SCOPE) |
| 24 | +endfunction() |
| 25 | + |
| 26 | +# TODO use file sets |
| 27 | +function(axiom_install_library_headers) |
| 28 | + # Find any headers and install them relative to the source tree in include. |
| 29 | + file(GLOB _hdrs "*.h") |
| 30 | + if(NOT "${_hdrs}" STREQUAL "") |
| 31 | + cmake_path( |
| 32 | + RELATIVE_PATH |
| 33 | + CMAKE_CURRENT_SOURCE_DIR |
| 34 | + BASE_DIRECTORY |
| 35 | + "${CMAKE_SOURCE_DIR}" |
| 36 | + OUTPUT_VARIABLE |
| 37 | + _hdr_dir) |
| 38 | + install(FILES ${_hdrs} DESTINATION include/${_hdr_dir}) |
| 39 | + endif() |
| 40 | +endfunction() |
| 41 | + |
| 42 | +# Base add axiom library call to add a library and install it. |
| 43 | +function(axiom_base_add_library TARGET) |
| 44 | + add_library(${TARGET} ${ARGN}) |
| 45 | + install(TARGETS ${TARGET} DESTINATION lib/axiom) |
| 46 | + axiom_install_library_headers() |
| 47 | +endfunction() |
| 48 | + |
| 49 | +# This is extremely hackish but presents an easy path to installation. |
| 50 | +function(axiom_add_library TARGET) |
| 51 | + set(options OBJECT STATIC SHARED INTERFACE) |
| 52 | + set(oneValueArgs) |
| 53 | + set(multiValueArgs) |
| 54 | + cmake_parse_arguments( |
| 55 | + AXIOM |
| 56 | + "${options}" |
| 57 | + "${oneValueArgs}" |
| 58 | + "${multiValueArgs}" |
| 59 | + ${ARGN}) |
| 60 | + |
| 61 | + # Remove library type specifiers from ARGN |
| 62 | + set(library_type) |
| 63 | + if(AXIOM_OBJECT) |
| 64 | + set(library_type OBJECT) |
| 65 | + elseif(AXIOM_STATIC) |
| 66 | + set(library_type STATIC) |
| 67 | + elseif(AXIOM_SHARED) |
| 68 | + set(library_type SHARED) |
| 69 | + elseif(AXIOM_INTERFACE) |
| 70 | + set(library_type INTERFACE) |
| 71 | + endif() |
| 72 | + |
| 73 | + list(REMOVE_ITEM ARGN OBJECT) |
| 74 | + list(REMOVE_ITEM ARGN STATIC) |
| 75 | + list(REMOVE_ITEM ARGN SHARED) |
| 76 | + list(REMOVE_ITEM ARGN INTERFACE) |
| 77 | + # Propagate to the underlying add_library and then install the target. |
| 78 | + if(AXIOM_MONO_LIBRARY) |
| 79 | + if(TARGET axiom) |
| 80 | + # Target already exists, append sources to it. |
| 81 | + target_sources(axiom PRIVATE ${ARGN}) |
| 82 | + else() |
| 83 | + set(_type STATIC) |
| 84 | + if(AXIOM_BUILD_SHARED) |
| 85 | + set(_type SHARED) |
| 86 | + endif() |
| 87 | + # Create the target if this is the first invocation. |
| 88 | + add_library(axiom ${_type} ${ARGN}) |
| 89 | + set_target_properties(axiom PROPERTIES LIBRARY_OUTPUT_DIRECTORY |
| 90 | + ${PROJECT_BINARY_DIR}/lib) |
| 91 | + set_target_properties(axiom PROPERTIES ARCHIVE_OUTPUT_DIRECTORY |
| 92 | + ${PROJECT_BINARY_DIR}/lib) |
| 93 | + install(TARGETS axiom DESTINATION lib/axiom EXPORT axiom_targets) |
| 94 | + endif() |
| 95 | + # create alias for compatability |
| 96 | + if(NOT TARGET ${TARGET}) |
| 97 | + add_library(${TARGET} ALIAS axiom) |
| 98 | + endif() |
| 99 | + else() |
| 100 | + # Create a library for each invocation. |
| 101 | + axiom_base_add_library(${TARGET} ${library_type} ${ARGN}) |
| 102 | + endif() |
| 103 | + axiom_install_library_headers() |
| 104 | +endfunction() |
| 105 | + |
| 106 | +function(axiom_link_libraries TARGET) |
| 107 | + # TODO(assignUser): Handle scope keywords (they currently are empty calls ala |
| 108 | + # target_link_libraries(target PRIVATE)) |
| 109 | + if(AXIOM_MONO_LIBRARY) |
| 110 | + # These targets follow the axiom_* name for consistency but are NOT actually |
| 111 | + # aliases to axiom when building the mono lib and need to be linked |
| 112 | + # explicitly (this is a hack) |
| 113 | + set(explicit_targets) |
| 114 | + |
| 115 | + foreach(_arg ${ARGN}) |
| 116 | + list(FIND explicit_targets ${_arg} _explicit) |
| 117 | + if(_explicit EQUAL -1 AND "${_arg}" MATCHES "^axiom_*") |
| 118 | + message(DEBUG "\t\tDROP: ${_arg}") |
| 119 | + else() |
| 120 | + message(DEBUG "\t\tADDING: ${_arg}") |
| 121 | + target_link_libraries(axiom ${_arg}) |
| 122 | + endif() |
| 123 | + endforeach() |
| 124 | + else() |
| 125 | + target_link_libraries(${TARGET} ${ARGN}) |
| 126 | + endif() |
| 127 | +endfunction() |
| 128 | + |
| 129 | +function(axiom_include_directories TARGET) |
| 130 | + if(AXIOM_MONO_LIBRARY) |
| 131 | + target_include_directories(axiom ${ARGN}) |
| 132 | + else() |
| 133 | + target_include_directories(${TARGET} ${ARGN}) |
| 134 | + endif() |
| 135 | +endfunction() |
| 136 | + |
| 137 | +function(axiom_compile_definitions TARGET) |
| 138 | + if(AXIOM_MONO_LIBRARY) |
| 139 | + target_compile_definitions(axiom ${ARGN}) |
| 140 | + else() |
| 141 | + target_compile_definitions(${TARGET} ${ARGN}) |
| 142 | + endif() |
| 143 | +endfunction() |
| 144 | + |
| 145 | +function(axiom_sources TARGET) |
| 146 | + if(AXIOM_MONO_LIBRARY) |
| 147 | + target_sources(axiom ${ARGN}) |
| 148 | + else() |
| 149 | + target_sources(${TARGET} ${ARGN}) |
| 150 | + endif() |
| 151 | +endfunction() |
0 commit comments