From ceffaf3d85bb5ae61302956945481fa376a915d2 Mon Sep 17 00:00:00 2001 From: Teon Banek Date: Thu, 30 Aug 2018 10:40:59 +0200 Subject: [PATCH] Move add_lcp and add_capnp to cmake/functions.cmake Summary: These functions were defined in multiple places. They are moved to cmake/functions.cmake to keep only one source of truth. Reviewers: mferencevic, msantl, mculinovic Reviewed By: mferencevic Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D1578 --- cmake/functions.cmake | 76 ++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 60 +------------------------ src/communication/CMakeLists.txt | 19 +------- src/io/CMakeLists.txt | 18 +------- src/stats/CMakeLists.txt | 61 ++----------------------- src/utils/CMakeLists.txt | 22 ++------- 6 files changed, 87 insertions(+), 169 deletions(-) diff --git a/cmake/functions.cmake b/cmake/functions.cmake index b285b6a51..4e228c738 100644 --- a/cmake/functions.cmake +++ b/cmake/functions.cmake @@ -85,3 +85,79 @@ function(get_target_cxx_flags target result) endif() set(${result} ${flags} PARENT_SCOPE) endfunction() + +# Define `add_capnp` function for registering a capnp file for generation. +# +# The `define_add_capnp` expects 2 arguments: +# * main_src_files -- variable to be updated with generated cpp files +# * generated_capnp_files -- variable to be updated with generated hpp and cpp files +# +# The `add_capnp` function expects a single argument, path to capnp file. +# Each added file is standalone and we avoid recompiling everything. +macro(define_add_capnp main_src_files generated_capnp_files) + function(add_capnp capnp_src_file) + set(cpp_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.c++) + set(h_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.h) + add_custom_command(OUTPUT ${cpp_file} ${h_file} + COMMAND ${CAPNP_EXE} compile -o${CAPNP_CXX_EXE} ${capnp_src_file} -I ${CMAKE_SOURCE_DIR}/src + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file} capnproto-proj + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + # Update *global* generated_capnp_files + set(${generated_capnp_files} ${${generated_capnp_files}} ${cpp_file} ${h_file} PARENT_SCOPE) + # Update *global* main_src_files + set(${main_src_files} ${${main_src_files}} ${cpp_file} PARENT_SCOPE) + endfunction(add_capnp) +endmacro(define_add_capnp) + +# Lisp C++ Preprocessing + +set(lcp_exe ${CMAKE_SOURCE_DIR}/tools/lcp) +set(lcp_src_files ${CMAKE_SOURCE_DIR}/src/lisp/lcp.lisp ${lcp_exe}) + +# Define `add_lcp` function for registering a lcp file for generation. +# +# The `define_add_lcp` expects 2 arguments: +# * main_src_files -- variable to be updated with generated cpp files +# * generated_lcp_files -- variable to be updated with generated hpp, cpp and capnp files +# +# The `add_lcp` function expects at least a single argument, path to lcp file. +# Each added file is standalone and we avoid recompiling everything. +# You may pass a CAPNP_SCHEMA keyword argument to generate the Cap'n Proto +# serialization code from .lcp file. You still need to add the generated capnp +# file through `add_capnp` function. To generate the use `capnp id` +# invocation, and specify it here. This preserves correct id information across +# multiple schema generations. If this wasn't the case, wrong typeId +# information will break RPC between different compilations of memgraph. +# Instead of CAPNP_SCHEMA, you may pass the CAPNP_DECLARATION option. This will +# generate serialization declarations but not the implementation. +macro(define_add_lcp main_src_files generated_lcp_files) + function(add_lcp lcp_file) + set(options CAPNP_DECLARATION) + set(one_value_kwargs CAPNP_SCHEMA) + set(multi_value_kwargs DEPENDS) + # NOTE: ${${}ARGN} syntax escapes evaluating macro's ARGN variable; see: + # https://stackoverflow.com/questions/50365544/how-to-access-enclosing-functions-arguments-from-within-a-macro + cmake_parse_arguments(KW "${options}" "${one_value_kwargs}" "${multi_value_kwargs}" ${${}ARGN}) + string(REGEX REPLACE "\.lcp$" ".hpp" h_file + "${CMAKE_CURRENT_SOURCE_DIR}/${lcp_file}") + if (KW_CAPNP_SCHEMA AND NOT KW_CAPNP_DECLARATION) + string(REGEX REPLACE "\.lcp$" ".capnp" capnp_file + "${CMAKE_CURRENT_SOURCE_DIR}/${lcp_file}") + set(capnp_id ${KW_CAPNP_SCHEMA}) + set(capnp_depend capnproto-proj) + set(cpp_file ${CMAKE_CURRENT_SOURCE_DIR}/${lcp_file}.cpp) + # Update *global* main_src_files + set(${main_src_files} ${${main_src_files}} ${cpp_file} PARENT_SCOPE) + endif() + if (KW_CAPNP_DECLARATION) + set(capnp_id "--capnp-declaration") + endif() + add_custom_command(OUTPUT ${h_file} ${cpp_file} ${capnp_file} + COMMAND ${lcp_exe} ${lcp_file} ${capnp_id} + VERBATIM + DEPENDS ${lcp_file} ${lcp_src_files} ${capnp_depend} ${KW_DEPENDS} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + # Update *global* generated_lcp_files + set(${generated_lcp_files} ${${generated_lcp_files}} ${h_file} ${cpp_file} ${capnp_file} PARENT_SCOPE) + endfunction(add_lcp) +endmacro(define_add_lcp) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 947c514fd..36b187655 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -81,65 +81,9 @@ set(memgraph_src_files ) # ----------------------------------------------------------------------------- -# Use this function to add each capnp file to generation. This way each file is -# standalone and we avoid recompiling everything. -# NOTE: memgraph_src_files and generated_capnp_files are globally updated. -function(add_capnp capnp_src_file) - set(cpp_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.c++) - set(h_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.h) - add_custom_command(OUTPUT ${cpp_file} ${h_file} - COMMAND ${CAPNP_EXE} compile -o${CAPNP_CXX_EXE} ${capnp_src_file} -I ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file} capnproto-proj - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - # Update *global* generated_capnp_files - set(generated_capnp_files ${generated_capnp_files} ${cpp_file} ${h_file} PARENT_SCOPE) - # Update *global* memgraph_src_files - set(memgraph_src_files ${memgraph_src_files} ${cpp_file} PARENT_SCOPE) -endfunction(add_capnp) +define_add_capnp(memgraph_src_files generated_capnp_files) -# Lisp C++ Preprocessing - -set(lcp_exe ${CMAKE_SOURCE_DIR}/tools/lcp) -set(lcp_src_files lisp/lcp.lisp ${lcp_exe}) - -# Use this function to add each lcp file to generation. This way each file is -# standalone and we avoid recompiling everything. -# -# You may pass a CAPNP_SCHEMA keyword argument to generate the Cap'n Proto -# serialization code from .lcp file. You still need to add the generated capnp -# file through `add_capnp` function. To generate the use `capnp id` -# invocation, and specify it here. This preserves correct id information across -# multiple schema generations. If this wasn't the case, wrong typeId -# information will break RPC between different compilations of memgraph. -# -# NOTE: memgraph_src_files and generated_lcp_files are globally updated. -function(add_lcp lcp_file) - set(options CAPNP_DECLARATION) - set(one_value_kwargs CAPNP_SCHEMA) - set(multi_value_kwargs DEPENDS) - cmake_parse_arguments(KW "${options}" "${one_value_kwargs}" "${multi_value_kwargs}" ${ARGN}) - string(REGEX REPLACE "\.lcp$" ".hpp" h_file - "${CMAKE_CURRENT_SOURCE_DIR}/${lcp_file}") - if (KW_CAPNP_SCHEMA AND NOT KW_CAPNP_DECLARATION) - string(REGEX REPLACE "\.lcp$" ".capnp" capnp_file - "${CMAKE_CURRENT_SOURCE_DIR}/${lcp_file}") - set(capnp_id ${KW_CAPNP_SCHEMA}) - set(capnp_depend capnproto-proj) - set(cpp_file ${CMAKE_CURRENT_SOURCE_DIR}/${lcp_file}.cpp) - # Update *global* memgraph_src_files - set(memgraph_src_files ${memgraph_src_files} ${cpp_file} PARENT_SCOPE) - endif() - if (KW_CAPNP_DECLARATION) - set(capnp_id "--capnp-declaration") - endif() - add_custom_command(OUTPUT ${h_file} ${cpp_file} ${capnp_file} - COMMAND ${lcp_exe} ${lcp_file} ${capnp_id} - VERBATIM - DEPENDS ${lcp_file} ${lcp_src_files} ${capnp_depend} ${KW_DEPENDS} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - # Update *global* generated_lcp_files - set(generated_lcp_files ${generated_lcp_files} ${h_file} ${cpp_file} ${capnp_file} PARENT_SCOPE) -endfunction(add_lcp) +define_add_lcp(memgraph_src_files generated_lcp_files) add_lcp(database/counters_rpc_messages.lcp CAPNP_SCHEMA @0x95a2c3ea3871e945) add_capnp(database/counters_rpc_messages.capnp) diff --git a/src/communication/CMakeLists.txt b/src/communication/CMakeLists.txt index c9bd02f58..bfc48660b 100644 --- a/src/communication/CMakeLists.txt +++ b/src/communication/CMakeLists.txt @@ -13,24 +13,7 @@ set(communication_src_files set(communication_src_files ${communication_src_files} ${CMAKE_SOURCE_DIR}/src/data_structures/concurrent/skiplist_gc.cpp) -# Use this function to add each capnp file to generation. This way each file is -# standalone and we avoid recompiling everything. -# NOTE: communication_src_files and communication_capnp_files are globally updated. -# TODO: This is duplicated from src/CMakeLists.txt and -# src/utils/CMakeLists.txt, find a good way to generalize this on per -# subdirectory basis. -function(add_capnp capnp_src_file) - set(cpp_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.c++) - set(h_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.h) - add_custom_command(OUTPUT ${cpp_file} ${h_file} - COMMAND ${CAPNP_EXE} compile -o${CAPNP_CXX_EXE} ${capnp_src_file} -I ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file} capnproto-proj - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - # Update *global* communication_capnp_files - set(communication_capnp_files ${communication_capnp_files} ${cpp_file} ${h_file} PARENT_SCOPE) - # Update *global* communication_src_files - set(communication_src_files ${communication_src_files} ${cpp_file} PARENT_SCOPE) -endfunction(add_capnp) +define_add_capnp(communication_src_files communication_capnp_files) add_capnp(rpc/messages.capnp) diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt index 10cb3263a..e6bd87933 100644 --- a/src/io/CMakeLists.txt +++ b/src/io/CMakeLists.txt @@ -4,23 +4,7 @@ set(io_src_files network/socket.cpp network/utils.cpp) -# Use this function to add each capnp file to generation. This way each file is -# standalone and we avoid recompiling everything. -# NOTE: io_src_files and io_capnp_files are globally updated. -# TODO: This is duplicated from src/CMakeLists.txt, find a good way to -# generalize this on per subdirectory basis. -function(add_capnp capnp_src_file) - set(cpp_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.c++) - set(h_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.h) - add_custom_command(OUTPUT ${cpp_file} ${h_file} - COMMAND ${CAPNP_EXE} compile -o${CAPNP_CXX_EXE} ${capnp_src_file} -I ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file} capnproto-proj - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - # Update *global* io_capnp_files - set(io_capnp_files ${io_capnp_files} ${cpp_file} ${h_file} PARENT_SCOPE) - # Update *global* io_src_files - set(io_src_files ${io_src_files} ${cpp_file} PARENT_SCOPE) -endfunction(add_capnp) +define_add_capnp(io_src_files io_capnp_files) add_capnp(network/endpoint.capnp) diff --git a/src/stats/CMakeLists.txt b/src/stats/CMakeLists.txt index d03a74d33..a1ecbd8fd 100644 --- a/src/stats/CMakeLists.txt +++ b/src/stats/CMakeLists.txt @@ -2,64 +2,8 @@ set(stats_src_files metrics.cpp stats.cpp) -set(lcp_exe ${CMAKE_SOURCE_DIR}/tools/lcp) -set(lcp_src_files ../lisp/lcp.lisp ${lcp_exe}) - -# Use this function to add each lcp file to generation. This way each file is -# standalone and we avoid recompiling everything. -# -# You may pass a CAPNP_SCHEMA keyword argument to generate the Cap'n Proto -# serialization code from .lcp file. You still need to add the generated capnp -# file through `add_capnp` function. To generate the use `capnp id` -# invocation, and specify it here. This preserves correct id information across -# multiple schema generations. If this wasn't the case, wrong typeId -# information will break RPC between different compilations of memgraph. -# -# NOTE: stats_src_files and stats_lcp_files are globally updated. -# TODO: This is duplicated from src/CMakeLists.txt, -# find a good way to generalize this on per -# subdirectory basis. -function(add_lcp lcp_file) - set(one_value_kwargs CAPNP_SCHEMA) - cmake_parse_arguments(KW "" "${one_value_kwargs}" "" ${ARGN}) - string(REGEX REPLACE "\.lcp$" ".hpp" h_file - "${CMAKE_CURRENT_SOURCE_DIR}/${lcp_file}") - if (KW_CAPNP_SCHEMA) - string(REGEX REPLACE "\.lcp$" ".capnp" capnp_file - "${CMAKE_CURRENT_SOURCE_DIR}/${lcp_file}") - set(capnp_id ${KW_CAPNP_SCHEMA}) - set(capnp_depend capnproto-proj) - set(cpp_file ${CMAKE_CURRENT_SOURCE_DIR}/${lcp_file}.cpp) - # Update *global* stats_src_files - set(stats_src_files ${stats_src_files} ${cpp_file} PARENT_SCOPE) - endif() - add_custom_command(OUTPUT ${h_file} ${cpp_file} ${capnp_file} - COMMAND ${lcp_exe} ${lcp_file} ${capnp_id} - VERBATIM - DEPENDS ${lcp_file} ${lcp_src_files} ${capnp_depend} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - # Update *global* stats_lcp_files - set(stats_lcp_files ${stats_lcp_files} ${h_file} ${cpp_file} ${capnp_file} PARENT_SCOPE) -endfunction(add_lcp) - -# Use this function to add each capnp file to generation. This way each file is -# standalone and we avoid recompiling everything. -# NOTE: stats_src_files and stats_capnp_files are globally updated. -# TODO: This is duplicated from src/CMakeLists.txt and -# src/utils/CMakeLists.txt, find a good way to generalize this on per -# subdirectory basis. -function(add_capnp capnp_src_file) - set(cpp_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.c++) - set(h_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.h) - add_custom_command(OUTPUT ${cpp_file} ${h_file} - COMMAND ${CAPNP_EXE} compile -o${CAPNP_CXX_EXE} ${capnp_src_file} -I ${CMAKE_CURRENT_SOURCE_DIR}/.. - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file} capnproto-proj - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - # Update *global* stats_capnp_files - set(stats_capnp_files ${stats_capnp_files} ${cpp_file} ${h_file} PARENT_SCOPE) - # Update *global* stats_src_files - set(stats_src_files ${stats_src_files} ${cpp_file} PARENT_SCOPE) -endfunction(add_capnp) +define_add_capnp(stats_src_files stats_capnp_files) +define_add_lcp(stats_src_files stats_lcp_files) add_lcp(stats_rpc_messages.lcp CAPNP_SCHEMA @0xc19a87c81b9b4512) add_capnp(stats_rpc_messages.capnp) @@ -70,4 +14,5 @@ add_custom_target(generate_stats_capnp DEPENDS generate_stats_lcp ${stats_capnp_ add_library(mg-stats STATIC ${stats_src_files}) target_link_libraries(mg-stats Threads::Threads mg-utils mg-io mg-communication fmt glog gflags) target_link_libraries(mg-stats capnp kj) +add_dependencies(mg-stats generate_stats_lcp) add_dependencies(mg-stats generate_stats_capnp) diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 0222f24d8..d7b334a20 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -7,27 +7,13 @@ set(utils_src_files uuid.cpp watchdog.cpp) -# Use this function to add each capnp file to generation. This way each file is -# standalone and we avoid recompiling everything. -# NOTE: utils_src_files and utils_capnp_files are globally updated. -# TODO: This is duplicated from src/CMakeLists.txt, -# find a good way to generalize this on per -# subdirectory basis. -function(add_capnp capnp_src_file) - set(cpp_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.c++) - set(h_file ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file}.h) - add_custom_command(OUTPUT ${cpp_file} ${h_file} - COMMAND ${CAPNP_EXE} compile -o${CAPNP_CXX_EXE} ${capnp_src_file} -I ${CMAKE_CURRENT_SOURCE_DIR}/.. - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_src_file} capnproto-proj - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - # Update *global* stats_capnp_files - set(utils_capnp_files ${utils_capnp_files} ${cpp_file} ${h_file} PARENT_SCOPE) - # Update *global* stats_src_files - set(utils_src_files ${utils_src_files} ${cpp_file} PARENT_SCOPE) -endfunction(add_capnp) +define_add_capnp(utils_src_files utils_capnp_files) add_capnp(serialization.capnp) +add_custom_target(generate_utils_capnp DEPENDS ${utils_capnp_files}) + add_library(mg-utils STATIC ${utils_src_files}) target_link_libraries(mg-utils stdc++fs Threads::Threads fmt glog gflags uuid) target_link_libraries(mg-utils capnp kj) +add_dependencies(mg-utils generate_utils_capnp)