ParseOGLExt.cxx 25.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    ParseOGLExt.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
Mathieu Malaterre's avatar
Mathieu Malaterre committed
15 16 17 18
/* A program that will read in OpenGL extension header files and output VTK
 * code that handles extensions in a more platform-independent manner.
 */

19 20 21 22 23 24 25 26 27 28 29 30

/*
 * Copyright 2003 Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the
 * U.S. Government. Redistribution and use in source and binary forms, with
 * or without modification, are permitted provided that this Notice and any
 * statement of authorship are reproduced on all copies.
 */

#include "Tokenizer.h"

31 32
#include <iostream>
#include <fstream>
33 34 35
#include <list>
#include <set>
#include <map>
36

37
#include <stdio.h>
38 39 40
#include <string.h>
#include <ctype.h>

41 42 43 44 45 46 47
using std::cerr;
using std::endl;
using std::ofstream;
using std::ifstream;
using std::istream;
using std::ostream;

48 49 50 51
// #define this if you want debug output as the parser does its work

// #define DEBUG_PARSE

52
static std::set< std::pair< std::string, std::string > > ConstantsAlreadyWritten;
53

54
static std::string ToUpper(std::string s)
55
{
56
  std::string u;
57

58
  for (std::string::size_type i = 0; i < s.length(); i++)
59
    {
60
    u.append(1, static_cast<char>(toupper(s[i])));
61 62 63 64 65 66 67
    }

  return u;
}

class Extension {
public:
68
  std::string GetName() const { return this->name; }
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  enum {GL, WGL, GLX} type;

  Extension() {}
  Extension(char *line);
  static bool isExtension(char *line);

  static inline void WriteSupportWrapperBegin(ostream &out, int itype) {
    switch (itype)
      {
      case WGL:
        out << "#ifdef WIN32" << endl;
        break;
      case GLX:
       out << "#ifdef VTK_USE_X" << endl;
        break;
      case GL:
        break;
      }
  }
  inline void WriteSupportWrapperBegin(ostream &out) const {
    WriteSupportWrapperBegin(out, this->type);
  }
  static inline void WriteSupportWrapperEnd(ostream &out, int itype) {
    if ((itype == WGL) || (itype == GLX))
      {
      out << "#endif" << endl;
      }
  }
  inline void WriteSupportWrapperEnd(ostream &out) const {
    WriteSupportWrapperEnd(out, this->type);
  }

  static inline const char *TypeToCapString(int t) {
    switch (t)
      {
      case GL: return "GL";
      case GLX: return "GLX";
      case WGL: return "WGL";
      }
    return NULL;
  }
  static inline const char *TypeToString(int t) {
    switch (t)
      {
      case GL: return "gl";
      case GLX: return "glX";
      case WGL: return "wgl";
      }
    return NULL;
  }

  bool operator<(const Extension &obj) const { return this->name < obj.name; }
121 122

protected:
123
  std::string name;
124 125 126 127 128 129 130 131 132 133 134
};

Extension::Extension(char *line)
{
  Tokenizer t(line);

  t.GetNextToken();

  this->name = t.GetNextToken();

  Tokenizer nameTokens(this->name, "_");
135
  std::string header = nameTokens.GetNextToken();
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  if (header == "WGL")
    {
    this->type = WGL;
    }
  else if (header == "GLX")
    {
    this->type = GLX;
    }
  else
    {
    this->type = GL;
    }
}

bool Extension::isExtension(char *line)
{
  Tokenizer t(line);

  if (t.GetNextToken() != "#ifndef") return false;

  Tokenizer nameTokens(t.GetNextToken(), "_");
157
  std::string header = nameTokens.GetNextToken();
158 159 160 161 162 163
  if ((header == "GL") || (header == "WGL") || (header == "GLX"))
    {
    return true;
    }

  return false;
164
}
165 166 167 168 169

static Extension currentExtension;

class Constant {
public:
170 171
  std::string GetName() const  { return this->name; }
  std::string GetValue() const;
172 173 174 175 176

  Constant(char *line);
  static bool isConstant(char *line);

  bool operator<(const Constant &obj) const { return this->name < obj.name; }
177 178

protected:
179 180
  std::string name;
  std::string value;
181 182
};

183
static std::map<std::string, std::string> EncounteredConstants;
184 185 186 187 188 189 190 191 192

