From eb78583329431d1f9f744e696cad8d90885beacc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 5 Jan 2017 01:36:06 +0000 Subject: Replace messy m4 scripts with a proper C++ app for creating .cpp and .h for SQL scripts --- icetray/tool/Jamfile.jam | 21 +++++++++- icetray/tool/embed.sql.cpp.m4 | 6 --- icetray/tool/embed.sql.h.m4 | 10 ----- icetray/tool/icetray.jam | 30 +++++--------- icetray/tool/icetraySql.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++ icetray/unittests/Jamfile.jam | 22 ++++++++++ 6 files changed, 148 insertions(+), 37 deletions(-) delete mode 100644 icetray/tool/embed.sql.cpp.m4 delete mode 100644 icetray/tool/embed.sql.h.m4 create mode 100644 icetray/tool/icetraySql.cpp diff --git a/icetray/tool/Jamfile.jam b/icetray/tool/Jamfile.jam index f24c409..43be3d1 100644 --- a/icetray/tool/Jamfile.jam +++ b/icetray/tool/Jamfile.jam @@ -1,4 +1,23 @@ import package ; -package.install-data install-tools : boost-build/tools : icetray.jam [ glob embed.*.m4 ] ; +lib boost_program_options ; +lib boost_filesystem ; +lib boost_system ; + +exe icetray-sql : + icetraySql.cpp + : + boost_program_options + boost_filesystem + boost_system + /usr/include/adhocutil/ + ; + +explicit install-tools ; +alias install-tools : + install-tools-data + install-tools-bin + ; +package.install-data install-tools-data : boost-build/tools : icetray.jam ; +package.install install-tools-bin : : icetray-sql ; diff --git a/icetray/tool/embed.sql.cpp.m4 b/icetray/tool/embed.sql.cpp.m4 deleted file mode 100644 index 28a9747..0000000 --- a/icetray/tool/embed.sql.cpp.m4 +++ /dev/null @@ -1,6 +0,0 @@ -changecom(`@@') -#include "NAME.sql.h" - -const IceTray::StaticSqlSource NAMESPACE::NAME ({ -patsubst(esyscmd(`xxd -p -c 12 ' SQL), `\(..\)', `0x\1, ')0x0a}); - diff --git a/icetray/tool/embed.sql.h.m4 b/icetray/tool/embed.sql.h.m4 deleted file mode 100644 index e8ccee7..0000000 --- a/icetray/tool/embed.sql.h.m4 +++ /dev/null @@ -1,10 +0,0 @@ -define(`foreach',`ifelse(eval($#>2),1, - `pushdef(`$1',`$3')$2`'popdef(`$1') - `'ifelse(eval($#>3),1,`$0(`$1',`$2',shift(shift(shift($@))))')')') -define(NAMESPACEC,`patsubst(NAMESPACE,`::', `,')') -#include - -foreach(`ns', `namespace ns { ', NAMESPACEC) - extern const IceTray::StaticSqlSource NAME; -foreach(`ns', `}', NAMESPACEC) - diff --git a/icetray/tool/icetray.jam b/icetray/tool/icetray.jam index f999e2d..1b40c0e 100644 --- a/icetray/tool/icetray.jam +++ b/icetray/tool/icetray.jam @@ -1,33 +1,23 @@ -import generators ; -import feature ; -import type ; -import path ; -import property ; -import project ; -import regex ; +import generators.register-standard ; +import feature.feature ; +import type.register ; +import toolset.flags ; +import project.initialize ; project.initialize $(__name__) ; project icetray ; feature.feature icetray.sql.namespace : : free ; +feature.feature icetray.sql.basedir : : free path ; type.register SQL : sql ; +toolset.flags icetray.c++.sql CONNECTOR ; +toolset.flags icetray.c++.sql NAMESPACE ; +toolset.flags icetray.c++.sql BASEDIR ; generators.register-standard icetray.c++.sql : SQL : CPP(%.sql) H(%.sql) ; -path-constant icetray-root : . ; - -rule icetray.c++.sql ( cpp h : sql : properties * ) -{ - local namespace = [ property.select icetray.sql.namespace : $(properties) ] ; - local relpath = [ path.relative-to . $(sql:D) ] ; - namespace = [ regex.replace $(namespace:G=)/$(relpath) "\\/\\." "" ] ; - namespace = [ regex.replace $(namespace) "\\/" "::" ] ; - NAMESPACE on $(<) = -DNAMESPACE=\"$(namespace)\" ; -} - actions icetray.c++.sql { - m4 $(NAMESPACE) -DNAME="$(2:B)" -DSQL="$(2[1])" "$(icetray-root)/embed.sql.cpp.m4" > "$(1[1])" - m4 $(NAMESPACE) -DNAME="$(2:B)" "$(icetray-root)/embed.sql.h.m4" > "$(1[2])" + icetray-sql --basedir="$(BASEDIR)" --namespace="$(NAMESPACE)" "$(2[1])" "$(1[1])" "$(1[2])" } IMPORT $(__name__) : icetray.c++.sql : : icetray.c++.sql ; diff --git a/icetray/tool/icetraySql.cpp b/icetray/tool/icetraySql.cpp new file mode 100644 index 0000000..5817ca8 --- /dev/null +++ b/icetray/tool/icetraySql.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +namespace po = boost::program_options; +namespace fs = boost::filesystem; + +AdHocFormatter(CPPHeader, + R"C(#include "%?.h" + +const IceTray::StaticSqlSource )C"); +AdHocFormatter(CPPNS, "%?::"); +AdHocFormatter(CPPOpen, R"C(%? ( +R"SQL()C"); +AdHocFormatter(CPPFooter, R"C()SQL"); + +)C"); +AdHocFormatter(HHeader, + R"H(#include + +)H"); +AdHocFormatter(HDeclartion, "extern const IceTray::StaticSqlSource %?;\n"); +AdHocFormatter(HFooter, "\n"); + +AdHocFormatter(OpenNamespace, "namespace %? {\n"); +AdHocFormatter(CloseNamespace, "} // %?\n"); + +int +main(int argc, char ** argv) +{ + po::options_description opts("IceTray SQL-to-C++ precompiler"); + fs::path sql, cpp, h, base; + std::string sqlns; + + opts.add_options() + ("help,h", "Show this help message") + ("sql", po::value(&sql)->required(), "Path of SQL script") + ("cpp", po::value(&cpp)->required(), "Path of C++ file to write") + ("h", po::value(&h)->required(), "Path of header file to write") + ("basedir,d", po::value(&base)->default_value(fs::current_path()), "Base directory of SQL scripts (namespaces are created relative to here)") + ("namespace", po::value(&sqlns), "Namespace to create SqlSource in") + ; + + po::positional_options_description p; + p.add("sql", 1); + p.add("cpp", 1); + p.add("h", 1); + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(opts).positional(p).run(), vm); + + if (vm.count("help")) { + std::cout << opts << std::endl; + return 1; + } + po::notify(vm); + + std::vector sqlnsparts; + boost::algorithm::split(sqlnsparts, sqlns, boost::algorithm::is_any_of(":"), boost::algorithm::token_compress_on); + auto r = fs::canonical(sql).lexically_relative(fs::canonical(base)).parent_path(); + if (!r.empty()) { + std::transform(r.begin(), r.end(), std::back_inserter(sqlnsparts), [](const auto & p) { return p.string(); }); + } + + std::ifstream sqlin(sql.string()); + std::ofstream cppout(cpp.string()); + std::ofstream hout(h.string()); + + CPPHeader::write(cppout, sql.leaf().string()); + std::for_each(sqlnsparts.begin(), sqlnsparts.end(), [&cppout](const auto & nsp) { + CPPNS::write(cppout, nsp); + }); + CPPOpen::write(cppout, sql.stem().string()); + std::copy(std::istreambuf_iterator(sqlin), + std::istreambuf_iterator(), + std::ostreambuf_iterator(cppout)); + CPPFooter::write(cppout); + + HHeader::write(hout); + std::for_each(sqlnsparts.begin(), sqlnsparts.end(), [&hout](const auto & nsp) { + OpenNamespace::write(hout, nsp); + }); + HDeclartion::write(hout, sql.stem().string()); + std::for_each(sqlnsparts.begin(), sqlnsparts.end(), [&hout](const auto & nsp) { + CloseNamespace::write(hout, nsp); + }); + HFooter::write(hout); + + return 0; +} + diff --git a/icetray/unittests/Jamfile.jam b/icetray/unittests/Jamfile.jam index aab3114..7403ae0 100644 --- a/icetray/unittests/Jamfile.jam +++ b/icetray/unittests/Jamfile.jam @@ -1,5 +1,8 @@ import testing ; import ../tool/icetray ; +import generators ; +import feature ; +import toolset.flags ; lib boost_utf : : boost_unit_test_framework ; lib boost_filesystem ; @@ -19,8 +22,25 @@ alias testCommon : : : : boost_utf ..//boost_system BOOST_TEST_DYN_LINK + ../tool//icetray-sql + ../tool//icetray-sql + $(me) ; +generators.register-standard $(__name__).icetray.c++.sql : SQL : CPP(%.sql) H(%.sql) ; +generators.override $(__name__).icetray.c++.sql : icetray.c++.sql ; + +feature.feature icetray.sql.tool : : free dependency ; + +toolset.flags $(__name__).icetray.c++.sql ICETRAYSQLBIN ; +toolset.flags $(__name__).icetray.c++.sql NAMESPACE ; +toolset.flags $(__name__).icetray.c++.sql BASEDIR ; + +actions icetray.c++.sql bind ICETRAYSQLBIN +{ + $(ICETRAYSQLBIN) --basedir="$(BASEDIR)" --namespace="$(NAMESPACE)" "$(2[1])" "$(1[1])" "$(1[2])" +} + run testIceTray.cpp testIceTrayService.ice @@ -62,3 +82,5 @@ run : testDefaultPool ; +IMPORT $(__name__) : icetray.c++.sql : : icetray.c++.sql ; + -- cgit v1.2.3