Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 1 | # Copyright 2020 The IREE Authors |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 2 | # |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 3 | # Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | # See https://llvm.org/LICENSE.txt for license information. |
| 5 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 6 | |
| 7 | include(CMakeParseArguments) |
| 8 | |
| 9 | # flatbuffer_c_library() |
| 10 | # |
| 11 | # CMake function to invoke the flatcc compiler. |
| 12 | # |
| 13 | # Parameters: |
| 14 | # NAME: name of target (see Note) |
| 15 | # SRCS: List of source files for the library |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 16 | # FLATCC_ARGS: List of flattbuffers arguments. Default: |
| 17 | # "--common" |
| 18 | # "--reader" |
| 19 | # PUBLIC: Add this so that this library will be exported under iree:: |
| 20 | # Also in IDE, target will appear in IREE folder while non PUBLIC will be in IREE/internal. |
| 21 | # TESTONLY: When added, this target will only be built if user passes -DIREE_BUILD_TESTS=ON to CMake. |
| 22 | # |
| 23 | # Note: |
| 24 | # By default, flatbuffer_c_library will always create a library named ${NAME}, |
| 25 | # and alias target iree::${NAME}. The iree:: form should always be used. |
| 26 | # This is to reduce namespace pollution. |
| 27 | # |
| 28 | # flatbuffer_c_library( |
| 29 | # NAME |
Ben Vanik | e5774c3 | 2020-11-15 17:06:09 -0800 | [diff] [blame] | 30 | # some_def |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 31 | # SRCS |
Ben Vanik | e5774c3 | 2020-11-15 17:06:09 -0800 | [diff] [blame] | 32 | # "some_def.fbs" |
| 33 | # FLATCC_ARGS |
| 34 | # "--reader" |
| 35 | # "--builder" |
| 36 | # "--verifier" |
| 37 | # "--json" |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 38 | # PUBLIC |
| 39 | # ) |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 40 | # iree_cc_binary( |
| 41 | # NAME |
| 42 | # main_lib |
| 43 | # ... |
| 44 | # DEPS |
Ben Vanik | e5774c3 | 2020-11-15 17:06:09 -0800 | [diff] [blame] | 45 | # iree::schemas::some_def |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 46 | # ) |
| 47 | function(flatbuffer_c_library) |
| 48 | cmake_parse_arguments(_RULE |
| 49 | "PUBLIC;TESTONLY" |
| 50 | "NAME" |
Ben Vanik | e5774c3 | 2020-11-15 17:06:09 -0800 | [diff] [blame] | 51 | "SRCS;FLATCC_ARGS" |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 52 | ${ARGN} |
| 53 | ) |
| 54 | |
| 55 | if(_RULE_TESTONLY AND NOT IREE_BUILD_TESTS) |
| 56 | return() |
| 57 | endif() |
| 58 | |
| 59 | # Prefix the library with the package name, so we get: iree_package_name |
| 60 | iree_package_name(_PACKAGE_NAME) |
| 61 | set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}") |
| 62 | |
| 63 | if(NOT DEFINED _RULE_FLATCC_ARGS) |
| 64 | set(_RULE_FLATCC_ARGS |
| 65 | "--common" |
| 66 | "--reader" |
| 67 | ) |
| 68 | else() |
| 69 | set(_RULE_FLATCC_ARGS ${_RULE_FLATCC_ARGS}) |
| 70 | endif() |
| 71 | |
| 72 | set(_OUTS "") |
| 73 | foreach(_SRC ${_RULE_SRCS}) |
| 74 | get_filename_component(_SRC_FILENAME ${_SRC} NAME_WE) |
| 75 | foreach(_ARG ${_RULE_FLATCC_ARGS}) |
| 76 | if(_ARG STREQUAL "--reader") |
| 77 | list(APPEND _OUTS "${_SRC_FILENAME}_reader.h") |
| 78 | elseif(_ARG STREQUAL "--builder") |
| 79 | list(APPEND _OUTS "${_SRC_FILENAME}_builder.h") |
| 80 | elseif(_ARG STREQUAL "--verifier") |
| 81 | list(APPEND _OUTS "${_SRC_FILENAME}_verifier.h") |
Ben Vanik | bc685ed | 2020-11-14 17:09:46 -0800 | [diff] [blame] | 82 | elseif(_ARG STREQUAL "--json") |
| 83 | list(APPEND _OUTS "${_SRC_FILENAME}_json_printer.h") |
| 84 | list(APPEND _OUTS "${_SRC_FILENAME}_json_parser.h") |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 85 | endif() |
| 86 | endforeach() |
| 87 | endforeach() |
| 88 | list(TRANSFORM _OUTS PREPEND "${CMAKE_CURRENT_BINARY_DIR}/") |
| 89 | |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 90 | add_custom_command( |
| 91 | OUTPUT |
| 92 | ${_OUTS} |
| 93 | COMMAND |
Scott Todd | 20d746a | 2023-01-12 09:11:59 -0800 | [diff] [blame] | 94 | iree-flatcc-cli |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 95 | -o "${CMAKE_CURRENT_BINARY_DIR}" |
| 96 | -I "${IREE_ROOT_DIR}" |
| 97 | ${_RULE_FLATCC_ARGS} |
| 98 | "${_RULE_SRCS}" |
| 99 | WORKING_DIRECTORY |
| 100 | "${CMAKE_CURRENT_SOURCE_DIR}" |
| 101 | MAIN_DEPENDENCY |
| 102 | ${_RULE_SRCS} |
| 103 | DEPENDS |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 104 | ${_RULE_SRCS} |
| 105 | COMMAND_EXPAND_LISTS |
| 106 | ) |
| 107 | |
| 108 | set(_GEN_TARGET "${_NAME}_gen") |
| 109 | add_custom_target( |
| 110 | ${_GEN_TARGET} |
| 111 | DEPENDS |
| 112 | ${_OUTS} |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 113 | ) |
| 114 | |
| 115 | add_library(${_NAME} INTERFACE) |
| 116 | add_dependencies(${_NAME} ${_GEN_TARGET}) |
Stella Laurenzo | b92ceb4 | 2023-12-29 16:29:36 -0800 | [diff] [blame] | 117 | target_include_directories(${_NAME} |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 118 | INTERFACE |
Stella Laurenzo | 15c306f | 2023-12-28 19:03:20 -0800 | [diff] [blame] | 119 | $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 120 | ) |
| 121 | target_link_libraries(${_NAME} |
| 122 | INTERFACE |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 123 | ${IREE_DEFAULT_LINKOPTS} |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 124 | ) |
| 125 | target_compile_options(${_NAME} |
| 126 | INTERFACE |
Ben Vanik | 94b11c3 | 2020-11-14 17:07:56 -0800 | [diff] [blame] | 127 | "-I${IREE_ROOT_DIR}/third_party/flatcc/include/" |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 128 | "-I${IREE_ROOT_DIR}/third_party/flatcc/include/flatcc/reflection/" |
| 129 | ) |
Stella Laurenzo | 15c306f | 2023-12-28 19:03:20 -0800 | [diff] [blame] | 130 | iree_install_targets( |
| 131 | TARGETS ${_NAME} |
| 132 | ) |
Scott Todd | 62efaee | 2024-05-31 13:33:55 -0700 | [diff] [blame] | 133 | |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 134 | # Alias the iree_package_name library to iree::package::name. |
| 135 | # This lets us more clearly map to Bazel and makes it possible to |
| 136 | # disambiguate the underscores in paths vs. the separators. |
| 137 | iree_package_ns(_PACKAGE_NS) |
Stella Laurenzo | 15c306f | 2023-12-28 19:03:20 -0800 | [diff] [blame] | 138 | iree_add_alias_library(${_PACKAGE_NS}::${_RULE_NAME} ${_NAME}) |
Ben Vanik | 2d1808b | 2020-07-17 19:02:16 -0700 | [diff] [blame] | 139 | endfunction() |