[CMake] CMake with IDL file generation

Andreas Haferburg ahaferburg at scopis.com
Wed Sep 12 11:11:51 EDT 2012


Here's what we're using (found somewhere on the internet, slightly modified):


################################################################################
# MACRO_ADD_INTERFACES(idl_files...)
#
# Syntax: MACRO_ADD_INTERFACES(<output list> <idl1> [<idl2> [...]])
# Notes: <idl1> should be absolute paths so the MIDL compiler can find them.
# For every idl file xyz.idl, two files xyz_h.h and xyz.c are generated, which
# are added to the <output list>

# Copyright (c) 2007, Guilherme Balena Versiani, <[EMAIL PROTECTED]>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
MACRO (MACRO_ADD_INTERFACES _output_list)
  FOREACH(_in_FILE ${ARGN})
    GET_FILENAME_COMPONENT(_out_FILE ${_in_FILE} NAME_WE)
    GET_FILENAME_COMPONENT(_in_PATH ${_in_FILE} PATH)

    SET(_out_header_name ${_out_FILE}_h.h)
    SET(_out_header ${CMAKE_CURRENT_BINARY_DIR}/${_out_header_name})
    SET(_out_iid_name ${_out_FILE}.c)
    SET(_out_iid ${CMAKE_CURRENT_BINARY_DIR}/${_out_iid_name})
    #message("_out_header_name=${_out_header_name}, _out_header=${_out_header}, _out_iid=${_out_iid}")
    ADD_CUSTOM_COMMAND(
      OUTPUT ${_out_header} ${_out_iid}
      DEPENDS ${_in_FILE}
      COMMAND midl /header ${_out_header_name} /iid ${_out_iid_name} ${_in_FILE}
      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    )

    MACRO_ADD_FILE_DEPENDENCIES(
      ${_out_header}
      ${_in_FILE}
    )

    SET_SOURCE_FILES_PROPERTIES(
      ${_out_header}
      ${_out_iid}
      PROPERTIES
      GENERATED TRUE
    )
    SET_SOURCE_FILES_PROPERTIES(${_in_FILE} PROPERTIES HEADER_FILE_ONLY TRUE)

    SET(${_output_list} ${${_output_list}}
      ${_out_header}
      ${_out_iid}
    )

  ENDFOREACH(_in_FILE ${ARGN})

ENDMACRO (MACRO_ADD_INTERFACES)


then in the main script:

# MIDL compiler
MACRO_ADD_INTERFACES(GENERATED_FILES_IDL ${PROJECT_IDL_FILES})

SOURCE_GROUP("IDL" FILES ${GENERATED_FILES_IDL} ${PROJECT_IDL_FILES})

ADD_EXECUTABLE( ${CURRENT_PROJECT}
   ...
   ${PROJECT_IDL_FILES}
   ${GENERATED_FILES_IDL}
)

Hope that helps.

Cheers
Andreas


On 11.09.2012 01:38, Robert Dailey wrote:
> I'm creating a shared library target that needs to compile and include
> source generated by an IDL. I want CMake to create a custom target
> that will invoke MIDL.EXE (comes with Windows SDK) against the IDL
> file to generate the header / source files needed. These header/source
> files will then be built by the shared library target. I saw a similar
> post on this here:
>
> http://www.cmake.org/pipermail/cmake/2011-July/045617.html
>
> This seems complicated and I'm not sure it even works, since from my
> tests, if source files don't exist by the time add_library,
> add_executable, etc is called, generation will fail. I also don't want
> CMake to invoke MIDL for me since that would require us to run CMake
> again every time the IDL file changes (this shouldn't be the case, we
> should just have to rebuild the target again using Visual Studio or
> NMake).
>
> Any ideas on a simple solution to this?
>
> Thanks in advance.
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>


-- 
Scopis GmbH
Blücherstr. 22
10961 Berlin
Germany

E-Mail: ahaferburg at scopis.com
Tel.: +49 (30) 39 82 05 98
Fax.: +49 (30) 39 82 05 99
Internet: www.scopis.com

HRB 128315 Berlin Charlottenburg
USt-IdNr.: DE272721463
Steuernummer: 29/014/02034
Geschäftsführer:  Bartosz Kosmecki

Diese E-mail, einschließlich der Anhänge, ist ausschließlich für den oben genannten Adressaten 
bestimmt und beinhaltet vertrauliche und/oder gesetzlich geschützte Informationen. Jedem anderen 
Empfänger ist die Vervielfältigung, Weitergabe oder Veröffentlichung untersagt. Falls Sie diese 
Mitteilung irrtümlicherweise erhalten haben, bitten wir um sofortige Information an den Absender und 
Vernichtung der E-mail.

This e-mail, including the attachments, is for the exclusive use of the above-named addresses and 
contains confidential information and/or information protected by law. Any other recipient is 
prohibited from duplicating, passing on to third parties, or publishing this information. If by 
error you are the recipient of this communication please inform the sender immediately and 
permanently delete this e-mail.


More information about the CMake mailing list