doc_header2doxygen.pl 20.7 KB
Newer Older
1
#!/usr/bin/env perl
2
# Time-stamp: <2011-01-16 21:15:53 barre>
3 4 5 6 7 8
#
# Convert VTK headers to doxygen format
#
# roeim : Vetle Roeim <vetler@ifi.uio.no>
# barre : Sebastien Barre <sebastien@barre.nom.fr>
#
9 10 11
# 0.9 (barre) :
#   - add --conds          : add \cond...\endcond around public:, private:, protected:
#
12 13 14
# 0.83 (barre) :
#   - add --stdout          : print converted file to standard output
#
15 16 17 18
# 0.82 (barre) :
#   - add --relativeto path : each file/directory to document is considered
#     relative to 'path', where --to and --relativeto should be absolute
#
19 20 21 22
# 0.81 (barre) :
#   - fix pb if both --to and path to the file to document were absolute
#   - remove warning when date or revision not found
#
23 24 25 26 27 28 29
# 0.8 (barre) :
#   - update to match the new VTK 4.0 tree
#   - change default --dirs so that it can be launched from Utilities/Doxygen
#   - change default --to so that it can be launched from Utilities/Doxygen
#   - handle more .SECTION syntax
#   - add group support (at last)
#
30
# 0.76 (barre) :
31
#   - add 'parallel' to the default set of directories
32 33 34 35 36
#
# 0.75 (barre) :
#   - change default --to to '../vtk-doxygen' to comply with Kitware's doxyfile
#
# 0.74 (barre) :
37
#   - as doxygen now handles RCS/CVS tags of the form $word:text$, use them
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#
# 0.73 (barre) :
#   - change doxygen command style from \ to @ to match javadoc, autodoc, etc.
#
# 0.72 (barre) :
#   - change default --to to '../vtk-dox'
#
# 0.71 (barre) :
#   - fix O_TEXT flag problem
#   - switch to Unix CR/LF format
#
# 0.7 (barre) :
#   - change name
#   - remove -c option
#
# 0.6 (barre) :
#   - change (warning) default --to to '../vtk2' because I ruined my own
#     VTK distrib too many times :(
#   - add automatic creation of missing directory trees
57
#   - add check for current OS (if Windows, do not perform tests based
58 59 60 61 62 63 64 65 66 67 68 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
#     on stat()/idev/ino features)
#
# 0.5 (barre) :
#   - better .SECTION handling
#   - add support for empty lines in documentation block
#   - fix problem with headers not corresponding to classes
#   - change name to doc_header2doxygen (removed vtk_)
#   - change '-s' (silent) to '-v' (verbose)
#   - add function description reformatting
#
# 0.4 (barre) :
#   - change /*! ... */ position upon request
#   - add 'Date:' support as @date
#   - add 'Version:' support as @version
#   - add 'Thanks:' support as @par Thanks
#
# 0.3 (barre) :
#   - fix various " // Description" spelling problems :)
#
# 0.2 (barre) :
#   - fix problem with classes with no brief documentation
#
# 0.1 (barre) :
#   - add Perl syntactic sugar, options...
#   - add standard output (filter) mode (-c)
#   - add silent mode (-s)
#   - add update mode, convert only if newer (-u)
#   - add conversion to another directory (--to)
#   - add '.SECTION Caveats' support as @warning
#   - add/fix '.SECTION whatever' support as @par
#   - add default directories to process
#
# 0.0 (roeim)
#   - first release (thanks to V. Roeim !)


use Carp;
95
use Cwd 'abs_path';
96 97 98 99 100 101 102 103
use Getopt::Long;
use Fcntl;
use File::Basename;
use File::Find;
use File::Path;
use Text::Wrap;
use strict;

Dave DeMarle's avatar
Dave DeMarle committed
104 105 106 107
sub trim {
   return $_[0] =~ s/^\s+|\s+$//rg;
}

