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

     1  cmake_minimum_required(VERSION 2.8)
     2  
     3  macro(log_debug msg)
     4      get_filename_component(name ${CMAKE_CURRENT_LIST_FILE} NAME)
     5      string(TIMESTAMP TIME_NOW "%Y-%m-%d %H:%M:%S")
     6      message("${TIME_NOW} - ${name}:${CMAKE_CURRENT_LIST_LINE} - ${msg}")
     7  endmacro(log_debug)
     8  
     9  macro(log_warn msg)
    10      get_filename_component(name ${CMAKE_CURRENT_LIST_FILE} NAME)
    11      string(TIMESTAMP TIME_NOW "%Y-%m-%d %H:%M:%S")
    12      message(WARNING "${TIME_NOW} - ${name}:${CMAKE_CURRENT_LIST_LINE} - ${msg}")
    13  endmacro(log_warn)
    14  
    15  macro(log_error msg)
    16      get_filename_component(name ${CMAKE_CURRENT_LIST_FILE} NAME)
    17      string(TIMESTAMP TIME_NOW "%Y-%m-%d %H:%M:%S")
    18      message(SEND_ERROR  "${TIME_NOW} - ${name}:${CMAKE_CURRENT_LIST_LINE} - ${msg}")
    19  endmacro(log_error)
    20  
    21  macro(debug_vars)
    22      FOREACH(A ${ARGN})
    23        log_debug("${A}:${${A}}")
    24      ENDFOREACH(A)
    25  endmacro(debug_vars)
    26  
    27  macro(log_execute_process)
    28      set(options "" )
    29      set(oneValueArgs WORKING_DIRECTORY )
    30      set(multiValueArgs COMMAND)
    31      cmake_parse_arguments(log_execute_process "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
    32      string(REPLACE ";" " " CMD "${log_execute_process_COMMAND}")
    33  
    34      if(NOT log_execute_process_WORKING_DIRECTORY)
    35           log_error("bad cmd:cd ${log_execute_process_WORKING_DIRECTORY} && ${CMD}")
    36      endif()
    37  
    38      if(NOT EXISTS ${log_execute_process_WORKING_DIRECTORY})
    39           log_error("bad cmd:cd ${log_execute_process_WORKING_DIRECTORY} && ${CMD}")
    40      endif()
    41  
    42      log_debug("cd ${log_execute_process_WORKING_DIRECTORY} && ${CMD}")
    43      execute_process(COMMAND ${log_execute_process_COMMAND}
    44                      WORKING_DIRECTORY ${log_execute_process_WORKING_DIRECTORY}
    45                      RESULT_VARIABLE rv ERROR_VARIABLE er
    46                      OUTPUT_FILE ${log_execute_process_WORKING_DIRECTORY}/out.log
    47                      ERROR_FILE ${log_execute_process_WORKING_DIRECTORY}/error.log)
    48      if(rv)
    49         log_debug("RESULT:${rv}, ERROR:${er}")
    50      endif()
    51  endmacro(log_execute_process)
    52  
    53  macro(add_files_to_qtcreator DIR)
    54      get_filename_component(dir_name ${DIR} NAME)
    55      string(REGEX REPLACE "/" "_" source_target ${DIR})
    56      set(source_target "${dir_name}_${source_target}")
    57  
    58      log_debug("add_files_to_qtcreator: ${source_target}")
    59      set(files_sources "")
    60      foreach(fileprex ${ARGN})
    61         file(GLOB_RECURSE _src_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${DIR}/*${fileprex})
    62         foreach(src_file ${_src_files})
    63             if(NOT (src_file MATCHES ".*\\.git.*"))
    64                  list(APPEND files_sources ${src_file})
    65             endif()
    66         endforeach()
    67       endforeach()
    68  
    69       add_custom_target(${source_target}
    70           SOURCES ${files_sources}
    71           WORKING_DIRECTORY ${DIR})
    72  
    73  endmacro(add_files_to_qtcreator)
    74  
    75  macro(add_shared_library DIR)
    76      set_property(GLOBAL APPEND PROPERTY GLOBAL_INCLUDE_DIRS "${DIR}/include")
    77      SET(LIB_DIR ${DIR}/lib)
    78      if(NOT EXISTS ${LIB_DIR})
    79          SET(LIB_DIR ${DIR}/lib64)
    80      endif()
    81      set_property(GLOBAL APPEND PROPERTY GLOBAL_LINK_DIRS ${LIB_DIR})
    82      INCLUDE_DIRECTORIES(${DIR}/include)
    83      add_files_to_qtcreator(${DIR}/include .h .hpp)
    84  
    85      foreach(libname ${ARGN})
    86              add_library(${libname} SHARED IMPORTED)
    87              set_property(TARGET ${libname} PROPERTY IMPORTED_LOCATION ${DIR}/lib/lib${libname}.so)
    88              set(SHARED_SO_PATH  ${SHARED_SO_PATH}:${DIR}/lib)
    89      endforeach()
    90      log_debug("add_shared_library: ${ARGN} in: ${DIR}")
    91  endmacro(add_shared_library)
    92  
    93  macro(add_static_library DIR)
    94      set_property(GLOBAL APPEND PROPERTY GLOBAL_INCLUDE_DIRS "${DIR}/include")
    95      SET(LIB_DIR ${DIR}/lib)
    96      if(NOT EXISTS ${LIB_DIR})
    97          SET(LIB_DIR ${DIR}/lib64)
    98      endif()
    99      set_property(GLOBAL APPEND PROPERTY GLOBAL_LINK_DIRS ${LIB_DIR})
   100      INCLUDE_DIRECTORIES(${DIR}/include)
   101      add_files_to_qtcreator(${DIR}/include .h .hpp)
   102      foreach(libname ${ARGN})
   103        add_library(${libname} STATIC IMPORTED)
   104        set_property(TARGET ${libname} PROPERTY IMPORTED_LOCATION ${DIR}/lib/lib${libname}.a)
   105      endforeach()
   106      log_debug("add_static_library: ${ARGN} in: ${DIR}")
   107  endmacro(add_static_library)
   108  
   109  MACRO(SUBDIRLIST result curdir)
   110      FILE(GLOB children RELATIVE ${curdir} ${curdir}/[a-zA-Z0-9]*)
   111    SET(dirlist "")
   112    FOREACH(child ${children})
   113      IF(IS_DIRECTORY ${curdir}/${child})
   114        LIST(APPEND dirlist ${child})
   115      ENDIF()
   116    ENDFOREACH()
   117    SET(${result} ${dirlist})
   118  ENDMACRO()
   119  
   120  MACRO(SUBLIBLIST shared_result static_result curdir)
   121    FILE(GLOB children RELATIVE ${curdir} ${curdir}/lib*.so)
   122    SET(filelist "")
   123    FOREACH(child ${children})
   124      IF(IS_DIRECTORY ${curdir}/${child})
   125      ELSE(IS_DIRECTORY ${curdir}/${child})
   126        LIST(APPEND filelist ${child})
   127      ENDIF()
   128    ENDFOREACH()
   129    SET(${shared_result} ${filelist})
   130  
   131    FILE(GLOB children RELATIVE ${curdir} ${curdir}/lib*.a)
   132    SET(filelist "")
   133    FOREACH(child ${children})
   134      IF(IS_DIRECTORY ${curdir}/${child})
   135      ELSE(IS_DIRECTORY ${curdir}/${child})
   136        LIST(APPEND filelist ${child})
   137      ENDIF()
   138    ENDFOREACH()
   139    SET(${static_result} ${filelist})
   140  ENDMACRO()
   141  
   142  MACRO(IMPORT_ONE_LIB DEP_DIR)
   143    SET(DEP_LIBDIR ${DEP_DIR}/lib)
   144    if(NOT EXISTS ${DEP_LIBDIR})
   145      SET(DEP_LIBDIR ${DEP_DIR}/lib64)
   146    endif()
   147    SUBLIBLIST(SHARED_LIBS STATIC_LIBS ${DEP_LIBDIR})
   148    list(LENGTH SHARED_LIBS shared_count)
   149    list(LENGTH STATIC_LIBS static_count)
   150    message(STATUS "${dep} shared_count : ${shared_count}")
   151    message(STATUS "${dep} static_count : ${static_count}")
   152    INCLUDE_DIRECTORIES(${DEP_DIR}/include)
   153  
   154    set_property(GLOBAL APPEND PROPERTY GLOBAL_INCLUDE_DIRS "${DEP_DIR}/include")
   155    set_property(GLOBAL APPEND PROPERTY GLOBAL_LINK_DIRS ${DEP_LIBDIR})
   156  
   157    set(mkl_regex ".*mkl.*")
   158    
   159    if (${shared_count} STREQUAL "0")
   160      if (${static_count} STREQUAL "0")
   161        message(STATUS "${DEP_DIR} has no lib to import")
   162      else (${static_count} STREQUAL "0")
   163        FILE(GLOB children RELATIVE ${DEP_LIBDIR} ${DEP_LIBDIR}/lib*.a)
   164        foreach(libs ${children})
   165            string(REGEX REPLACE "^lib" "" _lib_name ${libs})
   166            string(REGEX REPLACE "\\.a" "" _lib_name ${_lib_name})
   167  
   168            if(TARGET ${_lib_name})
   169                get_property(_lib_name_dir TARGET ${_lib_name}  PROPERTY IMPORTED_LOCATION)
   170                log_warn("${_lib_name} already add at:${_lib_name_dir} skip ${DEP_LIBDIR}/${libs}")
   171                continue()
   172            endif()
   173  
   174            add_library(${_lib_name} STATIC IMPORTED)
   175            set_property(TARGET ${_lib_name}  PROPERTY IMPORTED_LOCATION ${DEP_LIBDIR}/${libs})
   176            log_debug("SET ${_lib_name}: ${DEP_LIBDIR}/${libs}")
   177        endforeach()
   178      endif()
   179    else (${shared_count} STREQUAL "0")
   180      FILE(GLOB children RELATIVE ${DEP_LIBDIR} ${DEP_LIBDIR}/lib*.so)
   181      foreach(libs ${children})
   182          string(REGEX REPLACE "^lib" "" _lib_name ${libs})
   183          string(REGEX REPLACE "\\.so" "" _lib_name ${_lib_name})
   184  
   185          if(TARGET ${_lib_name})
   186              get_property(_lib_name_dir TARGET ${_lib_name}  PROPERTY IMPORTED_LOCATION)
   187              log_warn("${_lib_name} already add at:${_lib_name_dir} skip ${DEP_LIBDIR}/${libs}")
   188              continue()
   189          endif()
   190  
   191          add_library(${_lib_name} SHARED IMPORTED)
   192          set_property(TARGET ${_lib_name}  PROPERTY IMPORTED_LOCATION ${DEP_LIBDIR}/${libs})
   193          log_debug("SET ${_lib_name}: ${DEP_LIBDIR}/${libs}")
   194  
   195          if ("${_lib_name}" MATCHES "${mkl_regex}")
   196              log_debug("${_lib_name}: IMPORTED_NO_SONAME")
   197              set_property(TARGET ${_lib_name}  PROPERTY IMPORTED_NO_SONAME 1)
   198          endif()
   199  
   200      endforeach()
   201    endif()
   202  ENDMACRO()
   203  
   204  MACRO(ADD_THIRD_LIB THIRD_PATH)
   205    SUBDIRLIST(SUB_THIRD_DIRS ${THIRD_PATH})
   206    message(STATUS "SUB_THIRD_LIBS: ${SUB_THIRD_DIRS}")
   207    set(cuda8_regex ".*cuda8.*")
   208    set(cuda9_regex ".*cuda9.*")
   209    set(cuda10_regex ".*cuda10.*")
   210  
   211    foreach(dep ${SUB_THIRD_DIRS})
   212        message(STATUS "dep: ${dep}")
   213        if("${dep}" MATCHES "${cuda8_regex}")
   214            if(${CUDA8_0})
   215              message(STATUS "add ${THIRD_PATH}/${deps}")
   216              IMPORT_ONE_LIB(${THIRD_PATH}/${dep})
   217              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")
   218              message(STATUS "CUDA_NVCC_FLAGS:${CUDA_NVCC_FLAGS}")
   219            endif(${CUDA8_0})
   220        elseif("${dep}" MATCHES "${cuda9_regex}")
   221            if(${CUDA9_0})
   222              message(STATUS "add ${THIRD_PATH}/${deps}")
   223              IMPORT_ONE_LIB(${THIRD_PATH}/${dep})
   224              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")
   225              message(STATUS "CUDA_NVCC_FLAGS:${CUDA_NVCC_FLAGS}")
   226            endif(${CUDA9_0})
   227        elseif("${dep}" MATCHES "${cuda10_regex}")
   228            if(${CUDA10_0})
   229              message(STATUS "add ${THIRD_PATH}/${deps}")
   230              IMPORT_ONE_LIB(${THIRD_PATH}/${dep})
   231              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")
   232              message(STATUS "CUDA_NVCC_FLAGS:${CUDA_NVCC_FLAGS}")
   233            endif(${CUDA10_0})
   234        else("${dep}" MATCHES "${cuda8_regex}")
   235          message(STATUS "not cuda8 or cuda9 lib. normal import")
   236          IMPORT_ONE_LIB(${THIRD_PATH}/${dep})
   237        endif("${dep}" MATCHES "${cuda8_regex}")
   238    endforeach()
   239  ENDMACRO()
   240  
   241  # Usage in your CMakeLists:
   242  # Describe property
   243  define_property(GLOBAL PROPERTY GLOBAL_INCLUDE_DIRS
   244  	        BRIEF_DOCS "GLOBAL_INCLUDE_DIRS"
   245  	        FULL_DOCS "GLOBAL_INCLUDE_DIRS")
   246  define_property(GLOBAL PROPERTY GLOBAL_LINK_DIRS
   247  		BRIEF_DOCS "GLOBAL_LINK_DIRS"
   248  		FULL_DOCS "GLOBAL_LINK_DIRS")
   249  # Initialize property
   250  set_property(GLOBAL PROPERTY GLOBAL_INCLUDE_DIRS "")
   251  set_property(GLOBAL PROPERTY GLOBAL_LINK_DIRS "")
   252  
   253  
   254  #build type
   255  #flag to import CUDA8.0/9.0/10.0
   256  option(BUILD_CUDA8_0 "BUILD_CUDA8_0" OFF)
   257  set(CUDA8_0 FALSE)
   258  if(BUILD_CUDA8_0)
   259      set(CUDA8_0 TRUE)
   260  endif(BUILD_CUDA8_0)
   261  
   262  option(BUILD_CUDA9_0 "BUILD_CUDA9_0" OFF)
   263  set(CUDA9_0 FALSE)
   264  if(BUILD_CUDA9_0)
   265      set(CUDA9_0 TRUE)
   266  endif(BUILD_CUDA9_0)
   267  
   268  option(BUILD_CUDA10_0 "BUILD_CUDA10_0" OFF)
   269  set(CUDA10_0 FALSE)
   270  if(BUILD_CUDA10_0)
   271      set(CUDA10_0 TRUE)
   272  endif(BUILD_CUDA10_0)
   273  
   274  set(THIRD_PATH  ${CMAKE_SOURCE_DIR}/third_path)
   275  if(NOT EXISTS ${THIRD_PATH}) 
   276      set(THIRD_PATH ${CMAKE_SOURCE_DIR}/../third_path)
   277  endif()
   278  message(STATUS "THIRD_PATH;${THIRD_PATH}")
   279  ADD_THIRD_LIB(${THIRD_PATH})
   280  
   281  IF(CUDA8_0)
   282      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda8_0)
   283      include(FindCUDA)
   284      message(STATUS "FindCUDA - CUDA_TOOLKIT_ROOT_DIR: ${THIRD_PATH}/cuda8_0; ")
   285  ELSE(CUDA8_0)
   286      IF(IS_DIRECTORY ${THIRD_PATH}/cuda)
   287      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda)
   288      include(FindCUDA)
   289      ELSE(IS_DIRECTORY ${THIRD_PATH}/cuda)
   290      ENDIF()
   291  ENDIF()
   292  
   293  IF(CUDA9_0)
   294      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda9_0)
   295      include(FindCUDA)
   296      message(STATUS "FindCUDA - CUDA_TOOLKIT_ROOT_DIR: ${THIRD_PATH}/cuda9_0; ")
   297  ELSE(CUDA9_0)
   298      IF(IS_DIRECTORY ${THIRD_PATH}/cuda)
   299      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda)
   300      include(FindCUDA)
   301      ELSE(IS_DIRECTORY ${THIRD_PATH}/cuda)
   302      ENDIF()
   303  ENDIF()
   304  
   305  IF(CUDA10_0)
   306      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda10_0)
   307      include(FindCUDA)
   308      message(STATUS "FindCUDA - CUDA_TOOLKIT_ROOT_DIR: ${THIRD_PATH}/cuda10_0; ")
   309  ELSE(CUDA10_0)
   310      IF(IS_DIRECTORY ${THIRD_PATH}/cuda)
   311      set(CUDA_TOOLKIT_ROOT_DIR ${THIRD_PATH}/cuda)
   312      include(FindCUDA)
   313      ELSE(IS_DIRECTORY ${THIRD_PATH}/cuda)
   314      ENDIF()
   315  ENDIF()
   316  
   317  # Get property
   318  get_property(GLOBAL_INCLUDE_DIRS_LIST GLOBAL PROPERTY GLOBAL_INCLUDE_DIRS)
   319  get_property(GLOBAL_LINK_DIRS_LIST GLOBAL PROPERTY GLOBAL_LINK_DIRS)
   320  
   321  INCLUDE_DIRECTORIES(${GLOBAL_INCLUDE_DIRS_LIST})
   322  LINK_DIRECTORIES(${GLOBAL_LINK_DIRS_LIST})
   323