view Resources/CMake/FlatBuffersConfiguration.cmake @ 445:ee41b6a017d7 bgo-commands-codegen

dumb change
author bgo-osimis
date Wed, 16 Jan 2019 16:24:45 +0100
parents
children
line wrap: on
line source

# Stone of Orthanc
# Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
# Department, University Hospital of Liege, Belgium
# Copyright (C) 2017-2019 Osimis S.A., Belgium
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
include(${ORTHANC_ROOT}/Resources/CMake/DownloadPackage.cmake)
set(FLATC_AUTOGENERATED_SOURCES)

if (STATIC_BUILD OR NOT USE_SYSTEM_FLATBUFFERS)
  SET(FLATBUFFERS_SOURCES_DIR ${CMAKE_BINARY_DIR}/flatbuffers-1.10)
  SET(FLATBUFFERS_URL "http://127.0.0.1:8000/flatbuffers-1.10.tar.gz")
  SET(FLATBUFFERS_MD5 "d5f131809c14def9682d49385b452e43")

  DownloadPackage(${FLATBUFFERS_MD5} ${FLATBUFFERS_URL} "${FLATBUFFERS_SOURCES_DIR}")

  include_directories(${FLATBUFFERS_SOURCES_DIR}/include)

  # we only need the flatc compiler!
  set(FLATBUFFERS_BUILD_TESTS OFF)
  set(FLATBUFFERS_INSTALL OFF)
  set(FLATBUFFERS_BUILD_FLATLIB OFF)
  set(FLATBUFFERS_BUILD_FLATC ON)
  set(FLATBUFFERS_BUILD_FLATHASH OFF)
  set(FLATBUFFERS_BUILD_GRPCTEST OFF)
  set(FLATBUFFERS_BUILD_SHAREDLIB OFF)
  set(FLATBUFFERS_LIBCXX_WITH_CLANG OFF)

  # add_subdirectory(${FLATBUFFERS_SOURCES_DIR}
  #    ${CMAKE_CURRENT_BINARY_DIR}/flatbuffers-build
  #    EXCLUDE_FROM_ALL)

  # Now simply link against flatbuffers as needed to your already declared target.
  # The flatbuffers target carry header search path automatically if CMake > 2.8.11.
  #target_link_libraries(own_project_target PRIVATE flatbuffers)

else()
  find_package(FlatBuffers)
  include_directories(${SDL2_INCLUDE_DIRS})
  link_libraries(${SDL2_LIBRARIES})
endif()

macro(GetFilenameFromPath TargetVariable Path)
  #message(STATUS "GetFilenameFromPath (1): Path = ${Path} TargetVariable = ${${TargetVariable}}")
  string(REPLACE "\\" "/" PathWithFwdSlashes "${Path}")
  string(REGEX REPLACE "^.*/" "" ${TargetVariable} "${PathWithFwdSlashes}")
  #message(STATUS "GetFilenameFromPath (2): Path = ${Path} Path = ${PathWithFwdSlashes} TargetVariable = ${TargetVariable}")
endmacro()

macro(GetFilePathWithoutLastExtension TargetVariable FilePath)
  string(REGEX REPLACE "(^.*)\\.([^\\.]+)" "\\1" ${TargetVariable} "${FilePath}")
  #message(STATUS "GetFileNameWithoutLastExtension: FilePath = ${FilePath} TargetVariable = ${${TargetVariable}}")
endmacro()

macro(Test_GetFilePathWithoutLastExtension)
  set(tmp "/prout/zi/goui.goui.cpp")
  GetFilePathWithoutLastExtension(TargetVariable "${tmp}")
  if(NOT ("${TargetVariable}" STREQUAL "/prout/zi/goui.goui"))
    message(FATAL_ERROR "Test_GetFilePathWithoutLastExtension failed (1)")
  else()
    #message(STATUS "Test_GetFilePathWithoutLastExtension: <<OK>>")
  endif()
endmacro()

Test_GetFilePathWithoutLastExtension()

