Browse Source

Add another skeleton project for for script binding extractor tool.
[ci only: Annotate]

Yao Wei Tjong 姚伟忠 10 years ago
parent
commit
b3e01a1424

+ 1 - 1
.travis.yml

@@ -182,7 +182,7 @@ before_script:
   - export CXX=g++-4.9 CC=gcc-4.9
   - for compiler in gcc g++; do ln -s $(which ccache) $HOME/$compiler-4.9; done && export PATH=$HOME:$PATH
   - rake ci_setup_cache
-script: rake annotate
+script: rake cmake URHO3D_CLANG_TOOLS=1 && rake make annotate
 after_script: rake ci_teardown_cache
 
 ---

+ 1 - 1
Docs/GettingStarted.dox

@@ -285,7 +285,7 @@ The prerequisites are Doxygen and Graphviz. Tools to dump the \ref ScriptAPI "An
 
 \section Building_Clang_tools Clang-tools build
 
-If "URHO3D_CLANG_TOOLS" build option is set then CMake would generate a special build tree for the purpose of developing the Clang-tools. Before doing this, the development software package of LLVM/Clang must be already installed in the host system. Alternatively you can use built it from source and install it into the host system. If it is not installed in a system-wide installation location then use the LLVM_CLANG_ROOT environment variable to point to the root path of this custom location (the root is the parent directory containing the bin, lib, include and share subdirectories). If you have built Emscripten-SDK from its source in your host system then you can also just install the Fastcomp/Clang found inside the Emscripten-SDK by navigating to the Fastcomp/Clang build tree and issuing a 'make install' command as usual. Using Fastcomp/Clang is fine because we are only interested in using Clang as 3rd-party library instead of as compiler.
+If "URHO3D_CLANG_TOOLS" build option is set then CMake would generate a special build tree for the purpose of developing the Clang-tools. Before doing this, the development software package of LLVM/Clang must be already installed in the host system. Alternatively, you can download LLVM/Clang source, built it from source and install it into the host system. If it is not installed in a system-wide installation location then use the LLVM_CLANG_ROOT environment variable to point to the root path of this custom location (the root is the parent directory containing the bin, lib, include and share subdirectories). If you have built Emscripten-SDK in your host system then you can also just install the Fastcomp/Clang found inside the Emscripten-SDK by navigating to the Fastcomp/Clang build tree and issuing a 'make install' command. Using Fastcomp/Clang is fine because we are only interested in using Clang as 3rd-party library instead of as compiler.
 
 Check the Source/Clang-Tools/CMakeLists.txt to get the list of targets currently available or under development. Normal build targets won't work properly in this special build tree. See also https://github.com/urho3d/Urho3D/issues/887 to find out more about the current development plan and leave a comment there if you would like to help to contribute the development.
 

+ 0 - 6
Rakefile

@@ -194,12 +194,6 @@ task :android do
   android_test_run parameter, intent, package, success_indicator, payload or abort "Failed to test run #{package}/#{intent}, make sure the APK has been installed"
 end
 
-# Usage: NOT intended to be used manually
-desc 'Build and run the annotate migration tool'
-task :annotate do
-  system 'rake cmake URHO3D_CLANG_TOOLS=1 && rake make annotate' or abort 'Failed to annotate'
-end
-
 # Usage: NOT intended to be used manually (if you insist then try: rake ci)
 desc 'Configure, build, and test Urho3D project'
 task :ci do

+ 18 - 29
Source/Clang-Tools/Annotator/Annotator.cpp

@@ -20,17 +20,11 @@
 // THE SOFTWARE.
 //
 
-#include "clang/AST/ASTConsumer.h"
-#include "clang/Driver/Options.h"
-#include "clang/Frontend/ASTConsumers.h"
-#include "clang/Frontend/CompilerInstance.h"
-#include "clang/Rewrite/Frontend/FixItRewriter.h"
-#include "clang/Rewrite/Frontend/FrontendActions.h"
-#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
-#include "clang/Tooling/CommonOptionsParser.h"
-#include "clang/Tooling/Tooling.h"
-#include "llvm/Support/Signals.h"
+#include <clang/Driver/Options.h>
+#include <clang/Tooling/CommonOptionsParser.h>
+#include <clang/Tooling/Tooling.h>
 
