This is a demonstration project from the ManyDeps, for the C/C++ GMP library using package managers (vcpkg and conan) on windows/linux.
If you want to learn more about this repo, please read the Medium text Building Cross-platform C++ GMP library with VCPKG, CMake and Bazel: Lessons Learned.
This works fine on Windows Visual Studio 2022 and also Linux (including Windows WSL).
For Linux, the classic gmp
library is used, but for Windows, the alternative mpir
is used (since gmp
currently fails to build on Windows in this repo).
Notes for Windows only: Conan is much faster and requires /MD build (GMP works fine). VCPKG is much slower and requires /MT build (and only MPIR works).
Basic setup in both platforms include: CMake and Ninja. Also it is necessary to have vcpkg and conan, for the following scripts to work.
Please follow the next steps carefully.
vcpkg is installed as shown in vcpkg.io website, as a git submodule.
To get submodules, please run the following commands:
git submodule update --init --recursive
git pull --recurse-submodules
Check the folder tools/vcpkg to see if vcpkg is present.
On Windows Visual Studio, open the Developer Command Prompt and execute:
.\tools\vcpkg\bootstrap-vcpkg.bat
.\script-deps.bat
On Linux (or WSL), just run:
./tools/vcpkg/bootstrap-vcpkg.sh
./script-deps.sh
On Linux, sometimes dependencies are not fully available, so please double check:
python3 -m pip install --upgrade
apt-get install ninja-build
vcpkg requires some packages, such as pkg-config and autoconf:
apt-get install autoconf automake libtool pkg-config
Update your CMakePresets.json to include the desired toolchain.
"toolchainFile": "${sourceDir}/deps/vcpkg/scripts/buildsystems/vcpkg.cmake",
Read more on vcpkg integration: https://learn.microsoft.com/pt-br/vcpkg/users/buildsystems/cmake-integration
Install conan with:
python3 -m pip install conan
Setup toolchain with cmake and conan:
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_TOOLCHAIN_FILE": "build-conan/conan_toolchain.cmake"
},
The vcpkg tries to create some CMake toolchain with PkgConfig,
in order to get dependencies.
We try to build them as STATIC libraries:
libgmp.a
(on linux) and gmp.lib
(on windows).
This is the expected configuration after vcpkg install
:
[cmake] # gmp
[cmake] find_package(PkgConfig REQUIRED)
[cmake] pkg_check_modules(gmp REQUIRED IMPORTED_TARGET gmp)
[cmake] target_link_libraries(main PkgConfig::gmp)
[cmake]
[cmake] # gmpxx
[cmake] find_package(PkgConfig REQUIRED)
[cmake] pkg_check_modules(gmpxx REQUIRED IMPORTED_TARGET gmpxx)
[cmake] target_link_libraries(main PkgConfig::gmpxx)
However, this failed in both Linux and Windows (in our experiments here), so we need to make some adjustments (A LOT OF ADJUSTMENTS FOR WINDOWS).
The idea is to load packages locally on build/vcpkg_installed/
folder
(generated by vcpkg on our script-deps
).
We still could not fully understand why it fails so bad in Windows,
but it seems that some files such as gmp.pc
are missing!
For Linux they exist on build/vcpkg_installed/x64-linux/lib/pkgconfig/
folder.
If you find a better solution, please Let Us Know!
vcpkg install
# will generate local folder vcpkg_installed
bazel run @hedron_compile_commands//:refresh_all
# will generate local compile_commands.json
bazel build ... --config linux
bazel test ... --config windows
Note some important flags on .bazelrc file.
On windows, there are two types of .lib
library, so we assume here that GMP/MPIR is built with /MT
flag,
meaning that we need to add static_link_msvcrt
configuration on .bazelrc:
build:windows --cxxopt=/std:c++17 --cxxopt=/MT --linkopt=/NODEFAULTLIB:MSVCRT --features=static_link_msvcrt
Ongoing work...
Conan automatically generates a Software Bill of Materials (SBOM):
conan config install https://github.com/conan-io/conan-extensions.git
conan sbom:cyclonedx --format 1.4_json .
{
"components": [
// ...
{
"bom-ref": "pkg:conan/gmp@6.3.0?rref=f925bd29cb8f70972507c699d681de85",
"description": "GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.",
"externalReferences": [
{
"type": "website",
"url": "https://gmplib.org"
}
],
"licenses": [
{
"license": {
"id": "GPL-2.0"
}
},
{
"license": {
"id": "LGPL-3.0"
}
}
],
"name": "gmp",
"purl": "pkg:conan/gmp@6.3.0?rref=f925bd29cb8f70972507c699d681de85",
"type": "library",
"version": "6.3.0"
}
],
"dependencies": [
// ...
],
"metadata": {
// ...
},
"serialNumber": "urn:uuid:42ecb73d-d67e-41e3-817f-ce68dd582711",
"version": 1,
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.4"
}
(... or at your choice)
License is a combination of:
- GMP (GPL-2.0-or-later OR LGPL-3.0-or-later)