Automatically Download and Install Third-Party Libraries with CMake

* GreatSQL Community Original Content Cannot Be Used Without Authorization, Please Contact the Editor for Reprinting and Indicate the Source.

Introduction

In daily development, it is inevitable to use third-party libraries or to store some libraries separately. If the libraries are placed together with the code, it will inevitably lead to a large project. At this time, the method of separating libraries from source code can be adopted, and the need to download them can be determined during compilation.

Steps

Below are the specific operation steps, taking the download of the Oracle OCI library as an example.

UNSET Command to Initialize Parameter Variables Used in This Compilation

   # Initialize the path parameter for the oci library zip package LOCAL_OCI_LIB_ZIP

   UNSET(LOCAL_OCI_LIB_ZIP CACHE)

   # Initialize the extraction path parameter for the oci library LOCAL_OCI_LIB_DIR

    UNSET(LOCAL_OCI_LIB_DIR CACHE)

    # Initialize the download path parameter for the oci library zip package LOCAL_OCI_LIB_ZIP_DL_DIR

    UNSET(LOCAL_OCI_LIB_ZIP_DL_DIR CACHE)

SET Command to Set Initial Path or Name of Parameters

   # Set the folder name for storing the oci library

   SET(OCI_LIB_NAME "oci_x86_lib")

   # Set the folder name for extracting the oci library zip package    SET(OCI_LIB_UNZIP_DIR_NAME ${OCI_LIB_NAME})

    # Set the folder name for storing the oci library header files

    SET(LOCAL_OCI_INCLUDE_DIR_NAME "oci_include")

    # Set the name of the oci library zip package

    SET(OCI_LIB_ZIP_NAME "oci_lib.tar.gz")

    # Set the remote download URL for the oci library

    SET(OCI_LIB_DOWNLOAD_URL "http://xxxxxx/oci_lib.tar.gz")

    # Set the timeout for remote download of the oci library

    SET(DOWNLOAD_OCI_LIB_TIMEOUT 600 CACHE STRING "Timeout in seconds when downloading oci_lib.")

FIND_FILE Command to Check if Corresponding File or Folder Exists in the Path

    # Check if the zip package exists in the folder ora_oci_lib

         FIND_FILE(LOCAL_OCI_LIB_ZIP

              NAMES ${OCI_LIB_ZIP_NAME}

              PATHS "ora_oci_lib"

              NO_DEFAULT_PATH

            )

       # Check if the zip package has been extracted in the folder ora_oci_lib

        FIND_FILE(LOCAL_OCI_LIB_DIR

              NAMES ${OCI_LIB_NAME}

              PATHS "ora_oci_lib"

              NO_DEFAULT_PATH

            )

       # Check the download path of the zip package

       FIND_FILE(LOCAL_OCI_LIB_ZIP_DL_DIR

              NAMES ""

              PATHS "ora_oci_lib"

              NO_DEFAULT_PATH

            )

MESSAGE Command to Output Prompt Information for the Corresponding Steps

     # Output the information of the current operation step

     MESSAGE(STATUS "Checking dblink ${OCI_LIB_UNZIP_DIR_NAME} + ${LOCAL_OCI_LIB_ZIP} .....")

Check if the Current Zip Package Exists; If Not, Start Downloading Based on the Set URL

     # Check if the zip package already exists

      IF(NOT LOCAL_OCI_LIB_ZIP)

           # If it does not exist, perform the download operation and output the operation prompt information

            MESSAGE(STATUS "Downloading ${OCI_LIB_ZIP_NAME} to ${LOCAL_OCI_LIB_ZIP_DL_DIR}")

           # Download the corresponding zip package from the set URL

            FILE(DOWNLOAD ${OCI_LIB_DOWNLOAD_URL}

            ${LOCAL_OCI_LIB_ZIP_DL_DIR}/${OCI_LIB_ZIP_NAME}

            TIMEOUT ${DOWNLOAD_OCI_LIB_TIMEOUT}

            STATUS ERR

            SHOW_PROGRESS 

             )

       # Check if there is an error in the download

       IF(ERR EQUAL 0)

          # If there is no error in the download, set the zip package name to mark the download as successful

           SET(LOCAL_OCI_LIB_ZIP "${LOCAL_OCI_LIB_ZIP_DL_DIR}/${OCI_LIB_ZIP_NAME}")
        ELSE()

            # Output relevant prompt information for download error

            MESSAGE(STATUS "Download failed, error: ${ERR}")

            MESSAGE(FATAL_ERROR

              "You can try downloading ${OCI_LIB_DOWNLOAD_URL} manually"

                " using curl/wget or a similar tool"

            )
           ENDIF()

        ENDIF()

