thrift: Add -cpp_use_include_prefix flag to compiler
Summary: Adds a new flag to allow for a mode where #include statements in generated c++ will include path context information. For example, if my .thrift file includes "foo/bar/baz.thrift", the generated source files will contain #include statements like:
#include "foo/bar/gen-cpp/baz_types.h"
instead of just:
#include "baz_types.h"
-cpp_use_include_prefix is OFF by default.
Reviewed By: dreiss
Test Plan: Tested against multiple thrift input files both with and without the new flag.
Revert: OK
DiffCamp Revision: 5522
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665431 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/main.cc b/compiler/cpp/src/main.cc
index 131820a..e0eb4b7 100644
--- a/compiler/cpp/src/main.cc
+++ b/compiler/cpp/src/main.cc
@@ -106,6 +106,12 @@
vector<string> g_incl_searchpath;
/**
+ * Should C++ include statements use path prefixes for other thrift-generated
+ * header files
+ */
+bool g_cpp_use_include_prefix = false;
+
+/**
* Global debug state
*/
int g_debug = 0;
@@ -613,6 +619,8 @@
fprintf(stderr, " (default: current directory)\n");
fprintf(stderr, " -I dir Add a directory to the list of directories\n");
fprintf(stderr, " searched for include directives\n");
+ fprintf(stderr, " -cpp_use_include_prefix\n");
+ fprintf(stderr, " Make C++ include statements use path prefixes\n");
fprintf(stderr, " -dense Generate metadata for TDenseProtocol (C++)\n");
fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n");
fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
@@ -832,6 +840,7 @@
if (gen_cpp) {
pverbose("Generating C++\n");
t_cpp_generator* cpp = new t_cpp_generator(program, gen_dense);
+ cpp->set_use_include_prefix(g_cpp_use_include_prefix);
cpp->generate_program();
delete cpp;
}
@@ -933,7 +942,7 @@
csharp->generate_program();
delete csharp;
}
-
+
if (dump_docs) {
dump_docstrings(program);
}
@@ -1034,6 +1043,8 @@
gen_st = true;
} else if (strcmp(arg, "-csharp") == 0) {
gen_csharp = true;
+ } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
+ g_cpp_use_include_prefix = true;
} else if (strcmp(arg, "-I") == 0) {
// An argument of "-I\ asdf" is invalid and has unknown results
arg = argv[++i];
@@ -1097,6 +1108,18 @@
if (out_path.size()) {
program->set_out_path(out_path);
}
+ if (g_cpp_use_include_prefix) {
+ // infer this from the filename passed in
+ string input_filename = argv[i];
+ string include_prefix;
+
+ string::size_type last_slash = string::npos;
+ if ((last_slash = input_filename.rfind("/")) != string::npos) {
+ include_prefix = input_filename.substr(0, last_slash);
+ }
+
+ program->set_include_prefix(include_prefix);
+ }
// Initialize global types
g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);