RulesPrecisions.cmake 9.12 KB
Newer Older
1 2 3 4 5
###
#
# @copyright (c) 2009-2014 The University of Tennessee and The University
#                          of Tennessee Research Foundation.
#                          All rights reserved.
6
# @copyright (c) 2012-2016 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
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
#
###
#
#  @file RulesPrecisions.cmake
#
#  @project MORSE
#  MORSE is a software package provided by:
#     Inria Bordeaux - Sud-Ouest,
#     Univ. of Tennessee,
#     King Abdullah Univesity of Science and Technology
#     Univ. of California Berkeley,
#     Univ. of Colorado Denver.
#
#  @version 0.9.0
#  @author Thomas Herault
#  @author George Bosilca
#  @author Aurelien Bouteiller
#  @author Mathieu Faverge
#  @author Florent Puvost
#  @date 13-07-2012
#
###
cmake_minimum_required(VERSION 2.8)
include(CMakeDependentOption)

set(RP_GENDEPENDENCIES ${MORSE_CMAKE_MODULE_PATH}/precision_generator/genDependencies.py)
set(RP_CODEGEN         ${MORSE_CMAKE_MODULE_PATH}/precision_generator/codegen.py)

# Dictionnary
# -----------
if( NOT DEFINED RP_${CMAKE_PROJECT_NAME}_DICTIONNARY )
  message( WARNING "RulesPrecisions included before RP_${CMAKE_PROJECT_NAME}_DICTIONNARY was defined (Default is used)" )
  set(RP_${CMAKE_PROJECT_NAME}_DICTIONNARY ${MORSE_CMAKE_MODULE_PATH}/precision_generator/subs.py
    CACHE INTERNAL "Dictionnary used for precision generation" )
else()
  set(RP_${CMAKE_PROJECT_NAME}_DICTIONNARY ${RP_${CMAKE_PROJECT_NAME}_DICTIONNARY}
    CACHE INTERNAL "Dictionnary used for precision generation" )
endif()

# Default Precisions
# ------------------
if( NOT DEFINED RP_${CMAKE_PROJECT_NAME}_PRECISIONS )
  message( WARNING "RulesPrecisions included before RP_${CMAKE_PROJECT_NAME}_PRECISIONS was defined (\"s;d;c;z\" is used)" )
  set(RP_${CMAKE_PROJECT_NAME}_PRECISIONS "s;d;c;z"
    CACHE INTERNAL "Set of available precisions for the project" )
else()
  set(RP_${CMAKE_PROJECT_NAME}_PRECISIONS ${RP_${CMAKE_PROJECT_NAME}_PRECISIONS}
    CACHE INTERNAL "Set of available precisions for the project" )
endif()

# Detect if compilation is done in or out of place
# ------------------------------------------------
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" __inplace)
set( ${CMAKE_PROJECT_NAME}_COMPILE_INPLACE ${__inplace}
  CACHE INTERNAL "Defines if the project compilation is made inplace or not" )

# Detect default settings
# -----------------------
set( _prec_S  OFF )
set( _prec_D  OFF )
set( _prec_DS OFF )
set( _prec_C  OFF )
set( _prec_Z  OFF )
set( _prec_ZC OFF )
foreach(_prec ${RP_${CMAKE_PROJECT_NAME}_PRECISIONS})
PRUVOST Florent's avatar
PRUVOST Florent committed
72
  if ( ${_prec} STREQUAL zc )
73 74 75 76 77
    set( _prec_S  ON )
    set( _prec_D  ON )
    set( _prec_C  ON )
    set( _prec_Z  ON )
    set( _prec_ZC ON )
PRUVOST Florent's avatar
PRUVOST Florent committed
78
  elseif( ${_prec} STREQUAL z )
79 80
    set( _prec_D  ON )
    set( _prec_Z  ON )
PRUVOST Florent's avatar
PRUVOST Florent committed
81
  elseif( ${_prec} STREQUAL c )
82 83
    set( _prec_S  ON )
    set( _prec_C  ON )
PRUVOST Florent's avatar
PRUVOST Florent committed
84
  elseif( ${_prec} STREQUAL ds )
85 86 87
    set( _prec_S  ON )
    set( _prec_D  ON )
    set( _prec_DS ON )
PRUVOST Florent's avatar
PRUVOST Florent committed
88
  elseif( ${_prec} STREQUAL d )
89
    set( _prec_S  ON )
PRUVOST Florent's avatar
PRUVOST Florent committed
90
  elseif( ${_prec} STREQUAL s )
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    set( _prec_S  ON )
  endif()