+using namespace clang;
 using namespace clang::driver;
 using namespace clang::tooling;
 using namespace llvm;
@@ -38,37 +32,32 @@ using namespace llvm;
 static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
 static cl::extrahelp MoreHelp(
     "\tFor example, to run Annotator on all files in a subtree of the\n"
-        "\tsource tree, use:\n"
-        "\n"
-        "\t  find path/in/substree -name '*.cpp'|xargs Annotator -p build/path\n"
-        "\n"
-        "\tNote, that path/in/subtree and current directory should follow the\n"
-        "\trules described above.\n"
-        "\n"
+    "\tsource tree, use:\n"
+    "\n"
+    "\t  find path/in/substree -name '*.cpp'|xargs Annotator -p build/path\n"
+    "\n"
+    "\tNote, that path/in/subtree and current directory should follow the\n"
+    "\trules described above.\n"
+    "\n"
 );
 
 static cl::OptionCategory AnnotatorCategory("Annotator options");
 static std::unique_ptr<opt::OptTable> Options(createDriverOptTable());
-static cl::opt<bool> ASTDump("ast-dump", cl::desc(Options->getOptionHelpText(options::OPT_ast_dump)), cl::cat(AnnotatorCategory));
-static cl::opt<std::string> ASTDumpFilter
-    ("ast-dump-filter", cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter)), cl::cat(AnnotatorCategory));
+static cl::opt<std::string> BindingsFile
+    ("b", cl::desc("Bindings file in JSON format (output of ScriptBindingExtractor tool)"), cl::cat(AnnotatorCategory));
 
-class AnnotatorActionFactory
+class AnnotateFrontendAction : public ASTFrontendAction
 {
-public:
-    std::unique_ptr<clang::ASTConsumer> newASTConsumer()
+protected:
+    virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance& CI, StringRef InFile)
     {
-        if (ASTDump)
-            return clang::CreateASTDumper(ASTDumpFilter, /*DumpDecls=*/true, /*DumpLookups=*/false);
-        return llvm::make_unique<clang::ASTConsumer>();
+        return make_unique<ASTConsumer>();
     }
 };
 
 int main(int argc, const char** argv)
 {
-    llvm::sys::PrintStackTraceOnErrorSignal();
     CommonOptionsParser OptionsParser(argc, argv, AnnotatorCategory);
     ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());
-    AnnotatorActionFactory annotatorFactory;
-    return Tool.run(newFrontendActionFactory(&annotatorFactory).get());
+    return Tool.run(newFrontendActionFactory<AnnotateFrontendAction>().get());
 }

+ 13 - 7
Source/Clang-Tools/CMakeLists.txt

@@ -23,10 +23,11 @@
 # Set project name
 project (Urho3D-Clang-Tools)
 
-# LLVM/Clang libs and headers are assumed to be installed in system-wide location when not explicitily defined using env-var
+# LLVM/Clang is assumed to be installed in a system-wide location when not explicitily defined using env-var
 if (DEFINED ENV{LLVM_CLANG_ROOT})
     link_directories ($ENV{LLVM_CLANG_ROOT}/lib)
     include_directories ($ENV{LLVM_CLANG_ROOT}/include)
+    set (BINDIR $ENV{LLVM_CLANG_ROOT}/bin/)
 endif ()
 
 # No exception and no RTTI
@@ -43,22 +44,27 @@ set_tool_output_directories ()
 get_target_property (SOURCES Urho3D SOURCES)
 string (REGEX REPLACE "[^;]+\\.h" "" SOURCES "${SOURCES}")   # Stringify to preserve the semicolons
 string (REGEX REPLACE "[^;]+generated[^;]+\\.cpp" "" SOURCES "${SOURCES}")
