automoc-1.0~version-0.9.88/0000755000176500017650000000000011136140055014305 5ustar ekaiaekaiaautomoc-1.0~version-0.9.88/Automoc4Config.cmake0000644000176500017650000002541311136137741020145 0ustar ekaiaekaia # It also adds the following macros # AUTOMOC4( ) # Use this to run automoc4 on all files contained in the list . # # AUTOMOC4_MOC_HEADERS( header1.h header2.h ...) # Use this to add more header files to be processed with automoc4. # # AUTOMOC4_ADD_EXECUTABLE( src1 src2 ...) # This macro does the same as ADD_EXECUTABLE, but additionally # adds automoc4 handling for all source files. # # AUTOMOC4_ADD_LIBRARY( src1 src2 ...) # This macro does the same as ADD_LIBRARY, but additionally # adds automoc4 handling for all source files. # Internal helper macro, may change or be removed anytime: # _ADD_AUTOMOC4_TARGET( ) # # Since version 0.9.88: # The following two macros are only to be used for KDE4 projects # and do something which makes sure automoc4 works for KDE. Don't # use them anywhere else. See kdelibs/cmake/modules/KDE4Macros.cmake. # _AUTOMOC4_KDE4_PRE_TARGET_HANDLING( ) # _AUTOMOC4_KDE4_POST_TARGET_HANDLING() # Copyright (C) 2007 Matthias Kretz # Copyright (C) 2008-2009 Alexander Neundorf # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. get_filename_component(_AUTOMOC4_CURRENT_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) # set the automoc version number include(${_AUTOMOC4_CURRENT_DIR}/Automoc4Version.cmake) # are we in the source tree or already installed ? if(EXISTS ${_AUTOMOC4_CURRENT_DIR}/kde4automoc.cpp) get_target_property(AUTOMOC4_EXECUTABLE automoc4 LOCATION) # this dependency is required to make parallel builds of kdesupport work: set(_AUTOMOC4_EXECUTABLE_DEP automoc4) else(EXISTS ${_AUTOMOC4_CURRENT_DIR}/kde4automoc.cpp) get_filename_component(_AUTOMOC4_BIN_DIR "${_AUTOMOC4_CURRENT_DIR}" PATH) get_filename_component(_AUTOMOC4_BIN_DIR "${_AUTOMOC4_BIN_DIR}" PATH) find_program(AUTOMOC4_EXECUTABLE automoc4 PATHS "${_AUTOMOC4_BIN_DIR}/bin" NO_DEFAULT_PATH) set(_AUTOMOC4_EXECUTABLE_DEP) mark_as_advanced(AUTOMOC4_EXECUTABLE) endif(EXISTS ${_AUTOMOC4_CURRENT_DIR}/kde4automoc.cpp) macro (AUTOMOC4_MOC_HEADERS _target_NAME) set (_headers_to_moc) foreach (_current_FILE ${ARGN}) get_filename_component(_suffix "${_current_FILE}" EXT) if (".h" STREQUAL "${_suffix}" OR ".hpp" STREQUAL "${_suffix}" OR ".hxx" STREQUAL "${_suffix}" OR ".H" STREQUAL "${_suffix}") list(APPEND _headers_to_moc ${_current_FILE}) else (".h" STREQUAL "${_suffix}" OR ".hpp" STREQUAL "${_suffix}" OR ".hxx" STREQUAL "${_suffix}" OR ".H" STREQUAL "${_suffix}") message(STATUS "AUTOMOC4_MOC_HEADERS: ignoring non-header file ${_current_FILE}") endif (".h" STREQUAL "${_suffix}" OR ".hpp" STREQUAL "${_suffix}" OR ".hxx" STREQUAL "${_suffix}" OR ".H" STREQUAL "${_suffix}") endforeach (_current_FILE) # need to create moc_.cpp file using automoc4 # and add it to the target if(_headers_to_moc) set(_automoc4_headers_${_target_NAME} "${_headers_to_moc}") set(_automoc4_headers_${_target_NAME}_automoc "${_headers_to_moc}") endif(_headers_to_moc) endmacro (AUTOMOC4_MOC_HEADERS) macro(AUTOMOC4 _target_NAME _SRCS) set(_moc_files) # first list all explicitly set headers foreach(_header_to_moc ${_automoc4_headers_${_target_NAME}} ) get_filename_component(_abs_header ${_header_to_moc} ABSOLUTE) list(APPEND _moc_files ${_abs_header}) endforeach(_header_to_moc) # now add all the sources for the automoc foreach (_current_FILE ${${_SRCS}}) get_filename_component(_abs_current_FILE "${_current_FILE}" ABSOLUTE) get_source_file_property(_skip "${_abs_current_FILE}" SKIP_AUTOMOC) get_source_file_property(_generated "${_abs_current_FILE}" GENERATED) if(NOT _generated AND NOT _skip) get_filename_component(_suffix "${_current_FILE}" EXT) # skip every source file that's not C++ if(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C") list(APPEND _moc_files ${_abs_current_FILE}) endif(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C") endif(NOT _generated AND NOT _skip) endforeach (_current_FILE) if(_moc_files) set(_automoc_source "${CMAKE_CURRENT_BINARY_DIR}/${_target_NAME}_automoc.cpp") get_directory_property(_moc_incs INCLUDE_DIRECTORIES) get_directory_property(_moc_defs DEFINITIONS) get_directory_property(_moc_cdefs COMPILE_DEFINITIONS) # configure_file replaces _moc_files, _moc_incs, _moc_cdefs and _moc_defs configure_file(${_AUTOMOC4_CURRENT_DIR}/automoc4.files.in ${_automoc_source}.files) add_custom_command(OUTPUT ${_automoc_source} COMMAND ${AUTOMOC4_EXECUTABLE} ${_automoc_source} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${QT_MOC_EXECUTABLE} ${CMAKE_COMMAND} --touch DEPENDS ${_automoc_source}.files ${_AUTOMOC4_EXECUTABLE_DEP} COMMENT "" VERBATIM ) set(${_SRCS} ${_automoc_source} ${${_SRCS}}) endif(_moc_files) endmacro(AUTOMOC4) macro(_ADD_AUTOMOC4_TARGET _target_NAME _SRCS) set(_moc_files) set(_moc_headers) # first list all explicitly set headers foreach(_header_to_moc ${_automoc4_headers_${_target_NAME}} ) get_filename_component(_abs_header ${_header_to_moc} ABSOLUTE) list(APPEND _moc_files ${_abs_header}) list(APPEND _moc_headers ${_abs_header}) endforeach(_header_to_moc) # now add all the sources for the automoc foreach (_current_FILE ${${_SRCS}}) get_filename_component(_abs_current_FILE "${_current_FILE}" ABSOLUTE) get_source_file_property(_skip "${_abs_current_FILE}" SKIP_AUTOMOC) get_source_file_property(_generated "${_abs_current_FILE}" GENERATED) if(NOT _generated AND NOT _skip) get_filename_component(_suffix "${_current_FILE}" EXT) # skip every source file that's not C++ if(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C") get_filename_component(_basename "${_current_FILE}" NAME_WE) get_filename_component(_abs_path "${_abs_current_FILE}" PATH) set(_header "${_abs_path}/${_basename}.h") if(EXISTS "${_header}") list(APPEND _moc_headers ${_header}) endif(EXISTS "${_header}") set(_pheader "${_abs_path}/${_basename}_p.h") if(EXISTS "${_pheader}") list(APPEND _moc_headers ${_pheader}) endif(EXISTS "${_pheader}") list(APPEND _moc_files ${_abs_current_FILE}) endif(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C") endif(NOT _generated AND NOT _skip) endforeach (_current_FILE) if(_moc_files) set(_automoc_source "${CMAKE_CURRENT_BINARY_DIR}/${_target_NAME}.cpp") set(_automoc_dotFiles "${CMAKE_CURRENT_BINARY_DIR}/${_target_NAME}.cpp.files") get_directory_property(_moc_incs INCLUDE_DIRECTORIES) get_directory_property(_moc_defs DEFINITIONS) get_directory_property(_moc_cdefs COMPILE_DEFINITIONS) # configure_file replaces _moc_files, _moc_incs, _moc_cdefs and _moc_defs configure_file(${_AUTOMOC4_CURRENT_DIR}/automoc4.files.in ${_automoc_dotFiles}) add_custom_target(${_target_NAME} COMMAND ${AUTOMOC4_EXECUTABLE} ${_automoc_source} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${QT_MOC_EXECUTABLE} ${CMAKE_COMMAND} COMMENT "" VERBATIM ) if(_AUTOMOC4_EXECUTABLE_DEP) add_dependencies(${_target_NAME} ${_AUTOMOC4_EXECUTABLE_DEP}) endif(_AUTOMOC4_EXECUTABLE_DEP) set_source_files_properties(${_automoc_source} PROPERTIES GENERATED TRUE) set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${_automoc_source}) set(${_SRCS} ${_automoc_source} ${${_SRCS}}) endif(_moc_files) endmacro(_ADD_AUTOMOC4_TARGET) macro(AUTOMOC4_ADD_EXECUTABLE _target_NAME) set(_SRCS ${ARGN}) set(_add_executable_param) foreach(_argName "WIN32" "MACOSX_BUNDLE" "EXCLUDE_FROM_ALL") list(FIND _SRCS ${_argName} _index) if(_index GREATER -1) list(APPEND _add_executable_param ${_argName}) list(REMOVE_AT _SRCS ${_index}) endif(_index GREATER -1) endforeach(_argName) _add_automoc4_target("${_target_NAME}_automoc" _SRCS) add_executable(${_target_NAME} ${_add_executable_param} ${_SRCS}) add_dependencies(${_target_NAME} "${_target_NAME}_automoc") endmacro(AUTOMOC4_ADD_EXECUTABLE) macro(AUTOMOC4_ADD_LIBRARY _target_NAME) set(_SRCS ${ARGN}) set(_add_executable_param) foreach(_argName "STATIC" "SHARED" "MODULE" "EXCLUDE_FROM_ALL") list(FIND _SRCS ${_argName} _index) if(_index GREATER -1) list(APPEND _add_executable_param ${_argName}) list(REMOVE_AT _SRCS ${_index}) endif(_index GREATER -1) endforeach(_argName) _add_automoc4_target("${_target_NAME}_automoc" _SRCS) add_library(${_target_NAME} ${_add_executable_param} ${_SRCS}) add_dependencies(${_target_NAME} "${_target_NAME}_automoc") endmacro(AUTOMOC4_ADD_LIBRARY) macro(_AUTOMOC4_KDE4_PRE_TARGET_HANDLING _target _srcs) _add_automoc4_target("${_target}_automoc" ${_srcs}) endmacro(_AUTOMOC4_KDE4_PRE_TARGET_HANDLING) macro(_AUTOMOC4_KDE4_POST_TARGET_HANDLING _target) add_dependencies(${_target} "${_target}_automoc") endmacro(_AUTOMOC4_KDE4_POST_TARGET_HANDLING) automoc-1.0~version-0.9.88/Automoc4Version.cmake0000644000176500017650000000035411136137741020362 0ustar ekaiaekaia# set the current version number set(AUTOMOC4_VERSION_MAJOR "0") set(AUTOMOC4_VERSION_MINOR "9") set(AUTOMOC4_VERSION_PATCH "88") set(AUTOMOC4_VERSION "${AUTOMOC4_VERSION_MAJOR}.${AUTOMOC4_VERSION_MINOR}.${AUTOMOC4_VERSION_PATCH}") automoc-1.0~version-0.9.88/CMakeLists.txt0000644000176500017650000000213111136137741017052 0ustar ekaiaekaiaproject(Automoc4) cmake_minimum_required(VERSION 2.4.5 FATAL_ERROR) find_package(Qt4 REQUIRED) if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER MATCHES "icc") set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor -Wno-long-long -ansi -Wundef -Wcast-align -Wchar-subscripts -Wall -W -Wpointer-arith -Wformat-security -fno-check-new -fno-common") endif (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER MATCHES "icc") # set the current version number include(Automoc4Version.cmake) configure_file(automoc4_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/automoc4_config.h) # Always include srcdir and builddir in include path set(CMAKE_INCLUDE_CURRENT_DIR ON) include_directories(${QT_INCLUDE_DIR}) add_executable(automoc4 kde4automoc.cpp) set_target_properties(automoc4 PROPERTIES SKIP_BUILD_RPATH FALSE INSTALL_RPATH_USE_LINK_PATH TRUE ) target_link_libraries(automoc4 ${QT_QTCORE_LIBRARY}) install(TARGETS automoc4 DESTINATION bin) install(FILES Automoc4Config.cmake Automoc4Version.cmake automoc4.files.in DESTINATION lib${LIB_SUFFIX}/automoc4) automoc-1.0~version-0.9.88/automoc4.files.in0000644000176500017650000000042211136137741017477 0ustar ekaiaekaiaSOURCES: @_moc_files@ MOC_COMPILE_DEFINITIONS: @_moc_cdefs@ MOC_DEFINITIONS: @_moc_defs@ MOC_INCLUDES: @_moc_incs@ CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE: @CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE@ CMAKE_BINARY_DIR: @CMAKE_BINARY_DIR@ CMAKE_SOURCE_DIR: @CMAKE_SOURCE_DIR@ automoc-1.0~version-0.9.88/automoc4_config.h.in0000644000176500017650000000024411136137741020153 0ustar ekaiaekaia#ifndef AUTOMOC4_CONFIG_H #define AUTOMOC4_CONFIG_H #define AUTOMOC4_VERSION "@AUTOMOC4_VERSION_MAJOR@.@AUTOMOC4_VERSION_MINOR@.@AUTOMOC4_VERSION_PATCH@" #endif automoc-1.0~version-0.9.88/kde4automoc.cpp0000644000176500017650000005760511136137741017255 0ustar ekaiaekaia/* Copyright (C) 2007 Matthias Kretz Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef Q_OS_WIN #include #include #else #include #endif // currently this is only used for the version number, Alex #include "automoc4_config.h" class AutoMoc { public: AutoMoc(); bool run(); private: void dotFilesCheck(bool); void lazyInitMocDefinitions(); void lazyInit(); bool touch(const QString &filename); bool generateMoc(const QString &sourceFile, const QString &mocFileName); void printUsage(const QString &); void printVersion(); void echoColor(const QString &msg) { QProcess cmakeEcho; cmakeEcho.setProcessChannelMode(QProcess::ForwardedChannels); QStringList args(cmakeEchoColorArgs); args << msg; cmakeEcho.start(cmakeExecutable, args, QIODevice::NotOpen); cmakeEcho.waitForFinished(-1); } QString builddir; QString mocExe; QStringList mocIncludes; QStringList mocDefinitions; QStringList cmakeEchoColorArgs; QString cmakeExecutable; QFile dotFiles; const bool verbose; QTextStream cerr; QTextStream cout; bool failed; bool automocCppChanged; bool generateAll; bool doTouch; }; void AutoMoc::printUsage(const QString &path) { cout << "Usage: " << path << " [--touch]" << endl; } void AutoMoc::printVersion() { cout << "automoc4 " << AUTOMOC4_VERSION << endl; } void AutoMoc::dotFilesCheck(bool x) { if (!x) { cerr << "Error: syntax error in " << dotFiles.fileName() << endl; ::exit(EXIT_FAILURE); } } int main(int argc, char **argv) { QCoreApplication app(argc, argv); if (!AutoMoc().run()) { return EXIT_FAILURE; } return 0; } AutoMoc::AutoMoc() : verbose(!qgetenv("VERBOSE").isEmpty()), cerr(stderr), cout(stdout), failed(false), automocCppChanged(false), generateAll(false), doTouch(false) { const QByteArray colorEnv = qgetenv("COLOR"); cmakeEchoColorArgs << QLatin1String("-E") << QLatin1String("cmake_echo_color") << QLatin1String("--switch=") + colorEnv << QLatin1String("--blue") << QLatin1String("--bold"); } void AutoMoc::lazyInitMocDefinitions() { static bool done = false; if (done) { return; } done = true; QByteArray line = dotFiles.readLine(); dotFilesCheck(line == "MOC_COMPILE_DEFINITIONS:\n"); line = dotFiles.readLine().trimmed(); const QStringList &cdefList = QString::fromUtf8(line).split(';', QString::SkipEmptyParts); line = dotFiles.readLine(); dotFilesCheck(line == "MOC_DEFINITIONS:\n"); line = dotFiles.readLine().trimmed(); if (!cdefList.isEmpty()) { foreach (const QString &def, cdefList) { Q_ASSERT(!def.isEmpty()); mocDefinitions << QLatin1String("-D") + def; } } else { const QStringList &defList = QString::fromUtf8(line).split(' ', QString::SkipEmptyParts); foreach (const QString &def, defList) { Q_ASSERT(!def.isEmpty()); if (def.startsWith(QLatin1String("-D"))) { mocDefinitions << def; } } } } void AutoMoc::lazyInit() { const QStringList &args = QCoreApplication::arguments(); mocExe = args[4]; cmakeExecutable = args[5]; if (args.size() > 6) { if (args[6] == QLatin1String("--touch")) { doTouch = true; } } lazyInitMocDefinitions(); QByteArray line = dotFiles.readLine(); dotFilesCheck(line == "MOC_INCLUDES:\n"); line = dotFiles.readLine().trimmed(); const QStringList &incPaths = QString::fromUtf8(line).split(';', QString::SkipEmptyParts); foreach (const QString &path, incPaths) { Q_ASSERT(!path.isEmpty()); mocIncludes << "-I" + path; } // on the Mac, add -F always, otherwise headers in the frameworks won't be found // is it necessary to do this only optionally ? Alex #ifdef Q_OS_MAC mocIncludes << "-F/Library/Frameworks"; #endif line = dotFiles.readLine(); dotFilesCheck(line == "CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE:\n"); line = dotFiles.readLine(); if (line == "ON\n") { line = dotFiles.readLine(); dotFilesCheck(line == "CMAKE_BINARY_DIR:\n"); const QString &binDir = QLatin1String("-I") + QString::fromUtf8(dotFiles.readLine().trimmed()); line = dotFiles.readLine(); dotFilesCheck(line == "CMAKE_SOURCE_DIR:\n"); const QString &srcDir = QLatin1String("-I") + QString::fromUtf8(dotFiles.readLine().trimmed()); QStringList sortedMocIncludes; QMutableListIterator it(mocIncludes); while (it.hasNext()) { if (it.next().startsWith(binDir)) { sortedMocIncludes << it.value(); it.remove(); } } it.toFront(); while (it.hasNext()) { if (it.next().startsWith(srcDir)) { sortedMocIncludes << it.value(); it.remove(); } } sortedMocIncludes += mocIncludes; mocIncludes = sortedMocIncludes; } } bool AutoMoc::run() { const QStringList &args = QCoreApplication::arguments(); Q_ASSERT(args.size() > 0); if (args.size() == 2) { if ((args[1]=="--help") || (args[1]=="-h")) { printUsage(args[0]); ::exit(0); } else if (args[1]=="--version") { printVersion(); ::exit(0); } else { printUsage(args[0]); ::exit(EXIT_FAILURE); } } else if (args.size() < 5) { printUsage(args[0]); ::exit(EXIT_FAILURE); } QFile outfile(args[1]); const QFileInfo outfileInfo(outfile); QString srcdir(args[2]); if (!srcdir.endsWith('/')) { srcdir += '/'; } builddir = args[3]; if (!builddir.endsWith('/')) { builddir += '/'; } dotFiles.setFileName(args[1] + QLatin1String(".files")); dotFiles.open(QIODevice::ReadOnly | QIODevice::Text); const QByteArray &line = dotFiles.readLine(); dotFilesCheck(line == "SOURCES:\n"); const QStringList &sourceFiles = QString::fromUtf8(dotFiles.readLine().trimmed()).split(';', QString::SkipEmptyParts); if (outfile.exists()) { // set generateAll = true if MOC_COMPILE_DEFINITIONS changed outfile.open(QIODevice::ReadOnly | QIODevice::Text); QByteArray buf = outfile.readLine(); // the second line contains the joined mocDefinitions buf = outfile.readLine(); buf.chop(1); // remove trailing \n lazyInitMocDefinitions(); generateAll = (buf != mocDefinitions.join(QString(QLatin1Char(' '))).toUtf8()); outfile.close(); } else { generateAll = true; } // the program goes through all .cpp files to see which moc files are included. It is not really // interesting how the moc file is named, but what file the moc is created from. Once a moc is // included the same moc may not be included in the _automoc.cpp file anymore. OTOH if there's a // header containing Q_OBJECT where no corresponding moc file is included anywhere a // moc_.cpp file is created and included in the _automoc.cpp file. QHash includedMocs; // key = moc source filepath, value = moc output filepath QHash notIncludedMocs; // key = moc source filepath, value = moc output filename QRegExp mocIncludeRegExp(QLatin1String("[\n]\\s*#\\s*include\\s+[\"<]((?:[^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]")); QRegExp qObjectRegExp(QLatin1String("[\n]\\s*Q_OBJECT\\b")); QStringList headerExtensions; #if defined(Q_OS_WIN) || defined(Q_OS_MAC) // not case sensitive headerExtensions << ".h" << ".hpp" << ".hxx"; #else headerExtensions << ".h" << ".hpp" << ".hxx" << ".H"; #endif /* not safe: if a moc file is missing it's hard to get it generated if this check is "active" const QDateTime &lastRun = QFileInfo(dotFiles).lastModified(); if (!generateAll) { bool dirty = false; foreach (const QString &absFilename, sourceFiles) { const QFileInfo sourceFileInfo(absFilename); if (sourceFileInfo.lastModified() >= lastRun) { dirty = true; break; } const QString &absPathBaseName = sourceFileInfo.absolutePath() + QLatin1Char('/') + sourceFileInfo.completeBaseName(); foreach (const QString &ext, headerExtensions) { const QFileInfo header(absPathBaseName + ext); if (header.exists() && header.lastModified() >= lastRun) { dirty = true; break; } const QFileInfo pheader(absPathBaseName + QLatin1String("_p") + ext); if (pheader.exists() && pheader.lastModified() >= lastRun) { dirty = true; break; } } if (dirty) { break; } } if (!dirty) { return true; } } */ foreach (const QString &absFilename, sourceFiles) { //qDebug() << absFilename; const QFileInfo sourceFileInfo(absFilename); if (absFilename.endsWith(".cpp") || absFilename.endsWith(".cc") || absFilename.endsWith(".cxx") || absFilename.endsWith(".C")) { //qDebug() << "check .cpp file"; QFile sourceFile(absFilename); sourceFile.open(QIODevice::ReadOnly); const QByteArray contents = sourceFile.readAll(); if (contents.isEmpty()) { cerr << "automoc4: empty source file: " << absFilename << endl; continue; } const QString contentsString = QString::fromUtf8(contents); const QString absPath = sourceFileInfo.absolutePath() + '/'; Q_ASSERT(absPath.endsWith('/')); int matchOffset = mocIncludeRegExp.indexIn(contentsString); if (matchOffset < 0) { // no moc #include, look whether we need to create a moc from the .h nevertheless //qDebug() << "no moc #include in the .cpp file"; const QString basename = sourceFileInfo.completeBaseName(); foreach (const QString &ext, headerExtensions) { const QString headername = absPath + basename + ext; if (QFile::exists(headername) && !includedMocs.contains(headername) && !notIncludedMocs.contains(headername)) { const QString currentMoc = "moc_" + basename + ".cpp"; QFile header(headername); header.open(QIODevice::ReadOnly); const QByteArray contents = header.readAll(); if (qObjectRegExp.indexIn(QString::fromUtf8(contents)) >= 0) { //qDebug() << "header contains Q_OBJECT macro"; notIncludedMocs.insert(headername, currentMoc); } break; } } foreach (const QString &ext, headerExtensions) { const QString privateHeaderName = absPath + basename + "_p" + ext; if (QFile::exists(privateHeaderName) && !includedMocs.contains(privateHeaderName) && !notIncludedMocs.contains(privateHeaderName)) { const QString currentMoc = "moc_" + basename + "_p.cpp"; QFile header(privateHeaderName); header.open(QIODevice::ReadOnly); const QByteArray contents = header.readAll(); if (qObjectRegExp.indexIn(QString::fromUtf8(contents)) >= 0) { //qDebug() << "header contains Q_OBJECT macro"; notIncludedMocs.insert(privateHeaderName, currentMoc); } break; } } } else { do { // call this for every moc include in the file const QString currentMoc = mocIncludeRegExp.cap(1); //qDebug() << "found moc include: " << currentMoc << " at offset " << matchOffset; const QFileInfo currentMocInfo(currentMoc); QString basename = currentMocInfo.completeBaseName(); const bool moc_style = basename.startsWith("moc_"); // If the moc include is of the moc_foo.cpp style we expect the Q_OBJECT class // declaration in a header file. // If the moc include is of the foo.moc style we need to look for a Q_OBJECT // macro in the current source file, if it contains the macro we generate the // moc file from the source file, else from the header. // // TODO: currently any .moc file name will be used if the source contains // Q_OBJECT if (moc_style || qObjectRegExp.indexIn(contentsString) < 0) { if (moc_style) { // basename should be the part of the moc filename used for finding the // correct header, so we need to remove the moc_ part basename = basename.right(basename.length() - 4); } bool headerFound = false; foreach (const QString &ext, headerExtensions) { const QString &sourceFilePath = absPath + basename + ext; if (QFile::exists(sourceFilePath)) { headerFound = true; includedMocs.insert(sourceFilePath, currentMoc); notIncludedMocs.remove(sourceFilePath); break; } } if (!headerFound) { // the moc file is in a subdir => look for the header in the same subdir if (currentMoc.indexOf('/') != -1) { const QString &filepath = absPath + currentMocInfo.path() + QLatin1Char('/') + basename; foreach (const QString &ext, headerExtensions) { const QString &sourceFilePath = filepath + ext; if (QFile::exists(sourceFilePath)) { headerFound = true; includedMocs.insert(sourceFilePath, currentMoc); notIncludedMocs.remove(sourceFilePath); break; } } if (!headerFound) { cerr << "automoc4: The file \"" << absFilename << "\" includes the moc file \"" << currentMoc << "\", but neither \"" << absPath + basename + "{" + headerExtensions.join(",") + "}\" nor \"" << filepath + "{" + headerExtensions.join(",") + "}" << "\" exist." << endl; ::exit(EXIT_FAILURE); } } else { cerr << "automoc4: The file \"" << absFilename << "\" includes the moc file \"" << currentMoc << "\", but \"" << absPath + basename + "{" + headerExtensions.join(",") + "}" << "\" does not exist." << endl; ::exit(EXIT_FAILURE); } } } else { includedMocs.insert(absFilename, currentMoc); notIncludedMocs.remove(absFilename); } matchOffset = mocIncludeRegExp.indexIn(contentsString, matchOffset + currentMoc.length()); } while(matchOffset >= 0); } } else if (absFilename.endsWith(".h") || absFilename.endsWith(".hpp") || absFilename.endsWith(".hxx") || absFilename.endsWith(".H")) { if (!includedMocs.contains(absFilename) && !notIncludedMocs.contains(absFilename)) { // if this header is not getting processed yet and is explicitly mentioned for the // automoc the moc is run unconditionally on the header and the resulting file is // included in the _automoc.cpp file (unless there's a .cpp file later on that // includes the moc from this header) const QString currentMoc = "moc_" + sourceFileInfo.completeBaseName() + ".cpp"; notIncludedMocs.insert(absFilename, currentMoc); } } else { if (verbose) { cout << "automoc4: ignoring file '" << absFilename << "' with unknown suffix" << endl; } } } // run moc on all the moc's that are #included in source files QHash::ConstIterator end = includedMocs.constEnd(); QHash::ConstIterator it = includedMocs.constBegin(); for (; it != end; ++it) { generateMoc(it.key(), it.value()); } QByteArray automocSource; QTextStream outStream(&automocSource, QIODevice::WriteOnly); outStream << "/* This file is autogenerated, do not edit\n" << mocDefinitions.join(QString(QLatin1Char(' '))) << "\n*/\n"; if (notIncludedMocs.isEmpty()) { outStream << "enum some_compilers { need_more_than_nothing };\n"; } else { // run moc on the remaining headers and include them in the _automoc.cpp file end = notIncludedMocs.constEnd(); it = notIncludedMocs.constBegin(); for (; it != end; ++it) { if (generateMoc(it.key(), it.value())) { automocCppChanged = true; } outStream << "#include \"" << it.value() << "\"\n"; } } if (failed) { // if any moc process failed we don't want to touch the _automoc.cpp file so that // automoc4 is rerun until the issue is fixed cerr << "returning failed.."<< endl; return false; } outStream.flush(); if (!automocCppChanged) { // compare contents of the _automoc.cpp file outfile.open(QIODevice::ReadOnly | QIODevice::Text); const QByteArray oldContents = outfile.readAll(); outfile.close(); if (oldContents == automocSource) { // nothing changed: don't touch the _automoc.cpp file return true; } } // either the contents of the _automoc.cpp file or one of the mocs included by it have changed // source file that includes all remaining moc files (_automoc.cpp file) outfile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); outfile.write(automocSource); outfile.close(); // update the timestamp on the _automoc.cpp.files file to make sure we get called again dotFiles.close(); if (doTouch && !touch(dotFiles.fileName())) { return false; } return true; } bool AutoMoc::touch(const QString &_filename) { // sleep for 1s in order to make the modification time greater than the modification time of // the files written before. Equal modification time is not good enough. Just using utime with // time(NULL) + 1 is also not a good solution as then make will complain about clock skew. #ifdef Q_OS_WIN Sleep(1000); _wutime(reinterpret_cast(_filename.utf16()), 0); #else const QByteArray &filename = QFile::encodeName(_filename); const struct timespec sleepDuration = { 1, 0 }; nanosleep(&sleepDuration, NULL); int err = utime(filename.constData(), NULL); if (err == -1) { err = errno; cerr << strerror(err) << "\n"; return false; } #endif return true; } bool AutoMoc::generateMoc(const QString &sourceFile, const QString &mocFileName) { //qDebug() << Q_FUNC_INFO << sourceFile << mocFileName; const QString mocFilePath = builddir + mocFileName; QFileInfo mocInfo(mocFilePath); if (generateAll || mocInfo.lastModified() <= QFileInfo(sourceFile).lastModified()) { QDir mocDir = mocInfo.dir(); // make sure the directory for the resulting moc file exists if (!mocDir.exists()) { mocDir.mkpath(mocDir.path()); } static bool initialized = false; if (!initialized) { initialized = true; lazyInit(); } if (verbose) { echoColor("Generating " + mocFilePath + " from " + sourceFile); } else { echoColor("Generating " + mocFileName); } QProcess mocProc; mocProc.setProcessChannelMode(QProcess::ForwardedChannels); QStringList args(mocIncludes + mocDefinitions); #ifdef Q_OS_WIN args << "-DWIN32"; #endif args << QLatin1String("-o") << mocFilePath << sourceFile; //qDebug() << "executing: " << mocExe << args; if (verbose) { cout << mocExe << " " << args.join(QLatin1String(" ")) << endl; } mocProc.start(mocExe, args, QIODevice::NotOpen); if (mocProc.waitForStarted()) { const bool result = mocProc.waitForFinished(-1); if (!result || mocProc.exitCode()) { cerr << "automoc4: process for " << mocFilePath << " failed: " << mocProc.errorString() << endl; cerr << "pid to wait for: " << mocProc.pid() << endl; failed = true; QFile::remove(mocFilePath); } return true; } else { cerr << "automoc4: process for " << mocFilePath << "failed to start: " << mocProc.errorString() << endl; failed = true; } } return false; }