Skip to content

Commit

Permalink
Migrate overloaded macro to optional named parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
lukalt committed Nov 4, 2024
1 parent 926d280 commit dceca99
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 82 deletions.
118 changes: 39 additions & 79 deletions ThriftLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,18 @@ macro(bypass_source_check sources)
endmacro()

#
# thrift_generate_named
# thrift_generate
# Supports libary names that differ from the file name (two handle two libraries with the same filename on disk (in different folders))
# This is used to codegen thrift files using the thrift compiler
# Params:
# @file_name - The name of the library that is generated (without the language suffix)
# @real_file_name - The name of the thrift file on disk
# @services - A list of services that are declared in the thrift file
# @language - The generator to use (cpp, cpp2 or py3)
# @options - Extra options to pass to the generator
# @output_path - The directory where the thrift file lives
# @include_prefix - Prefix to use for thrift includes in generated sources
# @TARGET_NAME_BASE - (optional) - name used for target instead of real filename
# @THRIFT_INCLUDE_DIRECTORIES - (optional) path to thrift include directories
#
# Output:
# file-language-target - A custom target to add a dependenct
Expand All @@ -188,9 +190,8 @@ endmacro()
# bypass_source_check(${file_language-SOURCES})
# This will prevent cmake from complaining about missing source files
#
macro(thrift_generate_named
macro(thrift_generate
file_name
real_file_name
services
language
options
Expand All @@ -200,49 +201,54 @@ macro(thrift_generate_named
)
cmake_parse_arguments(THRIFT_GENERATE # Prefix
"" # Options
"" # One Value args
"TARGET_NAME_BASE" # One Value args
"THRIFT_INCLUDE_DIRECTORIES" # Multi-value args
"${ARGN}")

set(source_file_name ${file_name})
set(target_file_name ${file_name})
set(thrift_include_directories)
foreach(dir ${THRIFT_GENERATE_THRIFT_INCLUDE_DIRECTORIES})
list(APPEND thrift_include_directories "-I" "${dir}")
endforeach()
if(DEFINED THRIFT_GENERATE_TARGET_NAME_BASE AND NOT THRIFT_GENERATE_TARGET_NAME_BASE STREQUAL "")
set(target_file_name ${THRIFT_GENERATE_TARGET_NAME_BASE})
endif()

set("${file_name}-${language}-HEADERS"
${output_path}/gen-${language}/${real_file_name}_constants.h
${output_path}/gen-${language}/${real_file_name}_data.h
${output_path}/gen-${language}/${real_file_name}_metadata.h
${output_path}/gen-${language}/${real_file_name}_types.h
${output_path}/gen-${language}/${real_file_name}_types.tcc
set("${target_file_name}-${language}-HEADERS"
${output_path}/gen-${language}/${source_file_name}_constants.h
${output_path}/gen-${language}/${source_file_name}_data.h
${output_path}/gen-${language}/${source_file_name}_metadata.h
${output_path}/gen-${language}/${source_file_name}_types.h
${output_path}/gen-${language}/${source_file_name}_types.tcc
)
set("${file_name}-${language}-SOURCES"
${output_path}/gen-${language}/${real_file_name}_constants.cpp
${output_path}/gen-${language}/${real_file_name}_data.cpp
${output_path}/gen-${language}/${real_file_name}_types.cpp
set("${target_file_name}-${language}-SOURCES"
${output_path}/gen-${language}/${source_file_name}_constants.cpp
${output_path}/gen-${language}/${source_file_name}_data.cpp
${output_path}/gen-${language}/${source_file_name}_types.cpp
)
if("${options}" MATCHES "layouts")
set("${file_name}-${language}-SOURCES"
${${file_name}-${language}-SOURCES}
${output_path}/gen-${language}/${real_file_name}_layouts.cpp
set("${target_file_name}-${language}-SOURCES"
${${target_file_name}-${language}-SOURCES}
${output_path}/gen-${language}/${source_file_name}_layouts.cpp
)
endif()
if(NOT "${options}" MATCHES "no_metadata")
set("${file_name}-${language}-SOURCES"
${${file_name}-${language}-SOURCES}
${output_path}/gen-${language}/${real_file_name}_metadata.cpp
set("${target_file_name}-${language}-SOURCES"
${${target_file_name}-${language}-SOURCES}
${output_path}/gen-${language}/${source_file_name}_metadata.cpp
)
endif()
foreach(service ${services})
set("${file_name}-${language}-HEADERS"
${${real_file_name}-${language}-HEADERS}
set("${target_file_name}-${language}-HEADERS"
${${source_file_name}-${language}-HEADERS}
${output_path}/gen-${language}/${service}.h
${output_path}/gen-${language}/${service}.tcc
${output_path}/gen-${language}/${service}AsyncClient.h
${output_path}/gen-${language}/${service}_custom_protocol.h
)
set("${file_name}-${language}-SOURCES"
${${real_file_name}-${language}-SOURCES}
set("${target_file_name}-${language}-SOURCES"
${${source_file_name}-${language}-SOURCES}
${output_path}/gen-${language}/${service}.cpp
${output_path}/gen-${language}/${service}AsyncClient.cpp
)
Expand All @@ -260,25 +266,25 @@ macro(thrift_generate_named
set(gen_language "mstch_cpp2")
elseif("${language}" STREQUAL "py3")
set(gen_language "mstch_py3")
file(WRITE "${output_path}/gen-${language}/${real_file_name}/__init__.py")
file(WRITE "${output_path}/gen-${language}/${source_file_name}/__init__.py")
endif()
add_custom_command(
OUTPUT ${${file_name}-${language}-HEADERS}
${${file_name}-${language}-SOURCES}
OUTPUT ${${target_file_name}-${language}-HEADERS}
${${target_file_name}-${language}-SOURCES}
COMMAND ${THRIFT1}
--gen "${gen_language}:${options}${include_prefix_text}"
-o ${output_path}
${thrift_include_directories}
"${file_path}/${real_file_name}.thrift"
"${file_path}/${source_file_name}.thrift"
DEPENDS
${THRIFT1}
"${file_path}/${real_file_name}.thrift"
COMMENT "Generating ${file_name} files. Output: ${output_path}"
"${file_path}/${source_file_name}.thrift"
COMMENT "Generating ${target_file_name} files. Output: ${output_path}"
)
add_custom_target(
${file_name}-${language}-target ALL
${target_file_name}-${language}-target ALL
DEPENDS ${${language}-${language}-HEADERS}
${${file_name}-${language}-SOURCES}
${${target_file_name}-${language}-SOURCES}
)
install(
DIRECTORY gen-${language}
Expand All @@ -288,50 +294,4 @@ macro(thrift_generate_named
DIRECTORY gen-${language}
DESTINATION include/${include_prefix}
FILES_MATCHING PATTERN "*.tcc")
endmacro()


#
# thrift_generate
# This is used to codegen thrift files using the thrift compiler
# Params:
# @file_name - The name of tge thrift file
# @services - A list of services that are declared in the thrift file
# @language - The generator to use (cpp, cpp2 or py3)
# @options - Extra options to pass to the generator
# @output_path - The directory where the thrift file lives
#
# Output:
# file-language-target - A custom target to add a dependenct
# ${file-language-HEADERS} - The generated Header Files.
# ${file-language-SOURCES} - The generated Source Files.
#
# Notes:
# If any of the fields is empty, it is still required to provide an empty string
#
# When using file_language-SOURCES it should always call:
# bypass_source_check(${file_language-SOURCES})
# This will prevent cmake from complaining about missing source files
#
macro(thrift_generate
file_name
services
language
options
file_path
output_path
include_prefix
)

thrift_generate_named(
"${file_name}"
"${file_name}"
"${services}"
"${language}"
"${options}"
"${file_path}"
"${output_path}"
"${include_prefix}"
"${ARGN}"
)
endmacro()
6 changes: 3 additions & 3 deletions thrift/lib/cpp2/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ set(ADDITIONAL_SOURCE_FILES ../../thrift/detail/protocol.cpp ../../thrift/TypeTo

# generate conformance thrift files
foreach(conf_name "protocol" "type" "any" "serialization" "patch_data" "conformance")
thrift_generate_named(
"${conf_name}conformance" #file_name
"${conf_name}"
thrift_generate(
"${conf_name}" #file_name
"" #services
"cpp2" #language
"frozen2" #options
"${CMAKE_CURRENT_SOURCE_DIR}/../../../conformance/if" #file_path
"${CMAKE_CURRENT_BINARY_DIR}/../../../conformance/if" #output_path
"thrift/conformance/if" #include_prefix
THRIFT_INCLUDE_DIRECTORIES ${THRIFT_SOURCE_DIR}
TARGET_NAME_BASE "${conf_name}conformance"
)
set(GENERATED_THRIFT_SOURCES
${GENERATED_THRIFT_SOURCES}
Expand Down

0 comments on commit dceca99

Please sign in to comment.