[CMake] Custom target failing to build on Mac

Mike Jackson imikejackson at gmail.com
Sat Jun 7 13:08:53 EDT 2008


You may want to take a look at the CONFIGURE_FILE command and use  
that instead.

An example would be:
SET (MyVersion "1.0.4.5.6")
     CONFIGURE_FILE(Version.h.in
                Version.h @ONLY IMMEDIATE)

SET (Generated_Sources ${PROJECT_BINARY_DIR}/Version.h )
ADD_EXECUTABLE (MyExe ${Your_Sources} ${Generated_Sources})


So in Version.h.in you would have the following (among other things):

#define VERSION "@MyVersion@"

Also, you would probably need to add ${PROJECT_BINARY_DIR}/Version.h  
as a source file for each executable.

and you would get a file called Version.h in your build directory.  
Just remember to add your build directory to the include paths.

This works on all the platforms (Linux. OS X, Windows XP) that I have  
tried.
-- 
Mike Jackson   Senior Research Engineer
Innovative Management & Technology Services


On Jun 7, 2008, at 10:39 AM, Matthew Smith wrote:

> Hi everyone,
>
> I am trying to build a cross-platform Qt application, and use a header
> file, generated at build time, to produce a version number definition
> which can be included by any other source file, so I can define the
> version number once in the CMakeLists.txt file and have it reproduced
> anywhere else that it's needed.  The problem is that it is failing to
> generate on the Mac, which results in a compilation failure for the
> first source file that includes it.  This does not happen on  
> Windows or
> Linux (using CMake 2.6 on all platforms).
>
> I have attached the CMakeLists.txt file; the bit where the source file
> is built starts at line 158.  Looking at the CMakeFiles directory, a
> target directory (VERSION_HEADER.dir) has been produced, but the  
> target
> is not generated before the actual compilation starts; on other  
> targets,
> it is generated before "scanning dependencies" for the main executable
> target.  Can anyone help me with this?
>
> Regards,
>
> Matt Smith
>
> -- 
>
> http://qtm.blogistan.co.uk/
> project(QTM)
>
> set( QTM_VERSION 0.6.3 )
>
> IF(APPLE)
>     set( PROGNAME QTM )
>     set( MACOSX_BUNDLE_SHORT_VERSION_STRING ${QTM_VERSION} )
>     set( MACOSX_BUNDLE_VERSION ${QTM_VERSION} )
>     set( MACOSX_BUNDLE_LONG_VERSION_STRING ${QTM_VERSION} )
>     set( MACOSX_BUNDLE_ICON_FILE QTM.icns )
>     IF( MAC_UNIVERSAL )
>         set( CMAKE_OSX_ARCHITECTURES ppc;i386 ) #Comment out if not  
> universal binary
>     ENDIF( MAC_UNIVERSAL )
> ELSE(NOT APPLE)
>     set( PROGNAME qtm )
> ENDIF(APPLE)
>
> if( USE_DBUS )
>   set( USE_STI TRUE )
>   set( DBUSADAPTOR_CC DBusAdaptor.cc )
>   set( DBUSADAPTOR_H DBusAdaptor.h )
> else( USE_DBUS )
>   add_definitions( -DDONT_USE_DBUS )
> endif( USE_DBUS )
>
> if( USE_STI )
>   add_definitions( -DUSE_SYSTRAYICON )
>   set( SYSTRAYICON_H SysTrayIcon.h )
>   set( SYSTRAYICON_CC SysTrayIcon.cc )
>   set( QUICKPOSTTEMPLATEFORM_UI QuickpostTemplateForm.ui )
>   set( QUICKPOSTTEMPLATEDIALOG_H QuickpostTemplateDialog.h )
>   set( QUICKPOSTTEMPLATEDIALOG_CC QuickpostTemplateDialog.cc )
>   set( QUICKPOSTTEMPLATE_H QuickpostTemplate.h )
>   set( QUICKPOSTTEMPLATE_CC QuickpostTemplate.cc )
>   if( APPLE )
>     set( SUPERMENU_CC SuperMenu.cc )
>     set( SUPERMENU_H SuperMenu.h )
>     add_definitions( -DSUPERMENU )
>   endif( APPLE )
> endif( USE_STI )
>
> if( UNIX AND NOT APPLE )
>
> #Generate desktop entry file
>
>   if( NOT DESKTOP_ENTRY )
>     set( DESKTOP_ENTRY qtm.desktop )
>   endif( NOT DESKTOP_ENTRY )
>
>   add_custom_command( OUTPUT ${DESKTOP_ENTRY}
>     COMMAND touch ${QTM_BINARY_DIR}/${DESKTOP_ENTRY}
>     COMMAND sh ${QTM_SOURCE_DIR}/qtm-desktop.sh $ 
> {CMAKE_INSTALL_PREFIX} >${QTM_BINARY_DIR}/${DESKTOP_ENTRY}
>     DEPENDS qtm-desktop.sh
>     COMMENT "Generating desktop entry file"
>     )
>   add_custom_target( DESKTOP_ENTRY_FILE ALL
>     DEPENDS ${DESKTOP_ENTRY}
>     )
>
> #Generate manpage
>
>   if( NOT MANPAGE_DIRECTORY )
>     set( MANPAGE_DIRECTORY ${CMAKE_INSTALL_PREFIX}/share/man/man1 )
>   endif( NOT MANPAGE_DIRECTORY)
>
>   add_custom_command( OUTPUT qtm.1.gz
>     COMMAND touch qtm.1
>     COMMAND sh ${QTM_SOURCE_DIR}/qtm-manpage.sh ${QTM_VERSION} >$ 
> {QTM_BINARY_DIR}/qtm.1
>     COMMAND gzip -9 ${QTM_BINARY_DIR}/qtm.1
>     DEPENDS qtm-manpage.sh
>     COMMENT "Generating manpage"
>     )
>   add_custom_target( MANPAGE_FILE ALL
>     DEPENDS qtm.1.gz
>     )
>
>   set_directory_properties( ADDITIONAL_MAKE_CLEAN_FILES qtm.1 qtm. 
> 1.gz )
>
>   set( APP_ICON images/qtm-logo1.png )
> endif( UNIX AND NOT APPLE )
>
> if( QDEBUG )
>   set( CMAKE_VERBOSE_MAKEFILE ON )
>   add_definitions( -g -O1 )
> else( NOT QDEBUG )
>   add_definitions( -DNO_DEBUG_OUTPUT )
>   #set( CMAKE_SKIP_RPATH ON )
> endif( QDEBUG )
>
> if( NOT DESKTOP_ENTRY_PATH )
>   set( DESKTOP_ENTRY_PATH ${CMAKE_INSTALL_PREFIX}/share/applications )
> endif( NOT DESKTOP_ENTRY_PATH )
>
> if( NO_SSL )
>   add_definitions( -DDONT_USE_SSL )
> endif( NO_SSL )
>
> if( USE_DBUS )
>   cmake_minimum_required(VERSION 2.4.5)
> else( USE_DBUS )
>   cmake_minimum_required(VERSION 2.4.0)
> endif( USE_DBUS )
>
> set(QTM_SRCS
>     Application.cc
>     catkin.cc
>     locationlineedit.cc
>     main.cc
>     PrefsDialog.cc
>     QijSearchWidget.cc
>     ${QUICKPOSTTEMPLATE_CC}
>     ${QUICKPOSTTEMPLATEDIALOG_CC}
>     SafeHttp.cc
>     ${SUPERMENU_CC}
>     ${SYSTRAYICON_CC}
>     ${DBUSADAPTOR_CC}
>     XmlRpcHandler.cc
> )
>
> set(QTM_MOC_HDRS
>     Application.h
>     catkin.h
>     locationlineedit.h
>     PrefsDialog.h
>     QijSearchWidget.h
>     ${QUICKPOSTTEMPLATE_H}
>     ${QUICKPOSTTEMPLATEDIALOG_H}
>     SafeHttp.h
>     ${SUPERMENU_H}
>     ${SYSTRAYICON_H}
>     ${DBUSADAPTOR_H}
> )
>
> set(QTM_UIS
>     aboutbox.ui
>     PrefsForm.ui
>     PrefsForm41.ui
>     CatkinMainWindow.ui
>     ImageEntry.ui
>     LinkEntry.ui
>     password-form.ui
>     QijSearchWidget.ui
>     ${QUICKPOSTTEMPLATEFORM_UI}
>     SideWidget.ui
> )
>
> set(QTM_RCS
>     qtm.qrc
>     QijSearchWidget.qrc
> )
>
> if(COMMAND cmake_policy)
>   cmake_policy(SET CMP0003 NEW)
>   cmake_policy(SET CMP0005 OLD)
> endif(COMMAND cmake_policy)
>
> add_definitions( -Wall )
> if( WIN32 AND NOT UNIX )
>   file( REMOVE qtm_version.h )
>   add_custom_command( OUTPUT qtm_version.h
>     COMMAND qtm_version.bat ${QTM_VERSION}
>     DEPENDS qtm_version.bat	
>     COMMENT "Generating version header"
>   )
>   add_custom_target( WINDOWS_VERSION_HEADER ALL
>     DEPENDS qtm_version.h
>   )
> else( WIN32 AND NOT UNIX )
>   file( REMOVE qtm_version.h )
>   add_custom_command( OUTPUT qtm_version.h
>     COMMAND sh ${QTM_SOURCE_DIR}/qtm_version.sh ${QTM_VERSION}
>     DEPENDS qtm_version.sh
>     COMMENT "Generating version header"
>   )
>   add_custom_target( VERSION_HEADER ALL
>     DEPENDS qtm_version.h
>   )
> endif( WIN32 AND NOT UNIX )
>
> SET(QT_USE_QTNETWORK TRUE)
> SET(QT_USE_QTXML TRUE)
> if( USE_DBUS )
>   set( QT_USE_QTDBUS TRUE )
> endif( USE_DBUS )
>
> find_package (Qt4 REQUIRED)
>
> include (${QT_USE_FILE})
>
> if( NOT QT_QTDBUS_INCLUDE_DIR )
>   set( QT_QTDBUS_INCLUDE_DIR ${QT_INCLUDE_DIR}/QtDBus )
>   set( QTDBUS_EXTRA QtDBus )
>   add_definitions( -I${QT_QTDBUS_INCLUDE_DIR} )
> endif( NOT QT_QTDBUS_INCLUDE_DIR )
>
> QT4_WRAP_UI(QTM_UIS_H ${QTM_UIS})
>
> QT4_WRAP_CPP(QTM_MOC_SRCS ${QTM_MOC_HDRS})
>
> QT4_ADD_RESOURCES(QTM_RC_SRCS ${QTM_RCS})
>
> if(MINGW)
>     add_custom_command(OUTPUT qtmico.o
>                        COMMAND windres.exe -iqtm.rc -oqtmico.o)
>     set(QTM_SRCS ${QTM_SRCS} qtmico.o)
> endif(MINGW)
>
> IF(APPLE)
>     add_executable(${PROGNAME} MACOSX_BUNDLE ${QTM_SRCS} $ 
> {QTM_UIS_H} ${QTM_MOC_SRCS} ${QTM_RC_SRCS})
>     add_custom_command( TARGET ${PROGNAME} POST_BUILD
>       COMMAND mkdir ARGS -p ${CMAKE_CURRENT_BINARY_DIR}/$ 
> {PROGNAME}.app/Contents/Resources
>       COMMAND cp ARGS ${MACOSX_BUNDLE_ICON_FILE} $ 
> {CMAKE_CURRENT_BINARY_DIR}/${PROGNAME}.app/Contents/Resources
>       )
> ELSE(NOT APPLE)
>     add_executable(${PROGNAME} WIN32 ${QTM_SRCS} ${QTM_UIS_H} $ 
> {QTM_MOC_SRCS} ${QTM_RC_SRCS})
>     if( NOT QDEBUG )
>      if( MINGW )
>       add_custom_command( TARGET ${PROGNAME} POST_BUILD
> 	COMMAND strip ARGS ${QTM_BINARY_DIR}/${PROGNAME}.exe
> 	)
>      endif( MINGW )
>     endif( NOT QDEBUG )
> ENDIF(APPLE)
>
>
> INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR} ${QTM_SOURCE_DIR})
>
> #SET_TARGET_PROPERTIES( ${PROGNAME}
> #	PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE )
> TARGET_LINK_LIBRARIES(${PROGNAME} ${QT_LIBRARIES} ${QTDBUS_EXTRA} )
> if( QDEBUG AND MINGW )
>     SET_TARGET_PROPERTIES (${PROGNAME} PROPERTIES LINK_FLAGS -Wl,- 
> subsystem,console )
> endif( QDEBUG AND MINGW )
>
> install(TARGETS ${PROGNAME} DESTINATION bin)
> if(UNIX AND NOT APPLE)
>   install(FILES ${QTM_BINARY_DIR}/${DESKTOP_ENTRY} DESTINATION $ 
> {DESKTOP_ENTRY_PATH})
>   install( FILES ${APP_ICON} DESTINATION share/icons )
>   install( FILES ${QTM_BINARY_DIR}/qtm.1.gz DESTINATION $ 
> {MANPAGE_DIRECTORY} )
> endif(UNIX AND NOT APPLE)
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list