108
my ($VERSION, $PROGNAME, $AUTHOR) = (0.9, $0, "Sebastien Barre et al.");
109 110 111
$PROGNAME =~ s/^.*[\\\/]//;

# -------------------------------------------------------------------------
112
# Defaults  (add options as you want: "verbose" => 1 for default verbose mode)
113

114
my %default =
115
  (
116
   dirs => ["../../Charts",
117
            "../../Chemistry",
118
            "../../Common",
119 120 121
            "../../Filtering",
            "../../GenericFiltering",
            "../../GenericFiltering/Testing/Cxx",
122
            "../../Geovis",
123 124
            "../../Graphics",
            "../../GUISupport/MFC",
125
            "../../GUISupport/Qt",
126 127
            "../../Hybrid",
            "../../Imaging",
128
            "../../Infovis",
129 130
            "../../IO",
            "../../Parallel",
131
            "../../Rendering",
132
            "../../Views",
133
            "../../VolumeRendering",
134
            "../../Widgets"],
135
   relativeto => "",
136 137
   temp => "doc_header2doxygen.tmp",
   to => "../../../VTK-doxygen"
138 139 140 141 142 143 144
  );

# -------------------------------------------------------------------------
# Parse options

my %args;
Getopt::Long::Configure("bundling");
145
GetOptions (\%args, "help", "verbose|v", "update|u", "conds|c", "force|f", "temp=s", "to=s", "stdout", "relativeto=s");
146 147

print "$PROGNAME $VERSION, by $AUTHOR\n" if ! exists $args{"stdout"};
148 149 150

if (exists $args{"help"}) {
    print <<"EOT";
151
Usage : $PROGNAME [--help] [--verbose|-v] [--update|-u] [--conds|-c] [--force|-f] [--temp file] [--to path] [--relativeto path] [files|directories...]
152 153 154 155
  --help            : this message
  --verbose|-v      : verbose (display filenames while processing)
  --update|-u       : update (convert only if newer, requires --to)
  --force|-f        : force conversion for all files (overrides --update)
156
  --stdout          : print converted file to standard output
157 158 159
  --temp file       : use 'file' as temporary file (default: $default{temp})
  --to path         : use 'path' as destination directory (default: $default{to})
  --relativeto path : each file/directory to document is considered relative to 'path', where --to and --relativeto should be absolute (default: $default{relativeto})
160
  --conds|-c        : use \cond sections around public, protected, private
161 162 163 164 165 166 167 168

Example:
  $PROGNAME --to ../vtk-doxygen
  $PROGNAME contrib
EOT
    exit;
}

169 170
$args{"verbose"} = 1 if exists $default{"verbose"};
$args{"update"} = 1 if exists $default{"update"};
171
$args{"conds"} = 1 if exists $default{"conds"};
172 173
$args{"force"} = 1 if exists $default{"force"};
$args{"temp"} = $default{temp} if ! exists $args{"temp"};
174 175
$args{"to"} = $default{"to"} if ! exists $args{"to"};
$args{"to"} =~ s/[\\\/]*$// if exists $args{"to"};
176 177
$args{"relativeto"} = $default{"relativeto"} if ! exists $args{"relativeto"};
$args{"relativeto"} =~ s/[\\\/]*$// if exists $args{"relativeto"};
178

179
croak "$PROGNAME: --update requires --to\n"
180
  if exists $args{"update"} && ! exists $args{"to"};
181 182 183 184

my $os_is_win = ($^O =~ m/(MSWin32|Cygwin)/i);
my $open_file_as_text = $os_is_win ? O_TEXT : 0;
my $start_time = time();
185

186 187 188 189 190
# -------------------------------------------------------------------------
# Collect all files and directories

push @ARGV, @{$default{dirs}} if !@ARGV;

191
print "Collecting...\n" if ! exists $args{"stdout"};
192 193 194 195 196 197 198 199 200 201 202 203
my @files;
foreach my $file (@ARGV) {
    if (-f $file) {
        push @files, $file;
    } elsif (-d $file) {
        find sub { push @files, $File::Find::name; }, $file;
    }
}

