最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - How to compile the executable of a dependency on CMake - Stack Overflow

programmeradmin0浏览0评论

I'm trying to compile PDAL (/) 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, .cmake) to download the source code, but I can't compile the SQlite3 binaries before the compilation of PDAL starts.

set(SQLITE3_REPO ".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.

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/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/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.

Share Improve this question edited Mar 3 at 2:51 JP Cordova asked Mar 3 at 0:27 JP CordovaJP Cordova 12610 bronze badges 10
  • Didn't understood the question. Title says compile executable of dependency, does that mean you want to create the sqlite3 binary at configuration step of CMake? – kiner_shah Commented Mar 3 at 7:36
  • exactly, PDAL requires the sqlite binaries while compiling the PDAL source code, weird, I know. – JP Cordova Commented Mar 3 at 12:38
  • is this not just an issue with PDAL? proper usage of custom target/command DEPENDS should make the depended-upon target get built. or is PDAL expecting you to define those custom targets/commands? – starball Commented Mar 4 at 8:00
  • 1 @kiner_shah, indeed, seems like that is the answer, I'm finishing another project and I'll look back to this solution just to check it. – JP Cordova Commented Mar 4 at 20:20
  • 1 @kiner_shah "but I can't compile the SQlite3 binaries before the compilation of PDAL starts.". I'm still waiting for a reply from the asker for my first comment. – starball Commented Mar 5 at 8:38
 |  Show 5 more comments

1 Answer 1

Reset to default -1

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/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/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/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
  "name": "platdxf",
  "version": "0.1",
  "dependencies": [
    {
      "name": "cgal"
    },
    {
      "name": "qt5"
    }
  ]
}
发布评论

评论列表(0)

  1. 暂无评论