Skip to content

Commit

Permalink
Auto-generate well_known_types_embed.cc
Browse files Browse the repository at this point in the history
Until now this file was just checked into the repo, but actually it
should be generated from any.js, struct.js, and timestamp.js. This
change updates the build system to make this happen. To make it work I
also had to remove some C++11 features from embed.cc.
  • Loading branch information
acozzette committed Dec 19, 2016
1 parent f39cf88 commit 1b3a0c1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 325 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -47,6 +47,7 @@ any_test.pb.*
map*unittest.pb.*
unittest*.pb.*
cpp_test*.pb.*
src/google/protobuf/compiler/js/well_known_types_embed.cc
src/google/protobuf/util/**/*.pb.cc
src/google/protobuf/util/**/*.pb.h

Expand All @@ -60,6 +61,7 @@ python/build/
python/google/protobuf/compiler/
python/google/protobuf/util/

src/js_embed
src/protoc
src/unittest_proto_middleman

Expand Down
12 changes: 12 additions & 0 deletions cmake/libprotoc.cmake
Expand Up @@ -94,6 +94,18 @@ set(libprotoc_files
${protobuf_source_dir}/src/google/protobuf/compiler/zip_writer.cc
)

set(js_well_known_types_sources,
${protobuf_source_dir}/src/google/protobuf/compiler/js/well_known_types/any.js
${protobuf_source_dir}/src/google/protobuf/compiler/js/well_known_types/struct.js
${protobuf_source_dir}/src/google/protobuf/compiler/js/well_known_types/timestamp.js
)
add_executable(js_embed ${protobuf_source_dir}/src/google/protobuf/compiler/js/embed.cc)
add_custom_command(
OUTPUT ${protobuf_source_dir}/src/google/protobuf/compiler/js/well_known_types_embed.cc
DEPENDS js_embed ${js_well_known_types_sources}
COMMAND js_embed ${js_well_known_types_sources} > ${protobuf_source_dir}/src/google/protobuf/compiler/js/well_known_types_embed.cc
)

add_library(libprotoc ${protobuf_SHARED_OR_STATIC}
${libprotoc_files})
target_link_libraries(libprotoc libprotobuf)
Expand Down
14 changes: 12 additions & 2 deletions src/Makefile.am
Expand Up @@ -483,10 +483,20 @@ libprotoc_la_SOURCES = \
google/protobuf/compiler/csharp/csharp_wrapper_field.cc \
google/protobuf/compiler/csharp/csharp_wrapper_field.h

bin_PROGRAMS = protoc
bin_PROGRAMS = protoc js_embed
protoc_LDADD = $(PTHREAD_LIBS) libprotobuf.la libprotoc.la
protoc_SOURCES = google/protobuf/compiler/main.cc

# The special JS code for the well-known types is linked into the compiler via
# well_known_types_embed.cc, which is generated from .js source files.
js_embed_SOURCES = google/protobuf/compiler/js/embed.cc
js_well_known_types_sources = \
google/protobuf/compiler/js/well_known_types/any.js \
google/protobuf/compiler/js/well_known_types/struct.js \
google/protobuf/compiler/js/well_known_types/timestamp.js
google/protobuf/compiler/js/well_known_types_embed.cc: js_embed$(EXEEXT) $(js_well_known_types_sources)
./js_embed$(EXEEXT) $(js_well_known_types_sources) > $@

# Tests ==============================================================

protoc_inputs = \
Expand Down Expand Up @@ -661,7 +671,7 @@ protoc_outputs = \
google/protobuf/util/message_differencer_unittest.pb.cc \
google/protobuf/util/message_differencer_unittest.pb.h

BUILT_SOURCES = $(protoc_outputs)
BUILT_SOURCES = $(protoc_outputs) google/protobuf/compiler/js/well_known_types_embed.cc

if USE_EXTERNAL_PROTOC

Expand Down
17 changes: 8 additions & 9 deletions src/google/protobuf/compiler/js/embed.cc
Expand Up @@ -48,7 +48,8 @@ static char ToDecimalDigit(int num) {
static std::string CEscape(const std::string& str) {
std::string dest;

for (unsigned char ch : str) {
for (int i = 0; i < str.size(); ++i) {
unsigned char ch = str[i];
switch (ch) {
case '\n': dest += "\\n"; break;
case '\r': dest += "\\r"; break;
Expand Down Expand Up @@ -96,18 +97,16 @@ static void AddFile(const char* name, std::basic_ostream<char>* out) {
}

int main(int argc, char *argv[]) {
auto& out = std::cout;

out << "#include "
"\"google/protobuf/compiler/js/well_known_types_embed.h\"\n";
out << "struct FileToc well_known_types_js[] = {\n";
std::cout << "#include "
"\"google/protobuf/compiler/js/well_known_types_embed.h\"\n";
std::cout << "struct FileToc well_known_types_js[] = {\n";

for (int i = 1; i < argc; i++) {
AddFile(argv[i], &out);
AddFile(argv[i], &std::cout);
}

out << " {NULL, NULL} // Terminate the list.\n";
out << "};\n";
std::cout << " {NULL, NULL} // Terminate the list.\n";
std::cout << "};\n";

return EXIT_SUCCESS;
}

0 comments on commit 1b3a0c1

Please sign in to comment.