| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 1 | # Copyright 2024 The Dawn & Tint Authors |
| 2 | # |
| 3 | # Redistribution and use in source and binary forms, with or without |
| 4 | # modification, are permitted provided that the following conditions are met: |
| 5 | # |
| 6 | # 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | # list of conditions and the following disclaimer. |
| 8 | # |
| 9 | # 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | # this list of conditions and the following disclaimer in the documentation |
| 11 | # and/or other materials provided with the distribution. |
| 12 | # |
| 13 | # 3. Neither the name of the copyright holder nor the names of its |
| 14 | # contributors may be used to endorse or promote products derived from |
| 15 | # this software without specific prior written permission. |
| 16 | # |
| 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #[==[.rst: |
| 29 | .. cmake:command:: dawn_add_library |
| 30 | |
| 31 | Create a library. |
| 32 | |
| 33 | .. code-block:: cmake |
| 34 | |
| 35 | dawn_add_library(<name> |
| 36 | [FORCE_STATIC|FORCE_SHARED|FORCE_OBJECT] |
| 37 | [HEADER_ONLY] |
| Loko Kung | 90fdaa8 | 2024-07-24 00:59:58 +0000 | [diff] [blame] | 38 | [ENABLE_EMSCRIPTEN] |
| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 39 | [UTILITY_TARGET <target>] |
| 40 | [HEADERS <header>...] |
| Jaswant Panchumarti | dfa913f | 2024-06-26 01:02:01 +0000 | [diff] [blame] | 41 | [PRIVATE_HEADERS <header>...] |
| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 42 | [SOURCES <source>...] |
| 43 | [DEPENDS <library>...] |
| 44 | [PRIVATE_DEPENDS <library>...]) |
| 45 | |
| 46 | * ``FORCE_STATIC`` or ``FORCE_SHARED`` or ``FORCE_OBJECT``: Forces a |
| 47 | static (respectively, shared and object) library to be created. |
| 48 | If none is provided, ``BUILD_SHARED_LIBS`` will control the library type. |
| 49 | * ``HEADER_ONLY``: The library only contains headers (or templates) and contains |
| 50 | no compilation steps. Mutually exclusive with ``FORCE_STATIC``. |
| Loko Kung | 90fdaa8 | 2024-07-24 00:59:58 +0000 | [diff] [blame] | 51 | * ``ENABLE_EMSCRIPTEN``: Enables the library target when building with |
| 52 | Emscripten. By default, targets are not built with Emscripten. |
| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 53 | * ``UTILITY_TARGET``: If specified, all libraries and executables made by the |
| 54 | Dawn library API will privately link to this target. This may be used to |
| 55 | provide things such as project-wide compilation flags or similar. |
| 56 | * ``HEADERS``: A list of header files. |
| Jaswant Panchumarti | dfa913f | 2024-06-26 01:02:01 +0000 | [diff] [blame] | 57 | * ``PRIVATE_HEADERS``: A list of private header files. |
| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 58 | * ``SOURCES``: A list of source files which require compilation. |
| 59 | * ``DEPENDS``: A list of libraries that this library must link against, |
| 60 | equivalent to PUBLIC deps in target_link_libraries. |
| 61 | * ``PRIVATE_DEPENDS``: A list of libraries that this library must link against, |
| 62 | equivalent to PRIVATE deps in target_link_libraries. |
| 63 | #]==] |
| 64 | function(dawn_add_library name) |
| 65 | set(kwargs) |
| 66 | cmake_parse_arguments(PARSE_ARGV 1 arg |
| Loko Kung | 90fdaa8 | 2024-07-24 00:59:58 +0000 | [diff] [blame] | 67 | "FORCE_STATIC;FORCE_SHARED;FORCE_OBJECT;HEADER_ONLY;ENABLE_EMSCRIPTEN" |
| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 68 | "UTILITY_TARGET" |
| Jaswant Panchumarti | dfa913f | 2024-06-26 01:02:01 +0000 | [diff] [blame] | 69 | "HEADERS;PRIVATE_HEADERS;SOURCES;DEPENDS;PRIVATE_DEPENDS") |
| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 70 | |
| 71 | if (arg_UNPARSED_ARGUMENTS) |
| 72 | message(FATAL_ERROR |
| 73 | "Unparsed arguments for dawn_add_library: " |
| 74 | "${arg_UNPARSED_ARGUMENTS}") |
| 75 | endif () |
| 76 | |
| Loko Kung | 90fdaa8 | 2024-07-24 00:59:58 +0000 | [diff] [blame] | 77 | # Skip targets that shouldn't be built with Emscripten. |
| Kai Ninomiya | 3e80b5d | 2025-03-10 17:48:00 -0700 | [diff] [blame] | 78 | if (EMSCRIPTEN AND NOT arg_ENABLE_EMSCRIPTEN) |
| Loko Kung | 90fdaa8 | 2024-07-24 00:59:58 +0000 | [diff] [blame] | 79 | return() |
| 80 | endif () |
| 81 | |
| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 82 | if (arg_HEADER_ONLY AND arg_FORCE_STATIC) |
| 83 | message(FATAL_ERROR |
| 84 | "The ${name} library cannot be header only yet forced static.") |
| 85 | endif () |
| 86 | |
| 87 | if (NOT (arg_SOURCES OR arg_HEADERS)) |
| 88 | message(FATAL_ERROR |
| 89 | "The ${name} library needs at least one of sources or headers.") |
| 90 | endif () |
| 91 | |
| 92 | if (arg_FORCE_SHARED AND arg_FORCE_STATIC) |
| 93 | message(FATAL_ERROR |
| 94 | "The ${name} library cannot be both shared and static.") |
| 95 | elseif (arg_FORCE_SHARED AND arg_FORCE_OBJECT) |
| 96 | message(FATAL_ERROR |
| 97 | "The ${name} library cannot be both shared and object.") |
| 98 | elseif (arg_FORCE_STATIC AND arg_FORCE_OBJECT) |
| 99 | message(FATAL_ERROR |
| 100 | "The ${name} library cannot be both static and object.") |
| 101 | endif () |
| 102 | |
| 103 | if (NOT arg_SOURCES AND NOT arg_HEADER_ONLY) |
| 104 | message(AUTHOR_WARNING |
| 105 | "The ${name} library has no source files. Did you mean to " |
| 106 | "pass the `HEADER_ONLY` flag?") |
| 107 | endif () |
| 108 | |
| 109 | set(library_type) |
| 110 | if (arg_FORCE_STATIC) |
| 111 | set(library_type STATIC) |
| 112 | elseif (arg_FORCE_OBJECT) |
| 113 | set(library_type OBJECT) |
| 114 | elseif (arg_FORCE_SHARED) |
| 115 | set(library_type SHARED) |
| 116 | elseif (BUILD_SHARED_LIBS) |
| 117 | set(library_type SHARED) |
| 118 | else () |
| 119 | set(library_type STATIC) |
| 120 | endif () |
| 121 | |
| 122 | if (arg_HEADER_ONLY) |
| 123 | add_library("${name}" INTERFACE) |
| 124 | target_link_libraries("${name}" |
| 125 | INTERFACE |
| 126 | ${arg_DEPENDS}) |
| 127 | target_sources("${name}" |
| Jaswant Panchumarti | dfa913f | 2024-06-26 01:02:01 +0000 | [diff] [blame] | 128 | PUBLIC |
| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 129 | ${arg_HEADERS}) |
| 130 | else () |
| 131 | add_library("${name}" ${library_type}) |
| Jaswant Panchumarti | dfa913f | 2024-06-26 01:02:01 +0000 | [diff] [blame] | 132 | if (arg_HEADERS) |
| 133 | target_sources("${name}" |
| 134 | PUBLIC |
| 135 | ${arg_HEADERS}) |
| 136 | endif () |
| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 137 | target_sources("${name}" |
| 138 | PRIVATE |
| Jaswant Panchumarti | dfa913f | 2024-06-26 01:02:01 +0000 | [diff] [blame] | 139 | ${arg_PRIVATE_HEADERS} |
| Jaswant Panchumarti | 85b7675 | 2024-06-25 04:24:49 +0000 | [diff] [blame] | 140 | ${arg_SOURCES}) |
| 141 | target_link_libraries("${name}" |
| 142 | PUBLIC |
| 143 | ${arg_DEPENDS} |
| 144 | PRIVATE |
| 145 | ${arg_PRIVATE_DEPENDS} |
| 146 | ${arg_UTILITY_TARGET} |
| 147 | ) |
| 148 | common_compile_options("${name}") |
| 149 | endif () |
| 150 | add_library("dawn::${name}" ALIAS "${name}") |
| 151 | endfunction() |
| Jaswant Panchumarti | 5a26bdd | 2024-07-04 14:13:55 +0000 | [diff] [blame] | 152 | |
| 153 | #[==[.rst: |
| 154 | .. cmake:command:: dawn_install_target |
| 155 | |
| 156 | Install a target and associated header files. |
| 157 | |
| 158 | .. code-block:: cmake |
| 159 | |
| 160 | dawn_install_target(<name> |
| 161 | [HEADERS <header>...] |
| 162 | ) |
| 163 | |
| 164 | * ``HEADERS``: A list of header files to install. |
| 165 | #]==] |
| 166 | function(dawn_install_target name) |
| 167 | cmake_parse_arguments(PARSE_ARGV 1 arg |
| 168 | "" |
| 169 | "" |
| 170 | "HEADERS") |
| 171 | if (arg_UNPARSED_ARGUMENTS) |
| 172 | message(FATAL_ERROR |
| 173 | "Unparsed arguments for dawn_install_target: " |
| 174 | "${arg_UNPARSED_ARGUMENTS}") |
| 175 | endif () |
| 176 | install(TARGETS "${name}" |
| 177 | EXPORT DawnTargets |
| 178 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} |
| 179 | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 180 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 181 | ) |
| Elie Michel | ac1fe84 | 2024-09-25 17:14:43 +0000 | [diff] [blame] | 182 | # When building in debug mode with MSVC, install PDB files together with binaries |
| 183 | if (MSVC) |
| 184 | get_target_property(target_type "${name}" TYPE) |
| 185 | if ((target_type STREQUAL "STATIC_LIBRARY") OR (target_type STREQUAL "SHARED_LIBRARY") OR (target_type STREQUAL "EXECUTABLE")) |
| 186 | install(FILES $<TARGET_PDB_FILE:${name}> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL) |
| 187 | endif() |
| 188 | endif (MSVC) |
| Jaswant Panchumarti | 5a26bdd | 2024-07-04 14:13:55 +0000 | [diff] [blame] | 189 | |
| Corentin Wallez | ee3713d | 2025-07-25 08:55:54 -0700 | [diff] [blame] | 190 | # Automatically determine where each header should go based on its subdirectory in the include dir. |
| 191 | foreach(header IN LISTS arg_HEADERS) |
| 192 | cmake_path(REMOVE_FILENAME header OUTPUT_VARIABLE subdir) |
| 193 | |
| 194 | string(FIND "${subdir}" "${DAWN_INCLUDE_DIR}" found) |
| 195 | if (found EQUAL 0) |
| 196 | cmake_path(RELATIVE_PATH subdir BASE_DIRECTORY "${DAWN_INCLUDE_DIR}") |
| 197 | endif() |
| 198 | string(FIND "${subdir}" "${DAWN_BUILD_GEN_DIR}/include/" found) |
| 199 | if (found EQUAL 0) |
| 200 | cmake_path(RELATIVE_PATH subdir BASE_DIRECTORY "${DAWN_BUILD_GEN_DIR}/include/") |
| 201 | endif() |
| 202 | |
| 203 | if (IS_ABSOLUTE "${headerRelative}") |
| 204 | message(FATAL_ERROR "Unsupported include dir for \"${header}\"") |
| 205 | endif() |
| Jaswant Panchumarti | 5a26bdd | 2024-07-04 14:13:55 +0000 | [diff] [blame] | 206 | install(FILES "${header}" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${subdir}") |
| 207 | endforeach() |
| 208 | endfunction() |