|
| 1 | +# ============================================================================= |
| 2 | +# Copyright (C) 2016-2019 Blue Brain Project |
| 3 | +# |
| 4 | +# See top-level LICENSE file for details. |
| 5 | +# ============================================================================= |
| 6 | + |
| 7 | +# Macro sets up build rule for .cpp files from .mod files. Parameters: |
| 8 | +# KEY name : An arbitrary name to keep track of output .c files |
| 9 | +# MODFILE input : The path to the mod file |
| 10 | +# OUTTYPE <SERIAL|ISPC> : The output type (optional, defaults to serial) |
| 11 | +# |
| 12 | +# Because nmodl/mod2c_core wants to write the .cpp file in the same directory as the mod file, we copy the |
| 13 | +# mod file to the binary directory first |
| 14 | +# |
| 15 | +# The macro appends the names of the output files to NMODL_${name}_OUTPUTS and the names of the mod |
| 16 | +# files (without directories) to NMODL_${name}_MODS |
| 17 | + |
| 18 | +macro(nmodl_to_cpp_target) |
| 19 | + |
| 20 | + # first parse the arguments |
| 21 | + set(options) |
| 22 | + set(oneValueArgs TARGET MODFILE KEY) |
| 23 | + set(multiValueArgs) |
| 24 | + cmake_parse_arguments(mod2c "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
| 25 | + |
| 26 | + if("${mod2c_MODFILE}" STREQUAL "") |
| 27 | + message(FATAL_ERROR "mod2c_target requires a MODFILE argument") |
| 28 | + endif() |
| 29 | + if("${mod2c_KEY}" STREQUAL "") |
| 30 | + message(FATAL_ERROR "mod2c_target requires a KEY argument") |
| 31 | + endif() |
| 32 | + |
| 33 | + # get name of mod file and it's path |
| 34 | + get_filename_component(mod2c_source_ ${mod2c_MODFILE} ABSOLUTE) |
| 35 | + get_filename_component(mod2c_modname_ ${mod2c_MODFILE} NAME) |
| 36 | + |
| 37 | + # check for nmodl target |
| 38 | + if("${mod2c_TARGET}" STREQUAL "serial") |
| 39 | + string(REGEX |
| 40 | + REPLACE "\\.mod$" |
| 41 | + ".cpp" |
| 42 | + mod2c_outname_ |
| 43 | + "${mod2c_modname_}") |
| 44 | + set(nmodl_modearg "--c") |
| 45 | + elseif("${mod2c_TARGET}" STREQUAL "ispc") |
| 46 | + file(STRINGS ${mod2c_MODFILE} mod2c_mod_artcell REGEX "ARTIFICIAL_CELL") |
| 47 | + if(NOT "${mod2c_mod_artcell}" STREQUAL "") |
| 48 | + string(REGEX |
| 49 | + REPLACE "\\.mod$" |
| 50 | + ".cpp" |
| 51 | + mod2c_outname_ |
| 52 | + "${mod2c_modname_}") |
| 53 | + set(nmodl_modearg "--c") |
| 54 | + else() |
| 55 | + string(REGEX |
| 56 | + REPLACE "\\.mod$" |
| 57 | + ".ispc" |
| 58 | + mod2c_outname_ |
| 59 | + "${mod2c_modname_}") |
| 60 | + string(REGEX |
| 61 | + REPLACE "\\.mod$" |
| 62 | + ".cpp" |
| 63 | + mod2c_wrapper_outname_ |
| 64 | + "${mod2c_modname_}") |
| 65 | + set(nmodl_modearg "--ispc") |
| 66 | + endif() |
| 67 | + elseif("${mod2c_TARGET}" STREQUAL "") |
| 68 | + # default case |
| 69 | + string(REGEX |
| 70 | + REPLACE "\\.mod$" |
| 71 | + ".cpp" |
| 72 | + mod2c_outname_ |
| 73 | + "${mod2c_modname_}") |
| 74 | + set(nmodl_modearg "--c") |
| 75 | + else() |
| 76 | + message( |
| 77 | + SEND_ERROR |
| 78 | + "mod2c_target only supports serial and ispc generation for now: \"${mod2c_TARGET}\"") |
| 79 | + endif() |
| 80 | + |
| 81 | + set(mod2c_output_ "${CMAKE_CURRENT_BINARY_DIR}/${mod2c_outname_}") |
| 82 | + list(APPEND NMODL_${mod2c_KEY}_OUTPUTS "${mod2c_output_}") |
| 83 | + |
| 84 | + if(DEFINED mod2c_wrapper_outname_) |
| 85 | + set(mod2c_wrapper_output_ "${CMAKE_CURRENT_BINARY_DIR}/${mod2c_wrapper_outname_}") |
| 86 | + list(APPEND NMODL_${mod2c_KEY}_OUTPUTS "${mod2c_wrapper_output_}") |
| 87 | + unset(mod2c_wrapper_outname_) |
| 88 | + endif() |
| 89 | + |
| 90 | + list(APPEND NMODL_${mod2c_KEY}_MODS "${mod2c_modname_}") |
| 91 | + if(CORENRN_ENABLE_NMODL AND NMODL_FOUND) |
| 92 | + add_custom_command(OUTPUT ${mod2c_output_} ${mod2c_wrapper_output_} |
| 93 | + DEPENDS ${mod2c_MODFILE} ${CORENRN_NMODL_BINARY} |
| 94 | + COMMAND ${CMAKE_COMMAND} -E copy "${mod2c_source_}" |
| 95 | + "${CMAKE_CURRENT_BINARY_DIR}" |
| 96 | + COMMAND ${CORENRN_NMODL_COMMAND} "${mod2c_modname_}" -o "${CMAKE_CURRENT_BINARY_DIR}" |
| 97 | + host ${nmodl_modearg} passes --inline ${NMODL_EXTRA_FLAGS_LIST} |
| 98 | + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") |
| 99 | + else() |
| 100 | + add_custom_command(OUTPUT ${mod2c_output_} |
| 101 | + DEPENDS ${mod2c_MODFILE} mod2c ${CORENRN_NMODL_BINARY} |
| 102 | + COMMAND ${CMAKE_COMMAND} -E copy "${mod2c_source_}" |
| 103 | + "${CMAKE_CURRENT_BINARY_DIR}" |
| 104 | + COMMAND ${CORENRN_NMODL_COMMAND} "${mod2c_modname_}" |
| 105 | + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") |
| 106 | + endif() |
| 107 | +endmacro() |
0 commit comments