From 86c00c1d694cfc69b58ac2fbd0d7744c7f95e39a Mon Sep 17 00:00:00 2001 From: Hugo Lima Date: Fri, 21 Aug 2009 14:21:39 -0300 Subject: Added boostpython as a generator plugin. --- CMakeLists.txt | 22 +++---- generator.h | 13 ++++- generators/CMakeLists.txt | 1 + generators/boostpython/CMakeLists.txt | 14 +++++ generators/boostpython/boostpython.cpp | 29 ++++++++++ generators/boostpython/boostpythongenerator.cpp | 76 ++++++++++++++++++++++++- generators/boostpython/boostpythongenerator.h | 6 +- generators/boostpython/cppgenerator.cpp | 4 +- main.cpp | 1 - 9 files changed, 147 insertions(+), 19 deletions(-) create mode 100644 generators/CMakeLists.txt create mode 100644 generators/boostpython/CMakeLists.txt create mode 100644 generators/boostpython/boostpython.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2eeb84ca1..0d61f4300 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 2.6) find_package(Qt4 4.5.0 REQUIRED) find_package(ApiExtractor REQUIRED) +# lib generator version +set(generator_VERSION "0.1") + add_definitions(${QT_DEFINITIONS}) set(boostpythongenerator_VERSION 0.2) @@ -13,21 +16,23 @@ set(CMAKE_BUILD_TYPE Debug) set(boostpythongenerator_SRC main.cpp -generator.cpp ) include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${APIEXTRACTOR_INCLUDE_DIR} ${APIEXTRACTOR_INCLUDE_DIR}/.. - ${QT_INCLUDE_DIR} ${QT_QTCORE_INCLUDE_DIR}) +add_library(generator SHARED generator.cpp) +set_target_properties(generator PROPERTIES SOVERSION ${generator_VERSION}) +target_link_libraries(generator ${QT_QTCORE_LIBRARY} ${APIEXTRACTOR_LIBRARY}) + add_executable(boostpythongenerator ${boostpythongenerator_SRC}) target_link_libraries(boostpythongenerator + generator ${APIEXTRACTOR_LIBRARY} - ${QT_QTCORE_LIBRARY} - ${QT_QTXML_LIBRARY}) + ${QT_QTCORE_LIBRARY}) # uninstall target configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake" @@ -37,11 +42,6 @@ add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake") -# "make dist", in fact "make package_source" -#set(CPACK_SOURCE_PACKAGE_FILE_NAME "boostpythongenerator-${boostpythongenerator_VERSION}") -#set(CPACK_SOURCE_GENERATOR TGZ) -#set(CPACK_SOURCE_IGNORE_FILES "~$" ".svn" "debian/" "build/" ".swp$" "*.kdev4") -#include(CPack) set(ARCHIVE_NAME ${CMAKE_PROJECT_NAME}-${boostpythongenerator_VERSION}) add_custom_target(dist @@ -50,8 +50,10 @@ add_custom_target(dist WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) install(TARGETS boostpythongenerator DESTINATION bin) +install(TARGETS generator DESTINATION lib) +install(FILES generator.h DESTINATION include) enable_testing() #add_subdirectory(libbindgen) # add_subdirectory(tests) - +add_subdirectory(generators) diff --git a/generator.h b/generator.h index 14526d7f0..f9e0c714e 100644 --- a/generator.h +++ b/generator.h @@ -26,12 +26,19 @@ #include #include -#include "abstractmetalang.h" +#include +#include class ApiExtractor; class AbstractMetaBuilder; class QFile; +#define EXPORT_GENERATOR_PLUGIN(X)\ +extern "C" Q_DECL_EXPORT GeneratorList getGenerators()\ +{\ + return GeneratorList() << X;\ +}\ + /** * Base class for all generators. The default implementations does nothing, * you must subclass this to create your own generators. @@ -245,7 +252,7 @@ protected: */ virtual QString fileNameForClass(const AbstractMetaClass* metaClass) const = 0; - virtual bool doSetup(QMap args) = 0; + virtual bool doSetup(const QMap& args) = 0; /** * Returns the subdirectory path for a given package @@ -293,6 +300,8 @@ private: QString m_licenseComment; }; +typedef QLinkedList GeneratorList; + /** * Utility class to store the identation level, use it in a QTextStream. */ diff --git a/generators/CMakeLists.txt b/generators/CMakeLists.txt new file mode 100644 index 000000000..f0c6537e1 --- /dev/null +++ b/generators/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(boostpython) diff --git a/generators/boostpython/CMakeLists.txt b/generators/boostpython/CMakeLists.txt new file mode 100644 index 000000000..1e47cac00 --- /dev/null +++ b/generators/boostpython/CMakeLists.txt @@ -0,0 +1,14 @@ +project(boostpython) + +set(boostpython_SRC +boostpythongenerator.cpp +convertergenerator.cpp +cppgenerator.cpp +hppgenerator.cpp +boostpython.cpp +) + +add_library(boostpython SHARED ${boostpython_SRC}) +target_link_libraries(boostpython ${APIEXTRACTOR_LIBRARY} ${QT_QTCORE_LIBRARY} generator) + +install(TARGETS boostpython DESTINATION lib) diff --git a/generators/boostpython/boostpython.cpp b/generators/boostpython/boostpython.cpp new file mode 100644 index 000000000..e165f93b9 --- /dev/null +++ b/generators/boostpython/boostpython.cpp @@ -0,0 +1,29 @@ +/* +* This file is part of the API Extractor project. +* +* Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +* +* Contact: PySide team +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* version 2 as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +* 02110-1301 USA +* +*/ + +#include "generator.h" +#include "hppgenerator.h" +#include "cppgenerator.h" +#include "convertergenerator.h" + +EXPORT_GENERATOR_PLUGIN(new HppGenerator << new CppGenerator << new ConverterGenerator) diff --git a/generators/boostpython/boostpythongenerator.cpp b/generators/boostpython/boostpythongenerator.cpp index 70ce4a5ed..d5fd5e69b 100644 --- a/generators/boostpython/boostpythongenerator.cpp +++ b/generators/boostpython/boostpythongenerator.cpp @@ -36,6 +36,78 @@ static Indentor INDENT; static void dump_function(AbstractMetaFunctionList lst); +static QString formattedCodeHelper(QTextStream &s, Indentor &indentor, QStringList &lines) +{ + bool multilineComment = false; + bool lastEmpty = true; + QString lastLine; + while (!lines.isEmpty()) { + const QString line = lines.takeFirst().trimmed(); + if (line.isEmpty()) { + if (!lastEmpty) + s << endl; + lastEmpty = true; + continue; + } else + lastEmpty = false; + + if (line.startsWith("/*")) + multilineComment = true; + + if (multilineComment) { + s << indentor; + if (line.startsWith("*")) + s << " "; + s << line << endl; + if (line.endsWith("*/")) + multilineComment = false; + } else if (line.startsWith("}")) + return line; + else if (line.endsWith("")) { + s << indentor << line << endl; + return 0; + } else if (line.endsWith("{")) { + s << indentor << line << endl; + QString tmp; + { + Indentation indent(indentor); + tmp = formattedCodeHelper(s, indentor, lines); + } + if (!tmp.isNull()) + s << indentor << tmp << endl; + + lastLine = tmp; + continue; + } else { + s << indentor; + if (!lastLine.isEmpty() && + !lastLine.endsWith(";") && + !line.startsWith("@") && + !line.startsWith("//") && + !lastLine.startsWith("//") && + !lastLine.endsWith("}") && + !line.startsWith("{")) + s << " "; + s << line << endl; + } + lastLine = line; + } + return 0; +} + +QTextStream& formatCode(QTextStream &s, const QString& code, Indentor &indentor) +{ + QStringList lst(code.split("\n")); + while (!lst.isEmpty()) { + QString tmp = formattedCodeHelper(s, indentor, lst); + if (!tmp.isNull()) + s << indentor << tmp << endl; + + } + s.flush(); + return s; +} + FunctionModificationList BoostPythonGenerator::functionModifications(const AbstractMetaFunction *metaFunction) { FunctionModificationList mods; @@ -419,7 +491,7 @@ void BoostPythonGenerator::writeCodeSnips(QTextStream &s, QString code; QTextStream tmpStream(&code); - snip.formattedCode(tmpStream, INDENT); + formatCode(tmpStream, snip.code(), INDENT); if (func) replaceTemplateVariables(code, func); @@ -488,7 +560,7 @@ static void dump_function(AbstractMetaFunctionList lst) } -bool BoostPythonGenerator::prepareGeneration(const QMap&) +bool BoostPythonGenerator::doSetup(const QMap&) { return true; } diff --git a/generators/boostpython/boostpythongenerator.h b/generators/boostpython/boostpythongenerator.h index 4ad191b50..709612e14 100644 --- a/generators/boostpython/boostpythongenerator.h +++ b/generators/boostpython/boostpythongenerator.h @@ -24,11 +24,13 @@ #ifndef BOOSTPYTHONGENERATOR_H #define BOOSTPYTHONGENERATOR_H -#include #include +#include "generator.h" class DocParser; +QTextStream& formatCode(QTextStream &s, const QString& code, Indentor &indentor); + /** * Abstract generator that contains common methods used in CppGenerator and HppGenerator. */ @@ -127,7 +129,7 @@ public: static QString getWrapperName(const AbstractMetaClass* clazz); - virtual bool prepareGeneration(const QMap& args); + virtual bool doSetup(const QMap& args); protected: // verify if the class is copyalbe diff --git a/generators/boostpython/cppgenerator.cpp b/generators/boostpython/cppgenerator.cpp index 36bd067cf..4a013694f 100644 --- a/generators/boostpython/cppgenerator.cpp +++ b/generators/boostpython/cppgenerator.cpp @@ -1405,9 +1405,9 @@ void CppGenerator::writeGlobalFunctions() if (moduleEntry && moduleEntry->codeSnips().size() > 0) { foreach (CodeSnip snip, moduleEntry->codeSnips()) { if (snip.position == CodeSnip().Beginning) - snip.formattedCode(s, INDENT); + formatCode(s, snip.code(), INDENT); else - snip.formattedCode(snipEnd, INDENT); + formatCode(snipEnd, snip.code(), INDENT); } } diff --git a/main.cpp b/main.cpp index af6d9d186..14ebad64d 100644 --- a/main.cpp +++ b/main.cpp @@ -49,7 +49,6 @@ static void printOptions(QTextStream& s, const QMap& options) } typedef QLinkedList (*getGeneratorsFunc)(); -typedef QLinkedList GeneratorList; QMap getCommandLineArgs(int argc, char** argv) { -- cgit v1.2.3