共用方式為


vcpkg_check_features

檢查一或多個功能是否為套件安裝的一部分。

使用方式

vcpkg_check_features(
    OUT_FEATURE_OPTIONS <out-var>
    [PREFIX <prefix>]
    [FEATURES
        [<feature-name> <feature-var>]...
        ]
    [INVERTED_FEATURES
        [<feature-name> <feature-var>]...
        ]
)

<out-var>應該依照慣例會設定為 FEATURE_OPTIONS

vcpkg_check_features() 將:

  • 針對每個傳入FEATURES<feature-name>
    • 如果功能已設定,請將 新增 -D<feature-var>=ON<out-var>,並將 設定 <prefix>_<feature-var> 為 ON。
    • 如果未設定功能,請將 新增 -D<feature-var>=OFF<out-var>,並將 設定 <prefix>_<feature-var> 為 OFF。
  • 針對每個傳入INVERTED_FEATURES<feature-name>
    • 如果功能已設定,請將 新增 -D<feature-var>=OFF<out-var>,並設定 <prefix>_<feature-var> 為 OFF。
    • 如果未設定功能,請將 新增 -D<feature-var>=ON<out-var>,並將 設定 <prefix>_<feature-var> 為 ON。

如果未 <prefix> 傳遞,則功能 vars 集合只是 <feature-var>,而不是 _<feature-var>

如果未 INVERTED_FEATURES 傳遞,則 FEATURES 關鍵詞是選擇性的。 這個行為已被取代。

如果相同 <feature-var> 多次傳遞,則 vcpkg_check_features 會導致嚴重錯誤,因為這是錯誤。

範例

範例 1:一般功能

$ ./vcpkg install mimalloc[asm,secure]

# ports/mimalloc/portfile.cmake
vcpkg_check_features(OUT_FEATURE_OPTIONS FEATURE_OPTIONS
    FEATURES
        asm       MI_SEE_ASM
        override  MI_OVERRIDE
        secure    MI_SECURE
)

vcpkg_cmake_configure(
    SOURCE_PATH "${SOURCE_PATH}"
    OPTIONS
        # Expands to "-DMI_SEE_ASM=ON;-DMI_OVERRIDE=OFF;-DMI_SECURE=ON"
        ${FEATURE_OPTIONS}
)

範例 2:反轉功能

$ ./vcpkg install cpprestsdk[websockets]

# ports/cpprestsdk/portfile.cmake
vcpkg_check_features(OUT_FEATURE_OPTIONS FEATURE_OPTIONS
    INVERTED_FEATURES
        brotli      CPPREST_EXCLUDE_BROTLI
        websockets  CPPREST_EXCLUDE_WEBSOCKETS
)

vcpkg_cmake_configure(
    SOURCE_PATH "${SOURCE_PATH}"
    OPTIONS
        # Expands to "-DCPPREST_EXCLUDE_BROTLI=ON;-DCPPREST_EXCLUDE_WEBSOCKETS=OFF"
        ${FEATURE_OPTIONS}
)

範例 3:設定相同功能的多個選項

$ ./vcpkg install pcl[cuda]

# ports/pcl/portfile.cmake
vcpkg_check_features(OUT_FEATURE_OPTIONS FEATURE_OPTIONS
    FEATURES
        cuda  WITH_CUDA
        cuda  BUILD_CUDA
        cuda  BUILD_GPU
)

vcpkg_cmake_configure(
    SOURCE_PATH "${SOURCE_PATH}"
    OPTIONS
        # Expands to "-DWITH_CUDA=ON;-DBUILD_CUDA=ON;-DBUILD_GPU=ON"
        ${FEATURE_OPTIONS}
)

範例 4:使用一般和反向功能

$ ./vcpkg install rocksdb[tbb]

# ports/rocksdb/portfile.cmake
vcpkg_check_features(OUT_FEATURE_OPTIONS FEATURE_OPTIONS
    FEATURES
        tbb   WITH_TBB
    INVERTED_FEATURES
        tbb   ROCKSDB_IGNORE_PACKAGE_TBB
)

vcpkg_cmake_configure(
    SOURCE_PATH "${SOURCE_PATH}"
    OPTIONS
        # Expands to "-DWITH_TBB=ON;-DROCKSDB_IGNORE_PACKAGE_TBB=OFF"
        ${FEATURE_OPTIONS}
)

portfiles 中的範例

來源

scripts/cmake/vcpkg_check_features.cmake