Constant::Constant(char *line)
{
  // Assumes isConstant is true.
  Tokenizer t(line);

  t.GetNextToken();

  this->name = t.GetNextToken();
193
  std::string fullname = this->name;
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  if (currentExtension.type == Extension::GL)
    {
    // Skip the "GL_"
    this->name = this->name.substr(3);
    }
  else
    {
    // Skip the "GLX_" or "WGL_"
    this->name = this->name.substr(4);
    }
  // Make sure name does not start with a numeric.
  if ((this->name[0] >= '0') && (this->name[0] <= '9'))
    {
    this->name = '_' + this->name;
    }

  this->value = t.GetNextToken();
211 212 213 214 215

  // Now record this as found.
  EncounteredConstants[fullname] = this->value;
}

216
std::string Constant::GetValue() const
217
{
218
  // Sometimes, one constant points to another.  Handle this properly.
219
  std::map<std::string, std::string>::iterator found
220
    = EncounteredConstants.find(this->value);
221 222
  if (found != EncounteredConstants.end())
    {
223
    return found->second;
224
    }
225
  return this->value;
226 227 228 229 230 231 232 233 234 235 236
}

bool Constant::isConstant(char *line)
{
  Tokenizer t(line);

  if (t.GetNextToken() != "#define")
    {
    return false;
    }

237
  std::string n = t.GetNextToken();
238 239 240 241 242 243 244 245 246
  if (   (   (currentExtension.type == Extension::GL)
          && (strncmp(n.c_str(), "GL_", 3) == 0) )
      || (   (currentExtension.type == Extension::WGL)
          && (strncmp(n.c_str(), "WGL_", 4) == 0) )
      || (   (currentExtension.type == Extension::GLX)
          && (strncmp(n.c_str(), "GLX_", 4) == 0) ) )
    {
    return true;
    }
247
  return false;
248 249 250 251
}

class Typedef {
public:
252
  std::string definition;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

  Typedef(char *line);
  static bool isTypedef(char *line);

  bool operator<(const Typedef &obj) const { return this->definition < obj.definition; }
};

Typedef::Typedef(char *line)
{
  // Assumes isTypedef is true.
  this->definition = line;
}

bool Typedef::isTypedef(char *line)
{
  Tokenizer t(line);

  // Hack for some SGI stuff that declares a multiline struct.
  if (   (t.GetNextToken() == "typedef")
      && ((t.GetNextToken() != "struct") || (t.GetNextToken() != "{")) )
    {
    return true;
    }

  // Hack for how some WIN32 things are declared.
  if (strncmp(line, "DECLARE_HANDLE(", 15) == 0)
    {
    return true;
    }

  return false;
}

class Function {
public:
288 289 290 291
  std::string GetReturnType() const { return this->returnType; }
  std::string GetEntry() const { return this->entry; }
  std::string GetName() const { return this->name; }
  std::string GetArguments() const { return this->arguments; }
292
  int GetExtensionType() const { return this->extensionType; }
293 294 295 296 297 298

  Function(char *line);
  static bool isFunction(char *line);
  const char *GetProcType();

  bool operator<(const Function &obj) const { return this->name < obj.name; }
299 300

protected:
301 302 303 304
  std::string returnType;
  std::string entry;
  std::string name;
  std::string arguments;
305
  int extensionType;
306 307 308 309 310 311 312 313 314
};

Function::Function(char *line) : extensionType(currentExtension.type)
{
  // Assumes isFunction returns true.

  Tokenizer t(line, " \n\t(");

  t.GetNextToken();
315
  std::string token = t.GetNextToken();
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
  this->returnType = "";
  while ((token == "const") || (token == "unsigned"))
    {
    this->returnType += token + " ";
    token = t.GetNextToken();
    }
  this->returnType += token;

  token = t.GetNextToken();
  if (token == "*")
    {
    this->returnType += " *";
    token = t.GetNextToken();
    }
  else if (token[0] == '*')
    {
    this->returnType += " *";
    token = token.substr(1);
    }

#ifdef DEBUG_PARSE
  cerr << "Function return type: " << this->returnType << endl;
#endif

  if (currentExtension.type == Extension::GL)
    {
    this->entry = "APIENTRY";
    token = t.GetNextToken();
    }
  else if (currentExtension.type == Extension::WGL)
    {
    this->entry = "WINAPI";
    token = t.GetNextToken();
    }
  else
    {
    this->entry = "";
    }

#ifdef DEBUG_PARSE
  cerr << "Function entry: " << this->entry << endl;
#endif

  if (currentExtension.type == Extension::GL)
    {
    // Strip off "gl"
    this->name = token.substr(2);
    }
  else
    {
    // Strip off "glX" or "wgl"
    this->name = token.substr(3);
    }

#ifdef DEBUG_PARSE
  cerr << "Function name: " << this->name << endl;
#endif

  this->arguments = t.GetRemainingString();

376
#ifdef DEBUG_PARSE
377 378 379 380 381 382 383 384
  cerr << "Function arguments: " << this->arguments << endl;
#endif
}

