From cca037e8fc09822effdd65d230515ac06eb08ae9 Mon Sep 17 00:00:00 2001 From: Alex Cameron Date: Fri, 2 Dec 2022 21:28:14 +1100 Subject: [PATCH] Move to explicitly setting CMake toolchain file (#313) * Move to CMake toolchain file * Stop specifying compilers in CMake presets --- .github/workflows/ci.yml | 2 +- .github/workflows/diff_tests.yml | 6 ++-- CMakeLists.txt | 11 +++--- CMakePresets.json | 16 ++++----- README.md | 2 +- cmake/vcpkg_helper.cmake | 57 -------------------------------- scripts/build-preset.sh | 22 +++--------- scripts/build.sh | 16 ++++++--- 8 files changed, 33 insertions(+), 99 deletions(-) delete mode 100644 cmake/vcpkg_helper.cmake diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0a7e8d52..02076685 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,7 +70,7 @@ jobs: - name: Build with CMake Presets shell: bash run: | - export VCPKG_ROOT=${GITHUB_WORKSPACE}/${VCPKG_ROOT_PART} + export CMAKE_TOOLCHAIN_FILE=${GITHUB_WORKSPACE}/${VCPKG_ROOT_PART}/scripts/buildsystems/vcpkg.cmake export INSTALL_DIR=${GITHUB_WORKSPACE}/${INSTALL_DIR_PART} scripts/build-preset.sh debug env: diff --git a/.github/workflows/diff_tests.yml b/.github/workflows/diff_tests.yml index b927daab..ced760d0 100644 --- a/.github/workflows/diff_tests.yml +++ b/.github/workflows/diff_tests.yml @@ -14,7 +14,7 @@ jobs: - { name: 'ubuntu', tag: '20.04', codename: 'focal' } llvm: [ '14' ] common_base: [ 'https://github.com/lifting-bits/cxx-common/releases/latest/download' ] - + env: CC: clang-${{ matrix.llvm }} CXX: clang++-${{ matrix.llvm }} @@ -62,13 +62,13 @@ jobs: - name: Build old rellic shell: bash run: | - cmake -G Ninja -S old -B rellic-build-old -DVCPKG_ROOT=$GITHUB_WORKSPACE/vcpkg_${{ matrix.image.name }}-${{ matrix.image.tag }}_llvm-${{ matrix.llvm }}_amd64 + cmake -G Ninja -S old -B rellic-build-old -DCMAKE_TOOLCHAIN_FILE=$GITHUB_WORKSPACE/vcpkg_${{ matrix.image.name }}-${{ matrix.image.tag }}_llvm-${{ matrix.llvm }}_amd64/scripts/buildsystems/vcpkg.cmake cmake --build rellic-build-old - name: Build new rellic shell: bash run: | - cmake -G Ninja -S . -B rellic-build -DVCPKG_ROOT=$GITHUB_WORKSPACE/vcpkg_${{ matrix.image.name }}-${{ matrix.image.tag }}_llvm-${{ matrix.llvm }}_amd64 + cmake -G Ninja -S . -B rellic-build -DCMAKE_TOOLCHAIN_FILE=$GITHUB_WORKSPACE/vcpkg_${{ matrix.image.name }}-${{ matrix.image.tag }}_llvm-${{ matrix.llvm }}_amd64/scripts/buildsystems/vcpkg.cmake cmake --build rellic-build - name: Print job summary diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ebed8cf..32e24128 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,11 +9,10 @@ cmake_minimum_required(VERSION 3.21) include("cmake/options.cmake") -include("cmake/vcpkg_helper.cmake") project(rellic) -set(CMAKE_CXX_VISIBILITY_PRESET hidden) -set(CMAKE_C_VISIBILITY_PRESET hidden) +set(CMAKE_CXX_VISIBILITY_PRESET hidden) +set(CMAKE_C_VISIBILITY_PRESET hidden) set (VISIBILITY_INLINES_HIDDEN YES) include("cmake/settings.cmake") include(GNUInstallDirs) @@ -62,9 +61,9 @@ find_package(Clang CONFIG REQUIRED) if(RELLIC_ENABLE_INSTALL) export(PACKAGE "${PROJECT_NAME}") - + set(cmake_install_dir "lib/cmake/${PROJECT_NAME}") - + include(CMakePackageConfigHelpers) configure_package_config_file("${PROJECT_NAME}Config.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" @@ -112,7 +111,7 @@ if (RELLIC_ENABLE_TESTING) get_target_property(CLANG_PATH clang LOCATION) message(STATUS "Clang path for tests: \"${CLANG_PATH}\"") - + find_package(Python3 COMPONENTS Interpreter REQUIRED) message(STATUS "Python path for tests: \"${Python3_EXECUTABLE}\"") diff --git a/CMakePresets.json b/CMakePresets.json index 7bf72b54..da5dc548 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -60,10 +60,8 @@ "generator": "Ninja", "cacheVariables": { "VCPKG_TARGET_TRIPLET": "$env{VCPKG_TARGET_TRIPLET}", - "VCPKG_ROOT": "$env{VCPKG_ROOT}", + "CMAKE_TOOLCHAIN_FILE": "$env{CMAKE_TOOLCHAIN_FILE}", "CMAKE_INSTALL_PREFIX": "$env{INSTALL_DIR}/install", - "CMAKE_C_COMPILER": "$env{VCPKG_ROOT}/installed/$env{VCPKG_TARGET_TRIPLET}/tools/llvm/clang", - "CMAKE_CXX_COMPILER": "$env{VCPKG_ROOT}/installed/$env{VCPKG_TARGET_TRIPLET}/tools/llvm/clang++", "RELLIC_ENABLE_TESTING": "ON" } }, @@ -86,37 +84,37 @@ "name": "vcpkg-x64-dbg", "inherits": ["vcpkg-debug", "x86_64"], "displayName": "Debug Build (vcpkg) (x64)", - "description": "Build a Debug version against a VCPKG installation. Define 'VCPKG_ROOT', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" + "description": "Build a Debug version against a VCPKG installation. Define 'CMAKE_TOOLCHAIN_FILE', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" }, { "name": "vcpkg-x64-rel", "inherits": ["vcpkg-release", "x86_64"], "displayName": "Release Build (vcpkg) (x64)", - "description": "Build a Release version against a VCPKG installation. Define 'VCPKG_ROOT', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" + "description": "Build a Release version against a VCPKG installation. Define 'CMAKE_TOOLCHAIN_FILE', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" }, { "name": "vcpkg-x64-asan", "inherits": ["vcpkg-asan", "x86_64"], "displayName": "Debug ASAN Build (vcpkg) (x64)", - "description": "Build a Debug ASAN version against a VCPKG installation. Define 'VCPKG_ROOT', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" + "description": "Build a Debug ASAN version against a VCPKG installation. Define 'CMAKE_TOOLCHAIN_FILE', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" }, { "name": "vcpkg-arm64-dbg", "inherits": ["vcpkg-debug", "arm64"], "displayName": "Debug Build (vcpkg) (arm64)", - "description": "Build a Debug version against a VCPKG installation. Define 'VCPKG_ROOT', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" + "description": "Build a Debug version against a VCPKG installation. Define 'CMAKE_TOOLCHAIN_FILE', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" }, { "name": "vcpkg-arm64-rel", "inherits": ["vcpkg-release", "arm64"], "displayName": "Release Build (vcpkg) (arm64)", - "description": "Build a Release version against a VCPKG installation. Define 'VCPKG_ROOT', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" + "description": "Build a Release version against a VCPKG installation. Define 'CMAKE_TOOLCHAIN_FILE', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" }, { "name": "vcpkg-arm64-asan", "inherits": ["vcpkg-asan", "arm64"], "displayName": "Debug ASAN Build (vcpkg) (arm64)", - "description": "Build a Debug ASAN version against a VCPKG installation. Define 'VCPKG_ROOT', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" + "description": "Build a Debug ASAN version against a VCPKG installation. Define 'CMAKE_TOOLCHAIN_FILE', 'INSTALL_DIR', 'VCPKG_TARGET_TRIPLET' env vars!" } ], "buildPresets": [ diff --git a/README.md b/README.md index 69bfba18..564cfd67 100644 --- a/README.md +++ b/README.md @@ -257,7 +257,7 @@ Make sure to have the latest release of cxx-common for LLVM 14. Then, build with ```shell cmake \ -DCMAKE_BUILD_TYPE=RelWithDebInfo \ - -DVCPKG_ROOT=/path/to/vcpkg \ + -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake \ -DVCPKG_TARGET_TRIPLET=x64-osx-rel \ -DRELLIC_ENABLE_TESTING=OFF \ -DCMAKE_C_COMPILER=`which clang` \ diff --git a/cmake/vcpkg_helper.cmake b/cmake/vcpkg_helper.cmake deleted file mode 100644 index 8075263c..00000000 --- a/cmake/vcpkg_helper.cmake +++ /dev/null @@ -1,57 +0,0 @@ -set(USE_SYSTEM_DEPENDENCIES OFF CACHE BOOL "Use system dependencies instead of trying to find vcpkg") - -if (NOT USE_SYSTEM_DEPENDENCIES) - set(VCPKG_ROOT "" CACHE FILEPATH "Root directory to use for vcpkg-managed dependencies") - if (VCPKG_ROOT) - if (NOT EXISTS "${VCPKG_ROOT}") - message(FATAL_ERROR "VCPKG_ROOT directory does not exist: '${VCPKG_ROOT}'") - endif() - - set(VCPKG_ROOT_INSTALL_DIR "${VCPKG_ROOT}/installed") - if (NOT EXISTS "${VCPKG_ROOT_INSTALL_DIR}") - message(FATAL_ERROR "VCPKG_ROOT installation directory does not exist: '${VCPKG_ROOT_INSTALL_DIR}'") - endif() - - set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE FILEPATH "" FORCE) - else() - message(FATAL_ERROR "Please define a path to VCPKG_ROOT. See https://github.com/lifting-bits/cxx-common for more details. Or if you don't want to use vcpkg dependencies, add '-DUSE_SYSTEM_DEPENDENCIES=ON'") - endif() - - # Set default triplet to Release VCPKG build unless we can't find it - if (NOT DEFINED VCPKG_TARGET_TRIPLET) - set(_project_arch "x64") - if (UNIX) - execute_process(COMMAND uname -m - OUTPUT_VARIABLE _SYSTEM_ARCH - OUTPUT_STRIP_TRAILING_WHITESPACE) - else() - message(WARNING "No detection of architecture for this platform. Assuming x64") - endif() - if (_SYSTEM_ARCH MATCHES "^[Aa][Aa][Rr][Cc][Hh]64$" OR _SYSTEM_ARCH MATCHES "^[Aa][Rr][Mm]64$") - set(_project_arch "arm64") - endif() - - if (APPLE) - set(_project_vcpkg_triplet "${_project_arch}-osx-rel") - elseif(UNIX) - set(_project_vcpkg_triplet "${_project_arch}-linux-rel") - elseif(WIN32) - set(_project_vcpkg_triplet "${_project_arch}-windows-static-md-rel") - else() - message(FATAL_ERROR "Could not detect default release triplet") - endif() - - if (NOT EXISTS "${VCPKG_ROOT_INSTALL_DIR}/${_project_vcpkg_triplet}") - message(STATUS "Could not find installed project-default triplet '${_project_vcpkg_triplet}' using vcpkg-default for your system") - else() - set(VCPKG_TARGET_TRIPLET "${_project_vcpkg_triplet}" CACHE STRING "") - message(STATUS "Setting default vcpkg triplet to release-only libraries: ${VCPKG_TARGET_TRIPLET}") - endif() - endif() - - if (DEFINED VCPKG_TARGET_TRIPLET AND NOT EXISTS "${VCPKG_ROOT_INSTALL_DIR}/${VCPKG_TARGET_TRIPLET}") - message(FATAL_ERROR "Could not find vcpkg triplet (${VCPKG_TARGET_TRIPLET}) installation libraries '${VCPKG_ROOT_INSTALL_DIR}/${VCPKG_TARGET_TRIPLET}'.") - endif() - - message(STATUS "Using vcpkg installation directory at '${VCPKG_ROOT_INSTALL_DIR}/${VCPKG_TARGET_TRIPLET}'") -endif() diff --git a/scripts/build-preset.sh b/scripts/build-preset.sh index 69ecd125..4d194b23 100755 --- a/scripts/build-preset.sh +++ b/scripts/build-preset.sh @@ -11,11 +11,11 @@ VCPKG_SUFFIX="-rel" set -o pipefail function sanity_check { - if [ -z "${VCPKG_ROOT}" ]; then - echo "Please set the VCPKG_ROOT environment variable to the VCPKG root to build against" + if [ -z "${CMAKE_TOOLCHAIN_FILE}" ]; then + echo "Please set the CMAKE_TOOLCHAIN_FILE environment variable to the CMake toolchain file to build against" exit 1 else - echo "Building against VCPKG: [${VCPKG_ROOT}]" + echo "Building against CMake toolchain file: [${CMAKE_TOOLCHAIN_FILE}]" fi if [ -z "${INSTALL_DIR}" ]; then @@ -36,22 +36,11 @@ function show_usage { printf "\tArguments after '--' are passed to CMake during configuration (e.g. -DCMAKE_C_COMPILER=foo)\n" printf "\n" printf "INSTALL_DIR set to [${INSTALL_DIR}]\n" - printf "VCPKG_ROOT set to [${VCPKG_ROOT}]\n" + printf "CMAKE_TOOLCHAIN_FILE set to [${CMAKE_TOOLCHAIN_FILE}]\n" return 0 } -function compiler_check { - printf "Checking for clang/clang++ in [${VCPKG_ROOT}] [${VCPKG_TARGET_TRIPLET}]:\n" - for c in ${VCPKG_ROOT}/installed/${VCPKG_TARGET_TRIPLET}/tools/llvm/{clang,clang++} - do - ver=$(${c} --version) - printf "Found a clang [${c}]:\n" - printf "${ver}\n" - done - printf "\n" -} - function set_arch { local arch=$(uname -m) case ${arch} in @@ -135,9 +124,6 @@ ARCH=$(set_arch) OS=$(set_os) export VCPKG_TARGET_TRIPLET=${ARCH}-${OS}${VCPKG_SUFFIX} -compiler_check - - echo "Configuring [${BUILD_TYPE}] [${ARCH}] against vcpkg [${VCPKG_TARGET_TRIPLET}]..." if [[ "${@}" != "" ]] then diff --git a/scripts/build.sh b/scripts/build.sh index 16b72108..03badaf0 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -19,7 +19,7 @@ CURR_DIR=$( pwd ) BUILD_DIR="${CURR_DIR}/rellic-build" INSTALL_DIR=/usr/local LLVM_VERSION=llvm-14 -CXX_COMMON_VERSION=v0.2.10 +CXX_COMMON_VERSION=v0.2.16 OS_VERSION=unknown ARCH_VERSION=unknown BUILD_FLAGS= @@ -84,8 +84,8 @@ function GetArchVersion ARCH_VERSION=amd64 return 0 ;; - aarch64) - ARCH_VERSION=aarch64 + arm64 | aarch64) + ARCH_VERSION=arm64 return 0 ;; *) @@ -204,12 +204,19 @@ function DownloadLibraries return 1 fi + VCPKG_TARGET_ARCH="${ARCH_VERSION}" + if [[ "${VCPKG_TARGET_ARCH}" == "amd64" ]]; then + VCPKG_TARGET_ARCH="x64" + fi + if [[ "${OS_VERSION}" == "macos-"* ]]; then # TODO Figure out Xcode compatibility LIBRARY_VERSION="vcpkg_${OS_VERSION}_${LLVM_VERSION}_xcode-${XCODE_VERSION}_${ARCH_VERSION}" + VCPKG_TARGET_TRIPLET="${VCPKG_TARGET_ARCH}-osx-rel" else # TODO Arch version LIBRARY_VERSION="vcpkg_${OS_VERSION}_${LLVM_VERSION}_${ARCH_VERSION}" + VCPKG_TARGET_TRIPLET="${VCPKG_TARGET_ARCH}-linux-rel" fi echo "[-] Library version is ${LIBRARY_VERSION}" @@ -235,7 +242,8 @@ function Configure -G Ninja \ -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \ -DCMAKE_VERBOSE_MAKEFILE=True \ - -DVCPKG_ROOT="${DOWNLOAD_DIR}/${LIBRARY_VERSION}" \ + -DCMAKE_TOOLCHAIN_FILE="${DOWNLOAD_DIR}/${LIBRARY_VERSION}/scripts/buildsystems/vcpkg.cmake" \ + -DVCPKG_TARGET_TRIPLET="${VCPKG_TARGET_TRIPLET}" \ ${BUILD_FLAGS} \ "${SRC_DIR}" ) || exit $?