Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C++ API. #77

Merged
merged 1 commit into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
jsonnet
_jsonnet.so
libjsonnet.so
libjsonnet++.so
libjsonnet.js
libjsonnet_test_file
libjsonnet_test_snippet
Expand Down
20 changes: 14 additions & 6 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ filegroup(
genrule(
name = "gen-std-jsonnet-h",
srcs = ["stdlib/std.jsonnet"],
outs = ["core/std.jsonnet.h"],
outs = ["std.jsonnet.h"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unrelated to this PR :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting an Bazel build error about being unable to find the generated header file:

❯❯❯ bazel build //:jsonnet-common
INFO: Found 1 target...
ERROR: /Volumes/Ocean/Projects/google/jsonnet/BUILD:17:1: undeclared inclusion(s) in rule '//:jsonnet-common':
this rule is missing dependency declarations for the following files included by 'core/desugarer.cpp':
  'bazel-out/local_darwin-fastbuild/genfiles/std.jsonnet.h'.
Target //:jsonnet-common failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.457s, Critical Path: 1.29s

This might be a bug in the C/C++ rules related to using the includes attribute while looking for a header file in the genfiles tree. Changing the genrule to output the file in the current package rather than core/ worked.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok as long as it works, no need for it to be the same location as the makefile for example.

cmd = "((od -v -Anone -t u1 $< | tr \" \" \"\n\" | grep -v \"^$$\" " +
"| tr \"\n\" \",\" ) && echo \"0\") > $@; " +
"echo >> $@",
Expand All @@ -19,12 +19,13 @@ cc_library(
srcs = [
"core/desugarer.cpp",
"core/formatter.cpp",
"core/libjsonnet.cpp",
"core/lexer.cpp",
"core/parser.cpp",
"core/static_analysis.cpp",
"core/string_utils.cpp",
"core/vm.cpp",
"core/std.jsonnet.h",
"std.jsonnet.h",
],
hdrs = [
"core/ast.h",
Expand All @@ -38,17 +39,24 @@ cc_library(
"core/string_utils.h",
"core/unicode.h",
"core/vm.h",
"include/libjsonnet.h",
],
deps = [
"//include:libjsonnet",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies that I haven't RTFM but how does this work? Does it take the libjsonnet cc_library rule and use the
only the "includes" param from it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...nevermind I just found include/BUILD

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. Yeah, //include:libjsonnet refers to the :libjsonnet target in include/.

],
linkopts = ["-lm"],
includes = ["core", "include"],
includes = [
"core",
"include",
],
)

cc_library(
name = "libjsonnet",
srcs = ["core/libjsonnet.cpp"],
hdrs = ["include/libjsonnet.h"],
deps = [":jsonnet-common"],
deps = [
":jsonnet-common",
"//include:libjsonnet",
],
includes = ["include"],
)

Expand Down
21 changes: 19 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,24 @@ LIB_SRC = \
core/static_analysis.cpp \
core/string_utils.cpp \
core/vm.cpp

LIB_OBJ = $(LIB_SRC:.cpp=.o)

LIB_CPP_SRC = \
cpp/libjsonnet++.cc

LIB_CPP_OBJ = $(LIB_OBJ) $(LIB_CPP_SRC:.cc=.o)

ALL = \
jsonnet \
libjsonnet.so \
libjsonnet++.so \
libjsonnet_test_snippet \
libjsonnet_test_file \
libjsonnet.js \
doc/js/libjsonnet.js \
$(LIB_OBJ)

ALL_HEADERS = \
core/ast.h \
core/desugarer.h \
Expand All @@ -72,7 +80,8 @@ ALL_HEADERS = \
core/string_utils.h \
core/vm.h \
core/std.jsonnet.h \
include/libjsonnet.h
include/libjsonnet.h \
include/libjsonnet++.h

default: jsonnet

Expand All @@ -91,7 +100,9 @@ test: jsonnet libjsonnet.so libjsonnet_test_snippet libjsonnet_test_file
MAKEDEPEND_SRCS = \
cmd/jsonnet.cpp \
core/libjsonnet_test_snippet.c \
core/libjsonnet_test_file.c
core/libjsonnet_test_file.c \
cpp/libjsonnet_cpp_test_snippet.c \
cpp/libjsonnet_cpp_test_file.c

depend:
makedepend -f- $(LIB_SRC) $(MAKEDEPEND_SRCS) > Makefile.depend
Expand All @@ -102,6 +113,9 @@ core/desugarer.cpp: core/std.jsonnet.h
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $< -o $@

%.o: %.cc
$(CXX) -c $(CXXFLAGS) $< -o $@

# Commandline executable.
jsonnet: cmd/jsonnet.cpp $(LIB_OBJ)
$(CXX) $(CXXFLAGS) $(LDFLAGS) $< $(LIB_SRC:.cpp=.o) -o $@
Expand All @@ -110,6 +124,9 @@ jsonnet: cmd/jsonnet.cpp $(LIB_OBJ)
libjsonnet.so: $(LIB_OBJ)
$(CXX) $(LDFLAGS) $(LIB_OBJ) $(SHARED_LDFLAGS) -o $@

libjsonnet++.so: $(LIB_CPP_OBJ)
$(CXX) $(LDFLAGS) $(LIB_CPP_OBJ) $(SHARED_LDFLAGS) -o $@

# Javascript build of C binding
JS_EXPORTED_FUNCTIONS = 'EXPORTED_FUNCTIONS=["_jsonnet_make", "_jsonnet_evaluate_snippet", "_jsonnet_realloc", "_jsonnet_destroy"]'

Expand Down
16 changes: 16 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
new_http_archive(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's all this? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This downloads the gmock archive so that we can use it for the unit tests. This is similar to CMake's ExternalProject mechanism but friendlier. :)

Here's the documentation for new_http_archive.

name = "gmock_archive",
url = "https://googlemock.googlecode.com/files/gmock-1.7.0.zip",
sha256 = "26fcbb5925b74ad5fc8c26b0495dfc96353f4d553492eb97e85a8a6d2f43095b",
build_file = "gmock.BUILD",
)

bind(
name = "gtest",
actual = "@gmock_archive//:gtest",
)

bind(
name = "gtest_main",
actual = "@gmock_archive//:gtest_main",
)
21 changes: 21 additions & 0 deletions cpp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "libjsonnet++",
srcs = ["libjsonnet++.cc"],
deps = [
"//:libjsonnet",
"//include:libjsonnet++",
],
includes = ["include"],
)

cc_test(
name = "libjsonnet++_test",
srcs = ["libjsonnet++_test.cc"],
data = ["//cpp/testdata"],
deps = [
":libjsonnet++",
"//external:gtest_main",
],
)
157 changes: 157 additions & 0 deletions cpp/libjsonnet++.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Copyright 2015 Google Inc. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this make sense as a header only library?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? Are you referring to the cc_library rules under include/? The problem is that since the target for cpp/libjsonnet++.cc are in cpp/BUILD, directly adding ../include/libjsonnet++.h would cross package boundaries, and Bazel would complain. The way to include headers in a separate package is to add a cc_library rule for the header and then add that rule to deps.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed it did not include many new headers, so if it (libjsonnet++) were a header only library, it would not need a new build target and would be much simpler. No need to do it in this PR but it's worth considering.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. Makes sense. Also, if we end up refactoring libjsonnet.cpp and exposing both a C and C++ APIs, then that will essentially be the case.

//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "libjsonnet++.h"

namespace jsonnet {

Jsonnet::Jsonnet() {}
Jsonnet::~Jsonnet() {
if (vm_ != nullptr) {
::jsonnet_destroy(vm_);
}
}

/* static */
std::string Jsonnet::Version() {
return ::jsonnet_version();
}

bool Jsonnet::Init() {
vm_ = static_cast<struct JsonnetVm*>(::jsonnet_make());
return vm_ != nullptr;
}

void Jsonnet::SetMaxStack(uint32_t depth) {
::jsonnet_max_stack(vm_, static_cast<unsigned>(depth));
}

void Jsonnet::SetGcMinObjects(uint32_t objects) {
::jsonnet_gc_min_objects(vm_, static_cast<unsigned>(objects));
}

void Jsonnet::SetGcGrowthTrigger(double growth) {
::jsonnet_gc_growth_trigger(vm_, growth);
}

void Jsonnet::SetStringOutput(bool string_output) {
::jsonnet_string_output(vm_, string_output);
}

void Jsonnet::AddImportPath(const std::string& path) {
::jsonnet_jpath_add(vm_, path.c_str());
}

void Jsonnet::SetMaxTrace(uint32_t lines) {
::jsonnet_max_trace(vm_, static_cast<unsigned>(lines));
}

void Jsonnet::BindExtVar(const std::string& key, const std::string& value) {
::jsonnet_ext_var(vm_, key.c_str(), value.c_str());
}

void Jsonnet::BindExtCodeVar(const std::string& key,
const std::string& value) {
::jsonnet_ext_code(vm_, key.c_str(), value.c_str());
}

bool Jsonnet::EvaluateFile(const std::string& filename, std::string* output) {
if (output == nullptr) {
return false;
}
int error = 0;
const char* jsonnet_output =
::jsonnet_evaluate_file(vm_, filename.c_str(), &error);
if (error != 0) {
last_error_.assign(jsonnet_output);
return false;
}
output->assign(jsonnet_output);
return true;
}

bool Jsonnet::EvaluateSnippet(const std::string& filename,
const std::string& snippet,
std::string* output) {
if (output == nullptr) {
return false;
}
int error = 0;
const char* jsonnet_output = ::jsonnet_evaluate_snippet(
vm_, filename.c_str(), snippet.c_str(), &error);
if (error != 0) {
last_error_.assign(jsonnet_output);
return false;
}
output->assign(jsonnet_output);
return true;
}

namespace {
void ParseMultiOutput(const char* jsonnet_output,
std::map<std::string, std::string>* outputs) {
for (const char* c = jsonnet_output; *c != '\0'; ) {
const char *filename = c;
const char *c2 = c;
while (*c2 != '\0') ++c2;
++c2;
const char *json = c2;
while (*c2 != '\0') ++c2;
++c2;
c = c2;
outputs->insert(std::make_pair(filename, json));
}
}
} // namespace

bool Jsonnet::EvaluateFileMulti(
const std::string& filename,
std::map<std::string, std::string>* outputs) {
if (outputs == nullptr) {
return false;
}
int error = 0;
const char* jsonnet_output =
::jsonnet_evaluate_file_multi(vm_, filename.c_str(), &error);
if (error != 0) {
last_error_.assign(jsonnet_output);
return false;
}
ParseMultiOutput(jsonnet_output, outputs);
return true;
}

bool Jsonnet::EvaluateSnippetMulti(
const std::string& filename,
const std::string& snippet,
std::map<std::string, std::string>* outputs) {
if (outputs == nullptr) {
return false;
}
int error = 0;
const char* jsonnet_output = ::jsonnet_evaluate_snippet_multi(
vm_, filename.c_str(), snippet.c_str(), &error);
if (error != 0) {
last_error_.assign(jsonnet_output);
return false;
}
ParseMultiOutput(jsonnet_output, outputs);
return true;
}

std::string Jsonnet::LastError() const {
return last_error_;
}

} // namespace jsonnet
Loading