view Resources/CMake/ProtobufCodeGeneration.cmake @ 627:b7fd0471281c am-dev

fix CodeGeneration unit tests
author Alain Mazy <alain@mazy.be>
date Wed, 08 May 2019 10:51:41 +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()