endforeach()

# set computed precisions
# -----------------------
if( ${_prec_S} )
  option(${PROJECT_NAME}_PREC_S
    "Build ${PROJECT_NAME} real single precision" ON)
endif()
if( ${_prec_D} )
  option(${PROJECT_NAME}_PREC_D
    "Build ${PROJECT_NAME} real double precision" ON)
endif()

if( ${_prec_C} )
  cmake_dependent_option(${PROJECT_NAME}_PREC_C
    "Build ${PROJECT_NAME} complex single precision" ON "${PROJECT_NAME}_PREC_S" OFF)
endif()
if( ${_prec_Z} )
  cmake_dependent_option(${PROJECT_NAME}_PREC_Z
    "Build ${PROJECT_NAME} complex double precision" ON "${PROJECT_NAME}_PREC_D" OFF)
endif()

if( ${_prec_DS} )
  cmake_dependent_option(${PROJECT_NAME}_PREC_DS
    "Build ${PROJECT_NAME} real mixed precision"    ON "${PROJECT_NAME}_PREC_S;${PROJECT_NAME}_PREC_D" OFF)
endif()
if( ${_prec_ZC} )
  cmake_dependent_option(${PROJECT_NAME}_PREC_ZC
    "Build ${PROJECT_NAME} complex mixed precision" ON "${PROJECT_NAME}_PREC_C;${PROJECT_NAME}_PREC_Z" OFF)
endif()

# Define precision supported by the project
# -----------------------------------------
set( ${CMAKE_PROJECT_NAME}_PRECISION "" )
if(${CMAKE_PROJECT_NAME}_PREC_S)
  list(APPEND ${CMAKE_PROJECT_NAME}_PRECISION "s")
endif()

if(${CMAKE_PROJECT_NAME}_PREC_D)
  list(APPEND ${CMAKE_PROJECT_NAME}_PRECISION "d")
endif()

if(${CMAKE_PROJECT_NAME}_PREC_DS)
  list(APPEND ${CMAKE_PROJECT_NAME}_PRECISION "ds")
endif()

if(${CMAKE_PROJECT_NAME}_PREC_C)
  list(APPEND ${CMAKE_PROJECT_NAME}_PRECISION "c")
endif()

if(${CMAKE_PROJECT_NAME}_PREC_Z)
  list(APPEND ${CMAKE_PROJECT_NAME}_PRECISION "z")
endif()

if(${CMAKE_PROJECT_NAME}_PREC_ZC)
  list(APPEND ${CMAKE_PROJECT_NAME}_PRECISION "zc")
endif()

#
# Generates a rule for every SOURCES file, to create the precisions in PRECISIONS. If TARGETDIR
# is not empty then all generated files will be prepended with the $TARGETDIR/.
# A new file is created, from a copy by default
# If the first precision is "/", all occurences of the basename in the file are remplaced by
# "pbasename" where p is the selected precision.
# the target receives a -DPRECISION_p in its cflags.
#
include(ParseArguments)
160 161 162 163

# Add a hint to help Cmake to find the correct python version:
# (see https://cmake.org/cmake/help/v3.0/module/FindPythonInterp.html)
set(Python_ADDITIONAL_VERSIONS 2)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
find_package(PythonInterp REQUIRED)

