Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / solenv / bin / fix-includes.pl
blob6daae9ef3d523d2e730c4fc34b7b3b2b66cb692c
1 #!/usr/bin/env perl
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 # fix-includes, a simple script replace local includes which should be global
10 # , to global includes. And global includes which should be local, to local includes.
11 # The script is expected to run in the root of the git repo, so it can fetch all the include directory's.
13 use strict;
14 use warnings;
15 use File::Basename;
16 use File::Find;
17 use IO::All;
19 my $dirname = "include";
21 # Fetch the list of includes
22 my @subdirs = map {basename $_} grep {-d} glob("$dirname/*");
24 # Add boost
25 push(@subdirs,"boost");
27 # Simple function to check and replace headers
28 sub check_headers
30 my ($dir,$file, @includes) = @_;
31 open(my $fh,"+<",$file) or die "Couldn't open file $file $!\n";
32 my @content = <$fh>;
33 my $line;
35 # seek to the first line, so we can replace then lines correctly
36 seek $fh,0,0;
37 foreach $line (@content){
38 if($line =~ m/#include "(\w*)\//){
39 # If a include is local and it should be global, make it global
40 if($1 ~~ @includes){
41 print "local header $line\n";
42 $line =~ s/"/</;
43 $line =~ s/"/>/;
44 print $fh $line;
45 print "converted to global header $line\n";
47 else {
48 print $fh $line;
51 # If a local file is defined global, make it local
52 elsif($line =~ /#include <((\w*)\.(hxx|h|hrc|src))>/){
53 # check if file exists, then it must be local so replace the <> to ""
54 if(-e "$dir/$1" ){
55 print "global header $line\n";
56 $line =~ s/</"/g;
57 $line =~ s/>/"/g;
58 print $fh $line;
59 print "converted to local header $line\n";
61 else {
62 print $fh $line;
65 else {
66 print $fh $line;
69 close($fh);
72 # routine that checks the headers of every cxx,hxx,c,h,hrc,src file in a directory
73 sub check_routine
75 my ($dir) = @_;
76 opendir(my $fh, $dir) or die "Program stopping, could't open directory \n";
77 while(my $file = readdir($fh)){
78 if($file =~ m/\.(cxx|hxx|c|h|hrc|src)$/i ){
79 check_headers($dir,"$dir/$file",@subdirs);
82 closedir($fh);
85 # Expect ARGV[0] to be a directory, then fetch all subdirectory's and check the header files.
86 if(-d $ARGV[0]){
87 my @directories = io->dir($ARGV[0])->All_Dirs;
88 foreach my $dir (@directories){
89 print "checking header files in $dir\n";
90 check_routine($dir);
93 else{
94 print "$ARGV[0] isn't a directory\n";