cgal/Scripts/scripts/cgal_conditional_include_re...

172 lines
4.8 KiB
Perl
Executable File

#!/usr/bin/env perl
# Copyright (c) 1999,2005,2006 Utrecht University (The Netherlands),
# ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
# INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
# (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
# and Tel-Aviv University (Israel). All rights reserved.
#
# This file is part of CGAL (www.cgal.org); you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; version 2.1 of the License.
# See the file LICENSE.LGPL distributed with CGAL.
#
# Licensees holding a valid commercial license may use this file in
# accordance with the commercial license agreement provided with the software.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# $URL$
# $Id$
#
# Author(s) : Geert-Jan Giezeman
# usage: cgal_conditional_include file1 ...
# Protects CGAL and other include directives in the following way:
# #ifndef CGAL_HANDLE_H
# #include <CGAL/Handle>
# #endif
#
# is converted to
#
# #include <CGAL/Handle>
# #ifndef CGAL_PROTECT_STDLIB_H
# #include <stdlib.h>
# #define CGAL_PROTECT_STDLIB_H
# #endif
#
# is converted to
#
# #include <stdlib.h>
use strict;
my $protect_name;
my $suffix;
my $tmpfilename = "CondIncl$$";
my $filename;
my $skipped = 0;
FILE:
foreach $filename (@ARGV) {
if (!open(INFILE, $filename)) {
print STDERR "Could not open ${filename} for reading\n$!\n";
next FILE;
}
open(TMPFILE, ">$tmpfilename")
|| die "Could not open temp file for writing\n$!\n";
my $last = '';
Line:
while (<INFILE>) {
if (($protect_name,$suffix) =
m|^[\s]*#[\s]*ifndef[\s]+CGAL_PROTECT_(.*?)(_H)?\b|)
{
my $protect_line = $_;
$_ = <INFILE>;
my $include_file;
if ( !(($include_file) =
m|^[\s]*#[\s]*include[\s]*<(.*?)(?:\.h)?>[\s]*|)) {
warn "'#include <...>' missing in line ",
$.-$skipped," ($filename)\n";
print TMPFILE $protect_line;
redo;
}
my $include_line = $_;
$include_file =~ s'/'_'g;
$include_file =~ tr/a-z/A-Z/;
if ($include_file ne $protect_name) {
warn "'#ifndef CGAL_PROTECT_$protect_name$suffix'",
" followed by non corresponding #include in line ",
$.-$skipped," ($filename)\n";
print TMPFILE $protect_line;
print TMPFILE $include_line;
next;
}
$_ = <INFILE>;
my $protect_name2;
if (!(($protect_name2) =
m|^[\s]*#[\s]*define[\s]+CGAL_PROTECT_(.*?)(?:_H)?[\s]*$|)) {
warn "'#define CGAL_PROTECT_$protect_name$suffix' missing in line ",
$.-$skipped," ($filename)\n";
print TMPFILE $protect_line;
print TMPFILE $include_line;
redo;
}
my $protect_line2 = $_;
if ($protect_name2 ne $protect_name) {
warn "'#ifndef CGAL_PROTECT_$protect_name$suffix' lacks ",
"'#define CGAL_PROTECT_$protect_name$suffix' in line ",
$.-$skipped," ($filename)\n";
print TMPFILE $protect_line;
print TMPFILE $include_line;
print TMPFILE $protect_line2;
next;
}
$_ = <INFILE>;
if ( ! m|^[\s]*#[\s]*endif|) {
warn "'#ifndef CGAL_PROTECT_$protect_name$suffix' lacks ",
"'#endif' in line ", $.-$skipped," ($filename)\n";
print TMPFILE $protect_line;
print TMPFILE $include_line;
print TMPFILE $protect_line2;
redo;
}
print TMPFILE $include_line;
$skipped += 3;
}
elsif ((($protect_name,$suffix) =
m|^[\s]*#[\s]*ifndef[\s]+CGAL_(.*?)(_H)?[\s]*$|))
{
my $protect_line;
$protect_line = $_;
$_ = <INFILE>;
my $include_file;
if ( !(($include_file) =
m|^[\s]*#[\s]*include[\s]*<CGAL\/(.*?)(?:\.h)?>[\s]*|)) {
print TMPFILE $protect_line;
redo;
}
my $include_line = $_;
$include_file =~ s'/'_'g;
$include_file =~ tr/a-z/A-Z/;
if ($include_file ne $protect_name) {
warn "'#ifndef CGAL_$protect_name$suffix'",
" followed by non corresponding #include in line ",
$.-$skipped," ($filename)\n";
print TMPFILE $protect_line;
print TMPFILE $include_line;
next;
}
$_ = <INFILE>;
if ( ! m|^[\s]*#[\s]*endif|) {
warn "'#ifndef CGAL_$protect_name$suffix' lacks ",
"'#endif' in line ", $.-$skipped," ($filename)\n";
print TMPFILE $protect_line;
print TMPFILE $include_line;
redo;
}
print TMPFILE $include_line;
$skipped += 2;
} else
{
# copy other lines
print TMPFILE $_;
}
}
close INFILE;
close TMPFILE;
if ($?) {
print STDERR "Conversion of ${filename} failed\n$!\n";
next FILE;
}
if (!rename($tmpfilename,${filename})) {
print STDERR "Cannot rename $tmpfilename to ${filename}.\n$!\n";
print STDERR "Conversion of ${filename} failed\n";
}
}