From d61c9fed9fcf81a8c2ecf43ca04e4d86acb67ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luca=20Pep=C3=A8=20Sciarria?= Date: Mon, 24 Feb 2025 16:20:50 +0100 Subject: [PATCH] init cmake build system --- cmake/CapitalizeString.cmake | 7 ++ cmake/CheckOutOfSourceBuild.cmake | 21 ++++++ cmake/FindMETIS.cmake | 95 ++++++++++++++++++++++++++ cmake/amg4psblas-VER-SHA256.txt.asc.in | 12 ++++ cmake/amg4psblas-VER-SHA256.txt.in | 3 + cmake/amg4psblasConfig.cmake.in | 11 +++ cmake/makeDist.cmake | 79 +++++++++++++++++++++ cmake/pkg/amg4psblasConfig.cmake.in | 16 +++++ cmake/setVersion.cmake | 90 ++++++++++++++++++++++++ cmake/uninstall.cmake.in | 23 +++++++ 10 files changed, 357 insertions(+) create mode 100644 cmake/CapitalizeString.cmake create mode 100644 cmake/CheckOutOfSourceBuild.cmake create mode 100644 cmake/FindMETIS.cmake create mode 100644 cmake/amg4psblas-VER-SHA256.txt.asc.in create mode 100644 cmake/amg4psblas-VER-SHA256.txt.in create mode 100644 cmake/amg4psblasConfig.cmake.in create mode 100644 cmake/makeDist.cmake create mode 100644 cmake/pkg/amg4psblasConfig.cmake.in create mode 100644 cmake/setVersion.cmake create mode 100644 cmake/uninstall.cmake.in diff --git a/cmake/CapitalizeString.cmake b/cmake/CapitalizeString.cmake new file mode 100644 index 00000000..ae50b474 --- /dev/null +++ b/cmake/CapitalizeString.cmake @@ -0,0 +1,7 @@ +function(CapitalizeString string output_variable) + string(TOUPPER "${string}" _upper_string) + string(TOLOWER "${string}" _lower_string) + string(SUBSTRING "${_upper_string}" 0 1 _start) + string(SUBSTRING "${_lower_string}" 1 -1 _end) + set(${output_variable} "${_start}${_end}" PARENT_SCOPE) +endfunction() diff --git a/cmake/CheckOutOfSourceBuild.cmake b/cmake/CheckOutOfSourceBuild.cmake new file mode 100644 index 00000000..ad176952 --- /dev/null +++ b/cmake/CheckOutOfSourceBuild.cmake @@ -0,0 +1,21 @@ +#-------------------------- +# Prohibit in-source builds +#-------------------------- +if ("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}") + message(FATAL_ERROR "ERROR! " + "CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}" + " == CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}" + "\nThis archive does not support in-source builds:\n" + "You must now delete the CMakeCache.txt file and the CMakeFiles/ directory under " + "the 'src' source directory or you will not be able to configure correctly!" + "\nYou must now run something like:\n" + " $ rm -r CMakeCache.txt CMakeFiles/" + "\n" + "Please create a directory outside the ${CMAKE_PROJECT_NAME} source tree and build under that outside directory " + "in a manner such as\n" + " $ mkdir build\n" + " $ cd build\n" + " $ CC=gcc FC=gfortran cmake -DBUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/path/to/install/dir /path/to/psblas3/src/dir \n" + "\nsubstituting the appropriate syntax for your shell (the above line assumes the bash shell)." + ) +endif() diff --git a/cmake/FindMETIS.cmake b/cmake/FindMETIS.cmake new file mode 100644 index 00000000..21f95f57 --- /dev/null +++ b/cmake/FindMETIS.cmake @@ -0,0 +1,95 @@ +if (METIS_INCLUDES AND METIS_LIBRARIES) + set(METIS_FIND_QUIETLY TRUE) +endif (METIS_INCLUDES AND METIS_LIBRARIES) + +if( DEFINED ENV{METISDIR} ) + if( NOT DEFINED METIS_ROOT ) + set(METIS_ROOT "$ENV{METISDIR}") + endif() +endif() + +if( (DEFINED ENV{METIS_ROOT}) OR (DEFINED METIS_ROOT) ) + if( NOT DEFINED METIS_ROOT) + set(METIS_ROOT "$ENV{METIS_ROOT}") + endif() + set(METIS_HINTS "${METIS_ROOT}") +endif() + +find_path(METIS_INCLUDES + NAMES + metis.h + HINTS + ${METIS_HINTS} + PATHS + "${INCLUDE_INSTALL_DIR}" + /usr/local/opt + /usr/local + PATH_SUFFIXES + include + ) + +if(METIS_INCLUDES) + foreach(include IN_LISTS METIS_INCLUDES) + get_filename_component(mts_include_dir "${include}" DIRECTORY) + get_filename_component(mts_abs_include_dir "${mts_include_dir}" ABSOLUTE) + get_filename_component(new_mts_hint "${include_dir}/.." ABSOLUTE ) + list(APPEND METIS_HINTS "${new_mts_hint}") + break() + endforeach() +endif() + +if(METIS_HINTS) + list(REMOVE_DUPLICATES METIS_HINTS) +endif() + +macro(_metis_check_version) + file(READ "${METIS_INCLUDES}/metis.h" _metis_version_header) + + string(REGEX MATCH "define[ \t]+METIS_VER_MAJOR[ \t]+([0-9]+)" _metis_major_version_match "${_metis_version_header}") + set(METIS_MAJOR_VERSION "${CMAKE_MATCH_1}") + string(REGEX MATCH "define[ \t]+METIS_VER_MINOR[ \t]+([0-9]+)" _metis_minor_version_match "${_metis_version_header}") + set(METIS_MINOR_VERSION "${CMAKE_MATCH_1}") + string(REGEX MATCH "define[ \t]+METIS_VER_SUBMINOR[ \t]+([0-9]+)" _metis_subminor_version_match "${_metis_version_header}") + set(METIS_SUBMINOR_VERSION "${CMAKE_MATCH_1}") + if(NOT METIS_MAJOR_VERSION) + message(STATUS "Could not determine Metis version. Assuming version 4.0.0") + set(METIS_VERSION 4.0.0) + else() + set(METIS_VERSION ${METIS_MAJOR_VERSION}.${METIS_MINOR_VERSION}.${METIS_SUBMINOR_VERSION}) + endif() + if(${METIS_VERSION} VERSION_LESS ${Metis_FIND_VERSION}) + set(METIS_VERSION_OK FALSE) + else() + set(METIS_VERSION_OK TRUE) + endif() + + if(NOT METIS_VERSION_OK) + message(STATUS "Metis version ${METIS_VERSION} found in ${METIS_INCLUDES}, " + "but at least version ${Metis_FIND_VERSION} is required") + endif(NOT METIS_VERSION_OK) +endmacro(_metis_check_version) + +if(METIS_INCLUDES AND Metis_FIND_VERSION) + _metis_check_version() +else() + set(METIS_VERSION_OK TRUE) +endif() + + +find_library(METIS_LIBRARIES metis + HINTS + ${METIS_HINTS} + PATHS + "${LIB_INSTALL_DIR}" + /usr/local/ + /usr/local/opt + PATH_SUFFIXES + lib + lib64 + metis/lib) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(METIS DEFAULT_MSG + METIS_INCLUDES METIS_LIBRARIES METIS_VERSION_OK) + +mark_as_advanced(METIS_INCLUDES METIS_LIBRARIES) diff --git a/cmake/amg4psblas-VER-SHA256.txt.asc.in b/cmake/amg4psblas-VER-SHA256.txt.asc.in new file mode 100644 index 00000000..22cd9211 --- /dev/null +++ b/cmake/amg4psblas-VER-SHA256.txt.asc.in @@ -0,0 +1,12 @@ +Mac users can use GPGTools - https://gpgtools.org +Comment: Download Izaak Beekman's GPG public key from your +Comment: trusted key server or from +Comment: https://izaakbeekman.com/izaak.pubkey.txt +Comment: Next add it to your GPG keyring, e.g., +Comment: `curl https://izaakbeekman.com/izaak.pubkey.txt | gpg --import` +Comment: Make sure you have verified that the release archive's +Comment: SHA256 checksum matches the provided +Comment: psblas-@git_version@-SHA256.txt and ensure that this file +Comment: and it's signature are in the same directory. Then +Comment: verify with: +Comment: `gpg --verify psblas-@git_version@-SHA256.txt.asc` diff --git a/cmake/amg4psblas-VER-SHA256.txt.in b/cmake/amg4psblas-VER-SHA256.txt.in new file mode 100644 index 00000000..1b3888c4 --- /dev/null +++ b/cmake/amg4psblas-VER-SHA256.txt.in @@ -0,0 +1,3 @@ +# To verify cryptographic checksums `shasum -c psblas-@git_version@-SHA256.txt` on Mac OS X, or +# `sha256sum -c psblas-@git_version@-SHA256.txt` on Linux. +@sha256_checksum@ diff --git a/cmake/amg4psblasConfig.cmake.in b/cmake/amg4psblasConfig.cmake.in new file mode 100644 index 00000000..a3c56410 --- /dev/null +++ b/cmake/amg4psblasConfig.cmake.in @@ -0,0 +1,11 @@ +# Config file for the installed package + +set(amg4psblas_VERSION "@amg4psblas_VERSION@") + +# Include directories +set(amg4psblas_INCLUDE_DIRS "@CMAKE_INSTALL_INCLUDEDIR@") + +include(CMakeFindDependencyMacro) + +# Provide the targets +include("${CMAKE_CURRENT_LIST_DIR}/@CMAKE_PROJECT_NAME@Targets.cmake") diff --git a/cmake/makeDist.cmake b/cmake/makeDist.cmake new file mode 100644 index 00000000..82bb4dc2 --- /dev/null +++ b/cmake/makeDist.cmake @@ -0,0 +1,79 @@ +# CMake file to be called in script mode (${CMAKE_COMMAND} -P ) to +# Generate a source archive release asset from add_custom_command +# +# See SourceDistTarget.cmake + +if(NOT CMAKE_ARGV3) + message(FATAL_ERROR "Must pass the top level src dir to ${CMAKE_ARGV2} as the first argument") +endif() + +if(NOT CMAKE_ARGV4) + message(FATAL_ERROR "Must pass the top level src dir to ${CMAKE_ARGV2} as the second argument") +endif() + +find_package(Git) +if(NOT GIT_FOUND) + message( FATAL_ERROR "You can't create a source archive release asset without git!") +endif() + +execute_process(COMMAND "${GIT_EXECUTABLE}" describe --always + RESULT_VARIABLE git_status + OUTPUT_VARIABLE git_version + WORKING_DIRECTORY "${CMAKE_ARGV3}" + OUTPUT_STRIP_TRAILING_WHITESPACE) +if(NOT (git_status STREQUAL "0")) + message( FATAL_ERROR "git describe --always failed with exit status: ${git_status} and message: +${git_version}") +endif() + +set(archive "AMG4PSBLAS-${git_version}") +set(l_archive "AMG4PSBLAS-${git_version}") +set(release_asset "${CMAKE_ARGV4}/${archive}.tar.gz") +execute_process( + COMMAND "${GIT_EXECUTABLE}" archive "--prefix=${archive}/" -o "${release_asset}" "${git_version}" + RESULT_VARIABLE git_status + OUTPUT_VARIABLE git_output + WORKING_DIRECTORY "${CMAKE_ARGV3}" + OUTPUT_STRIP_TRAILING_WHITESPACE) + +if(NOT (git_status STREQUAL "0")) + message( FATAL_ERROR "git archive ... failed with exit status: ${git_status} and message: +${git_output}") +else() + message( STATUS "Source code release asset created from `git archive`: ${release_asset}") +endif() + +file(SHA256 "${release_asset}" tarball_sha256) +set(sha256_checksum "${tarball_sha256} ${archive}.tar.gz") +configure_file("${CMAKE_ARGV3}/cmake/AMG4PSBLAS-VER-SHA256.txt.in" + "${CMAKE_ARGV4}/${l_archive}-SHA256.txt" + @ONLY) +message( STATUS + "SHA 256 checksum of release tarball written out as: ${CMAKE_ARGV4}/${l_archive}-SHA256.txt" ) + +find_program(GPG_EXECUTABLE + gpg + DOC "Location of GnuPG (gpg) executable") + +if(GPG_EXECUTABLE) + execute_process( + COMMAND "${GPG_EXECUTABLE}" --armor --detach-sign --comment "@gpg_comment@" "${CMAKE_ARGV4}/${l_archive}-SHA256.txt" + RESULT_VARIABLE gpg_status + OUTPUT_VARIABLE gpg_output + WORKING_DIRECTORY "${CMAKE_ARGV4}") + if(NOT (gpg_status STREQUAL "0")) + message( WARNING "GPG signing of ${CMAKE_ARGV4}/${l_archive}-SHA256.txt appears to have failed +with status: ${gpg_status} and output: ${gpg_output}") + else() + configure_file("${CMAKE_ARGV3}/cmake/AMG4PSBLAS-VER-SHA256.txt.asc.in" + "${CMAKE_ARGV4}/${l_archive}-GPG.comment" + @ONLY) + file(READ "${CMAKE_ARGV4}/${l_archive}-GPG.comment" gpg_comment) + configure_file("${CMAKE_ARGV4}/${l_archive}-SHA256.txt.asc" + "${CMAKE_ARGV4}/${l_archive}-SHA256.txt.asc.out" + @ONLY) + file(RENAME "${CMAKE_ARGV4}/${l_archive}-SHA256.txt.asc.out" + "${CMAKE_ARGV4}/${l_archive}-SHA256.txt.asc") + message(STATUS "GPG signed SHA256 checksum created: ${CMAKE_ARGV4}/${l_archive}-SHA256.txt.asc") + endif() +endif() diff --git a/cmake/pkg/amg4psblasConfig.cmake.in b/cmake/pkg/amg4psblasConfig.cmake.in new file mode 100644 index 00000000..3fe60345 --- /dev/null +++ b/cmake/pkg/amg4psblasConfig.cmake.in @@ -0,0 +1,16 @@ +# Config file for the INSTALLED package +# Allow other CMake projects to find this package if it is installed +# Requires the use of the standard CMake module CMakePackageConfigHelpers + +set ( @CMAKE_PROJECT_NAME@_VERSION @VERSION@ ) + +###@COMPILER_CONSISTENCY_CHECK@ + +@PACKAGE_INIT@ + +# Provide the targets +set_and_check ( @PACKAGE_NAME@_CONFIG_INSTALL_DIR "@PACKAGE_EXPORT_INSTALL_DIR@" ) +include ( "${@PACKAGE_NAME@_CONFIG_INSTALL_DIR}/@PACKAGE_NAME@-targets.cmake" ) + +# Make the module files available via include +set_and_check ( @CMAKE_PROJECT_NAME@_INCLUDE_DIRS "@PACKAGE_INSTALL_MOD_DIR@" ) diff --git a/cmake/setVersion.cmake b/cmake/setVersion.cmake new file mode 100644 index 00000000..f79ee619 --- /dev/null +++ b/cmake/setVersion.cmake @@ -0,0 +1,90 @@ +include(CMakeParseArguments) + +# Function to parse version info from git and/or .VERSION file +function(set_version) + set(options "") + set(oneValueArgs VERSION_VARIABLE GIT_DESCRIBE_VAR CUSTOM_VERSION_FILE CUSTOM_VERSION_REGEX ) + set(multiValueArgs "") + cmake_parse_arguments(set_version "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + # Algorithm: + # 1. Get first line of .VERSION file, which will be set via `git archive` so long as + # + # 2. If not a packaged release check if this is an active git repo + # 3. Get version info from `git describe` + # 4. First the most recent tag is fetched if available + # 5. Then the full `git describe` output is fetched + + + if(NOT set_version_CUSTOM_VERSION_REGEX) + set(_VERSION_REGEX "[vV]*[0-9]+\\.[0-9]+\\.[0-9]+") + else() + set(_VERSION_REGEX ${set_version_CUSTOM_VERSION_REGEX}) + endif() + if(NOT set_version_CUSTOM_VERSION_FILE) + set(_VERSION_FILE "${CMAKE_SOURCE_DIR}/.VERSION") + else() + set(_VERSION_FILE "${set_version_CUSTOM_VERSION_FILE}") + endif() + + file(STRINGS "${_VERSION_FILE}" first_line + LIMIT_COUNT 1 + ) + + string(REGEX MATCH ${_VERSION_REGEX} + _package_version "${first_line}") + + if((NOT (_package_version MATCHES ${_VERSION_REGEX})) AND (EXISTS "${CMAKE_SOURCE_DIR}/.git")) + message( STATUS "Build from git repository detected") + find_package(Git) + if(GIT_FOUND) + set(GIT_FOUND "${GIT_FOUND}" PARENT_SCOPE) + execute_process(COMMAND "${GIT_EXECUTABLE}" describe --abbrev=0 + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + RESULT_VARIABLE _git_status + OUTPUT_VARIABLE _git_output + OUTPUT_STRIP_TRAILING_WHITESPACE) + if((_git_status STREQUAL "0") AND (_git_output MATCHES ${_VERSION_REGEX})) + set(_package_version "${_git_output}") + endif() + execute_process(COMMAND "${GIT_EXECUTABLE}" describe --always + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + RESULT_VARIABLE _git_status + OUTPUT_VARIABLE _full_git_describe + OUTPUT_STRIP_TRAILING_WHITESPACE) + if(NOT (_git_status STREQUAL "0")) + set(_full_git_describe NOTFOUND) + endif() + else() + message( WARNING "Could not find git executable!") + endif() + endif() + + if(NOT (_package_version MATCHES ${_VERSION_REGEX})) + message( WARNING "Could not extract version from git, falling back on ${_VERSION_FILE}.") + file(STRINGS ".VERSION" _package_version + REGEX ${_VERSION_REGEX} + ) + endif() + + if(NOT _full_git_describe) + set(_full_git_describe ${_package_version}) + endif() + + # Strip leading "v" character from package version tags so that + # the version string can be passed to the CMake `project` command + string(REPLACE "v" "" _package_version "${_package_version}") + string(REPLACE "V" "" _package_version "${_package_version}") + + if(set_version_VERSION_VARIABLE) + set(${set_version_VERSION_VARIABLE} ${_package_version} PARENT_SCOPE) + else() + set(PROJECT_VERSION ${_package_version} PARENT_SCOPE) + endif() + if(set_version_GIT_DESCRIBE_VAR) + set(${set_version_GIT_DESCRIBE_VAR} ${_full_git_describe} PARENT_SCOPE) + else() + set(FULL_GIT_DESCRIBE ${_full_git_describe} PARENT_SCOPE) + endif() + +endfunction() diff --git a/cmake/uninstall.cmake.in b/cmake/uninstall.cmake.in new file mode 100644 index 00000000..dd395930 --- /dev/null +++ b/cmake/uninstall.cmake.in @@ -0,0 +1,23 @@ +# Adapted from http://www.cmake.org/Wiki/CMake_FAQ#Can_I_do_.22make_uninstall.22_with_CMake.3F May 1, 2014 + +if(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt") + message(FATAL_ERROR "Cannot find install manifest: @CMAKE_BINARY_DIR@/install_manifest.txt") +endif(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt") + +file(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files) +string(REGEX REPLACE "\n" ";" files "${files}") +foreach(file ${files}) + message(STATUS "Uninstalling $ENV{DESTDIR}${file}") + if(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}") + exec_program( + "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" + OUTPUT_VARIABLE rm_out + RETURN_VALUE rm_retval + ) + if(NOT "${rm_retval}" STREQUAL 0) + message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}") + endif(NOT "${rm_retval}" STREQUAL 0) + else(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}") + message(STATUS "File $ENV{DESTDIR}${file} does not exist.") + endif(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}") +endforeach(file)