From 66594db15d1bfcb8a02d3bad841fad1e29f03ecb Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 3 Jun 2014 16:16:41 +0000 Subject: Update tests to perform an end-to-end (albeit non-validating) slice, slicer, compile, link, load and run --- slicer/slicer/parser.cpp | 15 ++++++++---- slicer/slicer/parser.h | 4 ++-- slicer/test/Jamfile.jam | 58 +++++++++++++++++++++++++++++++++++++++++++++- slicer/test/do-slicer.cpp | 52 +++++++++++++++++++++++++++++++++++++++-- slicer/test/helpers.cpp | 32 +++++++++++++++++++++++++ slicer/test/helpers.h | 18 ++++++++++++++ slicer/test/run-slicer.cpp | 34 +++++++++++++++++++++++++++ slicer/test/types.ice | 4 ---- 8 files changed, 203 insertions(+), 14 deletions(-) create mode 100644 slicer/test/helpers.cpp create mode 100644 slicer/test/helpers.h create mode 100644 slicer/test/run-slicer.cpp diff --git a/slicer/slicer/parser.cpp b/slicer/slicer/parser.cpp index a67f483..fd1d9b1 100644 --- a/slicer/slicer/parser.cpp +++ b/slicer/slicer/parser.cpp @@ -5,6 +5,9 @@ #include #include #include +#include + +namespace fs = boost::filesystem; namespace Slicer { Slicer::Slicer(FILE * c) : @@ -12,16 +15,20 @@ namespace Slicer { { } - void - Slicer::leadIn() + bool + Slicer::visitUnitStart(const Slice::UnitPtr & u) { + fs::path topLevelFile(u->topLevelFile()); + fprintf(cpp, "// Begin Slicer code\n\n"); + fprintf(cpp, "#include <%s>\n\n", fs::change_extension(topLevelFile.filename(), ".h").string().c_str()); fprintf(cpp, "#include \n\n"); fprintf(cpp, "namespace Slicer {\n"); + return true; } void - Slicer::leadOut() + Slicer::visitUnitEnd(const Slice::UnitPtr&) { fprintf(cpp, "}\n\n"); fprintf(cpp, "// End Slicer code\n\n"); @@ -258,9 +265,7 @@ namespace Slicer { } Slicer s(cppfile.get()); - s.leadIn(); u->visit(&s, false); - s.leadOut(); u->destroy(); } diff --git a/slicer/slicer/parser.h b/slicer/slicer/parser.h index c660cc6..90aa05a 100644 --- a/slicer/slicer/parser.h +++ b/slicer/slicer/parser.h @@ -14,9 +14,9 @@ namespace Slicer { static void Apply(const boost::filesystem::path & ice, const boost::filesystem::path & cpp); - void leadIn(); + virtual bool visitUnitStart(const Slice::UnitPtr&) override; - void leadOut(); + virtual void visitUnitEnd(const Slice::UnitPtr&) override; virtual bool visitModuleStart(const Slice::ModulePtr & m) override; diff --git a/slicer/test/Jamfile.jam b/slicer/test/Jamfile.jam index b4e05b3..88c981f 100644 --- a/slicer/test/Jamfile.jam +++ b/slicer/test/Jamfile.jam @@ -1,3 +1,59 @@ import testing ; -run do-slicer.cpp ../slicer//slicer : : types.ice ; +lib dl ; +lib pthread ; +lib Ice ; +lib IceUtil ; +lib boost_system ; +lib boost_filesystem ; +lib slicer-test : : slicertypes bin/slicer ; + +lib types : + types.ice + : + pthread + Ice + IceUtil + ; + +lib common : + helpers.cpp + ../../libmisc/misc.cpp + : + dl + boost_system + boost_filesystem + : : + boost_filesystem + boost_system + ; + +unit-test do-slicer : + types + common + do-slicer.cpp + : + dl + .. + pthread + Ice + IceUtil + ../slicer//slicer + types + ; + +unit-test run-slicer : + run-slicer.cpp + types + common + : + types + do-slicer + slicer-test + .. + pthread + Ice + IceUtil + ../slicer//slicer + ../xml//slicer-xml +; diff --git a/slicer/test/do-slicer.cpp b/slicer/test/do-slicer.cpp index 8b03a7d..30c5cfb 100644 --- a/slicer/test/do-slicer.cpp +++ b/slicer/test/do-slicer.cpp @@ -1,12 +1,60 @@ #include #include #include +#include +#include +#include "../../libmisc/misc.h" +#include +#include "helpers.h" + +namespace fs = boost::filesystem; int main(int, char ** argv) { - const boost::filesystem::path slice = argv[1]; - Slicer::Slicer::Apply(slice, "/dev/null"); + const fs::path me = argv[0]; + const fs::path base = "types"; + const fs::path bjamout = me.parent_path(); + const fs::path root = me.parent_path().parent_path().parent_path().parent_path(); + fprintf(stderr, "root -- %s\n", root.string().c_str()); + const fs::path slice = fs::change_extension(root / base, ".ice"); + fprintf(stderr, "slice - %s\n", slice.string().c_str()); + + // Tmp + const fs::path tmp = root / "bin" / "slicer"; + fprintf(stderr, "tmp --- %s\n", tmp.string().c_str()); + fs::create_directory(tmp); + + // Slicer + const fs::path cpp = fs::change_extension(tmp / base, ".cpp"); + fprintf(stderr, "cpp --- %s\n", cpp.string().c_str()); + fs::remove(cpp); + Slicer::Slicer::Apply(slice, cpp); + + // Compile + const fs::path obj = fs::change_extension(tmp / base, ".o"); + fprintf(stderr, "obj --- %s\n", obj.string().c_str()); + system(stringbf( + "g++ -Os -fPIC -c -std=c++0x -I tmp -I /usr/include/Ice -I /usr/include/IceUtil -I %s -I %s %s -o %s", + bjamout, + root / "..", + cpp, obj)); + + // Link + const fs::path so = fs::change_extension(tmp / ("libslicer" + slice.filename().string()), ".so"); + fprintf(stderr, "so ---- %s\n", so.string().c_str()); + system(stringbf( + "g++ -shared -lIce -lIceUtil %s/lib%s.so %s -o %s", + bjamout, base, + obj, so)); + + // Load + fprintf(stderr, "load -- %s\n", so.string().c_str()); + auto handle = loadlib(so); + + // Unload + fprintf(stderr, "unload %p\n", handle); + closelib(handle); return 0; } diff --git a/slicer/test/helpers.cpp b/slicer/test/helpers.cpp new file mode 100644 index 0000000..5527f5a --- /dev/null +++ b/slicer/test/helpers.cpp @@ -0,0 +1,32 @@ +#include "helpers.h" +#include +#include +#include + +void +system(const std::string & cmd) +{ + if (system(cmd.c_str())) { + fprintf(stderr, "Failed to execute:\n\t%s\n", cmd.c_str()); + throw std::runtime_error(cmd); + } +} + +void * +loadlib(const boost::filesystem::path & so) +{ + auto handle = dlopen(so.string().c_str(), RTLD_NOW | RTLD_GLOBAL); + if (!handle) { + throw std::runtime_error(dlerror()); + } + return handle; +} + +void +closelib(void * handle) +{ + if (dlclose(handle)) { + throw std::runtime_error(dlerror()); + } +} + diff --git a/slicer/test/helpers.h b/slicer/test/helpers.h new file mode 100644 index 0000000..f8f7a7d --- /dev/null +++ b/slicer/test/helpers.h @@ -0,0 +1,18 @@ +#ifndef SLICER_TEST_HELPERS_H +#define SLICER_TEST_HELPERS_H + +#include +#include + +// These are just thin wrappers that throw exceptions +void +system(const std::string &); + +void * +loadlib(const boost::filesystem::path &); + +void +closelib(void *); + +#endif + diff --git a/slicer/test/run-slicer.cpp b/slicer/test/run-slicer.cpp new file mode 100644 index 0000000..6a73736 --- /dev/null +++ b/slicer/test/run-slicer.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +namespace fs = boost::filesystem; + +int +main(int, char ** argv) +{ + const fs::path me = argv[0]; + const fs::path base = "types"; + const fs::path bjamout = me.parent_path(); + const fs::path root = me.parent_path().parent_path().parent_path().parent_path(); + fprintf(stderr, "root -- %s\n", root.string().c_str()); + const fs::path slice = fs::change_extension(root / base, ".ice"); + fprintf(stderr, "slice - %s\n", slice.string().c_str()); + + // Tmp + const fs::path tmp = root / "bin" / "slicer"; + fprintf(stderr, "tmp --- %s\n", tmp.string().c_str()); + fs::create_directory(tmp); + + // Execute + TestModule::BuiltInsPtr p(new TestModule::BuiltIns()); + Slicer::Serialize(p, tmp / "out.xml"); + + return 0; +} + diff --git a/slicer/test/types.ice b/slicer/test/types.ice index fdacb2d..0fef708 100644 --- a/slicer/test/types.ice +++ b/slicer/test/types.ice @@ -17,10 +17,6 @@ module TestModule { int a; int b; }; - class Optionals { - optional(1) int simple; - optional(2) StructType str; - }; sequence Classes; sequence Structs; dictionary ClassMap; -- cgit v1.2.3