|
| 1 | +# A function which allows to automatically generate C++ sources from zserio schemas. |
| 2 | +# |
| 3 | +# Prerequisites: |
| 4 | +# CMake 3.15+ |
| 5 | +# JAVA must be available - calls find_program(JAVA java) |
| 6 | +# ZSERIO_JAR_FILE must be set either as an environment or CMake variable |
| 7 | +# |
| 8 | +# Usage: |
| 9 | +# zserio_generate_cpp( |
| 10 | +# TARGET <target> |
| 11 | +# [SRC_DIR <directory>] |
| 12 | +# [MAIN_ZS <file>] |
| 13 | +# [GEN_DIR <directory>] |
| 14 | +# [EXTRA_ARGS <argument>...] |
| 15 | +# [GENERATED_SOURCES_VAR <variable>] |
| 16 | +# [OUTPUT_VAR <variable>] |
| 17 | +# [ERROR_VAR <variable>] |
| 18 | +# [RESULT_VAR <variable>] |
| 19 | +# [FORCE_REGENERATION] |
| 20 | +# [CLEAN_GEN_DIR] |
| 21 | +# |
| 22 | +# Arguments: |
| 23 | +# TARGET - Target to which the generated sources will be assigned. |
| 24 | +# SRC_DIR - Source directory for zserio schemas. Optional, defaults to CMAKE_CURRENT_SOURCE_DIR. |
| 25 | +# MAIN_ZS - Main zserio schema. Optional if the MAIN_ZS file is specified as a source for the given TARGET. |
| 26 | +# GEN_DIR - Directory where the C++ sources will be generated. |
| 27 | +# EXTRA_ARGS - Extra arguments to be passed to the Zserio tool. |
| 28 | +# GENERATED_SOURCES_VAR - The variable will be set with a list of generated source files (full paths). |
| 29 | +# Optional. |
| 30 | +# OUTPUT_VAR - The variable will be set with the contents of the standard output pipe. Optional. |
| 31 | +# If not set, the standard output pipe is printed. |
| 32 | +# ERROR_VAR - The variable will be set with the contents of the standard error pipe. Optional. |
| 33 | +# If not set, the standard error pipe is printed. |
| 34 | +# RESULT_VAR - The variable will be set to contain the result of the zserio generator. Optional. |
| 35 | +# If not set, a FATAL_ERROR is raised in case of the zserio generator error. |
| 36 | +# FORCE_RECONFIGURE - Forces regeneration every time the CMake configure is run. |
| 37 | +# CLEAN_GEN_DIR - Cleans GEN_DIR when generation in CMake configure time is run. |
| 38 | +# |
| 39 | +# Note that OUTPUT_VAR and ERROR_VAR can be set to the same value and then both pipes will be merged. |
| 40 | +# |
| 41 | +# Note that OUTPUT_VAR, ERROR_VAR and RESULT_VAR are updated only when the generation is executed within |
| 42 | +# the configure-time - i.e. for the first time or when zserio schema sources are changed, etc. |
| 43 | +# See "How if works" for more info. |
| 44 | +# |
| 45 | +# Example: |
| 46 | +# set(CMAKE_MODULE_PATH "${ZSERIO_RELEASE}/cmake") |
| 47 | +# set(ZSERIO_JAR_FILE "${ZSERIO_RELEASE}/zserio.jar") |
| 48 | +# include(zserio_compiler) |
| 49 | +# |
| 50 | +# add_library(sample_zs sample.zs) |
| 51 | +# zserio_generate_cpp( |
| 52 | +# TARGET sample_zs |
| 53 | +# GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/gen) |
| 54 | +# |
| 55 | +# How it works: |
| 56 | +# |
| 57 | +# First time the CMake configure is run, the sources are generated using execute_process directly in |
| 58 | +# configure-time and auxilary information (timestamps, list of generated sources, etc.) is stored in the |
| 59 | +# CMake cache. The auxilary info is used to define a custom command which uses the same zserio command line |
| 60 | +# as the original execute_process and thus allows to re-generate sources when it's needed - e.g. after the |
| 61 | +# clean step. |
| 62 | +# |
| 63 | +# The custom command is sufficient as long as the generated sources remains unchanged. Otherwise the |
| 64 | +# execute_process must be re-run in configure-time to ensure that all generated sources are collected correctly. |
| 65 | +# This functionality is achieved using the auxilary information mentioned above. |
| 66 | +# |
| 67 | +# List of generated sources can change in following situations: |
| 68 | +# |
| 69 | +# - ZSERIO_JAR_FILE is changed |
| 70 | +# - zserio schema sources are changed |
| 71 | +# - EXTRA_ARGS are changed |
| 72 | +function(zserio_generate_cpp) |
| 73 | + find_program(JAVA java) |
| 74 | + if (NOT JAVA) |
| 75 | + message(FATAL_ERROR "Could not find java!") |
| 76 | + endif() |
| 77 | + if (DEFINED ENV{ZSERIO_JAR_FILE}) |
| 78 | + set(ZSERIO_JAR_FILE $ENV{ZSERIO_JAR_FILE}) |
| 79 | + endif () |
| 80 | + if (NOT DEFINED ZSERIO_JAR_FILE OR NOT EXISTS ${ZSERIO_JAR_FILE}) |
| 81 | + message(FATAL_ERROR "Could not find zserio.jar!") |
| 82 | + endif() |
| 83 | + |
| 84 | + cmake_parse_arguments(ZSERIO_GENERATE |
| 85 | + "FORCE_REGENERATE;CLEAN_GEN_DIR" |
| 86 | + "TARGET;SRC_DIR;MAIN_ZS;GEN_DIR;GENERATED_SOURCES_VAR;OUTPUT_VAR;ERROR_VAR;RESULT_VAR" |
| 87 | + "EXTRA_ARGS" |
| 88 | + ${ARGN}) |
| 89 | + |
| 90 | + if (ZSERIO_GENERATE_UNPARSED_ARGUMENTS) |
| 91 | + message(FATAL_ERROR "zserio_generate_cpp: Unknown arguments '${ZSERIO_GENERATE_UNPARSED_ARGUMENTS}'!") |
| 92 | + endif () |
| 93 | + |
| 94 | + # check required arguments |
| 95 | + foreach (ARG TARGET GEN_DIR) |
| 96 | + if (NOT DEFINED ZSERIO_GENERATE_${ARG}) |
| 97 | + message(FATAL_ERROR "No value defined for required argument ${ARG}!") |
| 98 | + endif () |
| 99 | + endforeach () |
| 100 | + |
| 101 | + # default values |
| 102 | + if (NOT DEFINED ZSERIO_GENERATE_SRC_DIR) |
| 103 | + set(ZSERIO_GENERATE_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}") |
| 104 | + endif () |
| 105 | + if (NOT DEFINED ZSERIO_GENERATE_MAIN_ZS) |
| 106 | + # try to get a single main zs |
| 107 | + get_target_property(ZS_SOURCES ${ZSERIO_GENERATE_TARGET} SOURCES) |
| 108 | + list(FILTER ZS_SOURCES INCLUDE REGEX "\\.zs$") |
| 109 | + list(LENGTH ZS_SOURCES ZS_SOURCES_LENGTH) |
| 110 | + if (${ZS_SOURCES_LENGTH} EQUAL 1) |
| 111 | + list(GET ZS_SOURCES 0 ZSERIO_GENERATE_MAIN_ZS) |
| 112 | + |
| 113 | + # try to map the found source to ZSERIO_GENERATE_SRC_DIR |
| 114 | + if (NOT IS_ABSOLUTE ${ZSERIO_GENERATE_MAIN_ZS}) |
| 115 | + set(ZSERIO_GENERATE_MAIN_ZS "${CMAKE_CURRENT_SOURCE_DIR}/${ZSERIO_GENERATE_MAIN_ZS}") |
| 116 | + endif () |
| 117 | + file(RELATIVE_PATH ZSERIO_GENERATE_MAIN_ZS |
| 118 | + "${ZSERIO_GENERATE_SRC_DIR}" "${ZSERIO_GENERATE_MAIN_ZS}") |
| 119 | + else () |
| 120 | + message(FATAL_ERROR "MAIN_ZS file not specifid and cannot be detected!") |
| 121 | + endif () |
| 122 | + endif () |
| 123 | + |
| 124 | + # ensure cmake reconfigure when zserio sources are changed |
| 125 | + file(GLOB_RECURSE ZSERIO_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" |
| 126 | + "${ZSERIO_GENERATE_SRC_DIR}/*.zs") |
| 127 | + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${ZSERIO_SOURCES} ${ZSERIO_JAR_FILE}) |
| 128 | + |
| 129 | + set(ZSERIO_COMMAND |
| 130 | + ${JAVA} -jar ${ZSERIO_JAR_FILE} |
| 131 | + -src ${ZSERIO_GENERATE_SRC_DIR} ${ZSERIO_GENERATE_MAIN_ZS} |
| 132 | + -cpp ${ZSERIO_GENERATE_GEN_DIR} |
| 133 | + ${ZSERIO_GENERATE_EXTRA_ARGS} |
| 134 | + ) |
| 135 | + |
| 136 | + _zserio_compiler_get_latest_timestamp(FILES ${ZSERIO_SOURCES} TIMESTAMP_VAR ZSERIO_SOURCES_TIMESTAMP) |
| 137 | + _zserio_compiler_get_latest_timestamp(FILES ${ZSERIO_JAR_FILE} TIMESTAMP_VAR ZSERIO_JAR_TIMESTAMP) |
| 138 | + |
| 139 | + set(REGENERATE_SOURCES 1) |
| 140 | + if (NOT ZSERIO_GENERATE_FORCE_REGENERATE AND |
| 141 | + DEFINED ZSERIO_COMPILER_ZSERIO_JAR_TIMESTAMP AND |
| 142 | + DEFINED ZSERIO_COMPILER_SOURCES_TIMESTAMP_${ZSERIO_GENERATE_TARGET} AND |
| 143 | + DEFINED ZSERIO_COMPILER_SOURCES_${ZSERIO_GENERATE_TARGET} AND |
| 144 | + DEFINED ZSERIO_COMPILER_EXTRA_ARGS_${ZSERIO_GENERATE_TARGET} AND |
| 145 | + DEFINED ZSERIO_COMPILER_GENERATED_SOURCES_${ZSERIO_GENERATE_TARGET}) |
| 146 | + if (${ZSERIO_COMPILER_ZSERIO_JAR_TIMESTAMP} EQUAL ${ZSERIO_JAR_TIMESTAMP} AND |
| 147 | + ${ZSERIO_COMPILER_SOURCES_TIMESTAMP_${ZSERIO_GENERATE_TARGET}} EQUAL ${ZSERIO_SOURCES_TIMESTAMP} AND |
| 148 | + "${ZSERIO_COMPILER_EXTRA_ARGS_${ZSERIO_GENERATE_TARGET}}" STREQUAL |
| 149 | + "${ZSERIO_GENERATE_EXTRA_ARGS}" AND |
| 150 | + "${ZSERIO_COMPILER_SOURCES_${ZSERIO_GENERATE_TARGET}}" STREQUAL "${ZSERIO_SOURCES}") |
| 151 | + set(REGENERATE_SOURCES 0) |
| 152 | + endif () |
| 153 | + endif () |
| 154 | + |
| 155 | + set(TOOL_COMMENT "Generating C++ sources from '${ZSERIO_GENERATE_MAIN_ZS}'.") |
| 156 | + |
| 157 | + if (REGENERATE_SOURCES) |
| 158 | + if ("${ZSERIO_GENERATE_OUTPUT_VAR}" STREQUAL "${ZSERIO_GENERATE_ERROR_VAR}") |
| 159 | + set(SEPARATE_OUTPUT OFF) |
| 160 | + set(ZSERIO_OUTPUT_VAR ZSERIO_OUTPUT) |
| 161 | + set(ZSERIO_ERROR_VAR ZSERIO_OUTPUT) |
| 162 | + else () |
| 163 | + set(SEPARATE_OUTPUT ON) |
| 164 | + set(ZSERIO_OUTPUT_VAR ZSERIO_OUTPUT) |
| 165 | + set(ZSERIO_ERROR_VAR ZSERIO_ERROR) |
| 166 | + endif () |
| 167 | + |
| 168 | + if (ZSERIO_GENERATE_CLEAN_GEN_DIR) |
| 169 | + file(REMOVE_RECURSE ${ZSERIO_GENERATE_GEN_DIR}) |
| 170 | + file(MAKE_DIRECTORY ${ZSERIO_GENERATE_GEN_DIR}) |
| 171 | + endif () |
| 172 | + |
| 173 | + message(STATUS ${TOOL_COMMENT}) |
| 174 | + |
| 175 | + # run the generator during configure phase for the first time |
| 176 | + execute_process( |
| 177 | + COMMAND ${ZSERIO_COMMAND} |
| 178 | + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
| 179 | + OUTPUT_VARIABLE ${ZSERIO_OUTPUT_VAR} |
| 180 | + ERROR_VARIABLE ${ZSERIO_ERROR_VAR} |
| 181 | + RESULT_VARIABLE ZSERIO_RESULT) |
| 182 | + |
| 183 | + if (${SEPARATE_OUTPUT}) |
| 184 | + if (DEFINED ZSERIO_GENERATE_OUTPUT_VAR) |
| 185 | + set(${ZSERIO_GENERATE_OUTPUT_VAR} ${ZSERIO_OUTPUT} PARENT_SCOPE) |
| 186 | + else () |
| 187 | + message(STATUS ${ZSERIO_OUTPUT}) |
| 188 | + endif () |
| 189 | + |
| 190 | + if (DEFINED ZSERIO_GENERATE_ERROR_VAR) |
| 191 | + set(${ZSERIO_GENERATE_ERROR_VAR} ${ZSERIO_ERROR} PARENT_SCOPE) |
| 192 | + else () |
| 193 | + message(STATUS ${ZSERIO_ERROR}) |
| 194 | + endif () |
| 195 | + else () |
| 196 | + # both OUTPUT_VAR and ERROR_VAR are either not set or set to the same value |
| 197 | + if (DEFINED ZSERIO_GENERATE_OUTPUT_VAR) |
| 198 | + set(${ZSERIO_GENERATE_OUTPUT_VAR} ${ZSERIO_OUTPUT} PARENT_SCOPE) |
| 199 | + else () |
| 200 | + message(STATUS ${ZSERIO_OUTPUT}) |
| 201 | + endif () |
| 202 | + endif () |
| 203 | + |
| 204 | + if (DEFINED ZSERIO_GENERATE_RESULT_VAR) |
| 205 | + set(${ZSERIO_GENERATE_RESULT_VAR} ${ZSERIO_RESULT} PARENT_SCOPE) |
| 206 | + endif () |
| 207 | + |
| 208 | + if (NOT ${ZSERIO_RESULT} EQUAL 0 AND NOT DEFINED ZSERIO_GENERATE_RESULT_VAR) |
| 209 | + message(STATUS ${${ZSERIO_ERROR_VAR}}) |
| 210 | + message(FATAL_ERROR "Zserio generator failed!") |
| 211 | + endif () |
| 212 | + |
| 213 | + file(GLOB_RECURSE GENERATED_SOURCES |
| 214 | + "${ZSERIO_GENERATE_GEN_DIR}/*.h" |
| 215 | + "${ZSERIO_GENERATE_GEN_DIR}/*.cpp") |
| 216 | + |
| 217 | + set(ZSERIO_COMPILER_ZSERIO_JAR_TIMESTAMP "${ZSERIO_JAR_TIMESTAMP}" |
| 218 | + CACHE INTERNAL "Timestamp of the '${ZSERIO_JAR_FILE}'" |
| 219 | + FORCE) |
| 220 | + set(ZSERIO_COMPILER_SOURCES_TIMESTAMP_${ZSERIO_GENERATE_TARGET} "${ZSERIO_SOURCES_TIMESTAMP}" |
| 221 | + CACHE INTERNAL "Latest timestamp of the Zserio schema sources for ${ZSERIO_GENERATE_TARGET}." |
| 222 | + FORCE) |
| 223 | + set(ZSERIO_COMPILER_SOURCES_${ZSERIO_GENERATE_TARGET} "${ZSERIO_SOURCES}" |
| 224 | + CACHE INTERNAL "List of Zserio schema sources for ${ZSERIO_GENERATE_TARGET}." |
| 225 | + FORCE) |
| 226 | + set(ZSERIO_COMPILER_EXTRA_ARGS_${ZSERIO_GENERATE_TARGET} "${ZSERIO_GENERATE_EXTRA_ARGS}" |
| 227 | + CACHE INTERNAL "Extra arguments to Zserio tool used for ${ZSERIO_GENERATE_TARGET}." |
| 228 | + FORCE) |
| 229 | + set(ZSERIO_COMPILER_GENERATED_SOURCES_${ZSERIO_GENERATE_TARGET} "${GENERATED_SOURCES}" |
| 230 | + CACHE INTERNAL "List of sources files generated by Zserio for ${ZSERIO_GENERATE_TARGET}." |
| 231 | + FORCE) |
| 232 | + else () |
| 233 | + set(GENERATED_SOURCES "${ZSERIO_COMPILER_GENERATED_SOURCES_${ZSERIO_GENERATE_TARGET}}") |
| 234 | + endif () |
| 235 | + |
| 236 | + if (NOT "${GENERATED_SOURCES}" STREQUAL "") |
| 237 | + if (DEFINED ZSERIO_GENERATE_GENERATED_SOURCES_VAR) |
| 238 | + set(${ZSERIO_GENERATE_GENERATED_SOURCES_VAR} "${GENERATED_SOURCES}" PARENT_SCOPE) |
| 239 | + endif () |
| 240 | + |
| 241 | + add_custom_command(OUTPUT ${GENERATED_SOURCES} |
| 242 | + COMMAND ${ZSERIO_COMMAND} |
| 243 | + DEPENDS ${ZSERIO_SOURCES} |
| 244 | + COMMENT ${TOOL_COMMENT}) |
| 245 | + |
| 246 | + target_sources(${ZSERIO_GENERATE_TARGET} PRIVATE ${GENERATED_SOURCES}) |
| 247 | + set_source_files_properties(${GENERATED_SOURCES} PROPERTIES GENERATED TRUE) |
| 248 | + |
| 249 | + get_target_property(TARGET_TYPE ${ZSERIO_GENERATE_TARGET} TYPE) |
| 250 | + if ("${TARGET_TYPE}" STREQUAL "INTERFACE_LIBRARY") |
| 251 | + target_include_directories(${ZSERIO_GENERATE_TARGET} INTERFACE ${ZSERIO_GENERATE_GEN_DIR}) |
| 252 | + else () |
| 253 | + target_include_directories(${ZSERIO_GENERATE_TARGET} PUBLIC ${ZSERIO_GENERATE_GEN_DIR}) |
| 254 | + endif () |
| 255 | + |
| 256 | + set_target_properties(${ZSERIO_GENERATE_TARGET} PROPERTIES |
| 257 | + ADDITIONAL_CLEAN_FILES "${GENERATED_SOURCES}") |
| 258 | + endif () |
| 259 | +endfunction() |
| 260 | + |
| 261 | +function(_zserio_compiler_get_latest_timestamp) |
| 262 | + cmake_parse_arguments(GET_LATEST_TIMESTAMP "" "TIMESTAMP_VAR" "FILES" ${ARGN}) |
| 263 | + |
| 264 | + set(LATEST_TIMESTAMP 0) |
| 265 | + foreach(FILE ${GET_LATEST_TIMESTAMP_FILES}) |
| 266 | + if (NOT IS_ABSOLUTE ${FILE}) |
| 267 | + set(FILE "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}") |
| 268 | + endif () |
| 269 | + |
| 270 | + file(TIMESTAMP ${FILE} FILE_TIMESTAMP "%s") |
| 271 | + if (${FILE_TIMESTAMP} GREATER ${LATEST_TIMESTAMP}) |
| 272 | + set(LATEST_TIMESTAMP ${FILE_TIMESTAMP}) |
| 273 | + endif () |
| 274 | + endforeach() |
| 275 | + set(${GET_LATEST_TIMESTAMP_TIMESTAMP_VAR} ${LATEST_TIMESTAMP} PARENT_SCOPE) |
| 276 | +endfunction() |
0 commit comments