view Resources/CMake/ProtobufCodeGeneration.cmake @ 670:5dd496343fad

Restored missing code + fixed key ordering when generating code
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 15 May 2019 18:29:42 +0200
parents 5055031f4a06
children
line wrap: on
line source

# HOW TO USE:
# the GenerateCodeFromProtobufSchema will generate files in ${CMAKE_BINARY_DIR} and will
# populate PROTOBUF_AUTOGENERATED_SOURCES with the list of generated files
# AS OF 2019-01-30, it requires protoc (version 3.6.1.x) to be available in the path
set(PROTOBUF_AUTOGENERATED_SOURCES)

# TODO: use find_program (<VAR> name1 [path1 path2 ...]) to located the protobuf compiler
# TODO: automated the TS plugin installation

macro(GenerateCodeFromProtobufSchema schemaFilePath outputBaseDirectory)
  # extract file name
  GetFilePathWithoutLastExtension(schemaFilePathWithoutExt ${schemaFilePath})
  
  # remove extension
  GetFilenameFromPath(schemaFileNameWithoutExt ${schemaFilePathWithoutExt})
  
  set(generatedFilePathWithoutExtension "${CMAKE_BINARY_DIR}/AUTOGENERATED/${schemaFileNameWithoutExt}")
  set(generatedCppSourceFilePath "${generatedFilePathWithoutExtension}.pb.cc")
  set(generatedCppHeaderFilePath "${generatedFilePathWithoutExtension}.pb.h")
  set(generatedJsFilePath "${generatedFilePathWithoutExtension}_pb.js")
  set(generatedTsFilePath "${generatedFilePathWithoutExtension}_pb.d.ts")
  # set(generatedJsFileName "${generatedFilePathWithoutExtension}.js")

  # set(AUTOGENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/AUTOGENERATED")
  # set(AUTOGENERATED_SOURCES)
  
  set(PROTOC_EXECUTABLE "PROTOC")
  find_program(PROTOC_EXECUTABLE_SEARCH ${FLATC_EXECUTABLE})
  if(NOT PROTOC_EXECUTABLE_SEARCH)
    message(FATAL_ERROR "The Protocol Buffers compiler (protoc[.exe]) cannot be found!")
  endif()

  # TODO CUSTOMIZE FOR TYPESCRIPT
  set(SCRIPT_CPP_OPTIONS)
  list(APPEND SCRIPT_CPP_OPTIONS "----cpp_out=${CMAKE_BINARY_DIR}/AUTOGENERATED")
  # list(APPEND SCRIPT_CPP_OPTIONS "gnagna")
  
  set(SCRIPT_TS_OPTIONS)

  list(APPEND SCRIPT_TS_OPTIONS "--ts")
  list(APPEND SCRIPT_TS_OPTIONS "gnagna")

  add_custom_command(
    OUTPUT
    ${generatedCppSourceFilePath}
    ${generatedCppHeaderFilePath}
    COMMAND 
    ${PROTOC_EXECUTABLE} ${SCRIPT_CPP_OPTIONS} ${schemaFilePath}
    DEPENDS
    ${schemaFilePath}
    )
  
  add_custom_command(
    OUTPUT
    ${generatedTsFileName}
    ${generatedJsFilePath}
    COMMAND 
    ${PROTOC_EXECUTABLE} ${SCRIPT_TS_OPTIONS} ${schemaFilePath}
    DEPENDS
    ${schemaFilePath}
    )
  
  # add_custom_command(
  #   OUTPUT
  #   ${generatedJsFileName}
  #   COMMAND 
  #   ${FLATC_EXECUTABLE} ${SCRIPT_JS_OPTIONS} ${schemaFilePath}
  #   DEPENDS
  #   ${schemaFilePath}
  #   )

    list(APPEND FLATC_AUTOGENERATED_SOURCES "${generatedCppFileName}") 
    # list(APPEND FLATC_AUTOGENERATED_SOURCES "${generatedJsFileName}") 
    list(APPEND FLATC_AUTOGENERATED_SOURCES "${generatedTsFileName}") 

endmacro()