1 # https://github.com/snikulov/cmake-modules/blob/master/FindThrift.cmake
3 # - Find Thrift (a cross platform RPC lib/tool)
5 # thrift_VERSION_STRING, version string of ant if found
6 # thrift_LIBRARIES, libraries to link
7 # thrift_INCLUDE_DIR, where to find thrift headers
8 # thrift_COMPILER, thrift compiler executable
9 # thrift_FOUND, If false, do not try to use ant
11 # thrift_gen_cpp(<path to thrift file> <output variable with file list>)
13 # Initial work was done by Cloudera https://github.com/cloudera/Impala
14 # 2014 - modified by snikulov
16 # prefer the thrift version supplied in thrift_HOME (cmake -Dthrift_HOME then environment)
17 find_path(thrift_INCLUDE_DIR
29 # prefer the thrift version supplied in thrift_HOME
30 find_library(thrift_LIBRARIES
42 find_program(thrift_COMPILER
55 exec_program(${thrift_COMPILER}
56 ARGS -version OUTPUT_VARIABLE __thrift_OUT RETURN_VALUE thrift_RETURN)
57 string(REGEX MATCH "[0-9]+.[0-9]+.[0-9]+-[a-z]+$" thrift_VERSION_STRING ${__thrift_OUT})
59 # define utility function to generate cpp files
60 function(thrift_gen_cpp thrift_file thrift_CPP_FILES_LIST thrift_GEN_INCLUDE_DIR)
63 if(EXISTS ${thrift_file})
64 get_filename_component(_target_dir ${thrift_file} NAME_WE)
65 message("thrif_gen_cpp: ${thrift_file}")
67 if(NOT EXISTS ${CMAKE_BINARY_DIR}/${_target_dir})
68 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/${_target_dir})
70 exec_program(${thrift_COMPILER}
71 ARGS -o "${CMAKE_BINARY_DIR}/${_target_dir}" --gen cpp ${thrift_file}
72 OUTPUT_VARIABLE __thrift_OUT
73 RETURN_VALUE thrift_RETURN)
74 file(GLOB_RECURSE __result_src "${CMAKE_BINARY_DIR}/${_target_dir}/*.cpp")
75 file(GLOB_RECURSE __result_hdr "${CMAKE_BINARY_DIR}/${_target_dir}/*.h")
76 list(APPEND _res ${__result_src})
77 list(APPEND _res ${__result_hdr})
79 list(GET __result_hdr 0 _res_inc_path)
80 get_filename_component(_res_inc_path ${_res_inc_path} DIRECTORY)
83 message("thrift_gen_cpp: file ${thrift_file} does not exists")
85 set(${thrift_CPP_FILES_LIST} "${_res}" PARENT_SCOPE)
86 set(${thrift_GEN_INCLUDE_DIR} "${_res_inc_path}" PARENT_SCOPE)
91 include(FindPackageHandleStandardArgs)
92 FIND_PACKAGE_HANDLE_STANDARD_ARGS(thrift DEFAULT_MSG thrift_LIBRARIES thrift_INCLUDE_DIR thrift_COMPILER)
93 mark_as_advanced(thrift_LIBRARIES thrift_INCLUDE_DIR thrift_COMPILER thrift_VERSION_STRING)