File: FindGMP.cmake

package info (click to toggle)
macaulay2 1.24.11%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 171,648 kB
  • sloc: cpp: 107,850; ansic: 16,307; javascript: 4,188; makefile: 3,947; lisp: 682; yacc: 604; sh: 476; xml: 177; perl: 114; lex: 65; python: 33
file content (126 lines) | stat: -rw-r--r-- 4,699 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Try to find the GNU Multiple Precision Arithmetic Library (GMP)
# See http://gmplib.org/
#
# This module supports requiring a minimum version, e.g. you can do
#   find_package(GMP 6.1.0)
# to require version 6.1.0 to newer of GMP.
#
# Once done this will define
#
#  GMP_FOUND             - system has GMP lib
#  GMP_ROOT              - the GMP install prefix
#  GMP_INCLUDE_DIRS      - the GMP include directory
#  GMP_LIBRARY_DIRS      - the GMP library directory
#  GMP_LIBRARIES         - Libraries needed to use GMP
#  GMP_VERSION           - GMP version

# Copyright (c) 2016 Jack Poulson, <jack.poulson@gmail.com>
# Copyright (c) 2020, Mahrud Sayrafi, <mahrud@umn.edu>
# Redistribution and use is allowed according to the terms of the BSD license.

# Set GMP_FIND_VERSION to 1.0.0 if no minimum version is specified
if(NOT GMP_FIND_VERSION)
  if(NOT GMP_FIND_VERSION_MAJOR)
    set(GMP_FIND_VERSION_MAJOR 5)
  endif()
  if(NOT GMP_FIND_VERSION_MINOR)
    set(GMP_FIND_VERSION_MINOR 1)
  endif()
  if(NOT GMP_FIND_VERSION_PATCH)
    set(GMP_FIND_VERSION_PATCH 0)
  endif()
  set(GMP_FIND_VERSION
    "${GMP_FIND_VERSION_MAJOR}.${GMP_FIND_VERSION_MINOR}.${GMP_FIND_VERSION_PATCH}")
endif()

macro(_gmp_check_version)
  # Since the GMP version macros may be in a file included by gmp.h of the form
  # gmp-.*[_]?.*.h (e.g., gmp-x86_64.h), we search each of them.
  file(GLOB GMP_HEADERS "${GMP_INCLUDE_DIRS}/gmp.h" "${GMP_INCLUDE_DIRS}/gmp-*.h")
  foreach(gmp_header_filename ${GMP_HEADERS})
    file(READ "${gmp_header_filename}" _gmp_version_header)
    string(REGEX MATCH
      "define[ \t]+__GNU_MP_VERSION[ \t]+([0-9]+)" _gmp_major_version_match
      "${_gmp_version_header}")
    if(_gmp_major_version_match)
      set(GMP_MAJOR_VERSION "${CMAKE_MATCH_1}")
      string(REGEX MATCH "define[ \t]+__GNU_MP_VERSION_MINOR[ \t]+([0-9]+)"
        _gmp_minor_version_match "${_gmp_version_header}")
      set(GMP_MINOR_VERSION "${CMAKE_MATCH_1}")
      string(REGEX MATCH "define[ \t]+__GNU_MP_VERSION_PATCHLEVEL[ \t]+([0-9]+)"
        _gmp_patchlevel_version_match "${_gmp_version_header}")
      set(GMP_PATCHLEVEL_VERSION "${CMAKE_MATCH_1}")
      set(GMP_VERSION
        ${GMP_MAJOR_VERSION}.${GMP_MINOR_VERSION}.${GMP_PATCHLEVEL_VERSION})
    endif()
  endforeach()
  #If a direct search fails, use try_run to read out gmp_version.
  #This is for systems that put multiarch headers in other places.
  if(NOT GMP_VERSION)
    file(WRITE ${CMAKE_BINARY_DIR}/gmp_version.c [[
      #include <stdio.h>
      #include "gmp.h"
      int main(){ printf("%s",gmp_version); return 0;}
      ]])
    try_run(GMP_TEST_SUCCESS GMP_COMPILE_SUCCESS
      ${CMAKE_BINARY_DIR} ${CMAKE_BINARY_DIR}/gmp_version.c
      LINK_LIBRARIES gmp
      RUN_OUTPUT_VARIABLE GMP_VERSION)
  endif()

  # Check whether found version exists and exceeds the minimum requirement
  if(NOT GMP_VERSION)
    set(GMP_VERSION_OK FALSE)
    message(STATUS "GMP version was not detected")
  elseif(${GMP_VERSION} VERSION_LESS ${GMP_FIND_VERSION})
    set(GMP_VERSION_OK FALSE)
    message(STATUS "GMP version ${GMP_VERSION} found in ${GMP_INCLUDE_DIRS}, "
                   "but at least version ${GMP_FIND_VERSION} is required")
  else()
    set(GMP_VERSION_OK TRUE)
  endif()
endmacro(_gmp_check_version)

if(NOT GMP_VERSION_OK)
  set(GMP_INCLUDE_DIRS NOTFOUND)
  set(GMP_LIBRARY_DIRS NOTFOUND)
  set(GMP_LIBRARIES NOTFOUND)
  set(GMPXX_LIBRARIES NOTFOUND)

  # search first if an GMPConfig.cmake is available in the system,
  # if successful this would set GMP_INCLUDE_DIRS and the rest of
  # the script will work as usual
  find_package(GMP ${GMP_FIND_VERSION} NO_MODULE QUIET)

  if(NOT GMP_INCLUDE_DIRS)
    find_path(GMP_INCLUDE_DIRS NAMES gmp.h
      HINTS ENV GMPDIR ENV GMPDIR
      PATHS ${INCLUDE_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX}/include)
  endif()

  if(GMP_INCLUDE_DIRS)
    _GMP_check_version()
  endif()

  if(NOT GMP_LIBRARIES)
    find_library(GMP_LIBRARIES NAMES gmp
      HINTS ENV GMPDIR ENV GMPDIR
      PATHS ${LIB_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX}/lib)
    find_library(GMPXX_LIBRARIES NAMES gmpxx
      HINTS ENV GMPDIR ENV GMPDIR
      PATHS ${LIB_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX}/lib)
    set(GMP_LIBRARIES ${GMPXX_LIBRARIES} ${GMP_LIBRARIES})
  endif()

  if(GMPXX_LIBRARIES)
    get_filename_component(GMP_LIBRARY_DIRS "${GMPXX_LIBRARIES}" DIRECTORY)
  endif()

  string(REGEX REPLACE "/include.*" "" GMP_ROOT "${GMP_INCLUDE_DIRS}")

  include(FindPackageHandleStandardArgs)
  find_package_handle_standard_args(GMP DEFAULT_MSG GMP_ROOT GMP_INCLUDE_DIRS GMP_LIBRARIES GMP_LIBRARY_DIRS GMP_VERSION_OK)

  mark_as_advanced(GMP_ROOT GMP_INCLUDE_DIRS GMP_LIBRARIES GMP_LIBRARY_DIRS)

endif()