jsonrpcstub.cmake 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # generates JSONRPC Stub Server && Client
  2. #
  3. # this script expects
  4. # ETH_SOURCE_DIR - main CMAKE_SOURCE_DIR
  5. # ETH_SPEC_PATH
  6. # ETH_SERVER_DIR
  7. # ETH_CLIENT_DIR
  8. # ETH_SERVER_NAME
  9. # ETH_CLIENT_NAME
  10. # ETH_JSON_RPC_STUB
  11. #
  12. # example usage:
  13. # cmake -DETH_SPEC_PATH=spec.json -DETH_SERVER_DIR=libweb3jsonrpc -DETH_CLIENT_DIR=test
  14. # -DETH_SERVER_NAME=AbstractWebThreeStubServer -DETH_CLIENT_NAME=WebThreeStubClient -DETH_JSON_RPC_STUB=/usr/local/bin/jsonrpcstub
  15. # setup names, and allow different filename from classname for namespaced classes.
  16. # For an example call look at libethereum/CMakeLists.txt for eth::dev::Sentinel
  17. if (ETH_SERVER_FILENAME)
  18. set(SERVER_TMPFILE "${ETH_SERVER_DIR}/${ETH_SERVER_FILENAME}.h.tmp")
  19. set(SERVER_TMPFILE2 "${ETH_SERVER_DIR}/${ETH_SERVER_FILENAME}.h.tmp2")
  20. set(SERVER_OUTFILE "${ETH_SERVER_DIR}/${ETH_SERVER_FILENAME}.h")
  21. else ()
  22. set(SERVER_TMPFILE "${ETH_SERVER_DIR}/${ETH_SERVER_NAME}.h.tmp")
  23. set(SERVER_TMPFILE2 "${ETH_SERVER_DIR}/${ETH_SERVER_NAME}.h.tmp2")
  24. set(SERVER_OUTFILE "${ETH_SERVER_DIR}/${ETH_SERVER_NAME}.h")
  25. endif()
  26. if (ETH_CLIENT_FILENAME)
  27. set(CLIENT_TMPFILE "${ETH_CLIENT_DIR}/${ETH_CLIENT_FILENAME}.h.tmp")
  28. set(CLIENT_OUTFILE "${ETH_CLIENT_DIR}/${ETH_CLIENT_FILENAME}.h")
  29. else ()
  30. set(CLIENT_TMPFILE "${ETH_CLIENT_DIR}/${ETH_CLIENT_NAME}.h.tmp")
  31. set(CLIENT_OUTFILE "${ETH_CLIENT_DIR}/${ETH_CLIENT_NAME}.h")
  32. endif ()
  33. # create tmp files
  34. if (NOT ETH_SERVER_DIR)
  35. execute_process(
  36. COMMAND ${ETH_JSON_RPC_STUB} ${ETH_SPEC_PATH}
  37. --cpp-client=${ETH_CLIENT_NAME} --cpp-client-file=${CLIENT_TMPFILE}
  38. OUTPUT_VARIABLE ERR ERROR_QUIET
  39. )
  40. else ()
  41. execute_process(
  42. COMMAND ${ETH_JSON_RPC_STUB} ${ETH_SPEC_PATH}
  43. --cpp-server=${ETH_SERVER_NAME} --cpp-server-file=${SERVER_TMPFILE}
  44. --cpp-client=${ETH_CLIENT_NAME} --cpp-client-file=${CLIENT_TMPFILE}
  45. OUTPUT_VARIABLE ERR ERROR_QUIET
  46. )
  47. # get name without namespace
  48. string(REPLACE "::" ";" SERVER_NAME_LIST "${ETH_SERVER_NAME}")
  49. list(LENGTH SERVER_NAME_LIST SERVER_NAME_LENGTH)
  50. math(EXPR SERVER_NAME_POS "${SERVER_NAME_LENGTH} - 1")
  51. list(GET SERVER_NAME_LIST ${SERVER_NAME_POS} SERVER_NAME)
  52. file(READ ${SERVER_TMPFILE} SERVER_CONTENT)
  53. # The following cmake regexps are equal to this sed command
  54. # sed -e s/include\ \<jsonrpccpp\\/server\.h\>/include\ ${INCLUDE_NAME}/g \
  55. # -e s/public\ jsonrpc::AbstractServer\<${NAME}\>/public\ ServerInterface\<${NAME}\>/g \
  56. # -e s/${NAME}\(jsonrpc::AbstractServerConnector\ \&conn,\ jsonrpc::serverVersion_t\ type\ =\ jsonrpc::JSONRPC_SERVER_V2\)\ :\ jsonrpc::AbstractServer\<${NAME}\>\(conn,\ type\)/${NAME}\(\)/g \
  57. string(REGEX REPLACE "include\ <jsonrpccpp/server.h>" "include\ \"ModularServer.h\"" SERVER_CONTENT "${SERVER_CONTENT}")
  58. string(REGEX REPLACE "public\ jsonrpc::AbstractServer<${SERVER_NAME}>" "public ServerInterface<${SERVER_NAME}>" SERVER_CONTENT "${SERVER_CONTENT}")
  59. string(REGEX REPLACE "${SERVER_NAME}\\(jsonrpc::AbstractServerConnector\ &conn,\ jsonrpc::serverVersion_t\ type\ =\ jsonrpc::JSONRPC_SERVER_V2\\)\ :\ jsonrpc::AbstractServer<${SERVER_NAME}>\\(conn, type\\)" "${SERVER_NAME}()" SERVER_CONTENT "${SERVER_CONTENT}")
  60. file(WRITE ${SERVER_TMPFILE2} "${SERVER_CONTENT}")
  61. endif()
  62. # don't throw fatal error on jsonrpcstub error, someone might have old version of jsonrpcstub,
  63. # he does not need to upgrade it if he is not working on JSON RPC
  64. # show him warning instead
  65. if (ERR)
  66. message(WARNING "Your version of jsonrcpstub tool is not supported. Please upgrade it.")
  67. message(WARNING "${ERR}")
  68. else()
  69. include("${ETH_CMAKE_DIR}/EthUtils.cmake")
  70. if (ETH_SERVER_DIR)
  71. file(REMOVE "${SERVER_TMPFILE}")
  72. replace_if_different("${SERVER_TMPFILE2}" "${SERVER_OUTFILE}")
  73. endif()
  74. replace_if_different("${CLIENT_TMPFILE}" "${CLIENT_OUTFILE}")
  75. endif()