bool Function::isFunction(char *line)
{
  Tokenizer t(line);

385 386
  std::string modifier = t.GetNextToken();
  std::string sreturnType = t.GetNextToken();
387 388 389 390 391 392 393
  if (sreturnType == "const")
    {
    // We don't really need the return type, just to skip over const.
    sreturnType += " ";
    sreturnType += t.GetNextToken();
    }

394
  std::string sentry = t.GetNextToken();
395 396 397 398 399
  if (sentry == "*")
    {
    sreturnType += " *";
    sentry = t.GetNextToken();
    }
400
  else if (sentry.size() && sentry[0] == '*')
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
    {
    sreturnType += " *";
    sentry = sentry.substr(1);
    }

  return (   (   (currentExtension.type == Extension::GL)
              && (modifier == "GLAPI") && (sentry == "APIENTRY") )
          || (   (currentExtension.type == Extension::GL)
              && (modifier == "extern") && (sentry == "APIENTRY") )
          || (   (currentExtension.type == Extension::WGL)
              && (modifier == "extern") && (sentry == "WINAPI") )
          || (   (currentExtension.type == Extension::GLX)
              && (modifier == "extern") ) );
}

const char *Function::GetProcType()
{
418
  static std::string proctype;
419 420 421 422 423 424 425 426 427

  proctype = "PFN";
  proctype += Extension::TypeToCapString(this->extensionType);
  proctype += ToUpper(this->name);
  proctype += "PROC";

  return proctype.c_str();
}

428 429 430 431 432
static std::list<Extension> extensions;
static std::set<Extension> extensionset;
static std::map<Extension, std::list<Constant> > consts;
static std::map<Extension, std::list<Typedef> > types;
static std::map<Extension, std::list<Function> > functs;
433 434 435 436 437 438 439

static void ParseLine(char *line)
{
  static bool inExtension = false;
  static int ifLevel = 0;

  Tokenizer tokens(line);
440
  std::string firstToken = tokens.GetNextToken();
441 442 443 444 445 446 447 448 449 450 451

  if (Extension::isExtension(line))
    {
    currentExtension = Extension(line);
#ifdef DEBUG_PARSE
    cerr << "Recognized extension: " << line << endl;
#endif

    // There are some exceptions to the extensions we support.  This is
    // because someone has placed some funky nonstandard stuff in the
    // header files.
452 453 454
    if (   (currentExtension.GetName() == "GLX_SGIX_video_source")
        || (currentExtension.GetName() == "GLX_SGIX_dmbuffer")
        || (currentExtension.GetName() == "GLX_SGIX_hyperpipe") )
455 456 457 458 459 460 461 462
      {
      inExtension = false;
      return;
      }

  // Only add extension to list if it is not already in it.
    if (extensionset.find(currentExtension) == extensionset.end())
      {
463
      if (currentExtension.GetName() == "GLX_ARB_get_proc_address")
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
        {
        // Special case where GLX_VERSION_1_4 depends on a typedef in
        // GLX_ARB_get_proc_address, so we have to move the latter up.
        extensions.push_front(currentExtension);
        }
      else
        {
        extensions.push_back(currentExtension);
        }
      extensionset.insert(currentExtension);
      }
    inExtension = true;
    ifLevel = 0;
    }
 else if (inExtension)
    {
    if (strncmp(firstToken.c_str(), "#if", 3) == 0)
      {
      ifLevel++;
      }
    else if (firstToken == "#endif")
      {
      if (ifLevel == 0)
        {
        inExtension = false;
        }
      else
        {
        ifLevel--;
        }
      }
    else if (   Constant::isConstant(line)
496 497
             && (strncmp(currentExtension.GetName().c_str(), (line+8),
                         currentExtension.GetName().length()) != 0) )
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
      {
#ifdef DEBUG_PARSE
      cerr << "Recognized constant: " << line << endl;
#endif
      consts[currentExtension].push_back(line);
      }
    else if (Function::isFunction(line))
      {
#ifdef DEBUG_PARSE
      cerr << "Recognized function: " << line << endl;
#endif
      functs[currentExtension].push_back(line);
      }
    else if (Typedef::isTypedef(line))
      {
#ifdef DEBUG_PARSE
      cerr << "Recognized typedef: " << line << endl;
#endif
      types[currentExtension].push_back(line);
      }
    }
  else
    {
#ifdef DEBUG_PARSE
    cerr << "Unrecognized line: " << line << endl;
#endif
    }
}