macro(Test_GetFilenameFromPath)
  set(tmp "/prout/../../dada/zi/goui.goui.cpp")
  GetFilenameFromPath(TargetVariable "${tmp}")
  if(NOT ("${TargetVariable}" STREQUAL "goui.goui.cpp"))
    message(FATAL_ERROR "Test_GetFilenameFromPath failed")
  else()
    #message(STATUS "Test_GetFilenameFromPath: <<OK>>")
  endif()
endmacro()

Test_GetFilenameFromPath()

macro(GenerateCodeFromFlatBufferSchema schemaFilePath)
  # extract file name
  GetFilePathWithoutLastExtension(schemaFilePathWithoutExt ${schemaFilePath})
  
  # remove extension
  GetFilenameFromPath(schemaFileNameWithoutExt ${schemaFilePathWithoutExt})
  
  set(generatedFilePathWithoutExtension "${CMAKE_BINARY_DIR}/${schemaFileNameWithoutExt}_generated")
  set(generatedCppFileName "${generatedFilePathWithoutExtension}.h")
  set(generatedTsFileName "${generatedFilePathWithoutExtension}.ts")
  set(generatedJsFileName "${generatedFilePathWithoutExtension}.js")

  set(AUTOGENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/AUTOGENERATED")
  set(AUTOGENERATED_SOURCES)
  
  set(FLATC_EXECUTABLE "flatc")
  find_program(FLATC_EXECUTABLE_SEARCH ${FLATC_EXECUTABLE})
  if(NOT FLATC_EXECUTABLE_SEARCH)
    message(FATAL_ERROR "FlatBuffers compiler not found")
  endif()

  set(SCRIPT_CPP_OPTIONS)
  list(APPEND SCRIPT_CPP_OPTIONS "--gen-object-api")
  list(APPEND SCRIPT_CPP_OPTIONS "--cpp")
  list(APPEND SCRIPT_CPP_OPTIONS "--gen-onefile")
  list(APPEND SCRIPT_CPP_OPTIONS "--gen-all")
  list(APPEND SCRIPT_CPP_OPTIONS "--force-empty")
  list(APPEND SCRIPT_CPP_OPTIONS "--reflect-types")
  list(APPEND SCRIPT_CPP_OPTIONS "--reflect-names")

  set(SCRIPT_TS_OPTIONS)
  list(APPEND SCRIPT_TS_OPTIONS "--gen-object-api")
  list(APPEND SCRIPT_TS_OPTIONS "--ts")
  list(APPEND SCRIPT_TS_OPTIONS "--gen-onefile")
  list(APPEND SCRIPT_TS_OPTIONS "--gen-all")
  list(APPEND SCRIPT_TS_OPTIONS "--no-js-exports")
  list(APPEND SCRIPT_TS_OPTIONS "--force-empty")
  list(APPEND SCRIPT_TS_OPTIONS "--reflect-types")
  list(APPEND SCRIPT_TS_OPTIONS "--reflect-names")

  set(SCRIPT_JS_OPTIONS)
  list(APPEND SCRIPT_JS_OPTIONS "--gen-object-api")
  list(APPEND SCRIPT_JS_OPTIONS "--js")
  list(APPEND SCRIPT_JS_OPTIONS "--gen-onefile")
  list(APPEND SCRIPT_JS_OPTIONS "--gen-all")
  list(APPEND SCRIPT_JS_OPTIONS "--no-js-exports")
  list(APPEND SCRIPT_JS_OPTIONS "--force-empty")
  list(APPEND SCRIPT_JS_OPTIONS "--reflect-types")
  list(APPEND SCRIPT_JS_OPTIONS "--reflect-names")

  add_custom_command(
    OUTPUT
    ${generatedCppFileName}
    COMMAND 
    ${FLATC_EXECUTABLE} ${SCRIPT_CPP_OPTIONS} ${schemaFilePath}
    DEPENDS
    ${schemaFilePath}
    )
  
  add_custom_command(
    OUTPUT
    ${generatedTsFileName}
    COMMAND 
    ${FLATC_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()