MACRO(precisions_rules_py)
  PARSE_ARGUMENTS(PREC_RULE "TARGETDIR;PRECISIONS" "" ${ARGN})

  message(STATUS "Generate precision dependencies in ${CMAKE_CURRENT_SOURCE_DIR}")

  # The first is the output variable list
  CAR(OUTPUTLIST ${PREC_RULE_DEFAULT_ARGS})
  # Everything else should be source files.
  CDR(SOURCES ${PREC_RULE_DEFAULT_ARGS})

  if( NOT DEFINED ${CMAKE_PROJECT_NAME}_COMPILE_INPLACE )
    message( FATAL_ERROR "precisions_rules_init must be called before any call to precisions_rules_py" )
  endif()

  if( NOT DEFINED RP_${CMAKE_PROJECT_NAME}_DICTIONNARY )
    message( FATAL_ERROR "precisions_rules_init must be called before any call to precisions_rules_py" )
  endif()

  # By default the TARGETDIR is the current binary directory
  if( "${PREC_RULE_TARGETDIR}" STREQUAL "" )
    set(PREC_RULE_TARGETDIR "./")
    set(PRECISIONPP_prefix "./")
    set(PRECISIONPP_arg "-P")
  else( "${PREC_RULE_TARGETDIR}" STREQUAL "" )
    if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${PREC_RULE_TARGETDIR})
    else(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${PREC_RULE_TARGETDIR})
      file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${PREC_RULE_TARGETDIR})
    endif(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${PREC_RULE_TARGETDIR})
    set(PRECISIONPP_arg "-P")
    set(PRECISIONPP_prefix "${PREC_RULE_TARGETDIR}")
  endif( "${PREC_RULE_TARGETDIR}" STREQUAL "" )

  set(options_list "")
  foreach(prec_rules_PREC ${PREC_RULE_PRECISIONS})
    set(options_list "${options_list} ${prec_rules_PREC}")
  endforeach()

  set(sources_list "")
  foreach(_src ${SOURCES})
    set(sources_list "${sources_list} ${_src}")
  endforeach()

  set(gencmd ${PYTHON_EXECUTABLE} ${RP_GENDEPENDENCIES} -f "${sources_list}" -p "${options_list}" -s "${CMAKE_CURRENT_SOURCE_DIR}" ${PRECISIONPP_arg} ${PRECISIONPP_prefix})
  EXECUTE_PROCESS(COMMAND ${gencmd} OUTPUT_VARIABLE dependencies_list)

  foreach(_dependency ${dependencies_list})

    string(STRIP "${_dependency}" _dependency)
    string(COMPARE NOTEQUAL "${_dependency}" "" not_empty)
    if( not_empty )

      string(REGEX REPLACE "^(.*),(.*),(.*)$" "\\1" _dependency_INPUT "${_dependency}")
      set(_dependency_PREC   "${CMAKE_MATCH_2}")
      set(_dependency_OUTPUT "${CMAKE_MATCH_3}")

      set(pythoncmd ${PYTHON_EXECUTABLE} ${RP_CODEGEN} -f ${CMAKE_CURRENT_SOURCE_DIR}/${_dependency_INPUT} -p ${_dependency_PREC} ${PRECISIONPP_arg} ${PRECISIONPP_prefix})

      string(STRIP "${_dependency_OUTPUT}" _dependency_OUTPUT)
      string(COMPARE NOTEQUAL "${_dependency_OUTPUT}" "" got_file)

      # Force the copy of the original files in the binary_dir
      # for VPATH compilation
      if( NOT ${CMAKE_PROJECT_NAME}_COMPILE_INPLACE )
PRUVOST Florent's avatar
PRUVOST Florent committed
229
	set(generate_out 1)
230
      else( NOT ${CMAKE_PROJECT_NAME}_COMPILE_INPLACE )
PRUVOST Florent's avatar
PRUVOST Florent committed
231
	string(COMPARE NOTEQUAL "${_dependency_OUTPUT}" "${_dependency_INPUT}" generate_out )
232 233 234 235
      endif()

      # We generate a dependency only if a file will be generated
      if( got_file )
PRUVOST Florent's avatar
PRUVOST Florent committed
236 237 238 239 240 241
	if( generate_out )
	  # the custom command is executed in CMAKE_CURRENT_BINARY_DIR
	  ADD_CUSTOM_COMMAND(
	    OUTPUT ${_dependency_OUTPUT}
	    COMMAND ${CMAKE_COMMAND} -E remove -f ${_dependency_OUTPUT} && ${pythoncmd} && chmod a-w ${_dependency_OUTPUT}
	    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_dependency_INPUT} ${RP_CODEGEN} ${RP_${CMAKE_PROJECT_NAME}_DICTIONNARY})
242

243
          set_source_files_properties(${_dependency_OUTPUT} PROPERTIES COMPILE_FLAGS "-DPRECISION_${_dependency_PREC}" GENERATED 1 IS_IN_BINARY_DIR 1 )
PRUVOST Florent's avatar
PRUVOST Florent committed
244
	else( generate_out )
245
          set_source_files_properties(${_dependency_OUTPUT} PROPERTIES COMPILE_FLAGS "-DPRECISION_${_dependency_PREC}" GENERATED 0 )
PRUVOST Florent's avatar
PRUVOST Florent committed
246
	endif( generate_out )
247

PRUVOST Florent's avatar
PRUVOST Florent committed
248
	list(APPEND ${OUTPUTLIST} ${_dependency_OUTPUT})
249 250 251 252 253 254 255 256 257 258 259
      endif( got_file )
    endif()
  endforeach()

  message(STATUS "Generate precision dependencies in ${CMAKE_CURRENT_SOURCE_DIR} - Done")

ENDMACRO(precisions_rules_py)

##
## @end file RulesPrecisions.cmake
##