[CMake] try_compile() to determine the largest available Fortran integer

Zaak Beekman zbeekman at gmail.com
Wed Oct 2 15:04:11 EDT 2013


Hi,
I managed to write a macro to determine the largest available Fortran
integer kind so that it can be #defined at compile time where needed--via
compiler supplied preprocessors  or the cpp. The macro I wrote needs some
more refinement/abstraction, but the basic technique is there. I am sharing
it here in the event that someone might find it useful. Others' suggestions
proved very helpful in implementing this, particularly Yngve Inntjore
Levinsen's suggestions. My solution relies on more guarantees  by the
standard, and I feel it is more portable, so long as one has an F95
conforming compiler. (Virtually all compilers released in the past decade
are F95 conforming.) Here is my macro:

macro(Fortran_biggest_int)
  message(STATUS "Checking for the largest available Fortran integer..")
  set(filename ${CMAKE_BINARY_DIR}/getbigint.f90)
  file(WRITE  ${filename} "program getbigint\n")
  file(APPEND ${filename} "  implicit none\n")
  file(APPEND ${filename} "  integer ,parameter :: DEFAULT = kind(1) ,
DEF_RANGE = range(kind(1))\n")
  file(APPEND ${filename} "  integer :: r, ktmp, k\n")
  file(APPEND ${filename} "  r = DEF_RANGE\n")
  file(APPEND ${filename} "  k = DEFAULT\n")
  file(APPEND ${filename} "  do\n")
  file(APPEND ${filename} "     r    = r + 1\n")
  file(APPEND ${filename} "     ktmp = selected_int_kind(r)\n")
  file(APPEND ${filename} "     if ( ktmp == -1 ) exit\n")
  file(APPEND ${filename} "     k = ktmp\n")
  file(APPEND ${filename} "  end do\n")
  file(APPEND ${filename} "  write(*,'(i0)') k\n")
  file(APPEND ${filename} "end program\n")
  try_run(BIG_INT_COMPILES BIG_INT_RUNS
    ${CMAKE_BINARY_DIR} ${filename}
    RUN_OUTPUT_VARIABLE FBIGINT)
  message(STATUS "Fortran big integer kind: ${FBIGINT}")
endmacro()


Izaak Beekman
===================================
(301)244-9367
Princeton University Doctoral Candidate
Mechanical and Aerospace Engineering
ibeekman at princeton.edu

UMD-CP Visiting Graduate Student
Aerospace Engineering
ibeekman at umiacs.umd.edu
ibeekman at umd.edu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20131002/9e13fa36/attachment.htm>


More information about the CMake mailing list