Skip to content

Instantly share code, notes, and snippets.

@raulcd
Last active October 4, 2022 16:03
Show Gist options
  • Save raulcd/5e7ea4c790348f7b34c5a5266929b978 to your computer and use it in GitHub Desktop.
Save raulcd/5e7ea4c790348f7b34c5a5266929b978 to your computer and use it in GitHub Desktop.
Blueprint to migrate third party dependencies to be built as part of Velox's CMake

Blueprint to migrate third party dependencies to be built as part of Velox's CMake

Velox uses CMake to be built and package.

Velox requires several third party dependencies and packages to be installed in order to be built. We are currently migrating how third party dependencies are installed, instead of requiring the dependencies to be installed as system dependencies we have started to modify our CMake build system to download and build the requirements as part of the build of Velox.

This download and build functionality can be found in the ThirdpartyToolchain.cmake file.

Steps to perform

This is a small blueprint of the things that have to be done in order to add a new library to be built as part of the CMake process.

  1. Add a new elseif block to the build_dependency macro on ThirdpartyToolchain.cmake. Substitute NEW_DEPENDENCY_NAME for the name of your dependency:
  elseif("${DEPENDENCY_NAME}" STREQUAL "NEW_DEPENDENCY_NAME")
    build_NEW_DEPENDENCY_NAME()
  1. Create a new macro called build_NEW_DEPENDENCY_NAME on ThirdpartyToolchain.cmake.:
macro(build_NEW_DEPENDENCY_NAME)
  message(STATUS "Building NEW_DEPENDENCY_NAME from source")
  FetchContent_Declare(
    NEW_DEPENDENCY_NAME
    URL ${NEW_DEPENDENCY_NAME_SOURCE_URL}
    URL_HASH SHA256=${VELOX_NEW_DEPENDENCY_NAME_BUILD_SHA256_CHECKSUM})
  if(NOT NEW_DEPENDENCY_NAME_POPULATED)
    # Fetch the content using previously declared details
    FetchContent_Populate(NEW_DEPENDENCY_NAME)
  endif()
  set(NEW_DEPENDENCY_NAME_LIBRARIES NEW_DEPENDENCY_NAME)
endmacro()
  1. Define the URL and SHA256 for the dependency:
if(DEFINED ENV{VELOX_NEW_DEPENDENCY_NAME_URL})
  set(NEW_DEPENDENCY_NAME_SOURCE_URL "$ENV{VELOX_NEW_DEPENDENCY_NAME_URL}")
else()
  set(VELOX_NEW_DEPENDENCY_NAME_BUILD_VERSION version)
  set(NEW_DEPENDENCY_NAME_SOURCE_URL
      "https://github.com/org/repo/archive/${VELOX_NEW_DEPENDENCY_NAME_BUILD_VERSION}.tar.gz"
  )
  set(VELOX_NEW_DEPENDENCY_NAME_BUILD_SHA256_CHECKSUM
      the_sha_for_the_dependency)
endif()
  1. Update the find_package on CMakeLists.txt call for the dependency with:
set(NEW_DEPENDENCY_NAME_SOURCE AUTO)
resolve_dependency(NEW_DEPENDENCY_NAME)

You might need to add some CXX_Flags to avoid some warnings when adding a new dependency.

You might have to update other CMake specifics as target_link_libraries, etcetera.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment