argtable / argtable3

A single-file, ANSI C, command-line parsing library that parses GNU-style command-line options.
http://www.argtable.org
Other
372 stars 65 forks source link

Enhance CMake scripts to take advantage of vcpkg's manifest mechanism #67

Closed tomghuang closed 3 years ago

tomghuang commented 3 years ago

The latest vcpkg release allows us to specify dependencies in the manifest file (vcpkg.json) in the project root directory. Since vcpkg already includes the 3.1.5 release in its ports, in theory developers should be able to add vcpkg.json to the root directory to add the argtable3 library:

{
    "name": "versions-test",
    "version": "1.0.0",
    "dependencies": [
        {
            "name": "fmt",
            "version>=": "7.1.3#4"
        }, 
        {
            "name": "argtable3",
            "version>=": "3.1.5"
        }, 
        "zlib"
    ],
    "builtin-baseline": "a3db16a4475b963cacf0260068c497fb72c8f3c0"
}

And include the dependencies in the root CMakeLists.txt:

cmake_minimum_required(VERSION 3.18)

set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/deps/vcpkg/scripts/buildsystems/vcpkg.cmake
 CACHE STRING "Vcpkg toolchain file")
set(VCPKG_TARGET_TRIPLET x64-windows-static CACHE STRING "Vcpkg target triplet")

project(versionstest CXX)

add_executable(main main.cpp)

find_package(ZLIB REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_package(Argtable3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE ZLIB::ZLIB fmt::fmt argtable3_static)
set_property(TARGET main PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

In the CMakeLists.txt file above, we specify that we want to use the x64-windows-static triplet, which means that we want to use the static library of the dependencies.

Now we can run the following commands to download the dependencies and build the project:

$ mkdir build
$ cd build
$ cmake ..
$ cmake --build .

However, CMake will show the following error messages:

CMake Error at build/vcpkg_installed/x64-windows-static/share/argtable3/Argtable3Config.cmake:76 (message):
  The imported target "argtable3" references the file

     "D:/Projects/testvcpkg/build/vcpkg_installed/x64-windows-static/debug/bin/argtable3.dll"

  but this file does not exist.  Possible reasons include:

  * The file was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and contained

     "D:/Projects/testvcpkg/build/vcpkg_installed/x64-windows-static/share/argtable3/Argtable3Config.cmake"

  but not all the files it references.

Call Stack (most recent call first):
  build/vcpkg_installed/x64-windows-static/share/argtable3/vcpkg-cmake-wrapper.cmake:1 (_find_package)
  deps/vcpkg/scripts/buildsystems/vcpkg.cmake:815 (include)
  CMakeLists.txt:15 (find_package)

The root cause of this problem is that argtable3 builds and installs both the dynamic and static libraries at the same time, while vcpkg expects argtable3 to install one of them. We need to modify how the CMake scripts export targets and install export files, so it won't conflict with vcpkg's design.

tomghuang commented 3 years ago

I've submitted a pull request to vcpkg (https://github.com/microsoft/vcpkg/pull/18325), and it has passed the code review. Now we're waiting for the pull request being merged.

tomghuang commented 3 years ago

vcpkg has merged the pull request, so everyone can use the latest vcpkg to install Argtable3 or add it as a dependency in the vcpkg manifest.