cmake调用动态宏名称

时间:2016-07-25 15:41:43

标签: cmake

我有两个宏名称,例如:

Dim dbs As DAO.Database
Dim tdf As DAO.TableDef

Set dbs = CurrentDb
For Each tdf In dbs.TableDefs
    With tdf
        If .Connect Like ";DATABASE=*" Then
            .Connect = ";DATABASE=" & strBack & strBackEnd
            .RefreshLink
        End If
    End With
Next

我想基于变量名动态调用宏,例如:

macro(my_macro1)
# stuff only to use in this macro
endmacro()

macro(my_macro2)
# stuff only to use in this macro
endmacro()

任何帮助?

1 个答案:

答案 0 :(得分:2)

正如@Tsyvarev所评论的那样,CMake不支持动态函数名称。所以这里有一些选择:

简单方法

macro(my_macro ver)
    if(${ver} EQUAL 1)
        my_macro1()
    elseif(${ver} EQUAL 2)
        my_macro2()
    else()
        message(FATAL_ERROR "Unsupported macro")
    endif()
endmacro()

set(ver 1)
my_macro(ver)
set(ver 2)
my_macro(ver)

call()功能实施

@Fraser work上构建此处是一个更通用的call()函数实现:

function(call _id)
    if (NOT COMMAND ${_id})
        message(FATAL_ERROR "Unsupported function/macro \"${_id}\"")
    else()
        set(_helper "${CMAKE_BINARY_DIR}/helpers/macro_helper_${_id}.cmake")
        if (NOT EXISTS "${_helper}")
            file(WRITE "${_helper}" "${_id}(\$\{ARGN\})\n")
        endif()
        include("${_helper}")
    endif()
endfunction()

set(ver 1)
call(my_macro${ver})
set(ver 2)
call(my_macro${ver})