JP Cordova
JP Cordova

Reputation: 119

How to compile the executable of a dependency on CMake

I'm trying to compile PDAL (https://pdal.io/) automating the download of all the dependencies to avoid using conda as requested in the documentation (on windows btw). One of the dependencies is SQlite3, needed by PDAL to use libraries and compiled binaries to create some databases at compilation time.

I already managed to use CPM (Setup-free CMake dependency management, https://github.com/cpm-cmake/CPM.cmake) to download the source code, but I can't compile the SQlite3 binaries before the compilation of PDAL starts.

set(SQLITE3_REPO "https://github.com/sqlite/sqlite.git"
    CACHE STRING "SQlite3 repository location."
)

CPMAddPackage(
    NAME SQLITE3
    GIT_REPOSITORY ${SQLITE3_REPO}
    GIT_TAG "version-3.45.1"
    SOURCE_DIR ${THIRD_PARTY_PATH}/sqlite3
    OUTPUT_DIR ${THIRD_PARTY_PATH}/bin
    BINARY_DIR ${THIRD_PARTY_PATH}/bin
    LIBRARY_OUTPUT_DIRECTORY ${THIRD_PARTY_PATH}/bin
)

At the end, I downloaded the binaries taking care to match the source version I'm using and adding the respective path to be found by cmake:

list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/third_party/bin)

Now, I want to see if I can use cmake to automate the process of download, compilation, and installation of sqlite3 in the binary directory, I'm quite lost here, between outdated information everywhere and my lack of understanding about cmake, probably the solution is just there or just can't be done as I want.

I'm aware that the control of the compilations is handled by cmake, and probably the only option I have is to use COMMAND to force the compilation, but I'm not sure if also is controlled by cmake independently of the position of the command on the file (I'm testing that now).

If there is any other way o the proper way to do that I'll appreciate it a lot if someone could tell me or guide me to the correct way to do it.

Thanks in advance.

Upvotes: 0

Views: 44

Answers (1)

Jakkapong Rattananen
Jakkapong Rattananen

Reputation: 167

My recent project linux use FetchContent FetchContent_Declare FetchContent_MakeAvailable for mange dependency. but should work for Windows as well.

CmakeLists.txt

cmake_minimum_required (VERSION 3.20)

project ("platdxf")

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".lib" ".so" ".dll")


include(FetchContent)

set(FT_DISABLE_ZLIB ON)
set(FT_DISABLE_BZIP2 ON)
set(FT_DISABLE_PNG ON)
set(FT_DISABLE_BROTLI ON)
set(FT_DISABLE_HARFBUZZ ON)
FetchContent_Declare(
  freetype
  GIT_REPOSITORY https://github.com/freetype/freetype.git
  GIT_TAG        VER-2-13-3
)

FetchContent_MakeAvailable(freetype)

find_package(CGAL REQUIRED COMPONENTS Qt5)


file(GLOB math_files 
    "src/plat/math/matrix.cpp"
    "src/plat/math/vector.cpp"
  )

file(GLOB lib_files 
    "src/plat/shape.hpp" 
    "src/plat/cut.hpp" 
    "src/plat/core.hpp"
    "src/plat/cgal.hpp"
    "src/plat/util.hpp") #for IDE intellisense

file(GLOB dxf_files 
    "src/plat/dxf/section_head.cpp" 
    "src/plat/dxf/section_tables.cpp"
    "src/plat/dxf/section_blocks.cpp"
    "src/plat/dxf/section_entities.cpp"
    "src/plat/dxf/section_objects.cpp"
    "src/plat/dxf/dxf.cpp"
  )

file(GLOB text_files 
  "src/plat/text/font.cpp"
  "src/plat/text/util.cpp"
)

add_library(math_lib ${math_files})

add_library(dxf_lib ${dxf_files})

add_library(text_lib ${text_files})
target_link_libraries(text_lib PUBLIC freetype)

add_executable (platdxf "src/main_export.cpp")
target_link_libraries(platdxf PRIVATE  CGAL::CGAL dxf_lib math_lib -static)

add_executable (platdxf_norm  "src/main_dxf_norm.cpp")
target_link_libraries(platdxf_norm PRIVATE dxf_lib math_lib -static)

add_executable (platdxf_ident  "src/main_dxf_ident.cpp")
target_link_libraries(platdxf_ident PRIVATE dxf_lib math_lib -static)

add_executable (platdxf_text "src/main_dxf_text.cpp")
target_link_libraries(platdxf_text PRIVATE dxf_lib math_lib text_lib -static)

add_executable (plat_text_info "src/main_text_info.cpp")
target_link_libraries(plat_text_info PRIVATE  math_lib text_lib -static)

add_executable (test_font  "src/main_font_test.cpp")
target_link_libraries(test_font PRIVATE  CGAL::CGAL CGAL::CGAL_Basic_viewer math_lib text_lib)

Actually freetype has package for find_package but it's build with full feature that require more dependencies. so use FetchContent to build with minimum feature.

for Windows you might consider vcpkg.io . it's working well with cmake-presets. by add two more file.

CMakePresets.json

{
    "version": 3,
    "configurePresets": [
      {
        "name": "windows-base",
        "hidden": true,
        "generator": "Ninja",
        "binaryDir": "${sourceDir}/out/build/${presetName}",
        "installDir": "${sourceDir}/out/install/${presetName}",
        "cacheVariables": {
          "CMAKE_C_COMPILER": "cl.exe",
          "CMAKE_CXX_COMPILER": "cl.exe"
        },
        "condition": {
          "type": "equals",
          "lhs": "${hostSystemName}",
          "rhs": "Windows"
        }
      },
      {
        "name": "x64-debug",
        "displayName": "x64 Debug",
        "inherits": "windows-base",
        "architecture": {
          "value": "x64",
          "strategy": "external"
        },
        "cacheVariables": {
          "CMAKE_BUILD_TYPE": "Debug",
          "CMAKE_TOOLCHAIN_FILE": {
            "value": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
            "type": "FILEPATH"
          }
        }
      },
      {
        "name": "x64-release",
        "displayName": "x64 Release",
        "inherits": "x64-debug",
        "cacheVariables": {
          "CMAKE_BUILD_TYPE": "Release"
        }
      },
      {
        "name": "linux-debug",
        "displayName": "Linux Debug",
        "generator": "Ninja",
        "binaryDir": "${sourceDir}/out/build/${presetName}",
        "installDir": "${sourceDir}/out/install/${presetName}",
        "architecture": {
          "value": "x64",
          "strategy": "external"
        },
        "cacheVariables": {
          "CMAKE_BUILD_TYPE": "Debug",
          "CMAKE_VERBOSE_MAKEFILE":true
        },
        "condition": {
          "type": "equals",
          "lhs": "${hostSystemName}",
          "rhs": "Linux"
        },
        "vendor": {
          "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
            "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
          }
        }
      },
      {
        "name": "linux-release",
        "displayName": "Linux Release",
        "inherits": "linux-debug",
        "cacheVariables": {
          "CMAKE_BUILD_TYPE": "Release"
        }
      }
    ]
  }
  

and vcpkg.json

{
  "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
  "name": "platdxf",
  "version": "0.1",
  "dependencies": [
    {
      "name": "cgal"
    },
    {
      "name": "qt5"
    }
  ]
}

Upvotes: 0

Related Questions