# -------------------------------------------------------------------------
# Process files corresponding to headers

204
print "Converting...\n" if ! exists $args{"stdout"};
205
my $intermediate_time = time();
206
my $nb_file = 0;
Dave DeMarle's avatar
Dave DeMarle committed
207
my @myval = "";
208 209

foreach my $source (@files) {
210

211 212
    next if $source !~ /vtk[^\\\/]*\.h\Z/;

213 214 215 216 217 218
    # Figure out destination file now

    my $dest;
    if (! exists $args{"to"}) {
        $dest = $args{"temp"};
    } else {
219 220
        # if source has absolute path, just use the basename, unless a
        # relativeto path has been set
221
        if ($source =~ m/^(\/|[a-zA-W]\:[\/\\])/) {
222
            if ($args{"relativeto"}) {
223
                my ($dir, $absrel) = (abs_path(dirname($source)),
224 225 226 227 228 229
                                      abs_path($args{"relativeto"}));
                $dir =~ s/$absrel//;
                $dest = $args{"to"} . $dir . '/' . basename($source);
            } else {
                $dest = $args{"to"} . '/' . basename($source);
            }
230 231
        } else {
            my $source2 = $source;
232
            # let's remove the ../ component before the source filename, so
233 234 235 236 237 238 239 240 241 242 243 244 245
            # that it might be appended to the "to" directory
            $source2 =~ s/^(\.\.[\/\\])*//;
            $dest = $args{"to"} . '/' . $source2;
        }
        # Ensure both source and target are different
        if (!$os_is_win) {
            my ($i_dev, $i_ino) = stat $source;
            my ($o_dev, $o_ino) = stat $dest;
            croak "$PROGNAME: sorry, $source and $dest are the same file\n"
              if ($i_dev == $o_dev && $i_ino == $o_ino);
        }
    }

246
    # Update mode : skip the file if it is not newer than the
247
    # previously converted target
248

249 250 251
    if (exists $args{"update"} && ! exists $args{"force"}) {
        next if -e $dest && (stat $source)[9] < (stat $dest)[9];
    }
252

253 254 255 256 257 258 259 260 261
    ++$nb_file;
    print "  $source\n" if exists $args{"verbose"};

    # Open file, feed it entirely to an array

    sysopen(HEADERFILE, $source, O_RDONLY|$open_file_as_text)
      or croak "$PROGNAME: unable to open $source\n";
    my @headerfile = <HEADERFILE>;
    close(HEADERFILE);
262

263 264 265
    my ($date, $revision) = ("", "");
    my @converted = ();
    my @thanks = ();
266

267
    # Parse the file until the beginning of the documentation block
268
    # is found. The copyright and disclaimer sections are parsed to
269
    # extract the 'Date', 'Version' and 'Thanks' values.
270

271 272 273
    my $line;
    while ($line = shift @headerfile) {

274
        # Quit if the beginning of the documentation block has been reached.
275 276
        # It is supposed to start with:
        # // .NAME vtkFooBar - foo bar class
277

278
        last if $line =~ /\/\/ \.NAME/;
279

280 281 282 283 284 285 286 287 288 289 290 291 292 293
        # Date. Example:
        # Date:      $Date$

        if ($line =~ /^\s*Date:\s*(.*)$/) {
            $date = $1;

        # Version. Example:
        # Version:   $Revision$

        } elsif ($line =~ /^\s*Version:\s*(.*)$/) {
            $revision = $1;

        # Thanks (maybe multi-lines). Example:
        # Thanks:    Thanks to Sebastien Barre who developed this class.
294

295 296 297 298 299 300 301 302 303 304 305 306 307 308
        } elsif ($line =~ /^\s*Thanks:\s*(.*)$/) {
            push @thanks, "             ", $1, "\n";
            # Handle multi-line thanks
            while ($line = shift @headerfile) {
                last if $line =~ /^\s*$/;
                $line =~ s/^(\s*)//;
                push @thanks, "             ", $line;
            }
            push @converted, $line;

        # Everything else goes to the converted file

        } else {
            push @converted, $line;
309
        }
310
    }
311

312 313 314
    # Process the documentation block
    # Extract the name of the class and its short description
    # // .NAME vtkFooBar - foo bar class
315

316
    if (defined($line) && $line =~ /\/\/ \.NAME (\w*)( \- (.*))?/) {
317

318 319
        my ($class_name, $class_short_description) = ($1, $3);
        $class_name =~ s/\.h//;
320

321
        # Insert class description, date, revision, thanks
322

Dave DeMarle's avatar
Dave DeMarle committed
323 324 325
        push @converted, "/**\n";
        push @converted, " * \@class   $class_name\n";
        push @converted, " * \@brief   $class_short_description\n"
326 327 328
          if $class_short_description;

        if ($date) {
Dave DeMarle's avatar
Dave DeMarle committed
329
            push @converted, "\n * $date\n";
330 331
        }

332
        # WARNING : need a blank line between RCS tags and previous dox tag
333

334 335
        if ($revision) {
            push @converted, "\n" if (!$date);
Dave DeMarle's avatar
Dave DeMarle committed
336
            push @converted, " * $revision\n";
337
        }
338

339 340
        # Do not add thanks anymore. Will be done externally.
        # push @converted, "    \@par     Thanks:\n", @thanks if @thanks;
341

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
        # Read until the end of the documentation block is reached
        # Translate 'See Also', 'Caveats' and whatever .SECTION
        # As of 24 sep 2001, there are:
        #      137	// .SECTION Caveats
        #        1	// .SECTION Credits
        #      702	// .SECTION Description
        #        3	// .SECTION Note
        #        1	// .SECTION note
        #      329	// .SECTION See Also
        #        4	// .SECTION See also
        #       70	// .SECTION see also
        #        1	// .SECTION Warning
        # find . -name vtk\*.h -exec grep "\.SECTION" {} \; | sort | uniq -c
        # Let's provide support for bugs too:
        #         	// .SECTION Bug
        #         	// .SECTION Bugs
        #         	// .SECTION Todo

        my ($tag, $inblock) = ("", 0);
        while ($line = shift @headerfile) {
362 363

            # Quit if the end of the documentation block has been reached.
364 365
            # Let'say that it is supposed to end as soon as the usual
            # inclusion directives are found, for example:
366 367
            # #ifndef vtkAbstractTransform_h
            # #define vtkAbstractTransform_h
368

369
            last if $line =~ /^\#/;
370

371 372
            # Process and recognize a .SECTION command and convert it to
            # the corresponding doxygen tag ($tag)
373

374
            if ($line =~ /^\/\/\s+\.SECTION\s+(.+)\s*$/i) {
375

376
                my $type = $1;
377

378 379 380
                # Bugs (@bugs). Starts with:
                # // .SECTION Bug
                # // .SECTION Bugs
381

382 383 384
                if ($type =~ /Bugs?/i) {
                    $tag = "\@bug";
                }
385

386 387 388 389
                # Caveats or Warnings (@warning). Starts with:
                # // .SECTION Caveats
                # // .SECTION Warning
                # // .SECTION Warnings
390

391 392 393
                elsif ($type =~ /(Caveats|Warnings?)/i) {
                    $tag = "\@warning";
                }
394

395 396
                # Description. Starts with:
                # // .SECTION Description
397

398 399
                elsif ($type =~ /Description/i) {
                    $tag = "";
Dave DeMarle's avatar
Dave DeMarle committed
400
                    push @converted, " *\n";
401
                }
402 403 404 405 406 407

                # Note (@attention). Starts with:
                # // .SECTION Note

                elsif ($type =~ /Note/i) {
                    $tag = "\@attention";
408
                }
409 410 411

                # See also (@sa). Starts with:
                # // .SECTION See Also
412

413 414
                elsif ($type =~ /See Also/i) {
                    $tag = "\@sa";
415
                }
416 417 418 419 420 421

                # Todo (@todo). Starts with:
                # // .SECTION Todo

                elsif ($type =~ /Todo/i) {
                    $tag = "\@todo";
422 423
                }

424 425
                # Any other .SECTION (@par). Starts with:
                # // .SECTION whatever
426

427 428 429 430 431 432
                else {
                    $tag =  "\@par " . $type . ":";
                }

                $inblock = 0;
            }
433

434
            # If the line starts with '//', we are still within the tag block.
435 436
            # Remove '//' for non empty lines, eventually put or duplicate
            # the tag name if an empty comment is found (meaning that a new
437 438 439 440 441 442 443 444 445 446 447 448 449 450
            # 'paragraph' is requested but with the same tag type)
            # Example:
            #  // .SECTION Caveats
            #  // blabla1q
            #  // blabla1b
            #  //
            #  // blabla2
            # Gets translated into:
            #      @warning
            #   blabla1q
            #   blabla1b
            #
            #      @warning
            #   blabla2
451

452 453 454
            elsif ($line =~ /^\/\/(.*)/) {
                my $remaining = $1;
                if ($remaining =~ /\S/) {
Dave DeMarle's avatar
Dave DeMarle committed
455
                    push @converted, " * $tag\n"
456
                      if $tag ne "" && ! $inblock;
Dave DeMarle's avatar
Dave DeMarle committed
457
                    push @converted, " *", $remaining, "\n";
458
                    $inblock = 1;
459
                } else {
Dave DeMarle's avatar
Dave DeMarle committed
460
                    push @converted, " *\n";
461
                    $inblock = 0;
462
                }
463 464 465 466 467
            } else {
                # Does not starts with // but still within block or just
                # before the end (#). Probably an empty line.
                # Hack : let's have a look at the next line... if it begins
                # with // then the current line is included (was a space).
468

469
                if (my $next_line = shift @headerfile) {
Dave DeMarle's avatar
Dave DeMarle committed
470
                    push @converted, " *", $line if $next_line =~ /^\/\//;
471
                    unshift @headerfile, $next_line;
472 473 474
                }
            }
        }
475

476
        # Close the doxygen documentation block describing the class
477

478 479
        push @converted, "*/\n\n", $line;
    }
480

481 482 483 484 485 486 487 488
    # Read until the end of the header and translate the description of
    # each function provided that it is located in a C++ comment
    # containing the 'Description:' keyword.
    # Example:
    #  // Description:
    #  // Construct with automatic computation of divisions, averaging
    #  // 25 points per bucket.
    #  static vtkPointLocator2D *New();
489 490 491

    my $in_section = "";

492
    while ($line = shift @headerfile) {
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
        # Track the public:, protected: and private: sections and put them
        # between \cond... \endcond so that they can be removed from the
        # documentation conditionally. Add them to ENABLED_SECTION
        # to show them.
        # IMPORTANT: *no* spaces are allowed between the beginning of the
        # line and the qualifier. This is mandatory to solve issues
        # with nested class definitions, since it is non-trivial to
        # track the fact that we are leaving a class definition to
        # re-enter the parent class definition, etc.

        if (exists $args{"conds"}) {
            if ($line =~ /^(public|protected|private):/) {
                if ($in_section ne "") {
                    push @converted, "// \@endcond\n";
                }
                $in_section = $1;
                push @converted, "// \@cond section_$in_section\n";
            }
        }

514
        if ($line =~ /^(\s*)\/\/\s*De(s|c)(s|c)?ription/) {
515

516 517
            my $indent = $1;
            $Text::Wrap::columns = 76;
518
            $Text::Wrap::huge = 'overflow';
519

520 521
            # While there are still lines beginning with '//' append them to
            # the function's description and trim spaces.
522

523 524 525 526 527 528 529 530
            my @description = ();
            while ($line = shift @headerfile) {
                last if $line !~ /^\s*\/\//;
                chop $line;
                $line =~ s/^\s*\/\/\s*//;
                $line =~ s/\s*$//;
                push @description, $line;
            }
531

532 533 534
            # While there are non-empty lines or a new Descrption line add
            # these lines to the list of declarations
            # (and/or inline definitions) pertaining to the same description.
535 536

            my @declarations = ();
Dave DeMarle's avatar
Dave DeMarle committed
537
            my $semicnt = 0;
538
            while ($line && $line =~ /\s*\S/) {
539
                push @declarations, $line;
Dave DeMarle's avatar
Dave DeMarle committed
540 541 542 543
                if ($line =~ /;$/)
                {
                    $semicnt = $semicnt + 1;
                }
544 545 546 547 548 549 550
                # terminate if we encounter another Description line
                # that doesn't have a leading empty line
                if ($headerfile[0] !~ /^(\s*)\/\/\s*De(s|c)(s|c)?ription/ ) {
                    $line = shift @headerfile;
                }else{
                    $line = "";
                }
551
            }
552 553 554

            # If there is more than one declaration or at least a macro,
            # enclose in a group (actually a single multiline declaration will
Dave DeMarle's avatar
Dave DeMarle committed
555 556 557
            # be enclosed too, but who cares :). (Berk cares, so try and
            # additionally check and only enclose if multiline has multiple
            # statements in it.)
558 559

            my $enclose =
Dave DeMarle's avatar
Dave DeMarle committed
560 561
                ($declarations[0] =~ /vtk.+Macro/ ||
                 (scalar @declarations > 1 && $semicnt > 1));
562

563
            push @converted, "$indent//@\{\n" if $enclose;
Dave DeMarle's avatar
Dave DeMarle committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581

            if (@description) {              
              for my $i (0 .. $#description)
                {
                  if ($i eq 0) {
                    push @converted, "$indent/**\n";
                  }
                  if (length(trim($description[$i]))) {
                    push @converted, "$indent * ", trim($description[$i]), "\n";
                  } else {
                    push @converted, "\n";
                  }
                    
                  if ($i eq $#description) {
                    push @converted, "$indent */\n";
                  }                  
                }
            }
582
            push @converted, @declarations;
Dave DeMarle's avatar
Dave DeMarle committed
583
            push @converted, "$indent//@\}\n" if $enclose;              
584
        }
585

586 587
        push @converted, $line;
    }
588

589 590 591 592 593 594
    if (exists $args{"conds"}) {
        if ($in_section ne "") {
            push @converted, "// \@endcond";
        }
    }

595
    # Write the converted header to its destination
596
    # or to standard output.
597

598
    if (exists $args{"stdout"}) {
599

600 601 602 603 604 605
        print  @converted;

    } else {

        # Open the target and create the missing directory if any

606 607
        if (!sysopen(DEST_FILE,
                     $dest,
608 609 610
                     O_WRONLY|O_TRUNC|O_CREAT|$open_file_as_text)) {
            my $dir = dirname($dest);
            mkpath($dir);
611 612
            sysopen(DEST_FILE,
                    $dest,
613 614 615 616 617
                    O_WRONLY|O_TRUNC|O_CREAT|$open_file_as_text)
              or croak "$PROGNAME: unable to open destination file $dest\n";
        }
        print DEST_FILE @converted;
        close(DEST_FILE);
618

619 620 621 622 623 624 625 626 627
        # If in-place conversion was requested, remove source and rename target
        # (or temp file) to source

        if (! exists $args{"to"}) {
            unlink($source)
              or carp "$PROGNAME: unable to delete original file $source\n";
            rename($args{"temp"}, $source)
              or carp "$PROGNAME: unable to rename ", $args{"temp"}, " to $source\n";
        }
628
    }
629 630
}

631 632 633 634
if (! exists $args{"stdout"}) {
    print " => $nb_file files converted in ", time() - $intermediate_time, " s. \n";
    print "Finished in ", time() - $start_time, " s.\n";
}