Check if the Download was Successful; If Successful, Perform Initial Validation of the Zip Package and Extract if No Issues

        IF(LOCAL_OCI_LIB_ZIP )

            # Output corresponding prompt information to check extraction paths, etc.

             MESSAGE(STATUS "Checking dblink ${OCI_LIB_UNZIP_DIR_NAME} + ${LOCAL_OCI_INCLUDE_DIR_NAME} + ${LOCAL_OTL_INCLUDE_DIR_NAME} ")
             IF(NOT EXISTS "${LOCAL_OCI_LIB_ZIP_DL_DIR}/${OCI_LIB_UNZIP_DIR_NAME}" OR NOT EXISTS               "${LOCAL_OCI_LIB_ZIP_DL_DIR}/${LOCAL_OCI_INCLUDE_DIR_NAME}" OR NOT EXISTS       "${LOCAL_OCI_LIB_ZIP_DL_DIR}/${LOCAL_OTL_INCLUDE_DIR_NAME}" )

            # Output prompt information to prepare for extraction

            MESSAGE(STATUS "cd ${LOCAL_OCI_LIB_ZIP_DL_DIR}; tar xfz ${LOCAL_OCI_LIB_ZIP}")

            # Get the size of the downloaded zip package for basic judgment

            GET_FILE_SIZE(${LOCAL_OCI_LIB_ZIP} LOCAL_ZIP_SIZE)

            # If the size of the zip package is 0, output the corresponding error information

            IF(LOCAL_ZIP_SIZE EQUAL 0)

                  # Remove the corrupted zip package and reset the corresponding parameters

                FILE(REMOVE ${LOCAL_OCI_LIB_ZIP})

                 UNSET(LOCAL_OCI_LIB_ZIP)

                UNSET(LOCAL_OCI_LIB_ZIP CACHE)

                MESSAGE(FATAL_ERROR  "${OCI_LIB_ZIP_NAME} is zero length. Deleting it.")
              ELSE()

               # If there are basically no issues with the downloaded file, enter the extraction path and start extracting the zip package

                 EXECUTE_PROCESS(

                  COMMAND ${CMAKE_COMMAND} -E tar xfz "${LOCAL_OCI_LIB_ZIP}"

                   WORKING_DIRECTORY "${LOCAL_OCI_LIB_ZIP_DL_DIR}"

                  RESULT_VARIABLE tar_result

                   )
               # Check if the extraction was successful

             IF (tar_result MATCHES 0)

                 # Set the success flag parameter if extraction is successful

                  SET(OCI_LIB_FOUND 1 CACHE INTERNAL "")
             ELSE()

                 # Output corresponding error prompt information for extraction failure

                MESSAGE(STATUS "Failed to extract files.\n"

                  "   Please try downloading and extracting yourself.\n"
                 "   The url is: ${OCI_LIB_DOWNLOAD_URL}")
             ENDIF()

        ENDIF()

       ENDIF()

      ENDIF()        

The Third-Party Library Has Been Downloaded and Extracted; Subsequent Compilation Can Be Set with the Corresponding Link Path Based on Your Source Code

      INCLUDE_DIRECTORIES command can set the include path for header files

     LINK_DIRECTORIES command can set the path for third-party library files

After Compilation, You Can Use the INSTALL Command to Copy the Libraries Needed for Subsequent Application Runtime to the Specified Installation Directory

   CMAKE_INSTALL_PREFIX is the set installation path

   OCI_LIB_NAME is the folder where the library is placed

     # copy  lib 

     INSTALL(

       FILES ${allCopyFiles}  DESTINATION ${CMAKE_INSTALL_PREFIX}/${OCI_LIB_NAME}

     )

Enjoy GreatSQL :)

Deep Dive into MGR Video Course

Click this app to go directly to Bilibili

https://www.bilibili.com/medialist/play/1363850082?business=space_collection&business_id=343928&desc=0

Recommended Articles:

  • How Postman Accesses MySQL via the xmysql Tool’s Restful API Interface

  • Using MySQL Master-Slave Replication Delay to Rescue Deleted Data

  • Basic Code Analysis of MySQL Table Operations

  • In-Depth Analysis of MySQL 8.0 Error Logs

  • Interpretation of MySQL Update Execution Process

  • Introduction to MySQL B+ Tree Index and Hash Index

  • Compiling and Installing MySQL Shell for GreatSQL 8.0.25-16

  • Solution for MySQL Shell Unable to Start MGR Cluster

If you want to see more technical articles, click on “Looking”!

Leave a Comment