github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/api/openapi-spec/thirdparty.srv.cmake (about)

     1  include(CMakeParseArguments)
     2  #
     3  # log_execute_process is same to execute_process but message cmd to log
     4  #
     5  
     6  macro(log_debug msg)
     7      get_filename_component(name ${CMAKE_CURRENT_LIST_FILE} NAME)
     8      string(TIMESTAMP TIME_NOW "%Y-%m-%d %H:%M:%S")
     9      message("${TIME_NOW} - ${name}:${CMAKE_CURRENT_LIST_LINE} - ${msg}")
    10  endmacro(log_debug)
    11  
    12  macro(log_warn msg)
    13      get_filename_component(name ${CMAKE_CURRENT_LIST_FILE} NAME)
    14      string(TIMESTAMP TIME_NOW "%Y-%m-%d %H:%M:%S")
    15      message(WARNING "${TIME_NOW} - ${name}:${CMAKE_CURRENT_LIST_LINE} - ${msg}")
    16  endmacro(log_warn)
    17  #
    18  macro(log_error msg)
    19      get_filename_component(name ${CMAKE_CURRENT_LIST_FILE} NAME)
    20      string(TIMESTAMP TIME_NOW "%Y-%m-%d %H:%M:%S")
    21      message(FATAL_ERROR "${TIME_NOW} - ${name}:${CMAKE_CURRENT_LIST_LINE} - ${msg}")
    22  endmacro(log_error)
    23  
    24  # debug(K1 K2 K3)
    25  #
    26  macro(debug)
    27      FOREACH(A ${ARGN})
    28        log_debug("${A}:${${A}}")
    29      ENDFOREACH(A)
    30  endmacro(debug)
    31  
    32  ###################
    33  #no Special symbol in cmd
    34  macro(log_execute_process)
    35      set(options "" )
    36      set(oneValueArgs WORKING_DIRECTORY )
    37      set(multiValueArgs COMMAND)
    38      cmake_parse_arguments(log_execute_process "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
    39      string(REPLACE ";" " " CMD "${log_execute_process_COMMAND}")
    40  
    41      if(NOT log_execute_process_WORKING_DIRECTORY)
    42           log_error("bad cmd:cd ${log_execute_process_WORKING_DIRECTORY} && ${CMD}")
    43      endif()
    44  
    45      if(NOT EXISTS ${log_execute_process_WORKING_DIRECTORY})
    46           log_error("bad cmd:cd ${log_execute_process_WORKING_DIRECTORY} && ${CMD}")
    47      endif()
    48  
    49      log_debug("cd ${log_execute_process_WORKING_DIRECTORY} && ${CMD}")
    50      execute_process(COMMAND ${log_execute_process_COMMAND}
    51                      WORKING_DIRECTORY ${log_execute_process_WORKING_DIRECTORY}
    52                      RESULT_VARIABLE rv ERROR_VARIABLE er
    53                      OUTPUT_FILE ${CMAKE_BINARY_DIR}/cmake_cmd_out.log
    54                      ERROR_FILE ${CMAKE_BINARY_DIR}/cmake_error.log)
    55      if(rv)
    56         log_debug("RESULT:${rv}, CMD:${log_execute_process_COMMAND}")
    57         execute_process(COMMAND cat ${CMAKE_BINARY_DIR}/cmake_error.log WORKING_DIRECTORY ${log_execute_process_WORKING_DIRECTORY})
    58         log_error("fatal error exit!")
    59      endif()
    60  
    61  endmacro(log_execute_process)
    62  
    63  macro(backup_and_mv_file)
    64      file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/.backup)
    65      FOREACH(C ${ARGN})
    66          if(EXISTS ${C})
    67               log_execute_process(COMMAND mv -f --backup=t ${C} -t ${CMAKE_SOURCE_DIR}/.backup WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
    68          endif()
    69      ENDFOREACH(C)
    70  endmacro(backup_and_mv_file)
    71  
    72  
    73  
    74  macro(uncompress_package package output_dirname)
    75      get_filename_component(package_name ${package} NAME)
    76      get_filename_component(package_path ${package} PATH)
    77  
    78      if(${package_name} MATCHES ".zip$")
    79             string(FIND ${package_name} ".zip" LAST_POS REVERSE)
    80             set(UMCOMPRESS_CMD unzip -o)
    81      elseif(${package_name} MATCHES ".tar.gz$")
    82             string(FIND ${package_name} ".tar.gz" LAST_POS REVERSE)
    83             set(UMCOMPRESS_CMD tar zxvf)
    84      elseif(${package_name} MATCHES ".tar$")
    85             string(FIND ${package_name} ".tar" LAST_POS REVERSE)
    86             set(UMCOMPRESS_CMD tar xvf)
    87      endif()
    88  
    89      string(SUBSTRING ${package_name} 0 ${LAST_POS} dirname)
    90      set(package_dirname ${dirname} PARENT_SCOPE)
    91  
    92      if(NOT EXISTS ${output_dirname})
    93             file(MAKE_DIRECTORY ${output_dirname})
    94      endif()
    95  
    96      if(NOT EXISTS ${output_dirname}/${dirname})
    97             log_execute_process(COMMAND ${UMCOMPRESS_CMD} ${package} WORKING_DIRECTORY ${output_dirname})
    98      elseif(${package} IS_NEWER_THAN ${output_dirname}/${dirname})
    99             #backup_and_mv_file(${output_dirname}/${dirname})
   100             log_execute_process(COMMAND rm -fr ${output_dirname}/${dirname} WORKING_DIRECTORY ${output_dirname})
   101             log_execute_process(COMMAND ${UMCOMPRESS_CMD} ${package} WORKING_DIRECTORY ${output_dirname})
   102      endif()
   103      
   104  endmacro(uncompress_package)
   105  
   106  
   107  #
   108  # check_and_install(PACKAGE TARGET)
   109  #
   110  function(check_and_install)
   111  
   112      get_filename_component(install_dir_path ${CMAKE_CURRENT_LIST_FILE} PATH)
   113      get_filename_component(install_dir_name ${install_dir_path} NAME)
   114  
   115      set(options "" )
   116      set(oneValueArgs "")
   117      set(multiValueArgs PACKAGE TARGET)
   118      cmake_parse_arguments(C "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
   119  
   120      #0 parse args
   121      if(NOT C_PACKAGE OR NOT C_TARGET)
   122           log_error("check_and_install PACKAGE xx\n      TARGET xx\n INSTALL xx\n  CLEAR xx\n")
   123      endif()
   124      #debug(C_PACKAGE C_TARGET)
   125  
   126      # #1 check is package
   127      FOREACH(P ${C_PACKAGE})
   128           if(NOT EXISTS ${P})
   129                log_error("check_and_install: ${P} NOT FOUND")
   130           endif()
   131      ENDFOREACH(P)
   132  
   133      #1 check is need rebuild
   134      SET(${install_dir_name}_NEED_INSTALL FALSE)
   135      FOREACH(P ${C_PACKAGE})
   136          FOREACH(T ${C_TARGET})
   137               #1.1 target not exists
   138               if(NOT EXISTS ${T})
   139                 SET(${install_dir_name}_NEED_INSTALL TRUE)
   140                 log_debug("check_and_install: ${T} NOT FOUND, INSTALL ${C_PACKAGE} to ${install_dir_path}, ${install_dir_name}_NEED_INSTALL:${${install_dir_name}_NEED_INSTALL}")
   141                 
   142                 break()
   143              endif()
   144  
   145              #1.2 target has been changed
   146              if(${P} IS_NEWER_THAN ${T})
   147                 log_debug("check_and_install: ${P} newer than ${T}, INSTALL ${C_PACKAGE} to ${install_dir_path}, ${install_dir_name}_NEED_INSTALL:${${install_dir_name}_NEED_INSTALL}")
   148                 SET(${install_dir_name}_NEED_INSTALL TRUE)
   149                 break()
   150              endif()
   151           ENDFOREACH(T)
   152      ENDFOREACH(P)
   153  
   154      #return
   155      SET(${install_dir_name}_NEED_INSTALL ${${install_dir_name}_NEED_INSTALL} PARENT_SCOPE)
   156  endfunction(check_and_install)
   157  
   158  function(find_and_set_package install_dir_path)
   159  
   160      set(options "" )
   161      set(oneValueArgs INCLUDE LIB BIN)
   162      set(multiValueArgs "")
   163      cmake_parse_arguments(find_and_set_package "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
   164  
   165      if(NOT  find_and_set_package_INCLUDE)
   166          SET(find_and_set_package_INCLUDE include)
   167      endif()
   168      if(NOT find_and_set_package_LIB)
   169          SET(find_and_set_package_LIB lib)
   170      endif()
   171      if(NOT  find_and_set_package_BIN)
   172          SET(find_and_set_package_BIN bin)
   173      endif()
   174  
   175      get_filename_component(install_dir_name ${install_dir_path} NAME)
   176      set(PRE ${install_dir_name})
   177      set(${PRE}_INSTALL_DIR ${install_dir_path})
   178      SET(${PRE}_INCLUDE ${${PRE}_INSTALL_DIR}/${find_and_set_package_INCLUDE})
   179      SET(${PRE}_LIB ${${PRE}_INSTALL_DIR}/${find_and_set_package_LIB})
   180      SET(${PRE}_BIN ${${PRE}_INSTALL_DIR}/${find_and_set_package_BIN})
   181      
   182  
   183      # include
   184      if(NOT EXISTS ${${PRE}_INCLUDE} )
   185           log_error("${PRE}_INCLUDE:${${PRE}_INCLUDE} NOT FOUND, Please check_and_install first!")
   186      endif()
   187      file(GLOB_RECURSE ${PRE}_INCLUDES  ${${PRE}_INCLUDE}/*.h)
   188      #if(NOT ${PRE}_INCLUDES)
   189      #    log_warn("${${PRE}_INCLUDE} NOT FOUND HEADES, Please check_and_install first!")
   190      #endif()
   191  
   192      # use lib64 instead 
   193      if(NOT EXISTS ${${PRE}_LIB})
   194          SET(${PRE}_LIB  ${${PRE}_INSTALL_DIR}/lib64)
   195      endif()    
   196      # libs
   197      if(NOT EXISTS ${${PRE}_LIB} )
   198           log_error("${${PRE}_LIB} NOT FOUND, Please check_and_install first!")
   199      endif()   
   200  
   201  
   202      #static lib
   203      file(GLOB ${PRE}_STATIC_LIBS ${${PRE}_LIB}/lib*.a)
   204      FOREACH(A ${${PRE}_STATIC_LIBS})
   205           get_filename_component(_lib_file_name ${A} NAME_WE)
   206           string(SUBSTRING  ${_lib_file_name} 3 -1  _lib_name)
   207           if(NOT _lib_name)
   208                log_error("Bad lib name: ${A} in ${${PRE}_LIB}")
   209           endif()
   210  
   211          SET(${_lib_name}_A ${A} PARENT_SCOPE)
   212          add_library(${_lib_name}-static STATIC IMPORTED)
   213          set_property(TARGET ${_lib_name}-static PROPERTY IMPORTED_LOCATION ${A})
   214          log_debug("SET ${_lib_name}-static: ${A}")
   215      ENDFOREACH(A)
   216  
   217      #sharelib
   218      file(GLOB ${PRE}_SHARED_LIBS ${${PRE}_LIB}/lib*.so)
   219      FOREACH(A ${${PRE}_SHARED_LIBS})
   220           get_filename_component(_lib_file_name ${A} NAME_WE)
   221           string(SUBSTRING  ${_lib_file_name} 3 -1  _lib_name)
   222           if(NOT _lib_name)
   223                log_error("Bad lib name: ${A} in ${${PRE}_LIB}")
   224           endif()
   225  
   226          SET(${_lib_name}_SO ${A})
   227          add_library(${_lib_name}-shared SHARED IMPORTED)
   228          set_property(TARGET ${_lib_name}-shared PROPERTY IMPORTED_LOCATION ${A})
   229          log_debug("SET ${_lib_name}-shared: ${A}")
   230      ENDFOREACH(A)
   231  
   232      # cmds
   233      file(GLOB ${PRE}_BINS ${${PRE}_BIN}/*)
   234      FOREACH(A ${${PRE}_BINS})
   235          get_filename_component(P ${A} NAME)
   236          SET(${P}_BIN ${A})
   237          log_debug("SET ${P}_BIN: ${${P}_BIN}")
   238          SET(${P}_BIN ${A} PARENT_SCOPE)
   239      ENDFOREACH(A)
   240  
   241      # FILES for ide
   242      #file(GLOB_RECURSE ${PRE}_FILES ${install_dir_path}/*)
   243  
   244      #source_group(${PRE}_IDE_VS FILES ${${PRE}_FILES})
   245      #add_custom_target(${PRE}_IDE_QT SOURCES  ${${PRE}_FILES})
   246  
   247      #export to parent
   248      #set(PRE ${PRE} PARENT_SCOPE)
   249      #set(${PRE}_INSTALL_DIR ${${PRE}_INSTALL_DIR}  PARENT_SCOPE)
   250      SET(${PRE}_INCLUDE ${${PRE}_INCLUDE} PARENT_SCOPE)
   251      SET(${PRE}_LIB ${${PRE}_LIB} PARENT_SCOPE)
   252      SET(${PRE}_BIN ${${PRE}_BIN} PARENT_SCOPE)
   253  
   254      #set(${PRE}_INCLUDES ${${PRE}_INCLUDES} PARENT_SCOPE)
   255      #set(${PRE}_STATIC_LIBS ${${PRE}_STATIC_LIBS} PARENT_SCOPE)
   256      #set(${PRE}_SHARED_LIBS ${${PRE}_SHARED_LIBS} PARENT_SCOPE)
   257      #set(${PRE}_BINS ${${PRE}_BINS} PARENT_SCOPE)
   258  
   259      #set(${PRE}_FILES ${${PRE}_FILES} PARENT_SCOPE)
   260      set(${PRE}_FOUND TRUE PARENT_SCOPE)
   261  
   262  endfunction(find_and_set_package)
   263  
   264  # include all subdirs
   265  function(recurse_include_directories DIR)
   266  
   267      file(GLOB_RECURSE _header_files FOLLOW_SYMLINKS ${DIR}/*.h)
   268      
   269      set(files_dirs "")
   270      foreach(src_file ${_header_files})        
   271               GET_FILENAME_COMPONENT(dir_path ${src_file} PATH)
   272               list(APPEND files_dirs ${dir_path})
   273      endforeach()
   274      LIST(REMOVE_DUPLICATES files_dirs)
   275      
   276      include_directories(${DIR})
   277      foreach(H ${files_dirs})
   278           include_directories(${H})
   279      endforeach(H)
   280      
   281      set(DIR_HEADERS ${_header_files} PARENT_SCOPE)
   282  endfunction(recurse_include_directories)
   283  
   284  
   285  function(add_source_to_qtcreator DIR)
   286      get_filename_component(dir_name ${DIR} NAME)
   287      string(REGEX REPLACE "/" "_" source_target ${DIR})
   288      set(source_target "${dir_name}_${source_target}")
   289  
   290      log_debug("add_files_to_qtcreator: ${source_target}")
   291      
   292      # all header store in HEADERS_${DIR}
   293      #recurse_include_directories(${DIR}) 
   294      
   295      # all c files .c / .cc / .cpp to c_src_files
   296      file(GLOB_RECURSE c_src_files FOLLOW_SYMLINKS ${DIR}/*[c|p|e])
   297      
   298      set(other_files "")
   299      foreach(suffix ${ARGN})
   300            file(GLOB_RECURSE other_src_files FOLLOW_SYMLINKS ${DIR}/*.${suffix})
   301            list(APPEND other_files ${other_src_files})
   302      endforeach()
   303      LIST(REMOVE_DUPLICATES other_files)
   304      
   305      add_custom_target(${source_target}
   306           SOURCES ${DIR_HEADERS} ${c_src_files} ${other_files}
   307           WORKING_DIRECTORY ${DIR})
   308      
   309      set(DIR_ALLS  ${DIR_HEADERS} ${c_src_files} ${other_files} PARENT_SCOPE)
   310  endfunction(add_source_to_qtcreator)
   311  
   312  
   313  macro(add_test_item)
   314  
   315      set(options "" )
   316      set(oneValueArgs "")
   317      set(multiValueArgs SOURCES INCLUDES LIBS)
   318      cmake_parse_arguments(C "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
   319      
   320      SET(TARGET "")
   321      FOREACH(P ${C_SOURCES})
   322           get_filename_component(FILE_NAME ${P} NAME_WE)
   323           SET(TARGET "${FILE_NAME}")
   324           
   325           if(${TARGET} MATCHES "test_*")
   326               break()
   327           endif()
   328      ENDFOREACH(P)
   329      
   330      add_executable(${TARGET} ${C_SOURCES})
   331  
   332      if(C_INCLUDES)
   333          target_include_directories(${TARGET} PUBLIC ${C_INCLUDES})
   334      endif()
   335  
   336      if(C_LIBS)
   337         target_link_libraries(${TARGET} ${C_LIBS})
   338      endif()  
   339      
   340      add_test(NAME ${TARGET} COMMAND ${TARGET} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
   341      log_debug("add_test:${TARGET}, srcs: ${C_SOURCES}")
   342      
   343  endmacro(add_test_item)
   344  
   345  
   346  # ngx_add_dynamic_module(DIR xxx SOURCES XXXX LIBS XXXX)
   347  macro(ngx_add_dynamic_module)
   348  
   349      set(options "" )
   350      set(oneValueArgs "DIR")
   351      set(multiValueArgs SOURCES INCLUDES LIBS)
   352      cmake_parse_arguments(C "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
   353      
   354      if(NOT C_SOURCES)
   355         file(GLOB C_SRCS "${C_DIR}/*.c")
   356         file(GLOB C_HEADERS "${C_DIR}/*.h")
   357         file(GLOB CC_SRCS "${C_DIR}/*.cc")
   358         file(GLOB CPP_SRCS "${C_DIR}/*.cpp")
   359         file(GLOB HPP_SRCS "${C_DIR}/*.hpp")       
   360         SET(C_SOURCES ${C_SRCS} ${C_HEADERS} ${CC_SRCS} ${CPP_SRCS} ${HPP_SRCS})
   361         #debug(C_SOURCES C_SRCS CMAKE_CURRENT_SOURCE_DIR)
   362      endif()
   363      
   364      get_filename_component(module_name ${C_DIR} NAME)
   365      
   366      log_debug("add_dynamic_module:${module_name}, srcs: ${C_SOURCES}")
   367      add_library(${module_name} SHARED ${C_SOURCES})
   368  
   369      if(C_INCLUDES)
   370         target_include_directories(${module_name} PUBLIC ${C_INCLUDES})
   371      endif()
   372      
   373      if(C_LIBS)
   374         target_link_libraries(${module_name} PUBLIC ${C_LIBS})
   375      endif()
   376  
   377      INSTALL(TARGETS ${C_DIR} DESTINATION shared)
   378  
   379  endmacro(ngx_add_dynamic_module)
   380  
   381  # ngx_add_static_module(DIR xxx SOURCES XXXX INCLUDES XXXLIBS XXXX)
   382  macro(ngx_add_static_module)
   383  
   384      set(options "" )
   385      set(oneValueArgs "DIR")
   386      set(multiValueArgs SOURCES INCLUDES LIBS)
   387      cmake_parse_arguments(C "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
   388      
   389      if(NOT C_SOURCES)
   390         file(GLOB C_SRCS "${C_DIR}/*.c")
   391         file(GLOB C_HEADERS "${C_DIR}/*.h")
   392         file(GLOB CC_SRCS "${C_DIR}/*.cc")
   393         file(GLOB CPP_SRCS "${C_DIR}/*.cpp")
   394         file(GLOB HPP_SRCS "${C_DIR}/*.hpp")       
   395         SET(C_SOURCES ${C_SRCS} ${C_HEADERS} ${CC_SRCS} ${CPP_SRCS} ${HPP_SRCS})
   396         #debug(C_SOURCES C_SRCS CMAKE_CURRENT_SOURCE_DIR)
   397      endif()
   398      
   399      get_filename_component(module_name ${C_DIR} NAME)
   400  
   401  
   402  
   403      log_debug("add_static_module:${module_name}, srcs: ${C_SOURCES}")
   404      add_library(${module_name} STATIC ${C_SOURCES})
   405  
   406      if(C_INCLUDES)
   407         target_include_directories(${module_name} PUBLIC ${C_INCLUDES})
   408      endif()
   409      
   410      if(C_LIBS)
   411         # target is nginx static link
   412         target_link_libraries(nginx PUBLIC ${module_name} ${C_LIBS})
   413      else()
   414         target_link_libraries(nginx PUBLIC ${module_name})
   415      endif()
   416  
   417      #INSTALL(TARGETS ${C_DIR} DESTINATION shared)
   418  endmacro(ngx_add_static_module)
   419  
   420  
   421  function(grpc_gen_code SRCS HDRS)
   422    if(NOT ARGN)
   423      message(SEND_ERROR "Error: grpc_gen_code() called without any proto files")
   424      return()
   425    endif()
   426  
   427    set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
   428    if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
   429      # Create an include path for each file specified
   430      foreach(FIL ${ARGN})
   431        get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
   432        get_filename_component(ABS_PATH ${ABS_FIL} PATH)
   433        list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
   434        if(${_contains_already} EQUAL -1)
   435            list(APPEND _protobuf_include_path -I ${ABS_PATH})
   436        endif()
   437      endforeach()
   438    else()
   439      set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
   440    endif()
   441  
   442    if(DEFINED PROTOBUF_IMPORT_DIRS)
   443      foreach(DIR ${PROTOBUF_IMPORT_DIRS})
   444        get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
   445        list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
   446        if(${_contains_already} EQUAL -1)
   447            list(APPEND _protobuf_include_path -I ${ABS_PATH})
   448        endif()
   449      endforeach()
   450    endif()
   451  
   452    set(${SRCS})
   453    set(${HDRS})
   454    
   455    foreach(FIL ${ARGN})
   456      get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
   457      get_filename_component(FIL_WE ${FIL} NAME_WE)
   458  
   459      list(APPEND ${SRCS} ${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc ${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc)
   460      list(APPEND ${HDRS} ${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h ${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h)
   461      
   462      add_custom_command(
   463        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc ${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc
   464               ${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h ${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h
   465               ${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_mock.grpc.pb.h
   466        COMMAND ${protoc_BIN}
   467        ARGS --grpc_out=generate_mock_code=true:${CMAKE_CURRENT_BINARY_DIR}
   468             --cpp_out=${CMAKE_CURRENT_BINARY_DIR}
   469             --plugin=protoc-gen-grpc=${grpc_cpp_plugin_BIN}
   470             ${_protobuf_include_path}
   471             ${ABS_FIL}
   472        DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
   473        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
   474        COMMENT "Running gRPC C++ protocol buffer compiler on ${FIL}"
   475        VERBATIM)
   476  
   477    endforeach()
   478  
   479    set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
   480    
   481    set(${SRCS} ${${SRCS}} PARENT_SCOPE)
   482    set(${HDRS} ${${HDRS}} PARENT_SCOPE)
   483  endfunction()
   484  
   485  macro(add_shared_library DIR)
   486  
   487      SET(LIB_DIR ${DIR}/lib)
   488      if(NOT EXISTS ${LIB_DIR})
   489          SET(LIB_DIR ${DIR}/lib64)
   490      endif()
   491  
   492      INCLUDE_DIRECTORIES(${DIR}/include)
   493  
   494  
   495      foreach(libname ${ARGN})
   496              add_library(${libname} SHARED IMPORTED)
   497              set_property(TARGET ${libname} PROPERTY IMPORTED_LOCATION ${LIB_DIR}/lib${libname}.so)            
   498      endforeach()
   499      log_debug("add_shared_library: ${ARGN} in: ${DIR}")
   500  endmacro(add_shared_library)
   501  
   502  macro(add_static_library DIR)
   503      set_property(GLOBAL APPEND PROPERTY GLOBAL_INCLUDE_DIRS "${DIR}/include")
   504      SET(LIB_DIR ${DIR}/lib)
   505      if(NOT EXISTS ${LIB_DIR})
   506          SET(LIB_DIR ${DIR}/lib64)
   507      endif()
   508      set_property(GLOBAL APPEND PROPERTY GLOBAL_LINK_DIRS ${LIB_DIR})
   509      INCLUDE_DIRECTORIES(${DIR}/include)
   510      #add_files_to_qtcreator(${DIR}/include .h .hpp)
   511      foreach(libname ${ARGN})
   512        add_library(${libname} STATIC IMPORTED)
   513        set_property(TARGET ${libname} PROPERTY IMPORTED_LOCATION ${DIR}/lib/lib${libname}.a)
   514      endforeach()
   515      log_debug("add_static_library: ${ARGN} in: ${DIR}")
   516  endmacro(add_static_library)
   517  
   518  
   519  function(protoc_gen_code SRCS HDRS)
   520    if(NOT ARGN)
   521      message(SEND_ERROR "Error: protoc_gen_code() called without any proto files")
   522      return()
   523    endif()
   524  
   525    set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
   526    if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
   527      # Create an include path for each file specified
   528      foreach(FIL ${ARGN})
   529        get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
   530        get_filename_component(ABS_PATH ${ABS_FIL} PATH)
   531        list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
   532        if(${_contains_already} EQUAL -1)
   533            list(APPEND _protobuf_include_path -I ${ABS_PATH})
   534        endif()
   535      endforeach()
   536    else()
   537      set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
   538    endif()
   539  
   540    if(DEFINED PROTOBUF_IMPORT_DIRS)
   541      foreach(DIR ${PROTOBUF_IMPORT_DIRS})
   542        get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
   543        list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
   544        if(${_contains_already} EQUAL -1)
   545            list(APPEND _protobuf_include_path -I ${ABS_PATH})
   546        endif()
   547      endforeach()
   548    endif()
   549  
   550    set(${SRCS})
   551    set(${HDRS})
   552    
   553    foreach(FIL ${ARGN})
   554      get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
   555      get_filename_component(FIL_WE ${FIL} NAME_WE)
   556      get_filename_component(FIL_PATH ${FIL} DIRECTORY)
   557  
   558      list(APPEND ${SRCS} ${FIL_PATH}/${FIL_WE}.pb.cc)
   559      list(APPEND ${HDRS} ${FIL_PATH}/${FIL_WE}.pb.h)
   560      
   561      #message("${${SRCS}} ${${HDRS}}")
   562      add_custom_command(
   563        OUTPUT ${FIL_PATH}/${FIL_WE}.pb.cc
   564               ${FIL_PATH}/${FIL_WE}.pb.h
   565               #${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_mock.grpc.pb.h
   566        COMMAND ${protoc_BIN}
   567        ARGS --cpp_out=${FIL_PATH}
   568             ${_protobuf_include_path}
   569             ${ABS_FIL}
   570        DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
   571        WORKING_DIRECTORY ${FIL_PATH}
   572        COMMENT "Running C++ protocol buffer compiler on ${FIL}"
   573        VERBATIM)
   574  
   575    endforeach()
   576  
   577    set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
   578    
   579    set(${SRCS} ${${SRCS}} PARENT_SCOPE)
   580    set(${HDRS} ${${HDRS}} PARENT_SCOPE)
   581  endfunction()
   582  
   583  
   584  MACRO(SUBDIRLIST result curdir)
   585      FILE(GLOB children RELATIVE ${curdir} ${curdir}/[a-zA-Z0-9]*)
   586    SET(dirlist "")
   587    FOREACH(child ${children})
   588      IF(IS_DIRECTORY ${curdir}/${child})
   589        LIST(APPEND dirlist ${child})
   590      ENDIF()
   591    ENDFOREACH()
   592    SET(${result} ${dirlist})
   593  ENDMACRO()
   594  
   595  MACRO(IMPORT_ONE_LIB dep)
   596      # include every include
   597      SET(CURRENT_DIR ${dep}/include)
   598      if(IS_DIRECTORY ${CURRENT_DIR})
   599          INCLUDE_DIRECTORIES(${CURRENT_DIR})
   600      endif()
   601  
   602      set(mkl_regex ".*mkl.*")
   603      # #for bin file
   604      # FILE(GLOB children RELATIVE ${dep}/bin ${dep}/bin/*)
   605      # foreach(_bin_name ${children})
   606      #     if(TARGET ${_bin_name})
   607      #         get_property(_bin_dir TARGET ${_bin_name}  PROPERTY IMPORTED_LOCATION)
   608      #         log_warn("${_bin_name} already add at:${_bin_dir} skip ${dep}/bin/${_bin_name}")
   609      #         continue()
   610      #     endif()
   611  
   612      #     add_executable(${_bin_name} IMPORTED)
   613      #     set_property(TARGET ${_bin_name} PROPERTY IMPORTED_LOCATION ${dep}/bin/${_bin_name})
   614      #     log_debug("SET ${_bin_name}: ${dep}/bin/${_bin_name}")
   615      # endforeach()
   616  
   617      #for lib file
   618      foreach(CURRENT_DIR ${dep}/lib64 ${dep}/lib)
   619  
   620          FILE(GLOB children RELATIVE ${CURRENT_DIR} ${CURRENT_DIR}/lib*.a)
   621          foreach(libs ${children})
   622              string(REGEX REPLACE "^lib" "" _lib_name ${libs})
   623              string(REGEX REPLACE "\\.a" "" _lib_name ${_lib_name})
   624              #set(_lib_name ${_lib_name})
   625  
   626              if(TARGET ${_lib_name})
   627                  get_property(_lib_name_dir TARGET ${_lib_name}  PROPERTY IMPORTED_LOCATION)
   628                  log_warn("${_lib_name} already add at:${_lib_name_dir} skip ${CURRENT_DIR}/${libs}")
   629                  continue()
   630              endif()
   631  
   632              add_library(${_lib_name} STATIC IMPORTED)
   633              set_property(TARGET ${_lib_name}  PROPERTY IMPORTED_LOCATION ${CURRENT_DIR}/${libs})
   634              log_debug("SET ${_lib_name}: ${CURRENT_DIR}/${libs}")
   635          endforeach()
   636  
   637          #for lib file
   638          FILE(GLOB children RELATIVE ${CURRENT_DIR} ${CURRENT_DIR}/lib*.so)
   639          foreach(libs ${children})
   640              string(REGEX REPLACE "^lib" "" _lib_name ${libs})
   641              string(REGEX REPLACE "\\.so" "" _lib_name ${_lib_name})
   642              set(_lib_name ${_lib_name}-so)
   643  
   644              if(TARGET ${_lib_name})
   645                  get_property(_lib_name_dir TARGET ${_lib_name}  PROPERTY IMPORTED_LOCATION)
   646                  log_warn("${_lib_name} already add at:${_lib_name_dir} skip ${CURRENT_DIR}/${libs}")
   647                  continue()
   648              endif()
   649  
   650              add_library(${_lib_name} SHARED IMPORTED)
   651              set_property(TARGET ${_lib_name}  PROPERTY IMPORTED_LOCATION ${CURRENT_DIR}/${libs})
   652              log_debug("SET ${_lib_name}: ${CURRENT_DIR}/${libs}")
   653  
   654              if ("${_lib_name}" MATCHES "${mkl_regex}")
   655                  log_debug("${_lib_name}: IMPORTED_NO_SONAME")
   656                  set_property(TARGET ${_lib_name}  PROPERTY IMPORTED_NO_SONAME 1)
   657              endif()
   658  
   659          endforeach()
   660      endforeach()
   661  ENDMACRO()
   662  
   663  
   664  MACRO(ADD_THIRD_LIB THIRD_PATH)
   665    SUBDIRLIST(SUB_THIRD_DIRS ${THIRD_PATH})
   666    message(STATUS "SUB_THIRD_LIBS: ${SUB_THIRD_DIRS}")
   667  
   668    set(cuda8_regex ".*cuda8.*")
   669    set(cuda9_regex ".*cuda9.*")
   670    set(cuda10_regex ".*cuda10.*")
   671  
   672    foreach(dep ${SUB_THIRD_DIRS})
   673        message(STATUS "dep: ${dep}")
   674        if("${dep}" MATCHES "${cuda8_regex}")
   675            if(${CUDA8_0})
   676              message(STATUS "add ${THIRD_PATH}/${deps}")
   677              IMPORT_ONE_LIB(${THIRD_PATH}/${dep})
   678              set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Xcompiler -fPIC -O3 --compiler-options -fno-strict-aliasing -lineinfo -Xptxas -dlcm=cg -use_fast_math -gencode arch=compute_61,code=sm_61 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_52,code=sm_52" CACHE STRING "cuda flags")
   679              message(STATUS "CUDA_NVCC_FLAGS:${CUDA_NVCC_FLAGS}")
   680            endif(${CUDA8_0})
   681        elseif("${dep}" MATCHES "${cuda9_regex}")
   682            if(${CUDA9_0})
   683              message(STATUS "add ${THIRD_PATH}/${deps}")
   684              IMPORT_ONE_LIB(${THIRD_PATH}/${dep})
   685              set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Xcompiler -fPIC -O3 --compiler-options -fno-strict-aliasing -lineinfo -Xptxas -dlcm=cg -use_fast_math -gencode arch=compute_70,code=sm_70 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_52,code=sm_52" CACHE STRING "cuda flags")
   686              message(STATUS "CUDA_NVCC_FLAGS:${CUDA_NVCC_FLAGS}")
   687            endif(${CUDA9_0})
   688        elseif("${dep}" MATCHES "${cuda10_regex}")
   689            if(${CUDA10_0})
   690              message(STATUS "add ${THIRD_PATH}/${deps}")
   691              IMPORT_ONE_LIB(${THIRD_PATH}/${dep})
   692              set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -Xcompiler -fPIC -O3 --compiler-options -fno-strict-aliasing -lineinfo -Xptxas -dlcm=cg -use_fast_math -gencode arch=compute_75,code=sm_75 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_52,code=sm_52" CACHE STRING "cuda flags")
   693              message(STATUS "CUDA_NVCC_FLAGS:${CUDA_NVCC_FLAGS}")
   694            endif(${CUDA10_0})
   695        else("${dep}" MATCHES "${cuda8_regex}")
   696          message(STATUS "not cuda8 or cuda9 lib. normal import")
   697          IMPORT_ONE_LIB(${THIRD_PATH}/${dep})
   698        endif("${dep}" MATCHES "${cuda8_regex}")
   699    endforeach()
   700  ENDMACRO()
   701  
   702  
   703  MACRO(INSTALL_SO LIB_DIR)
   704  # todo copy by bin
   705      foreach(so_target ${ARGN})
   706         get_property(SO_FILE TARGET ${so_target} PROPERTY IMPORTED_LOCATION)
   707  
   708         if(SO_FILE MATCHES ".so$")
   709             log_debug("INSTALL LIBRARY ${SO_FILE}")
   710             file(GLOB ALL_SOS "${SO_FILE}*")
   711             INSTALL(FILES ${ALL_SOS} DESTINATION ${LIB_DIR})
   712         endif()
   713      endforeach()
   714  
   715  ENDMACRO()
   716  
   717  MACRO(INSTALL_BIN BIN_DIR)
   718      foreach(bin_target ${ARGN})
   719          get_property(BIN_FILE TARGET ${bin_target} PROPERTY IMPORTED_LOCATION)
   720          log_debug("INSTALL BINARY ${BIN_FILE}")
   721          INSTALL(FILES ${BIN_FILE} DESTINATION ${BIN_DIR})
   722      endforeach()
   723  ENDMACRO()
   724  
   725  #build type
   726  #flag to import CUDA8.0/9.0/10.0
   727  option(BUILD_CUDA8_0 "BUILD_CUDA8_0" OFF)
   728  set(CUDA8_0 FALSE)
   729  if(BUILD_CUDA8_0)
   730      set(CUDA8_0 TRUE)
   731  endif(BUILD_CUDA8_0)
   732  
   733  option(BUILD_CUDA9_0 "BUILD_CUDA9_0" OFF)
   734  set(CUDA9_0 FALSE)
   735  if(BUILD_CUDA9_0)
   736      set(CUDA9_0 TRUE)
   737  endif(BUILD_CUDA9_0)
   738  
   739  option(BUILD_CUDA10_0 "BUILD_CUDA10_0" OFF)
   740  set(CUDA10_0 FALSE)
   741  if(BUILD_CUDA10_0)
   742      set(CUDA10_0 TRUE)
   743  endif(BUILD_CUDA10_0)
   744  
   745  set(THIRD_PATH  ${CMAKE_SOURCE_DIR}/third_path)
   746  if(NOT EXISTS ${THIRD_PATH}) 
   747      set(THIRD_PATH ${CMAKE_SOURCE_DIR}/../third_path)
   748  endif()
   749  message(STATUS "THIRD_PATH;${THIRD_PATH}")
   750  
   751  ADD_THIRD_LIB(${THIRD_PATH})
   752  
   753  
   754  IF(CUDA8_0)
   755      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda8_0)
   756      include(FindCUDA)
   757      message(STATUS "FindCUDA - CUDA_TOOLKIT_ROOT_DIR: ${THIRD_PATH}/cuda8_0; ")
   758  ELSE(CUDA8_0)
   759      IF(IS_DIRECTORY ${THIRD_PATH}/cuda)
   760      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda)
   761      include(FindCUDA)
   762      ELSE(IS_DIRECTORY ${THIRD_PATH}/cuda)
   763      ENDIF()
   764  ENDIF()
   765  
   766  IF(CUDA9_0)
   767      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda9_0)
   768      include(FindCUDA)
   769      message(STATUS "FindCUDA - CUDA_TOOLKIT_ROOT_DIR: ${THIRD_PATH}/cuda9_0; ")
   770  ELSE(CUDA9_0)
   771      IF(IS_DIRECTORY ${THIRD_PATH}/cuda)
   772      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda)
   773      include(FindCUDA)
   774      ELSE(IS_DIRECTORY ${THIRD_PATH}/cuda)
   775      ENDIF()
   776  ENDIF()
   777  
   778  IF(CUDA10_0)
   779      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda10_0)
   780      include(FindCUDA)
   781      message(STATUS "FindCUDA - CUDA_TOOLKIT_ROOT_DIR: ${THIRD_PATH}/cuda10_0; ")
   782  ELSE(CUDA10_0)
   783      IF(IS_DIRECTORY ${THIRD_PATH}/cuda)
   784      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda)
   785      include(FindCUDA)
   786      ELSE(IS_DIRECTORY ${THIRD_PATH}/cuda)
   787      ENDIF()
   788  ENDIF()