static void WriteHeader(ostream &file, const char *generator,
                        char **srcs, int num_srcs)
{
  file << "// -*- c++ -*-" << endl << endl;
  file << "//DO NOT EDIT!" << endl;
  file << "//This file was created with " << generator << endl
       << "//from";
  for (int i = 0; i < num_srcs; i++)
    {
    file << " " << srcs[i];
    }
  file << endl << endl;
  file << "/*" << endl
       << " * Copyright 2003 Sandia Corporation." << endl
       << " * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive" << endl
       << " * license for use of this work by or on behalf of the" << endl
       << " * U.S. Government. Redistribution and use in source and binary forms, with" << endl
       << " * or without modification, are permitted provided that this Notice and any" << endl
       << " * statement of authorship are reproduced on all copies." << endl
       << " */" << endl << endl;
}

static void WriteClassDeclarationGuts(ostream &hfile, int type)
{
551
  for (std::list<Extension>::iterator iextension = extensions.begin();
552 553 554
       iextension != extensions.end(); iextension++)
    {
    if (iextension->type != type) continue;
555
    hfile << endl << "  //Definitions for " << iextension->GetName().c_str() << endl;
556
    std::map<Extension, std::list<Constant> >::iterator cExts
557
      = consts.find(*iextension);
558 559
    if (cExts != consts.end())
      {
560
      for (std::list<Constant>::iterator iconst = cExts->second.begin();
561 562 563 564 565 566 567 568 569
           iconst != cExts->second.end(); iconst++)
        {
        // New versions of the NVIDIA OpenGL headers for Linux can
        // #define the same constant with the same value in multiple
        // sections.  This utility will happily parse those and write
        // out duplicate enums in different enum classes, which
        // confuses the C++ preprocessor terribly.  Don't write out a
        // definition for an enum with a name/value pair that's
        // already been used.
570
        if (ConstantsAlreadyWritten.find(std::make_pair(iconst->GetName(),
571 572
                                                           iconst->GetValue()))
             == ConstantsAlreadyWritten.end())
573
          {
574 575
          if(strcmp(iconst->GetName().c_str(),"TIMEOUT_IGNORED")==0)
            {
576
            // BCC/VS6/VS70 cannot digest this C99 macro
577
            hfile << "#if !defined(__BORLANDC__) && (!defined(_MSC_VER) || (defined(_MSC_VER) && _MSC_VER>=1310))" << endl;
578
            }
579 580


581 582
          hfile << "  const GLenum " << iconst->GetName().c_str()
                << " = static_cast<GLenum>(" << iconst->GetValue().c_str() << ");" << endl;
583

584
          ConstantsAlreadyWritten.insert(std::make_pair(iconst->GetName(),
585
                                                           iconst->GetValue()));
586 587 588 589 590
          if(strcmp(iconst->GetName().c_str(),"TIMEOUT_IGNORED")==0)
            {
            // really special case for non C99 compilers like BCC
            hfile << "#endif /* only for C99 compilers */" << endl;
            }
591 592 593
          }
        else
          {
594 595
          hfile << "  /* skipping duplicate " << iconst->GetName().c_str()
                << " = " << iconst->GetValue().c_str() << " */" << endl;
596 597 598
          }
        }
      }
599
    std::map<Extension, std::list<Typedef> >::iterator tExts
600
      = types.find(*iextension);
601 602
    if (tExts != types.end())
      {
603
      for (std::list<Typedef>::iterator itype = tExts->second.begin();
604 605
           itype != tExts->second.end(); itype++)
        {
606
        hfile << "  " << itype->definition.c_str() << endl;
607 608
        }
      }
609
    std::map<Extension, std::list<Function> >::iterator fExts
610
      = functs.find(*iextension);
611 612
    if (fExts != functs.end())
      {
613
      for (std::list<Function>::iterator ifunc = fExts->second.begin();
614 615
           ifunc != fExts->second.end(); ifunc++)
        {
616
        hfile << "  extern VTKRENDERINGOPENGL_EXPORT " << ifunc->GetProcType()
617
              << " " << ifunc->GetName().c_str() << ";" << endl;
618 619 620 621 622 623 624 625
        }
      }
    }
}

