2011-08-20 7 views
7

Attualmente, utilizzo una variabile MYPROJECT_CURRENT_HEADERS in CMake per elencare tutte le intestazioni. Come io uso Qt, il mio CMakeLists.txt contiene:CMake: rileva "Q_OBJECT" in un file e lo aggiunge a un elenco di file da trattare dal MOC

QT4_WRAP_CPP(MYPROJECT_CURRENT_MOC ${MYPROJECT_CURRENT_HEADERS}) 

Il problema è che tutte le intestazioni sono trattati da moc, anche quelli che non hanno un Q_OBJECT: così si genera molti file vuoto.

Esiste una soluzione per "grep"/rilevare se il file contiene la stringa Q_OBJECT e se è il caso, aggiungerlo a MYPROJECT_CURRENT_MOC?

Grazie

risposta

5

non so un semplice comando per scegliere le intestazioni con una stringa dalla lista, ma si può sempre fare un ciclo di trovare tutte queste intestazioni:

set(HEADERS_HAVING_Q_OBJECT) 
foreach(header ${MYPROJECT_CURRENT_HEADERS}) 
    file(STRINGS "${header}" lines REGEX "Q_OBJECT") 
    if(lines) 
     list(APPEND HEADERS_HAVING_Q_OBJECT "${header}") 
    endif() 
endforeach() 

ma questa soluzione ha il suo lato negativo: se si aggiunge uno Q_OBJECT in uno dei file filtrati è necessario rieseguire manualmente cmake. In caso contrario, il codice moc per il nuovo file non verrà generato automaticamente durante il processo di compilazione.

5

C'è una nuova proprietà di destinazione nel prossimo CMake 2.8.6 chiamato "AUTOMOC" che potrebbe essere d'aiuto.

La prova per questa funzione (che si può usare come una guida o un esempio) si trova qui:

http://cmake.org/gitweb?p=cmake.git;a=tree;f=Tests/QtAutomoc;h=7dae3b16a54dc0b2f63bbfa5c218c48b9bbf34a9;hb=nightly-master

Il file CMakeLists.txt molto semplice è qui:

http://cmake.org/gitweb?p=cmake.git;a=blob;f=Tests/QtAutomoc/CMakeLists.txt;h=4a5ff1099ba5249a6f22eea745a031b76e6f440f;hb=nightly-master

Se si utilizza questa funzione, cmake eseguirà la scansione delle intestazioni per Q_OBJECT e eseguirà automaticamente Moc per te.

Se vuoi provarlo prima del rilascio finale di CMake 2.8.6, è possibile scaricare uno dei candidati stampa qui:

http://cmake.org/files/v2.8/?C=M;O=D

I file "-rc2" comprendono la proprietà AUTOMOC.

Ecco il testo della guida da corsa "cmake --help-proprietà AUTOMOC":

 
cmake version 2.8.6-rc2 
    AUTOMOC 
     Should the target be processed with automoc (for Qt projects). 

     AUTOMOC is a boolean specifying whether CMake will handle the Qt moc 
     preprocessor automatically, i.e. without having to use the 
     QT4_WRAP_CPP() macro. Currently Qt4 is supported. When this property 
     is set to TRUE, CMake will scan the source files at build time and 
     invoke moc accordingly. If an #include statement like #include 
     "moc_foo.cpp" is found, the Q_OBJECT class declaration is expected in 
     the header, and moc is run on the header file. If an #include 
     statement like #include "foo.moc" is found, then a Q_OBJECT is 
     expected in the current source file and moc is run on the file itself. 
     Additionally, all header files are parsed for Q_OBJECT macros, and if 
     found, moc is also executed on those files. The resulting moc files, 
     which are not included as shown above in any of the source files are 
     included in a generated _automoc.cpp file, which is 
     compiled as part of the target.This property is initialized by the 
     value of the variable CMAKE_AUTOMOC if it is set when a target is 
     created. 
Problemi correlati