From 9e1495a935629b7c0646587d3a4e034f1d03429a Mon Sep 17 00:00:00 2001 From: "David Z. Chen" Date: Sat, 12 Sep 2015 04:10:01 -0700 Subject: [PATCH] Restructure source tree. * Move all C++ sources for core library into core/ * Move source for Jsonnet command line too into cmd/ * Move sources for Python bindings into python/ --- .gitignore | 1 - BUILD | 38 ++++----- MANIFEST.in | 2 +- Makefile | 82 ++++++++++++++----- jsonnet.cpp => cmd/jsonnet.cpp | 2 +- ast.h => core/ast.h | 4 +- lexer.cpp => core/lexer.cpp | 4 +- lexer.h => core/lexer.h | 4 +- libjsonnet.cpp => core/libjsonnet.cpp | 8 +- libjsonnet.h => core/libjsonnet.h | 2 +- libjsonnet_test.sh => core/libjsonnet_test.sh | 0 .../libjsonnet_test_file.c | 2 +- .../libjsonnet_test_snippet.c | 2 +- parser.cpp => core/parser.cpp | 10 +-- parser.h => core/parser.h | 6 +- state.h => core/state.h | 2 +- .../static_analysis.cpp | 7 +- static_analysis.h => core/static_analysis.h | 2 +- static_error.h => core/static_error.h | 2 +- vm.cpp => core/vm.cpp | 9 +- vm.h => core/vm.h | 4 +- _jsonnet.c => python/_jsonnet.c | 0 .../jsonnet_test_file.py | 0 .../jsonnet_test_snippet.py | 0 setup.py | 16 ++-- std.jsonnet => stdlib/std.jsonnet | 0 test_suite/format.jsonnet | 2 +- 27 files changed, 124 insertions(+), 87 deletions(-) rename jsonnet.cpp => cmd/jsonnet.cpp (99%) rename ast.h => core/ast.h (99%) rename lexer.cpp => core/lexer.cpp (99%) rename lexer.h => core/lexer.h (98%) rename libjsonnet.cpp => core/libjsonnet.cpp (98%) rename libjsonnet.h => core/libjsonnet.h (99%) rename libjsonnet_test.sh => core/libjsonnet_test.sh (100%) rename libjsonnet_test_file.c => core/libjsonnet_test_file.c (97%) rename libjsonnet_test_snippet.c => core/libjsonnet_test_snippet.c (97%) rename parser.cpp => core/parser.cpp (99%) rename parser.h => core/parser.h (95%) rename state.h => core/state.h (99%) rename static_analysis.cpp => core/static_analysis.cpp (98%) rename static_analysis.h => core/static_analysis.h (97%) rename static_error.h => core/static_error.h (99%) rename vm.cpp => core/vm.cpp (99%) rename vm.h => core/vm.h (98%) rename _jsonnet.c => python/_jsonnet.c (100%) rename jsonnet_test_file.py => python/jsonnet_test_file.py (100%) rename jsonnet_test_snippet.py => python/jsonnet_test_snippet.py (100%) rename std.jsonnet => stdlib/std.jsonnet (100%) diff --git a/.gitignore b/.gitignore index 10c988437..0284cd058 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,6 @@ libjsonnet.so /libjsonnet.js libjsonnet_test_file libjsonnet_test_snippet -core core.* vgcore vgcore.* diff --git a/BUILD b/BUILD index d4320f433..86b12a856 100644 --- a/BUILD +++ b/BUILD @@ -2,13 +2,13 @@ package(default_visibility = ["//visibility:public"]) filegroup( name = "std", - srcs = ["std.jsonnet"], + srcs = ["stdlib/std.jsonnet"], ) genrule( name = "gen-std-jsonnet-h", - srcs = ["std.jsonnet"], - outs = ["std.jsonnet.h"], + srcs = ["stdlib/std.jsonnet"], + outs = ["stdlib/std.jsonnet.h"], cmd = "((od -v -Anone -t u1 $< | tr \" \" \"\n\" | grep -v \"^$$\" " + "| tr \"\n\" \",\" ) && echo \"0\") > $@; " + "echo >> $@", @@ -19,47 +19,47 @@ genrule( cc_library( name = "jsonnet-common", srcs = [ - "lexer.cpp", - "parser.cpp", - "static_analysis.cpp", - "vm.cpp", - "std.jsonnet.h", + "core/lexer.cpp", + "core/parser.cpp", + "core/static_analysis.cpp", + "core/vm.cpp", + "stdlib/std.jsonnet.h", ], hdrs = [ - "lexer.h", - "parser.h", - "static_analysis.h", - "static_error.h", - "vm.h", + "core/lexer.h", + "core/parser.h", + "core/static_analysis.h", + "core/static_error.h", + "core/vm.h", ], includes = ["."], ) cc_library( name = "libjsonnet", - srcs = ["libjsonnet.cpp"], - hdrs = ["libjsonnet.h"], + srcs = ["core/libjsonnet.cpp"], + hdrs = ["core/libjsonnet.h"], deps = [":jsonnet-common"], includes = ["."], ) cc_binary( name = "jsonnet", - srcs = ["jsonnet.cpp"], + srcs = ["cmd/jsonnet.cpp"], deps = [":libjsonnet"], includes = ["."], ) cc_binary( name = "libjsonnet_test_snippet", - srcs = ["libjsonnet_test_snippet.c"], + srcs = ["core/libjsonnet_test_snippet.c"], deps = [":libjsonnet"], includes = ["."], ) cc_binary( name = "libjsonnet_test_file", - srcs = ["libjsonnet_test_file.c"], + srcs = ["core/libjsonnet_test_file.c"], deps = [":libjsonnet"], includes = ["."], ) @@ -71,7 +71,7 @@ filegroup( sh_test( name = "libjsonnet_test", - srcs = ["libjsonnet_test.sh"], + srcs = ["core/libjsonnet_test.sh"], data = [ ":jsonnet", ":libjsonnet_test_snippet", diff --git a/MANIFEST.in b/MANIFEST.in index dad7cf59f..6798e0165 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,2 @@ -include LICENSE libjsonnet.h libjsonnet.cpp _jsonnet.c lexer.h lexer.cpp ast.h parser.h parser.cpp static_error.h static_analysis.h static_analysis.cpp state.h vm.h vm.cpp std.jsonnet Makefile +include LICENSE core/*.cpp core/*.h python/*.c stdlib/std.jsonnet Makefile #recursive-include test_suite examples gc_stress benchmarks editors diff --git a/Makefile b/Makefile index 9afa5fbf5..c52babf9a 100644 --- a/Makefile +++ b/Makefile @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -################################################################################################### +################################################################################ # User-servicable parts: -################################################################################################### +################################################################################ # C/C++ compiler -- clang also works CXX ?= g++ @@ -29,23 +29,44 @@ OD ?= od OPT ?= -O3 -CXXFLAGS ?= -g $(OPT) -Wall -Wextra -pedantic -std=c++0x -fPIC -CFLAGS ?= -g $(OPT) -Wall -Wextra -pedantic -std=c99 -fPIC +CXXFLAGS ?= -g $(OPT) -Wall -Wextra -pedantic -std=c++0x -fPIC -I. +CFLAGS ?= -g $(OPT) -Wall -Wextra -pedantic -std=c99 -fPIC -I. EMCXXFLAGS = $(CXXFLAGS) --memory-init-file 0 -s DISABLE_EXCEPTION_CATCHING=0 EMCFLAGS = $(CFLAGS) --memory-init-file 0 -s DISABLE_EXCEPTION_CATCHING=0 LDFLAGS ?= SHARED_LDFLAGS ?= -shared -################################################################################################### +################################################################################ # End of user-servicable parts -################################################################################################### - -LIB_SRC = lexer.cpp parser.cpp static_analysis.cpp vm.cpp libjsonnet.cpp +################################################################################ + +LIB_SRC = \ + core/lexer.cpp \ + core/parser.cpp \ + core/static_analysis.cpp \ + core/vm.cpp \ + core/libjsonnet.cpp LIB_OBJ = $(LIB_SRC:.cpp=.o) -ALL = jsonnet libjsonnet.so libjsonnet_test_snippet libjsonnet_test_file libjsonnet.js doc/libjsonnet.js $(LIB_OBJ) -ALL_HEADERS = libjsonnet.h vm.h static_analysis.h parser.h lexer.h ast.h static_error.h state.h std.jsonnet.h +ALL = \ + jsonnet \ + libjsonnet.so \ + libjsonnet_test_snippet \ + libjsonnet_test_file \ + libjsonnet.js \ + doc/libjsonnet.js \ + $(LIB_OBJ) +ALL_HEADERS = \ + core/libjsonnet.h \ + core/vm.h \ + core/static_analysis.h \ + core/parser.h \ + core/lexer.h \ + core/ast.h \ + core/static_error.h \ + core/state.h \ + stdlib/std.jsonnet.h default: jsonnet @@ -60,17 +81,22 @@ test: jsonnet libjsonnet.so libjsonnet_test_snippet libjsonnet_test_file cd examples/terraform ; ./check.sh cd test_suite ; ./run_tests.sh +MAKEDEPEND_SRCS = \ + cmd/jsonnet.cpp \ + core/libjsonnet_test_snippet.c \ + core/libjsonnet_test_file.c + depend: - makedepend -f- $(LIB_SRC) jsonnet.cpp libjsonnet_test_snippet.c libjsonnet_test_file.c > Makefile.depend + makedepend -f- $(LIB_SRC) $(MAKEDEPEND_SRCS) > Makefile.depend -parser.cpp: std.jsonnet.h +core/parser.cpp: stdlib/std.jsonnet.h # Object files %.o: %.cpp $(CXX) -c $(CXXFLAGS) $< -o $@ # Commandline executable. -jsonnet: jsonnet.cpp $(LIB_OBJ) +jsonnet: cmd/jsonnet.cpp $(LIB_OBJ) $(CXX) $(CXXFLAGS) $(LDFLAGS) $< $(LIB_SRC:.cpp=.o) -o $@ # C binding. @@ -78,29 +104,41 @@ libjsonnet.so: $(LIB_OBJ) $(CXX) $(LDFLAGS) $(LIB_OBJ) $(SHARED_LDFLAGS) -o $@ # Javascript build of C binding +JS_EXPORTED_FUNCTIONS = 'EXPORTED_FUNCTIONS=["_jsonnet_make", "_jsonnet_evaluate_snippet", "_jsonnet_realloc", "_jsonnet_destroy"]' + libjsonnet.js: $(LIB_SRC) $(ALL_HEADERS) - $(EMCXX) -s 'EXPORTED_FUNCTIONS=["_jsonnet_make", "_jsonnet_evaluate_snippet", "_jsonnet_realloc", "_jsonnet_destroy"]' $(EMCXXFLAGS) $(LDFLAGS) $(LIB_SRC) -o $@ + $(EMCXX) -s $(JS_EXPORTED_FUNCTIONS) $(EMCXXFLAGS) $(LDFLAGS) $(LIB_SRC) -o $@ # Copy javascript build to doc directory doc/libjsonnet.js: libjsonnet.js $(CP) $^ $@ # Tests for C binding. -libjsonnet_test_snippet: libjsonnet_test_snippet.c libjsonnet.so libjsonnet.h +LIBJSONNET_TEST_SNIPPET_SRCS = \ + core/libjsonnet_test_snippet.c \ + libjsonnet.so \ + core/libjsonnet.h + +libjsonnet_test_snippet: $(LIBJSONNET_TEST_SNIPPET_SRCS) $(CC) $(CFLAGS) $(LDFLAGS) $< -L. -ljsonnet -o $@ -libjsonnet_test_file: libjsonnet_test_file.c libjsonnet.so libjsonnet.h +LIBJSONNET_TEST_FILE_SRCS = \ + core/libjsonnet_test_file.c \ + libjsonnet.so \ + core/libjsonnet.h + +libjsonnet_test_file: $(LIBJSONNET_TEST_FILE_SRCS) $(CC) $(CFLAGS) $(LDFLAGS) $< -L. -ljsonnet -o $@ # Encode standard library for embedding in C -%.jsonnet.h: %.jsonnet - (($(OD) -v -Anone -t u1 $< | tr " " "\n" | grep -v "^$$" | tr "\n" "," ) && echo "0") > $@ +stdlib/%.jsonnet.h: stdlib/%.jsonnet + (($(OD) -v -Anone -t u1 $< \ + | tr " " "\n" \ + | grep -v "^$$" \ + | tr "\n" "," ) && echo "0") > $@ echo >> $@ clean: - rm -vf */*~ *~ .*~ */.*.swp .*.swp $(ALL) *.o *.jsonnet.h + rm -vf */*~ *~ .*~ */.*.swp .*.swp $(ALL) *.o stdlib/*.jsonnet.h -include Makefile.depend - - - diff --git a/jsonnet.cpp b/cmd/jsonnet.cpp similarity index 99% rename from jsonnet.cpp rename to cmd/jsonnet.cpp index 19cb8ba5b..b597cdb0b 100644 --- a/jsonnet.cpp +++ b/cmd/jsonnet.cpp @@ -26,7 +26,7 @@ limitations under the License. #include extern "C" { - #include "libjsonnet.h" + #include "core/libjsonnet.h" } struct ImportCallbackContext { diff --git a/ast.h b/core/ast.h similarity index 99% rename from ast.h rename to core/ast.h index 11d95169b..d530a0837 100644 --- a/ast.h +++ b/core/ast.h @@ -24,7 +24,7 @@ limitations under the License. #include #include -#include "lexer.h" +#include "core/lexer.h" enum ASTType { AST_APPLY, @@ -409,4 +409,4 @@ class Allocator { } }; -#endif +#endif // JSONNET_AST_H diff --git a/lexer.cpp b/core/lexer.cpp similarity index 99% rename from lexer.cpp rename to core/lexer.cpp index 5f3ca4078..f368ad5dc 100644 --- a/lexer.cpp +++ b/core/lexer.cpp @@ -19,8 +19,8 @@ limitations under the License. #include #include -#include "static_error.h" -#include "lexer.h" +#include "core/static_error.h" +#include "core/lexer.h" static bool is_upper(char c) { diff --git a/lexer.h b/core/lexer.h similarity index 98% rename from lexer.h rename to core/lexer.h index 046ff1313..981d83429 100644 --- a/lexer.h +++ b/core/lexer.h @@ -23,7 +23,7 @@ limitations under the License. #include #include -#include "static_error.h" +#include "core/static_error.h" struct Token { enum Kind { @@ -153,4 +153,4 @@ static inline std::ostream &operator<<(std::ostream &o, const Token &v) std::list jsonnet_lex(const std::string &filename, const char *input); -#endif +#endif // JSONNET_LEXER_H diff --git a/libjsonnet.cpp b/core/libjsonnet.cpp similarity index 98% rename from libjsonnet.cpp rename to core/libjsonnet.cpp index d6458958a..0f8d3e9fd 100644 --- a/libjsonnet.cpp +++ b/core/libjsonnet.cpp @@ -24,12 +24,12 @@ limitations under the License. #include extern "C" { - #include "libjsonnet.h" +#include "core/libjsonnet.h" } -#include "parser.h" -#include "static_analysis.h" -#include "vm.h" +#include "core/parser.h" +#include "core/static_analysis.h" +#include "core/vm.h" static void memory_panic(void) { diff --git a/libjsonnet.h b/core/libjsonnet.h similarity index 99% rename from libjsonnet.h rename to core/libjsonnet.h index b7dbf5d2e..d9a73544b 100644 --- a/libjsonnet.h +++ b/core/libjsonnet.h @@ -154,4 +154,4 @@ char *jsonnet_evaluate_snippet_multi(struct JsonnetVm *vm, /** Complement of \see jsonnet_vm_make. */ void jsonnet_destroy(struct JsonnetVm *vm); -#endif +#endif // LIB_JSONNET_H diff --git a/libjsonnet_test.sh b/core/libjsonnet_test.sh similarity index 100% rename from libjsonnet_test.sh rename to core/libjsonnet_test.sh diff --git a/libjsonnet_test_file.c b/core/libjsonnet_test_file.c similarity index 97% rename from libjsonnet_test_file.c rename to core/libjsonnet_test_file.c index 7c2d2864e..49904f190 100644 --- a/libjsonnet_test_file.c +++ b/core/libjsonnet_test_file.c @@ -17,7 +17,7 @@ limitations under the License. #include #include -#include "libjsonnet.h" +#include "core/libjsonnet.h" int main(int argc, const char **argv) { diff --git a/libjsonnet_test_snippet.c b/core/libjsonnet_test_snippet.c similarity index 97% rename from libjsonnet_test_snippet.c rename to core/libjsonnet_test_snippet.c index 0a456c50e..b96e30d40 100644 --- a/libjsonnet_test_snippet.c +++ b/core/libjsonnet_test_snippet.c @@ -17,7 +17,7 @@ limitations under the License. #include #include -#include "libjsonnet.h" +#include "core/libjsonnet.h" int main(int argc, const char **argv) { diff --git a/parser.cpp b/core/parser.cpp similarity index 99% rename from parser.cpp rename to core/parser.cpp index 439edec41..2975a486a 100644 --- a/parser.cpp +++ b/core/parser.cpp @@ -25,10 +25,10 @@ limitations under the License. #include -#include "static_error.h" -#include "ast.h" -#include "parser.h" -#include "lexer.h" +#include "core/static_error.h" +#include "core/ast.h" +#include "core/parser.h" +#include "core/lexer.h" // For generated ASTs, use a bogus location. @@ -1078,7 +1078,7 @@ static AST *do_parse(Allocator *alloc, const std::string &file, const char *inpu } static constexpr char STD_CODE[] = { - #include "std.jsonnet.h" + #include "stdlib/std.jsonnet.h" }; AST *jsonnet_parse(Allocator *alloc, const std::string &file, const char *input) diff --git a/parser.h b/core/parser.h similarity index 95% rename from parser.h rename to core/parser.h index 54fc9eeb2..a2d5eace2 100644 --- a/parser.h +++ b/core/parser.h @@ -19,8 +19,8 @@ limitations under the License. #include -#include "lexer.h" -#include "ast.h" +#include "core/lexer.h" +#include "core/ast.h" /** Parse a given JSON++ string. * @@ -53,4 +53,4 @@ BuiltinDecl jsonnet_builtin_decl(unsigned long builtin); */ std::string jsonnet_unparse_jsonnet(const AST *ast); -#endif +#endif // JSONNET_PARSER_H diff --git a/state.h b/core/state.h similarity index 99% rename from state.h rename to core/state.h index b291282bd..eba49a067 100644 --- a/state.h +++ b/core/state.h @@ -445,4 +445,4 @@ namespace { } -#endif +#endif // JSONNET_STATE_H diff --git a/static_analysis.cpp b/core/static_analysis.cpp similarity index 98% rename from static_analysis.cpp rename to core/static_analysis.cpp index d597ff0e7..daa033929 100644 --- a/static_analysis.cpp +++ b/core/static_analysis.cpp @@ -16,10 +16,9 @@ limitations under the License. #include -#include "static_analysis.h" - -#include "static_error.h" -#include "ast.h" +#include "core/static_analysis.h" +#include "core/static_error.h" +#include "core/ast.h" typedef std::set IdSet; diff --git a/static_analysis.h b/core/static_analysis.h similarity index 97% rename from static_analysis.h rename to core/static_analysis.h index 3368d4e9a..c3220d387 100644 --- a/static_analysis.h +++ b/core/static_analysis.h @@ -17,7 +17,7 @@ limitations under the License. #ifndef JSONNET_STATIC_ANALYSIS_H #define JSONNET_STATIC_ANALYSIS_H -#include "ast.h" +#include "core/ast.h" /** Check the ast for appropriate use of self, super, and correctly bound variables. Also * initialize the freeVariables member of function and object ASTs. diff --git a/static_error.h b/core/static_error.h similarity index 99% rename from static_error.h rename to core/static_error.h index ed15e82c5..72a28832d 100644 --- a/static_error.h +++ b/core/static_error.h @@ -102,4 +102,4 @@ static inline std::ostream &operator<<(std::ostream &o, const StaticError &err) return o; } -#endif +#endif // JSONNET_ERROR_H diff --git a/vm.cpp b/core/vm.cpp similarity index 99% rename from vm.cpp rename to core/vm.cpp index f9dde8cb8..6550162cb 100644 --- a/vm.cpp +++ b/core/vm.cpp @@ -14,17 +14,16 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include "vm.h" +#include "core/vm.h" #include #include - #include #include -#include "parser.h" -#include "state.h" -#include "static_analysis.h" +#include "core/parser.h" +#include "core/state.h" +#include "core/static_analysis.h" namespace { diff --git a/vm.h b/core/vm.h similarity index 98% rename from vm.h rename to core/vm.h index e80e09f17..d3502d396 100644 --- a/vm.h +++ b/core/vm.h @@ -17,8 +17,8 @@ limitations under the License. #ifndef JSONNET_VM_H #define JSONNET_VM_H -#include "ast.h" -#include "libjsonnet.h" +#include "core/ast.h" +#include "core/libjsonnet.h" /** A single line of a stack trace from a runtime error. */ diff --git a/_jsonnet.c b/python/_jsonnet.c similarity index 100% rename from _jsonnet.c rename to python/_jsonnet.c diff --git a/jsonnet_test_file.py b/python/jsonnet_test_file.py similarity index 100% rename from jsonnet_test_file.py rename to python/jsonnet_test_file.py diff --git a/jsonnet_test_snippet.py b/python/jsonnet_test_snippet.py similarity index 100% rename from jsonnet_test_snippet.py rename to python/jsonnet_test_snippet.py diff --git a/setup.py b/setup.py index a3be4deb9..17325cd33 100644 --- a/setup.py +++ b/setup.py @@ -18,22 +18,26 @@ from setuptools.command.build_ext import build_ext as BuildExt from subprocess import Popen - DIR = os.path.abspath(os.path.dirname(__file__)) -LIB_OBJECTS = ['libjsonnet.o', 'lexer.o', 'parser.o', 'static_analysis.o', 'vm.o'] -MODULE_SOURCES = ['_jsonnet.c'] +LIB_OBJECTS = [ + 'core/libjsonnet.o', + 'core/lexer.o', + 'core/parser.o', + 'core/static_analysis.o', + 'core/vm.o' +] +MODULE_SOURCES = ['python/_jsonnet.c'] def get_version(): """ Parses the version out of libjsonnet.h """ - with open(os.path.join(DIR, 'libjsonnet.h')) as f: + with open(os.path.join(DIR, 'core/libjsonnet.h')) as f: for line in f: if '#define' in line and 'LIB_JSONNET_VERSION' in line: return line.partition('LIB_JSONNET_VERSION')[2].strip('\n "') - class BuildJsonnetExt(BuildExt): def run(self): p = Popen(['make'] + LIB_OBJECTS, cwd=DIR) @@ -42,7 +46,6 @@ def run(self): raise Exception('Could not build %s' % (', '.join(LIB_OBJECTS))) BuildExt.run(self) - jsonnet_ext = Extension( '_jsonnet', sources=MODULE_SOURCES, @@ -50,7 +53,6 @@ def run(self): language='c++' ) - setup(name='jsonnet', url='https://google.github.io/jsonnet/doc/', description='Python bindings for Jsonnet - The data templating language ', diff --git a/std.jsonnet b/stdlib/std.jsonnet similarity index 100% rename from std.jsonnet rename to stdlib/std.jsonnet diff --git a/test_suite/format.jsonnet b/test_suite/format.jsonnet index 6637b5a0c..30ad27544 100644 --- a/test_suite/format.jsonnet +++ b/test_suite/format.jsonnet @@ -15,7 +15,7 @@ limitations under the License. */ local old_std = std; -local std = old_std + import "../std.jsonnet"; +local std = old_std + import "../stdlib/std.jsonnet"; // # std.assertEqual(std.format("No format chars\n", []), "No format chars\n") &&