static void WriteFunctionPointerDeclarations(ostream &cxxfile, int type)
{
  Extension::WriteSupportWrapperBegin(cxxfile, type);
626
  for (std::map<Extension, std::list<Function> >::iterator fExts
627
         = functs.begin();
628 629 630
       fExts != functs.end(); fExts++)
    {
    if (fExts->first.type != type) continue;
631
    cxxfile << "//Functions for " << fExts->first.GetName().c_str() << endl;
632
    for (std::list<Function>::iterator ifunc = fExts->second.begin();
633 634 635 636 637
         ifunc != fExts->second.end(); ifunc++)
      {
      cxxfile << "vtk" << Extension::TypeToString(type) << "::"
              << ifunc->GetProcType()
              << " vtk" << Extension::TypeToString(type) << "::"
638
              << ifunc->GetName().c_str() << " = NULL;" << endl;
639 640 641 642 643 644 645 646 647
      }
    }
  Extension::WriteSupportWrapperEnd(cxxfile, type);
  cxxfile << endl;
}

static void WriteCode(ostream &hfile, ostream &cxxfile)
{
  // Write data for header file ---------------------------------
648 649
  hfile << "#ifndef vtkgl_h" << endl
        << "#define vtkgl_h" << endl << endl;
650
  hfile << "#include \"vtkRenderingOpenGLConfigure.h\"" << endl;
651 652 653 654 655
  hfile << "#include \"vtkSystemIncludes.h\"" << endl;
  hfile << "#include \"vtkWindows.h\"" << endl;
  hfile << "#include \"vtkOpenGL.h\"" << endl;
  hfile << "#include <stddef.h>" << endl << endl;
  hfile << "#ifdef VTK_USE_X" << endl
656 657
        << "/* To prevent glx.h to include glxext.h from the OS */" << endl
        << "#define GLX_GLXEXT_LEGACY" << endl
658
        << "#include <GL/glx.h>" << endl
659 660 661
        << "#endif" << endl << endl;
  hfile << "class vtkOpenGLExtensionManager;" << endl << endl;
  hfile << "#ifndef APIENTRY" << endl
662 663
        << "#define APIENTRY" << endl
        << "#define VTKGL_APIENTRY_DEFINED" << endl
664 665
        << "#endif" << endl << endl;
  hfile << "#ifndef APIENTRYP" << endl
666 667
        << "#define APIENTRYP APIENTRY *" << endl
        << "#define VTKGL_APIENTRYP_DEFINED" << endl
668 669 670 671 672
        << "#endif" << endl << endl;

  hfile << "/* Undefine all constants to avoid name conflicts.  They should be defined  */" << endl
        << "/* with GL_, GLX_, or WGL_ preprended to them anyway, but sometimes you run */" << endl
        << "/* into a header file that gets it wrong.                                   */" << endl;
673
  for (std::map<Extension, std::list<Constant> >::iterator constlist
674
         = consts.begin();
675 676
       constlist != consts.end(); constlist++)
    {
677
    for (std::list<Constant>::iterator c = (*constlist).second.begin();
678 679
         c != (*constlist).second.end(); c++)
      {
680 681
      hfile << "#ifdef " << (*c).GetName().c_str() << endl;
      hfile << "#undef " << (*c).GetName().c_str() << endl;
682 683 684 685 686 687
      hfile << "#endif" << endl;
      }
    }

  Extension::WriteSupportWrapperBegin(hfile, Extension::GL);
  hfile << endl << "namespace vtkgl {" << endl;
688 689
  // Add necessary type declarations.
  hfile << "  //Define int32_t, int64_t, and uint64_t." << endl;
690 691 692
  hfile << "  typedef vtkTypeInt32 int32_t;" << endl;
  hfile << "  typedef vtkTypeInt64 int64_t;" << endl;
  hfile << "  typedef vtkTypeUInt64 uint64_t;" << endl;
693 694 695 696
  // OpenGL 3.2 typedefs
  hfile << "  typedef int64_t GLint64;" << endl;
  hfile << "  typedef uint64_t GLuint64;" << endl;
  hfile << "  typedef struct __GLsync *GLsync;" << endl;
697

698 699 700
  ConstantsAlreadyWritten.clear();
  WriteClassDeclarationGuts(hfile, Extension::GL);
  hfile << endl << "  // Method to load functions for a particular extension.";
701
  hfile << endl << "  extern int VTKRENDERINGOPENGL_EXPORT LoadExtension(const char *name, "
702
        << "vtkOpenGLExtensionManager *manager);" << endl;
703
  hfile << endl << "  // Strings containing special version extensions.";
704
  hfile << endl << "  extern VTKRENDERINGOPENGL_EXPORT const char *GLVersionExtensionsString();" << endl;
705
  hfile << endl << "  const char *GLXVersionExtensionsString();" << endl;
706 707 708 709 710
  hfile << "}" << endl;
  Extension::WriteSupportWrapperEnd(hfile, Extension::GL);

  Extension::WriteSupportWrapperBegin(hfile, Extension::GLX);
  hfile << "namespace vtkglX {" << endl;
711 712 713 714 715 716 717
  // glxext.h is not written very well.  Add some typedefs that may not
  // be defined.
  hfile << "  //Miscellaneous definitions." << endl;
  hfile << "  typedef XID GLXContextID;" << endl;
  hfile << "  typedef XID GLXPbuffer;" << endl;
  hfile << "  typedef XID GLXWindow;" << endl;
  hfile << "  typedef XID GLXFBConfigID;" << endl;
718
  hfile << "  typedef struct __GLXFBConfigRec *GLXFBConfig;" << endl;
719 720
  hfile << "  typedef vtkTypeInt32 int32_t;" << endl;
  hfile << "  typedef vtkTypeInt64 int64_t;" << endl;
721 722 723 724 725 726 727 728 729 730 731 732 733 734
  ConstantsAlreadyWritten.clear();
  WriteClassDeclarationGuts(hfile, Extension::GLX);
  hfile << "}" << endl;
  Extension::WriteSupportWrapperEnd(hfile, Extension::GLX);

  Extension::WriteSupportWrapperBegin(hfile, Extension::WGL);
  hfile << "namespace vtkwgl {" << endl;
  ConstantsAlreadyWritten.clear();
  WriteClassDeclarationGuts(hfile, Extension::WGL);
  hfile << "}" << endl;
  Extension::WriteSupportWrapperEnd(hfile, Extension::WGL);

  hfile << endl
        << "#ifdef VTKGL_APIENTRY_DEFINED" << endl
735
        << "#undef APIENTRY" << endl
736 737
        << "#endif" << endl << endl;
  hfile << "#ifdef VTKGL_APIENTRYP_DEFINED" << endl
738
        << "#undef APIENTRYP" << endl
739 740 741 742 743 744 745 746 747 748 749 750
        << "#endif" << endl << endl;
  hfile << "#endif //_vtkgl_h" << endl;

  // Write data for C++ file --------------------------------------------
  cxxfile << "#include \"vtkgl.h\"" << endl;
  cxxfile << "#include \"vtkOpenGLExtensionManager.h\"" << endl << endl;

  // Write function pointer declarations.
  WriteFunctionPointerDeclarations(cxxfile, Extension::GL);
  WriteFunctionPointerDeclarations(cxxfile, Extension::GLX);
  WriteFunctionPointerDeclarations(cxxfile, Extension::WGL);

751
  std::list<Extension>::iterator iextension;
752

753 754 755
  // Write function to load function pointers.
  cxxfile << "int vtkgl::LoadExtension(const char *name, vtkOpenGLExtensionManager *manager)" << endl
          << "{" << endl;
756
  for (iextension = extensions.begin();
757 758 759
       iextension != extensions.end(); iextension++)
    {
    iextension->WriteSupportWrapperBegin(cxxfile);
760
    cxxfile << "  if (strcmp(name, \"" << iextension->GetName().c_str()
761 762
            << "\") == 0)" << endl
            << "    {" << endl;
763
    std::string vtkglclass = "vtk";
764
    vtkglclass += Extension::TypeToString(iextension->type);
765
    std::list<Function>::iterator ifunct;
766 767 768
    for (ifunct = functs[*iextension].begin();
         ifunct != functs[*iextension].end(); ifunct++)
      {
769
      cxxfile << "    " << vtkglclass.c_str() << "::"
770
              << ifunct->GetName().c_str() << " = reinterpret_cast<" << vtkglclass.c_str() << "::"
771
              << ifunct->GetProcType()
772
              << ">(manager->GetProcAddress(\""
773
              << Extension::TypeToString(iextension->type)
774
              << ifunct->GetName().c_str() << "\"));" << endl;
775 776 777 778 779
      }
    cxxfile << "    return 1";
    for (ifunct = functs[*iextension].begin();
         ifunct != functs[*iextension].end(); ifunct++)
      {
780
      cxxfile << " && (" << vtkglclass.c_str() << "::" << ifunct->GetName().c_str()
781
              << " != NULL)";
782 783 784 785 786
      }
    cxxfile << ";" << endl;
    cxxfile << "    }" << endl;
    iextension->WriteSupportWrapperEnd(cxxfile);
    }
787 788
  cxxfile << "  vtkGenericWarningMacro(<< \"Nothing known about extension \" << name" << endl
          << "                         << \".  vtkgl may need to be updated.\");" << endl;
789 790
  cxxfile << "  return 0;" << endl
          << "}" << endl;
791 792 793 794 795

  // Write functions to report special version extension strings.
  cxxfile << endl << "const char *vtkgl::GLVersionExtensionsString()" << endl
          << "{" << endl
          << "  return \"";
796
  for (iextension = extensions.begin();
797 798
       iextension != extensions.end(); iextension++)
    {
799
    if (strncmp("GL_VERSION_", iextension->GetName().c_str(), 11) == 0)
800
      {
801
      cxxfile << iextension->GetName().c_str() << " ";
802 803 804 805 806 807 808 809
      }
    }
  cxxfile << "\";" << endl
          << "}" << endl;

  cxxfile << endl << "const char *vtkgl::GLXVersionExtensionsString()" << endl
          << "{" << endl
          << "  return \"";
810
  for (iextension = extensions.begin();
811 812
       iextension != extensions.end(); iextension++)
    {
813
    if (strncmp("GLX_VERSION_", iextension->GetName().c_str(), 12) == 0)
814
      {
815
      cxxfile << iextension->GetName().c_str() << " ";
816 817 818 819
      }
    }
  cxxfile << "\";" << endl
          << "}" << endl;
820 821 822 823
}

int main(int argc, char **argv)
{
824
  if (argc < 3)
825
    {
826
    cerr << "USAGE: " << argv[0] << "<output dir> <header files>" << endl;
827 828 829
    return 1;
    }

830
  std::string outputDir = argv[1];
831 832

  for (int i = 2; i < argc; i++)
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
    {
#ifdef DEBUG_PARSE
    cerr << "*** Parsing declarations from file " << argv[i] << endl;
#endif
    ifstream file(argv[i]);
    if (!file)
      {
      cerr << "Could not open " << argv[i] << endl;
      return 2;
      }

    while (!file.eof())
      {
      static char buf[4096];    // What are the odds of needing more?
      file.getline(buf, 4096);
      ParseLine(buf);
      }
    file.close();
    }

853
  ofstream hfile((outputDir + "/vtkgl.h").c_str());
854
  WriteHeader(hfile, argv[0], argv+1, argc-1);
855
  ofstream cxxfile((outputDir + "/vtkgl.cxx").c_str());
856 857 858 859 860 861 862 863 864
  WriteHeader(cxxfile, argv[0], argv+1, argc-1);

  WriteCode(hfile, cxxfile);

  hfile.close();
  cxxfile.close();

  return 0;
}