"Fossies" - the Fresh Open Source Software Archive

Member "osquery-4.3.0/libraries/cmake/facebook/modules/api.cmake" (14 Apr 2020, 8914 Bytes) of package /linux/misc/osquery-4.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) CMake source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "api.cmake": 4.0.2_vs_4.2.0.

    1 # Copyright (c) 2014-present, Facebook, Inc.
    2 # All rights reserved.
    3 #
    4 # This source code is licensed in accordance with the terms specified in
    5 # the LICENSE file found in the root directory of this source tree.
    6 
    7 # Generates a target named identifier_downloader that will acquire the remote file while also verifying
    8 # its hash
    9 function(downloadRemoteFile identifier base_url file_name hash)
   10   set(destination "${CMAKE_CURRENT_BINARY_DIR}/${file_name}")
   11   set(url "${base_url}/${file_name}")
   12 
   13   set(command_prefix "${OSQUERY_PYTHON_EXECUTABLE}")
   14 
   15   add_custom_command(
   16     OUTPUT "${destination}"
   17     COMMAND "${command_prefix}" "${CMAKE_SOURCE_DIR}/tools/cmake/downloader.py" "${base_url}/${file_name}" "${destination}" "${hash}"
   18     COMMENT "Downloading: ${url}"
   19     VERBATIM
   20   )
   21 
   22   add_custom_target("${identifier}_downloader" DEPENDS "${destination}")
   23   set(downloadRemoteFile_destination "${destination}" PARENT_SCOPE)
   24 endfunction()
   25 
   26 # Generates a target named identifier_extractor that will extract the remote package
   27 function(extractLocalArchive identifier anchor_file archive_path working_directory)
   28   if(IS_ABSOLUTE "${anchor_file}")
   29     set(absolute_anchor_path "${anchor_file}")
   30   else()
   31     set(absolute_anchor_path "${working_directory}/${anchor_file}")
   32   endif()
   33 
   34   if("${archive_path}" MATCHES ".tar.gz" OR "${archive_path}" MATCHES ".zip")
   35     set(external_tool "${CMAKE_COMMAND}")
   36     set(external_tool_parameters "-E" "tar" "xzf")
   37 
   38   elseif("${archive_path}" MATCHES ".whl")
   39     set(external_tool "${CMAKE_COMMAND}")
   40     set(external_tool_parameters "-E" "tar" "x")
   41   endif()
   42 
   43   if(${ARGC} GREATER 4)
   44     foreach(additional_anchor ${ARGN})
   45       if(IS_ABSOLUTE "${additional_anchor}")
   46         list(APPEND additional_anchor_file_paths "${additional_anchor}")
   47       else()
   48         list(APPEND additional_anchor_file_paths "${working_directory}/${additional_anchor}")
   49       endif()
   50     endforeach()
   51   endif()
   52 
   53   add_custom_command(
   54     OUTPUT "${absolute_anchor_path}" ${additional_anchor_file_paths}
   55     COMMAND "${external_tool}" ${external_tool_parameters} "${archive_path}"
   56     DEPENDS "${identifier}_downloader"
   57     WORKING_DIRECTORY "${working_directory}"
   58     COMMENT "Extracting archive: ${archive_path}"
   59     VERBATIM
   60   )
   61 
   62   add_custom_target("${identifier}_extractor" DEPENDS "${absolute_anchor_path}" ${additional_anchor_file_paths})
   63 endfunction()
   64 
   65 # Generates a empty imported or interface library named thirdparty_name that will depends on the targets
   66 # that will download and extract the remote tarball
   67 function(importThirdPartyBinaryLibrary name version hash anchor_file_name)
   68   if(DEFINED PLATFORM_LINUX)
   69     set(platform_name "linux")
   70   elseif(DEFINED PLATFORM_MACOS)
   71     set(platform_name "macos")
   72   elseif(DEFINED PLATFORM_WINDOWS)
   73     set(platform_name "windows")
   74   else()
   75     message(FATAL_ERROR "Unrecognized system")
   76     return()
   77   endif()
   78 
   79   set(base_url "${THIRD_PARTY_REPOSITORY_URL}/third-party/pre-built/${platform_name}-x86_64")
   80   set(file_name "${name}-${version}.tar.gz")
   81   set(identifier "thirdparty_${name}")
   82   downloadRemoteFile("${identifier}" "${base_url}" "${file_name}" "${hash}")
   83 
   84   set(relative_anchor_path "${name}/${version}/${anchor_file_name}")
   85 
   86   if(${ARGC} GREATER 4)
   87     foreach(additional_anchor ${ARGN})
   88       list(APPEND additional_anchor_rel_paths "${name}/${version}/${additional_anchor}")
   89     endforeach()
   90   endif()
   91 
   92   extractLocalArchive("${identifier}" "${relative_anchor_path}" "${downloadRemoteFile_destination}" "${CMAKE_CURRENT_BINARY_DIR}" ${additional_anchor_rel_paths})
   93 
   94   set(base_folder "${CMAKE_CURRENT_BINARY_DIR}/${name}/${version}")
   95 
   96   if(additional_anchor_rel_paths)
   97     add_osquery_library("${identifier}" INTERFACE IMPORTED GLOBAL)
   98     set_target_properties("${identifier}" PROPERTIES INTERFACE_BINARY_DIR "${base_folder}")
   99 
  100     list(APPEND libraries "${relative_anchor_path}")
  101     list(APPEND libraries "${additional_anchor_rel_paths}")
  102 
  103     foreach(library ${libraries})
  104       get_filename_component(library_name "${library}" NAME_WE)
  105       add_osquery_library("${identifier}_${library_name}" STATIC IMPORTED GLOBAL)
  106       set_target_properties("${identifier}_${library_name}" PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/${library}")
  107       target_link_libraries("${identifier}" INTERFACE "${identifier}_${library_name}")
  108     endforeach()
  109   else()
  110     add_osquery_library("${identifier}" STATIC IMPORTED GLOBAL)
  111     set_target_properties("${identifier}" PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/${relative_anchor_path}")
  112   endif()
  113 
  114   add_dependencies("${identifier}" "${identifier}_extractor")
  115 
  116   # So that's possible to download and extract dependencies before building, to have the IDE working correctly
  117   add_dependencies("prepare_for_ide" "${identifier}_extractor")
  118 
  119   execute_process(COMMAND "${CMAKE_COMMAND}" -E make_directory "${base_folder}/include")
  120   target_include_directories("${identifier}" INTERFACE "${base_folder}/include")
  121 
  122   set(importThirdPartyBinaryLibrary_baseFolderPath "${base_folder}" PARENT_SCOPE)
  123 endfunction()
  124 
  125 # Generates an interface library named thirdparty_name that automatically includes the specified
  126 # include folder. This library will depend on the downloader and extractor targets
  127 function(importThirdPartyHeaderOnlyLibrary library_type name version hash anchor_file include_folder)
  128   if("${library_type}" STREQUAL "SOURCE")
  129     set(base_url "${THIRD_PARTY_REPOSITORY_URL}/third-party/src")
  130   elseif("${library_type}" STREQUAL "PREBUILT")
  131     if(DEFINED PLATFORM_LINUX)
  132     set(platform_name "linux")
  133     elseif(DEFINED PLATFORM_MACOS)
  134       set(platform_name "macos")
  135     elseif(DEFINED PLATFORM_WINDOWS)
  136       set(platform_name "windows")
  137     else()
  138       message(FATAL_ERROR "Unrecognized system")
  139       return()
  140     endif()
  141 
  142     set(base_url "${THIRD_PARTY_REPOSITORY_URL}/third-party/pre-built/${platform_name}-x86_64")
  143     set(base_folder "${CMAKE_CURRENT_BINARY_DIR}/${name}/${version}")
  144   else()
  145     set(base_folder "${CMAKE_CURRENT_BINARY_DIR}")
  146     message(FATAL_ERROR "Unknown header only library type ${library_type}")
  147   endif()
  148 
  149   set(file_name "${name}-${version}.tar.gz")
  150   set(identifier "thirdparty_${name}")
  151   downloadRemoteFile("${identifier}" "${base_url}" "${file_name}" "${hash}")
  152   extractLocalArchive("${identifier}" "${base_folder}/${anchor_file}" "${downloadRemoteFile_destination}" "${CMAKE_CURRENT_BINARY_DIR}")
  153 
  154   add_osquery_library("${identifier}" INTERFACE)
  155   add_dependencies("${identifier}" "${identifier}_extractor")
  156 
  157   # So that's possible to download and extract dependencies before building, to have the IDE working correctly
  158   add_dependencies("prepare_for_ide" "${identifier}_extractor")
  159 
  160   target_include_directories("${identifier}" INTERFACE "${base_folder}/${include_folder}")
  161 endfunction()
  162 
  163 # Initializes the PYTHONPATH folder in the binary directory, used to run the codegen scripts
  164 function(initializePythonPathFolder)
  165   if(NOT TARGET thirdparty_python_modules)
  166     add_custom_command(
  167       OUTPUT "${OSQUERY_PYTHON_PATH}"
  168       COMMAND "${CMAKE_COMMAND}" -E make_directory "${OSQUERY_PYTHON_PATH}"
  169       COMMENT "Initializing custom PYTHONPATH: ${OSQUERY_PYTHON_PATH}"
  170     )
  171 
  172     add_custom_target(thirdparty_pythonpath DEPENDS "${OSQUERY_PYTHON_PATH}")
  173     add_custom_target(thirdparty_python_modules)
  174   endif()
  175 endfunction()
  176 
  177 # Imports a remote Python module inside the PYTHONPATH folder (previously initialized
  178 # with the initializePythonPathFolder() function). The target will be named thirdparty_identifier
  179 function(importRemotePythonModule identifier base_url file_name hash)
  180   set(target_name "thirdparty_pythonmodule_${identifier}")
  181   downloadRemoteFile("${target_name}" "${base_url}" "${file_name}" "${hash}")
  182 
  183   extractLocalArchive("${target_name}" "${OSQUERY_PYTHON_PATH}/${identifier}" "${downloadRemoteFile_destination}" "${OSQUERY_PYTHON_PATH}")
  184   add_dependencies("${target_name}_extractor" thirdparty_pythonpath)
  185 
  186   add_osquery_library("${target_name}" INTERFACE)
  187   add_dependencies("${target_name}" "${target_name}_extractor")
  188   add_dependencies("thirdparty_python_modules" "${target_name}")
  189 endfunction()
  190 
  191 # Used by each find_package script
  192 function(importFacebookLibrary library_name)
  193   if("${library_name}" STREQUAL "modules")
  194     message(FATAL_ERROR "Invalid library name specified: ${library_name}")
  195   endif()
  196 
  197   add_subdirectory(
  198     "${CMAKE_SOURCE_DIR}/libraries/cmake/facebook/${library_name}"
  199     "${CMAKE_BINARY_DIR}/libs/fb/${library_name}"
  200   )
  201 endfunction()
  202 
  203 # Make sure that globals.cmake and options.cmake have been included
  204 if("${OSQUERY_PYTHON_PATH}" STREQUAL "")
  205   message(FATAL_ERROR "The OSQUERY_PYTHON_PATH variable was not found. Has globals.cmake been included?")
  206 endif()
  207 
  208 if("${THIRD_PARTY_REPOSITORY_URL}" STREQUAL "")
  209   message(FATAL_ERROR "The THIRD_PARTY_REPOSITORY_URL variable was not found. Has options.cmake been included?")
  210 endif()
  211 
  212 initializePythonPathFolder()
  213