annotate generator/generator.cpp @ 153:38675dcea44f

Bring the stub generator into this repo (from piper-vamp-cpp)
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 14 Jun 2017 10:06:30 +0100
parents
children 94050aba32c3
rev   line source
cannam@153 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@153 2 /*
cannam@153 3 Piper C++
cannam@153 4
cannam@153 5 An API for audio analysis and feature extraction plugins.
cannam@153 6
cannam@153 7 Centre for Digital Music, Queen Mary, University of London.
cannam@153 8 Copyright 2006-2017 Chris Cannam and QMUL.
cannam@153 9
cannam@153 10 Permission is hereby granted, free of charge, to any person
cannam@153 11 obtaining a copy of this software and associated documentation
cannam@153 12 files (the "Software"), to deal in the Software without
cannam@153 13 restriction, including without limitation the rights to use, copy,
cannam@153 14 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@153 15 of the Software, and to permit persons to whom the Software is
cannam@153 16 furnished to do so, subject to the following conditions:
cannam@153 17
cannam@153 18 The above copyright notice and this permission notice shall be
cannam@153 19 included in all copies or substantial portions of the Software.
cannam@153 20
cannam@153 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@153 22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@153 23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@153 24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@153 25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@153 26 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@153 27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@153 28
cannam@153 29 Except as contained in this notice, the names of the Centre for
cannam@153 30 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@153 31 shall not be used in advertising or otherwise to promote the sale,
cannam@153 32 use or other dealings in this Software without prior written
cannam@153 33 authorization.
cannam@153 34 */
cannam@153 35
cannam@153 36 #include "vamp-json/VampJson.h"
cannam@153 37 #include "vamp-support/RequestOrResponse.h"
cannam@153 38 #include "vamp-support/LoaderRequests.h"
cannam@153 39 #include "vamp-support/RdfTypes.h"
cannam@153 40
cannam@153 41 #include <iostream>
cannam@153 42 #include <sstream>
cannam@153 43 #include <stdexcept>
cannam@153 44
cannam@153 45 #include <map>
cannam@153 46 #include <set>
cannam@153 47
cannam@153 48 // for _setmode stuff and _dup
cannam@153 49 #ifdef _WIN32
cannam@153 50 #include <io.h>
cannam@153 51 #include <fcntl.h>
cannam@153 52 #endif
cannam@153 53
cannam@153 54 // for dup, open etc
cannam@153 55 #ifndef _WIN32
cannam@153 56 #include <fcntl.h>
cannam@153 57 #include <unistd.h>
cannam@153 58 #endif
cannam@153 59
cannam@153 60 using namespace std;
cannam@153 61 using namespace json11;
cannam@153 62 using namespace piper_vamp;
cannam@153 63 using namespace Vamp;
cannam@153 64
cannam@153 65 static string myname = "piper-vamp-stub-generator";
cannam@153 66
cannam@153 67 static void version()
cannam@153 68 {
cannam@153 69 cout << "1.0" << endl;
cannam@153 70 exit(0);
cannam@153 71 }
cannam@153 72
cannam@153 73 static void usage(bool successful = false)
cannam@153 74 {
cannam@153 75 cerr << "\n" << myname <<
cannam@153 76 ": Emit a preliminary version of the main code\nfor a Piper Adapter implementation of a Vamp plugin library\n\n"
cannam@153 77 " Usage: " << myname << " [-d] <libname>\n"
cannam@153 78 " " << myname << " -v\n"
cannam@153 79 " " << myname << " -h\n\n"
cannam@153 80 " where\n"
cannam@153 81 " <libname>: the Vamp plugin library name to emit code for\n"
cannam@153 82 " -v: print version number to stdout and exit\n"
cannam@153 83 " -h: print this text to stderr and exit\n\n";
cannam@153 84 if (successful) exit(0);
cannam@153 85 else exit(2);
cannam@153 86 }
cannam@153 87
cannam@153 88 // We write our output to stdout, but want to ensure that the plugin
cannam@153 89 // doesn't write anything itself. To do this we open a null file
cannam@153 90 // descriptor and dup2() it into place of stdout in the gaps between
cannam@153 91 // our own output activity.
cannam@153 92
cannam@153 93 static int normalFd = -1;
cannam@153 94 static int suspendedFd = -1;
cannam@153 95
cannam@153 96 static void initFds(bool binary)
cannam@153 97 {
cannam@153 98 #ifdef _WIN32
cannam@153 99 if (binary) {
cannam@153 100 int result = _setmode(0, _O_BINARY);
cannam@153 101 if (result == -1) {
cannam@153 102 throw runtime_error("Failed to set binary mode on stdin");
cannam@153 103 }
cannam@153 104 result = _setmode(1, _O_BINARY);
cannam@153 105 if (result == -1) {
cannam@153 106 throw runtime_error("Failed to set binary mode on stdout");
cannam@153 107 }
cannam@153 108 }
cannam@153 109 normalFd = _dup(1);
cannam@153 110 suspendedFd = _open("NUL", _O_WRONLY);
cannam@153 111 #else
cannam@153 112 (void)binary;
cannam@153 113 normalFd = dup(1);
cannam@153 114 suspendedFd = open("/dev/null", O_WRONLY);
cannam@153 115 #endif
cannam@153 116
cannam@153 117 if (normalFd < 0 || suspendedFd < 0) {
cannam@153 118 throw runtime_error("Failed to initialise fds for stdio suspend/resume");
cannam@153 119 }
cannam@153 120 }
cannam@153 121
cannam@153 122 static void suspendOutput()
cannam@153 123 {
cannam@153 124 #ifdef _WIN32
cannam@153 125 _dup2(suspendedFd, 1);
cannam@153 126 #else
cannam@153 127 dup2(suspendedFd, 1);
cannam@153 128 #endif
cannam@153 129 }
cannam@153 130
cannam@153 131 static void resumeOutput()
cannam@153 132 {
cannam@153 133 #ifdef _WIN32
cannam@153 134 _dup2(normalFd, 1);
cannam@153 135 #else
cannam@153 136 dup2(normalFd, 1);
cannam@153 137 #endif
cannam@153 138 }
cannam@153 139
cannam@153 140 ListResponse
cannam@153 141 makeRequest(string libname)
cannam@153 142 {
cannam@153 143 ListRequest req;
cannam@153 144 req.from.push_back(libname);
cannam@153 145 return LoaderRequests().listPluginData(req);
cannam@153 146 }
cannam@153 147
cannam@153 148 struct PlausibleMetadata
cannam@153 149 {
cannam@153 150 string className;
cannam@153 151 string adapterName;
cannam@153 152 };
cannam@153 153
cannam@153 154 PlausibleMetadata
cannam@153 155 inventPlausibleMetadata(string key)
cannam@153 156 {
cannam@153 157 string className, adapterName;
cannam@153 158 bool initial = true, interCap = false;
cannam@153 159
cannam@153 160 int idIdx = 0;
cannam@153 161 for (int i = 0; i < int(key.size()); ++i) {
cannam@153 162 char c = key[i];
cannam@153 163 if (c == ':') {
cannam@153 164 idIdx = i + 1;
cannam@153 165 break;
cannam@153 166 }
cannam@153 167 }
cannam@153 168
cannam@153 169 for (int i = idIdx; i < int(key.size()); ++i) {
cannam@153 170 char c = key[i];
cannam@153 171 if (isalpha(c)) {
cannam@153 172 if (initial || interCap) {
cannam@153 173 className += toupper(c);
cannam@153 174 } else {
cannam@153 175 className += c;
cannam@153 176 }
cannam@153 177 if (interCap) {
cannam@153 178 adapterName += toupper(c);
cannam@153 179 } else {
cannam@153 180 adapterName += c;
cannam@153 181 }
cannam@153 182 interCap = false;
cannam@153 183 } else {
cannam@153 184 interCap = true;
cannam@153 185 }
cannam@153 186 initial = false;
cannam@153 187 }
cannam@153 188 adapterName += "Adapter";
cannam@153 189
cannam@153 190 PlausibleMetadata pm;
cannam@153 191 pm.className = className;
cannam@153 192 pm.adapterName = adapterName;
cannam@153 193 return pm;
cannam@153 194 }
cannam@153 195
cannam@153 196 void
cannam@153 197 emitFor(string libname, const ListResponse &resp)
cannam@153 198 {
cannam@153 199 // The same plugin key may appear more than once in the available
cannam@153 200 // list, if the same plugin is installed in more than one location
cannam@153 201 // on the Vamp path. To avoid emitting its wrapper definition
cannam@153 202 // multiple times, we need to keep a record of which ones we've
cannam@153 203 // emitted for each stage
cannam@153 204 set<string> emitted;
cannam@153 205
cannam@153 206 cout <<
cannam@153 207 "\n#include \"PiperExport.h\"\n"
cannam@153 208 "\n"
cannam@153 209 "// !!! The following #include(s) are guesses. Replace them with the\n"
cannam@153 210 "// real header files in which your plugin classes are defined.\n"
cannam@153 211 "//\n";
cannam@153 212
cannam@153 213 for (const auto &plugin: resp.available) {
cannam@153 214
cannam@153 215 string key = plugin.pluginKey;
cannam@153 216 if (emitted.find(key) != emitted.end()) {
cannam@153 217 continue;
cannam@153 218 }
cannam@153 219
cannam@153 220 PlausibleMetadata pm = inventPlausibleMetadata(key);
cannam@153 221
cannam@153 222 cout << "#include \"" << pm.className << ".h\"\n";
cannam@153 223
cannam@153 224 emitted.insert(key);
cannam@153 225 }
cannam@153 226
cannam@153 227 cout <<
cannam@153 228 "\n"
cannam@153 229 "using piper_vamp_js::PiperAdapter;\n"
cannam@153 230 "using piper_vamp_js::PiperPluginLibrary;\n"
cannam@153 231 "\n"
cannam@153 232 "static std::string libname(\"" << libname << "\");\n"
cannam@153 233 "\n";
cannam@153 234
cannam@153 235 emitted.clear();
cannam@153 236 for (const auto &plugin: resp.available) {
cannam@153 237
cannam@153 238 string key = plugin.pluginKey;
cannam@153 239 if (emitted.find(key) != emitted.end()) {
cannam@153 240 continue;
cannam@153 241 }
cannam@153 242
cannam@153 243 PlausibleMetadata pm = inventPlausibleMetadata(key);
cannam@153 244 cout << "// !!! Replace " << pm.className << " in the following line with the real\n"
cannam@153 245 "// name of the class implementing the \"" << plugin.basic.identifier << "\" plugin.\n"
cannam@153 246 "//\n";
cannam@153 247 cout << "static PiperAdapter<"
cannam@153 248 << pm.className
cannam@153 249 << ">\n" << pm.adapterName
cannam@153 250 << "(\n libname,\n ";
cannam@153 251
cannam@153 252 string catString = "{ ";
cannam@153 253 bool first = true;
cannam@153 254 for (auto c: plugin.category) {
cannam@153 255 if (!first) catString += ", ";
cannam@153 256 catString += "\"" + c + "\"";
cannam@153 257 first = false;
cannam@153 258 }
cannam@153 259 catString += " }";
cannam@153 260 cout << catString << ",\n ";
cannam@153 261
cannam@153 262 cout << "{\n ";
cannam@153 263 first = true;
cannam@153 264 for (auto o: plugin.basicOutputInfo) {
cannam@153 265 if (!first) {
cannam@153 266 cout << ",\n ";
cannam@153 267 }
cannam@153 268 cout << " ";
cannam@153 269 string outputId = o.identifier;
cannam@153 270 cout << "{ \"" << outputId << "\",\n { \"";
cannam@153 271 if (plugin.staticOutputInfo.find(outputId) !=
cannam@153 272 plugin.staticOutputInfo.end()) {
cannam@153 273 cout << plugin.staticOutputInfo.at(outputId).typeURI;
cannam@153 274 }
cannam@153 275 cout << "\" }\n }";
cannam@153 276 first = false;
cannam@153 277 }
cannam@153 278 cout << "\n }\n";
cannam@153 279 cout << " );\n\n";
cannam@153 280 emitted.insert(key);
cannam@153 281 }
cannam@153 282
cannam@153 283 cout << "static PiperPluginLibrary library({\n ";
cannam@153 284 emitted.clear();
cannam@153 285 for (const auto &plugin: resp.available) {
cannam@153 286
cannam@153 287 string key = plugin.pluginKey;
cannam@153 288 if (emitted.find(key) != emitted.end()) {
cannam@153 289 continue;
cannam@153 290 }
cannam@153 291
cannam@153 292 PlausibleMetadata pm = inventPlausibleMetadata(key);
cannam@153 293 if (!emitted.empty()) {
cannam@153 294 cout << ",\n ";
cannam@153 295 }
cannam@153 296 cout << "&" << pm.adapterName;
cannam@153 297 emitted.insert(key);
cannam@153 298 }
cannam@153 299 cout << "\n});\n\n";
cannam@153 300 cout << "PIPER_EXPORT_LIBRARY(library);\n" << endl;
cannam@153 301 }
cannam@153 302
cannam@153 303 int main(int argc, char **argv)
cannam@153 304 {
cannam@153 305 if (argc != 2 && argc != 3) {
cannam@153 306 usage();
cannam@153 307 }
cannam@153 308
cannam@153 309 string arg = argv[1];
cannam@153 310 if (arg == "-h") {
cannam@153 311 if (argc == 2) {
cannam@153 312 usage(true);
cannam@153 313 } else {
cannam@153 314 usage();
cannam@153 315 }
cannam@153 316 } else if (arg == "-v") {
cannam@153 317 if (argc == 2) {
cannam@153 318 version();
cannam@153 319 } else {
cannam@153 320 usage();
cannam@153 321 }
cannam@153 322 }
cannam@153 323
cannam@153 324 string libname = arg;
cannam@153 325
cannam@153 326 auto li = libname.rfind('.');
cannam@153 327 if (li != std::string::npos) {
cannam@153 328 cerr << "NOTE: library name is to be specified without extension, dropping \"" << libname.substr(li) << "\"" << endl;
cannam@153 329 libname = libname.substr(0, li);
cannam@153 330 }
cannam@153 331
cannam@153 332 try {
cannam@153 333 initFds(false);
cannam@153 334 } catch (exception &e) {
cannam@153 335 cerr << "ERROR: " << e.what() << endl;
cannam@153 336 exit(1);
cannam@153 337 }
cannam@153 338
cannam@153 339 suspendOutput();
cannam@153 340
cannam@153 341 ListResponse resp = makeRequest(libname);
cannam@153 342
cannam@153 343 resumeOutput();
cannam@153 344
cannam@153 345 if (resp.available.empty()) {
cannam@153 346 cerr << "ERROR: No plugin(s) found in library named \""
cannam@153 347 << libname << "\"\nCheck that the library name is correct and the right files are in $VAMP_PATH.\nThat would mean either "
cannam@153 348 << libname << ".so, " << libname << ".dll, or " << libname
cannam@153 349 << ".dylib (depending on your platform), as well as "
cannam@153 350 << libname << ".n3 and " << libname << ".cat."
cannam@153 351 << endl;
cannam@153 352 exit(1);
cannam@153 353 }
cannam@153 354
cannam@153 355 emitFor(libname, resp);
cannam@153 356
cannam@153 357 exit(0);
cannam@153 358 }