From 8c5626cd66bf271a8691234ff147a8d03d0a2328 Mon Sep 17 00:00:00 2001 From: Prabhanjan Kambadur Date: Tue, 5 Oct 2004 23:52:35 +0000 Subject: [PATCH] Some perl scripts to fix headers in windows. Not yet perfect This commit was SVN r2942. --- contrib/fix_headers.pl | 125 +++++++++++++++++++++++++ contrib/test_headers_in_ompi.pl | 161 ++++++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+) create mode 100755 contrib/fix_headers.pl create mode 100755 contrib/test_headers_in_ompi.pl diff --git a/contrib/fix_headers.pl b/contrib/fix_headers.pl new file mode 100755 index 0000000000..37769ea793 --- /dev/null +++ b/contrib/fix_headers.pl @@ -0,0 +1,125 @@ +#!/usr/bin/perl + +if (scalar(@ARGV) != 1) { + print "Usage: + ./fix_headers.pl \n"; + exit(3); +} + +$header_file = @ARGV[0]; +$temp_file = "/tmp/temp.c"; + +open(HEADERS, "$header_file") || print "Could not open $header_file\n"; +open(MOD_FILES, "> modified_files.txt") || print "Could not open modified.txt\n"; + +while () { + + #open all the c files + #check if this header is present + #if it is present, then substitute it with the protection + + + $header_string = $_; + chomp($header_string); + $protection = $_; + $protection =~ s/\./_/; + $protection =~ s/\//_/; + $protection =~ s/#include//; + $protection =~ tr/a-z/A-z/; + $protection =~ s/\s//; + $protection = "HAVE_" . $protection; + + print $protection; + + $string_to_replace = "#ifdef $protection$_#endif\n"; + + print $string_to_replace; + + open(C_FILES, "find . -name *.c |") || print "find failed\n"; + while () { + $c_file = $_; + if (not /mca/) { + open(C_FILE, "$c_file") || print "Open failed on $c_file\n"; + chomp($protection); + + #ensure that this protection has not been already put in place + $protected = 0; + $written_to_file = 0; + + while () { + if (/$protection/) { + $protected = 1; + } + } + close (C_FILE); + if ($protected == 0) { + #this file is not yet protected + open(C_FILE, "$c_file") || print "Open failed on $c_file\n"; + open(TEMP, "> $temp_file") || print "Open failed on temp.c \n"; + + while () { + if (/$header_string/) { + print TEMP $string_to_replace; + print "Replacing defintion ---- $c_file"; + if ($written_to_file == 0) { + print MOD_FILES $c_file; + $written_to_file = 1; + } + } else { + print TEMP $_; + } + } + close (TEMP_C); + system("cp $temp_file $c_file"); + } + } + } + close (C_FILES); + + #Now to do the same for header files + open(H_FILES, "find . -name *.h |") || print "find failed\n"; + while () { + + $h_file = $_; + if (not /mca/) { + open(H_FILE, "$h_file") || print "Open failed on $h_file\n"; + chomp($protection); + + #ensure that this protection has not been already put in place + $protected = 0; + $written_to_file = 0; + + while () { + if (/$protection/) { + $protected = 1; + } + } + close (H_FILE); + if ($protected == 0) { + #this file is not yet protected + open(H_FILE, "$h_file") || print "Open failed on $h_file\n"; + open(TEMP, "> $temp_file") || print "Open failed on temp.c \n"; + + while () { + if (/$header_string/) { + print TEMP $string_to_replace; + print "Replacing defintion ---- $h_file"; + if ($written_to_file == 0) { + print MOD_FILES $h_file; + $written_to_file = 1; + } + } else { + print TEMP $_; + } + } + close (TEMP_C); + system("cp $temp_file $h_file"); + } + } + } + close (H_FILES); +} +close(HEADERS); +close(MOD_FILES); +system("rm -f $temp_file"); diff --git a/contrib/test_headers_in_ompi.pl b/contrib/test_headers_in_ompi.pl new file mode 100755 index 0000000000..edf0f6d5ef --- /dev/null +++ b/contrib/test_headers_in_ompi.pl @@ -0,0 +1,161 @@ +#!/usr/bin/perl + +#this is the perl scripty foo which does the following tasks +# 1. Extract the #include <*.h> files which are present in both header and source files +# 2. Do some basic formatting +# 3. Check if these included files are present on the platform which has been given + +if (scalar(@ARGV) != 2) { + print "Usage: + ./depend.pl \n"; + exit(3); +} + +$includes_file = "headers.txt"; +$return = &get_header_files($includes_file); + +$test_file = "test_headers.txt"; +$return = &parse_header_files($includes_file, $test_file); + +$source_tree = @ARGV[0]; +$CC = @ARGV[1]; +$result_file = "results.txt"; +$return = &test_for_headers($test_file, $result_file, $CC); + +# this file is used to extract which header files are included in a particular +# source file. Kind of a neat implementation +sub get_header_files { + + local($dump_file) = @_; + + open(C_FILES, "find $source_tree -name \*.c |") || print "could not find source files\n"; + open(H_FILES, "find $source_tree -name \*.c |") || print "could not find header files\n"; + + open(DUMP, "> $dump_file") || print "Could not open $dump_file\n"; + + while () { + $file_h = $_; + print DUMP "Processing $file_h"; + open(FILE_H, "$file_h") || print "could not open file for reading\n"; + + #grep for the pattern which we want + while () { + if (/#include ) { + $file_h = $_; + print DUMP "Processing $file_h"; + open(FILE_H, "$file_h") || print "could not open file for reading\n"; + + #grep for the pattern which we want + while () { + if (/#include $test_file") || print "Could not open $test_file for reading\n"; + + while () { + if (/#include temp.txt") || print "Could not open temp.txt for writing\n"; + + while(
) { + #remove leading white spaces + s/^\s*//; + #remove anything after <*.h> + s/>{1,1}.*\n/>\n/; + #remove anything before #include + s/^.*#include/#include/; + print TEMP $_; + } + + close(HEADER); + close(TEMP); + + #remove duplicate occurences of the file + system("sort temp.txt | uniq > $test_file"); + + return 0; +} + +#this suroutine is used to test if a particular header is present or absent in a particular language +sub test_for_headers { + + local($test_file, $result_file, $CC) = @_; + local($temp) = "temp.c"; + + print "CC = $CC\n"; + + open(HEADER, "$test_file") || print "Could not open $test_file for reading\n"; + open(RESULTS, "> $result_file") || print "Could not open $result_file for writing\n"; + + while(
) { + print $_; + + #create the file for compilation + chomp $_; + $string = " + + $_ /*this is the include file to be tested for*/ + + int main(int argc, char **argv) { + return 0; + } + + "; + + open(TEMP, "> $temp") || print "Could not open $temp for writing\n"; + print TEMP $string; + close(TEMP); + + $compiled = system("$CC $temp"); + + if ($compiled == 0) { + print "$_ is present\n"; + } else { + print RESULTS "$_\n"; + } + + system("rm -Rf $temp"); + system("rm -Rf temp.*"); + + } + + close(HEADER); + close(RESULTS); + + return 0; +}