1#!/usr/bin/perl 2############################################################################## 3# # 4# UNURAN -- Universal Non-Uniform Random number generator # 5# # 6############################################################################## 7# # 8# FILE: merge_h.pl # 9# # 10# Read all UNU.RAN header files, strip comments and create single header # 11# file 'unuran.h' # 12# # 13############################################################################## 14# # 15# Copyright (c) 2000-2006 Wolfgang Hoermann and Josef Leydold # 16# Department of Statistics and Mathematics, WU Wien, Austria # 17# # 18# This program is free software; you can redistribute it and/or modify # 19# it under the terms of the GNU General Public License as published by # 20# the Free Software Foundation; either version 2 of the License, or # 21# (at your option) any later version. # 22# # 23# This program is distributed in the hope that it will be useful, # 24# but WITHOUT ANY WARRANTY; without even the implied warranty of # 25# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # 26# GNU General Public License for more details. # 27# # 28# You should have received a copy of the GNU General Public License # 29# along with this program; if not, write to the # 30# Free Software Foundation, Inc., # 31# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA # 32# # 33############################################################################## 34 35use strict; 36 37my $DEBUG = 0; 38 39############################################################ 40# constants 41 42my $DEP_file = ".dep-unuran_h"; 43 44############################################################ 45 46sub usage { 47 my $progname = $0; 48 $progname =~ s#^.*/##g; 49 50 print STDERR <<EOM; 51usage: $progname <file.h> 52 53Scans <file.h> and inserts all header files found in subtree 54rooted at current working directory. 55Other header files and those with names containing "config" 56are ignored and just #included. 57All comments and blank lines are removed. 58The output is written on stdout. 59 60EOM 61 62 exit -1; 63} 64 65############################################################ 66 67# dependencies 68my $DEP = "unuran.h: "; 69 70############################################################ 71 72sub h_file_header { 73 print <<EOM; 74/* file automatically generated by unuran/scripts/merge_h.pl */ 75 76/***************************************************************************** 77 * * 78 * UNU.RAN -- Universal Non-Uniform Random number generator * 79 * * 80 ***************************************************************************** 81 * * 82 * FILE: unuran.h * 83 * * 84 ***************************************************************************** 85 * * 86 * Copyright (c) 2000-2006 Wolfgang Hoermann and Josef Leydold * 87 * Dept. for Statistics, University of Economics, Vienna, Austria * 88 * * 89 * This program is free software; you can redistribute it and/or modify * 90 * it under the terms of the GNU General Public License as published by * 91 * the Free Software Foundation; either version 2 of the License, or * 92 * (at your option) any later version. * 93 * * 94 * This program is distributed in the hope that it will be useful, * 95 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 96 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 97 * GNU General Public License for more details. * 98 * * 99 * You should have received a copy of the GNU General Public License * 100 * along with this program; if not, write to the * 101 * Free Software Foundation, Inc., * 102 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA * 103 * * 104 *****************************************************************************/ 105 106#undef __BEGIN_DECLS 107#undef __END_DECLS 108#ifdef __cplusplus 109# define __BEGIN_DECLS extern "C" { 110# define __END_DECLS } 111#else 112# define __BEGIN_DECLS /* empty */ 113# define __END_DECLS /* empty */ 114#endif 115 116__BEGIN_DECLS 117 118EOM 119 120} # end of h_file_header() 121 122############################################################ 123 124sub h_file_bottom { 125 print "__END_DECLS\n"; 126} # end of h_file_bottom() 127 128############################################################ 129 130use FileHandle; 131 132############################################################ 133 134# read master file name from argument list ... 135my $master_file = shift; 136(usage and die) unless $master_file; 137 138# header files in sub tree (except those containing "config") ... 139# (files are stored in an associate array with key=filename and 140# value=complete path of file.) 141my %header_files; 142# included header files ... 143my %header_included; 144 145# search subtree for header files ... 146open (FILES, "find . |"); 147while (<FILES>) { 148 chomp; 149 next unless /^.*\/+(.*\.h)$/; 150 next if /config/; 151 $header_files{$1} = $_; # store file and path of file 152} 153close FILES; 154 155# insert file header ... 156h_file_header; 157 158# scan master file ... 159scan_file ($master_file,0); 160 161# insert bottom of file 162h_file_bottom; 163 164# write dependencies 165open DEP, ">$DEP_file" or die "Cannot open file for writing: $DEP_file"; 166print DEP "$DEP\n"; 167close DEP; 168 169exit 0; 170 171############################################################ 172 173# scan given file ... 174sub scan_file { 175 my $file = $_[0]; 176 my $level = $_[1]+1; 177 my $handle = new FileHandle; 178 179 print STDERR "$file\n"; 180 181 # open file ... 182 open $handle, $file or die "cannot find file $file\n"; 183 184 # read file ... 185 my $content = ''; 186 while (<$handle>) { 187 $content .= $_; 188 } 189 close $handle; 190 191 # remove all comments and empty lines ... 192 $content =~ s {/\*.*?\*/} []gsx; 193 $content =~ s /\n\s*\n/\n/gsx; 194 195 # split into lines ... 196 my @lines = split /\n/, $content; 197 198 foreach my $line (@lines) { 199 unless ($line =~ /^\#include\s+[<\"](.*)[>\"]/) { 200 print "$line\n"; 201 next; 202 } 203 204 # have found a file to be included ... 205 my @tmp = split /\//, $1; 206 my $include_file = pop @tmp; 207 print STDERR "$include_file " if $DEBUG; 208 209 # we do not include header files out of the subtree ... 210 unless (defined( $header_files{$include_file} ) ) { 211 print STDERR "file not found in subtree ... #include\n" if $DEBUG; 212 print "\n$line\n\n"; 213 next; 214 } 215 216 # we include header files only once ... 217 if (defined( $header_included{$include_file} ) ) { 218 print STDERR "already included ... skip\n" if $DEBUG; 219 next; 220 } 221 222 $header_included{$include_file} .= 1; 223 224 # have found header file ... 225 print STDERR "to be inserted\n" if $DEBUG; 226 print "/*-----*/\n"; 227 print "/* <$level> `$include_file' */"; 228 229 # add dependency 230 $DEP .= "$header_files{$include_file} "; 231 232 # scan header file ... 233 scan_file ($header_files{$include_file},$level); 234 235 print "/* end of `$include_file' */\n"; 236 print "/*-----*/\n"; 237 } 238 239} 240 241############################################################ 242 243############################################################ 244 245