summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2017-01-05 01:36:06 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2017-01-08 19:33:26 +0000
commiteb78583329431d1f9f744e696cad8d90885beacc (patch)
tree0c05d48c6558a1c193f77a96cf6d5a1ff5379af8
parentAdd sql source wrappers to get commands with options containing hash and any ... (diff)
downloadicetray-eb78583329431d1f9f744e696cad8d90885beacc.tar.bz2
icetray-eb78583329431d1f9f744e696cad8d90885beacc.tar.xz
icetray-eb78583329431d1f9f744e696cad8d90885beacc.zip
Replace messy m4 scripts with a proper C++ app for creating .cpp and .h for SQL scripts
-rw-r--r--icetray/tool/Jamfile.jam21
-rw-r--r--icetray/tool/embed.sql.cpp.m46
-rw-r--r--icetray/tool/embed.sql.h.m410
-rw-r--r--icetray/tool/icetray.jam30
-rw-r--r--icetray/tool/icetraySql.cpp96
-rw-r--r--icetray/unittests/Jamfile.jam22
6 files changed, 148 insertions, 37 deletions
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
+ :
+ <library>boost_program_options
+ <library>boost_filesystem
+ <library>boost_system
+ <include>/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 <staticSqlSource.h>
-
-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 <icetray.sql.connector> ;
+toolset.flags icetray.c++.sql NAMESPACE <icetray.sql.namespace> ;
+toolset.flags icetray.c++.sql BASEDIR <icetray.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 <iostream>
+#include <fstream>
+#include <boost/program_options.hpp>
+#include <boost/filesystem/path.hpp>
+#include <boost/filesystem/convenience.hpp>
+#include <boost/algorithm/string/split.hpp>
+#include <boost/algorithm/string/classification.hpp>
+#include <compileTimeFormatter.h>
+
+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 <staticSqlSource.h>
+
+)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<std::string> 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<char>(sqlin),
+ std::istreambuf_iterator<char>(),
+ std::ostreambuf_iterator<char>(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 : : <name>boost_unit_test_framework ;
lib boost_filesystem ;
@@ -19,8 +22,25 @@ alias testCommon : : : :
<library>boost_utf
<library>..//boost_system
<define>BOOST_TEST_DYN_LINK
+ <icetray.sql.tool>../tool//icetray-sql
+ <dependency>../tool//icetray-sql
+ <icetray.sql.basedir>$(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 <icetray.sql.tool> ;
+toolset.flags $(__name__).icetray.c++.sql NAMESPACE <icetray.sql.namespace> ;
+toolset.flags $(__name__).icetray.c++.sql BASEDIR <icetray.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 ;
+