Setting variable in port overlay not taking effect in vcpkg

175 views Asked by At

I've been trying to build gflags using vcpkg. I know you can getting using vcpkg install gflags

The problem is that aparently I need to build it with component shared. this is the error I'm getting when I run cmake with -DCMAKE_TOOLCHAIN_FILE=D:\MTC_source_code\vcpkg\scripts\buildsystems\vcpkg.cmake

CMake Error at D:/src/vcpkg/installed/x64-windows/share/gflags/gflags-config.cmake:40 (message):
  Package gflags was installed without required component shared!

I notice that if I build gflags from scratch and then install it I get the correct behavior. I installed it from source in the following way.

git clone https://github.com/gflags/gflags.git
cd gflags
mkdir _build
cd _build 
cmake .. -G "NMake Makefiles" -DGFLAGS_BUILD_SHARED_LIBS="ON" -DBUILD_SHARED_LIBS="ON"
nmake
nmake install/local

That gets rid of the component shared issue.

I try to replicate this in vcpkg by creating a port overlay for gflags, I tried two things:

  1. Modifying portfile.cmake to reflect the needed flags:
vcpkg_cmake_configure(
    SOURCE_PATH "${SOURCE_PATH}"
    OPTIONS
        -DGFLAGS_REGISTER_BUILD_DIR:BOOL=OFF
        -DGFLAGS_REGISTER_INSTALL_PREFIX:BOOL=OFF
        -DBUILD_gflags_nothreads_LIB:BOOL=ON
        -DGFLAGS_USE_TARGET_NAMESPACE:BOOL=ON
        -DCMAKE_DEBUG_POSTFIX=d
        -DGFLAGS_BUILD_SHARED_LIBS="ON" 
        -DBUILD_SHARED_LIBS="ON"
)

But this didnt solve the issue.

  1. I then tried to create a patch, and apply it to portfile.cmake and it didnt work either.

Content of the patch:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 53e77cb..17faeb4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -93,7 +93,8 @@ set (PACKAGE_TARNAME     "${PACKAGE_NAME}-${PACKAGE_VERSION}")
 set (PACKAGE_BUGREPORT   "https://github.com/gflags/gflags/issues")
 set (PACKAGE_DESCRIPTION "A commandline flags library that allows for distributed flags.")
 set (PACKAGE_URL         "http://gflags.github.io/gflags")
-
+set (GFLAGS_BUILD_SHARED_LIBS "ON")
+set (BUILD_SHARED_LIBS "ON")
 project (${PACKAGE_NAME} VERSION ${PACKAGE_VERSION} LANGUAGES CXX)
 if (CMAKE_VERSION VERSION_LESS 3.4)
   # C language still needed because the following required CMake modules

It still fails with:

CMake Error at D:/src/vcpkg/installed/x64-windows/share/gflags/gflags-config.cmake:40 (message):
  Package gflags was installed without required component shared!

Interestingly enough, if I find the gflags folder in the vcpkg archives (vcpkg\buildtrees\gflags\src\v2.2.2-d9283b9285), dont modify anything and then build using:

mkdir _build
cd _build 
cmake .. -G "NMake Makefiles"
nmake
nmake install/local

it works, it seems like it only fails when built and installed from vcpkg.

What am I missing to get what I require?

2

There are 2 answers

0
Alexander Neumann On

vcpkg sets -DBUILD_SHARED_LIBS="ON" on its own depending on VCPKG_LIBRARY_LINKAGE within vcpkg_cmake_configure. Since it is added after OPTIONS in the portfile it will simply overrule the setting. You can either change VCPKG_LIBRARY_LINKAGE to static within the portfile/triplet or use VCPKG_CMAKE_CONFIGURE_OPTIONS to force your setting.

0
Thibault On

I assume you are working on Windows.

By default vcpkg is building dependencies while using triplets for the build configuration.
By default, the triplet is x64-linux (this triplet sets a static build) for linux and x64-windows (this triplet sets a dynamic build) for windows.

You can use the command vcpkg install gflags --triplet x64-windows just to be sure to build the lib as a shared lib.

You can look at the different triplets available in your vcpkg repo (vcpkg/triplets).

You also can call vcpkg from cmake configuration. If so, you should use the correct triplet for host and target.

Your cmake command line could be something like that:
cmake -S . -B build -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_TOOLCHAIN_FILE=<vcpkg_dir_path>/scripts/buildsystems/vcpkg.cmake -DVCPKG_MANIFEST_INSTALL:BOOL=ON -DVCPKG_HOST_TRIPLET=x64-windows -DVCPKG_HOST_TRIPLET=x64-windows

However, the flags should not be passed through the vcpkg ports but with the cmake vcpkg flags, as shown above. If you want/need to set the GFLAGS_BUILD_SHARED_LIBS flag, you should modify the port file in a way that sets GFLAGS_BUILD_SHARED_LIBS flag (I don't know gflags) according to the BUILD_SHARED_LIBS flag that is automatically forwarded by cmake.

Documentation:
https://learn.microsoft.com/en-us/vcpkg/concepts/triplets
https://learn.microsoft.com/en-us/vcpkg/users/buildsystems/cmake-integration#settings-reference