+file (GLOB API_SOURCES RELATIVE ${CMAKE_SOURCE_DIR}/Source/Urho3D ${CMAKE_SOURCE_DIR}/Source/Urho3D/Script/*API.cpp)
 
 # Define common dependency libs
-set (LIBS clangRewriteFrontend clangStaticAnalyzerFrontend clangTooling clangStaticAnalyzerCheckers clangStaticAnalyzerCore
-        clangFrontend clangDriver clangParse clangSerialization clangSema clangEdit clangAnalysis clangASTMatchers clangAST
-        clangToolingCore clangRewrite clangLex clangBasic
-        LLVMBitReader LLVMCore LLVMMC LLVMOption LLVMMCParser LLVMSupport)
+set (LIBS clangTooling clangFrontend clangDriver clangParse clangSerialization clangSema clangEdit clangAnalysis clangLex clangAST clangBasic
+        LLVMBitReader LLVMMC LLVMOption LLVMMCParser LLVMSupport)
 execute_process (COMMAND ${LLVM_CONFIG} --system-libs OUTPUT_VARIABLE LLVM_SYSLIBS OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
 string (REGEX REPLACE " *-l" ";" LLVM_SYSLIBS "${LLVM_SYSLIBS}")   # Stringify against empty output variable
 list (APPEND LIBS ${LLVM_SYSLIBS})
 
 # List of tools
+add_subdirectory (ScriptBindingExtractor)
 add_subdirectory (Annotator)
 
 # List of targets
+add_custom_target (ast      # Possible options are: dump|list|print; example usage: opt=dump make ast
+    COMMAND ${BINDIR}clang-check -p ${CMAKE_BINARY_DIR} -ast-$$opt ${API_SOURCES}
+    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/Source/Urho3D
+    COMMENT "Executing clang-check on AngelScript API bindings source files")
 add_custom_target (annotate
-    COMMAND ${CMAKE_BINARY_DIR}/bin/tool/Annotator -ast-dump -ast-dump-filter=Urho3D -p ${CMAKE_BINARY_DIR} ${SOURCES} >NULL_DEVICE
-    DEPENDS Annotator
+    COMMAND ${CMAKE_BINARY_DIR}/bin/tool/ScriptBindingExtractor -p ${CMAKE_BINARY_DIR} ${API_SOURCES} >${CMAKE_BINARY_DIR}/bindings.json
+    COMMAND ${CMAKE_BINARY_DIR}/bin/tool/Annotator -p ${CMAKE_BINARY_DIR} -b ${CMAKE_BINARY_DIR}/bindings.json ${SOURCES}
+    DEPENDS ScriptBindingExtractor Annotator
     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/Source/Urho3D
     COMMENT "Annotating Urho3D library source files")

+ 33 - 0
Source/Clang-Tools/ScriptBindingExtractor/CMakeLists.txt

@@ -0,0 +1,33 @@
+#
+# Copyright (c) 2008-2015 the Urho3D project.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+#
+
+# Define target name
+set (TARGET_NAME ScriptBindingExtractor)
+
+# Define source files
+define_source_files ()
+
+# Setup target
+if (APPLE)
+    setup_macosx_linker_flags (CMAKE_EXE_LINKER_FLAGS)
+endif ()
+setup_executable ()

+ 65 - 0
Source/Clang-Tools/ScriptBindingExtractor/ScriptBindingExtractor.cpp

@@ -0,0 +1,65 @@
+//
+// Copyright (c) 2008-2015 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include <clang/Driver/Options.h>
+#include <clang/Tooling/CommonOptionsParser.h>
+#include <clang/Tooling/Tooling.h>
+
+using namespace clang;
+using namespace clang::driver;
+using namespace clang::tooling;
+using namespace llvm;
+
+static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
+static cl::extrahelp MoreHelp(
+    "\tFor example, to run ScriptBindingExtractor on all files in a subtree of the\n"
+    "\tsource tree, use:\n"
+    "\n"
+    "\t  find path/in/substree -name '*.cpp'|xargs ScriptBindingExtractor -p build/path\n"
+    "\n"
+    "\tNote, that path/in/subtree and current directory should follow the\n"
+    "\trules described above.\n"
+    "\n"
+);
+
+static cl::OptionCategory ScriptAPIExtractorCategory("ScriptBindingExtractor options");
+static std::unique_ptr<opt::OptTable> Options(createDriverOptTable());
+
+class ExtractASTConsumer : public ASTConsumer
+{
+};
+
+class ExtractFrontendAction : public ASTFrontendAction
+{
+protected:
+    virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance& CI, StringRef InFile)
+    {
+        return make_unique<ExtractASTConsumer>();
+    }
+};
+
+int main(int argc, const char** argv)
+{
+    CommonOptionsParser OptionsParser(argc, argv, ScriptAPIExtractorCategory);
+    ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());
+    return Tool.run(newFrontendActionFactory<ExtractFrontendAction>().get());
+}