5

I am going to use MIDL compiler with CMAKE but unable to invoke MIDL compiler from CmakeList

That is command which I use to achieve my goal

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/IFace.h ${CMAKE_CURRENT_BINARY_DIR}/GUIDS.c ${CMAKE_CURRENT_BINARY_DIR}/Proxy.c ${CMAKE_CURRENT_BINARY_DIR}/ProxyDll.c
COMMAND midl /h IFace.h /iid GUIDS.c /proxy Proxy.c /dlldata ProxyDll.c ${CMAKE_CURRENT_LIST_DIR}/include/Server.idl
)

When I build my project there are no files produced by MIDL compiler in ${CMAKE_CURRENT_BINARY_DIR}

But with this signature files generates well

add_custom_command(
PRE_BUILD
TARGET ${PROJECT_NAME}
COMMAND midl /h IFace.h /iid GUIDS.c /proxy Proxy.c /dlldata ProxyDll.c ${CMAKE_CURRENT_LIST_DIR}/include/Server.idl
)

What am I doing wrong?

definename
  • 193
  • 1
  • 12
  • By itself, `add_custom_command` is not work; you need to have its *OUTPUT* as *DEPENDS* part of some `add_custom_target` call. `add_custom_command(PRE_BUILD)` is a special form of the command, which is automatically attached to its *TARGET*. – Tsyvarev Oct 22 '17 at 21:35
  • @Tsyvarev Thanks, I unrestood how it works. – definename Oct 23 '17 at 09:26

2 Answers2

8

That is how it works:

set(MIDL_OUTPUT
    ${CMAKE_CURRENT_BINARY_DIR}/IFace.h
    ${CMAKE_CURRENT_BINARY_DIR}/GUIDS.c
    ${CMAKE_CURRENT_BINARY_DIR}/Proxy.c
    ${CMAKE_CURRENT_BINARY_DIR}/ProxyDll.c
    )
set(MIDL_FILE
    ${CMAKE_CURRENT_LIST_DIR}/include/Server.idl
    )
add_custom_command(
    OUTPUT ${MIDL_OUTPUT}
    COMMAND midl /h IFace.h /iid GUIDS.c /proxy Proxy.c /dlldata ProxyDll.c ${MIDL_FILE}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    DEPENDS ${MIDL_FILE}
    VERBATIM
    )
add_custom_target(midl-cmplr
    DEPENDS ${MIDL_OUTPUT}
    )
add_dependencies(${PROJECT_NAME}
    midl-cmplr
    )
set_source_files_properties(
    ${MIDL_OUTPUT}
        PROPERTIES
            GENERATED TRUE
            )
definename
  • 193
  • 1
  • 12
  • It may not be necessary to do `add_custom_target` and `add_dependencies` so long as `${MIDL_OUTPUT}` added to your source files list. Also see: https://cmake.org/pipermail/cmake/2007-June/014802.html – Nathan Moinvaziri Feb 07 '19 at 20:46
  • I find if I don't add ```MAIN_DEPENDENCY ${MIDL_FILE}``` to the add_custom_command it doesn't show up under the idl file's property and doesn't compile when you hit compile on that file – arolson101 Jan 08 '20 at 19:31
3

to improve upon @definename's answer and @nathan-moinvaziri's comment, it's not necessary to do add_custom_target as long as you add the output to your source files list, so the revised answer is:

set(MIDL_OUTPUT
    ${CMAKE_CURRENT_BINARY_DIR}/IFace.h
    ${CMAKE_CURRENT_BINARY_DIR}/GUIDS.c
    ${CMAKE_CURRENT_BINARY_DIR}/Proxy.c
    ${CMAKE_CURRENT_BINARY_DIR}/ProxyDll.c
)
set(MIDL_FILE
    ${CMAKE_CURRENT_LIST_DIR}/BookSku.idl
)
add_custom_command(
    OUTPUT ${MIDL_OUTPUT}
    COMMAND midl /h IFace.h /iid GUIDS.c /proxy Proxy.c /dlldata ProxyDll.c ${MIDL_FILE}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    DEPENDS ${MIDL_FILE}
    MAIN_DEPENDENCY ${MIDL_FILE}
    VERBATIM
)
# add_custom_target(midl-cmplr
#     DEPENDS ${MIDL_OUTPUT}
# )
# add_dependencies(${PROJECT_NAME}
#     midl-cmplr
# )
# set_source_files_properties(${MIDL_OUTPUT} PROPERTIES
#   GENERATED TRUE
# )
target_sources(${PROJECT_NAME} PRIVATE ${MIDL_FILE} ${MIDL_OUTPUT})
arolson101
  • 1,473
  • 15
  • 29