summaryrefslogtreecommitdiff
path: root/lib/sbin/BuildHeaders.pl
blob: d7707acfe2c6565f5b73a8bae6edb881b4039bd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
 #! /usr/bin/perl -s

#/*@@
#  @routine BuildHeaders
#  @date Sun 13 Sep 1999
#  @author Gabrielle Allen
#  @desc
#  Creates the dynamic header files requested in interface.ccl files
#  and writes them into the Bindings include directory
#  @enddesc
#  @calls
#  @calledby
#  @history
#  @endhistory
#@@*/

require "$sbin_dir/CSTUtils.pl";

sub BuildHeaders
{
  my($cctk_home,$bindings_dir,%database) = @_;
  my($start_dir,$thorn,$inc_file,$inc_file1,$inc_file2,$tmpline);

  $start_dir = `pwd`;
  chdir $bindings_dir;
  chdir "include";

# First set all data strings 
  foreach $thorn (split(" ",$interface_database{"THORNS"}))
  {
    foreach $inc_file (split(" ",$interface_database{"\U$thorn USES HEADER"}))
    {
      $data{"$inc_file"} = "/* Include header file $inc_file */\n\n";
    }
    foreach $inc_file (split(" ",$interface_database{"\U$thorn USES SOURCE"}))
    {
      $data{"$inc_file"} = "/* Include source file $inc_file */\n\n";
    }
  }


  # Check consistency
  foreach $addingthorn (split(" ",$interface_database{"THORNS"}))
  {
    foreach $inc_file1 (split(" ",$interface_database{"\U$addingthorn ADD HEADER"}))
    {
      foreach $usingthorn (split(" ",$interface_database{"THORNS"}))
      {
	if ($interface_database{"\U$usingthorn USES SOURCE"} =~ $interface_database{"\U$addingthorn ADD HEADER $inc_file1 TO"})
	{
	  &CST_error(1,"$inc_file1 was added in $addingthorn as a header include but is being used as $interface_database{\"\U$addingthorn ADD HEADER $inc_file1 TO\"}  in $usingthorn as a source code include",'',__LINE__,__FILE__);
	}
      }
    }
  }


# Add the headers from thorns
  foreach $thorn (split(" ",$interface_database{"THORNS"}))
  {
    $arrangement = $interface_database{"\U$thorn ARRANGEMENT"};

    foreach $inc_file1 (split(" ",$interface_database{"\U$thorn ADD HEADER"}))
    {
      if ($inc_file1 !~ /^\s*$/)
      {
        $inc_file1 =~ s/ //g;
        $inc_file2 = $interface_database{"\U$thorn ADD HEADER $inc_file1 TO"};
	
        # Write information to the global include file
        $data{"$inc_file2"} .= "/* Including header file $inc_file1 from $thorn */\n";
        
        # Now have to find the include file and copy it
        if (-e "$cctk_home/arrangements/$arrangement/$thorn/src/$inc_file1")
        {
          $data{"$inc_file2"} .= "#include \"$arrangement/$thorn/src/$inc_file1\"\n\n";
        }
        elsif (-e "$cctk_home/arrangements/$arrangement/$thorn/src/include/$inc_file1")
        {
          $data{"$inc_file2"} .= "#include \"$arrangement/$thorn/src/include/$inc_file1\"\n\n";
        }
        else
        {
          $message = "Include file $inc_file1 not found in $arrangement/$thorn\n";
          &CST_error(0,$message,"",__LINE__,__FILE__);
        }
        $data{"$inc_file2"} .= "/* End of include header file $inc_file1 from $thorn */\n";
      }
    }

    foreach $inc_file1 (split(" ",$interface_database{"\U$thorn ADD SOURCE"}))
    {
      if ($inc_file1 !~ /^\s*$/)
      {
        $inc_file1 =~ s/ //g;
        $inc_file2 = $interface_database{"\U$thorn ADD SOURCE $inc_file1 TO"};

        # Write information to the global include file
        $data{"$inc_file2"} .= "/* Including source file $inc_file1 from $thorn */\n";
        
        # Now have to find the include file and copy it
        if (-e "$cctk_home/arrangements/$arrangement/$thorn/src/$inc_file1")
        {
          $tmpline = "#include \"$arrangement/$thorn/src/$inc_file1\"\n";
        }
        elsif (-e "$cctk_home/arrangements/$arrangement/$thorn/src/include/$inc_file1")
        {
          $tmpline = "#include \"$arrangement/$thorn/src/include/$inc_file1\"\n";
        }
        else
        {
          $message = "Include file $inc_file1 not found in $arrangement/$thorn\n";
          &CST_error(0,$message,"",__LINE__,__FILE__);
        }
         
        $data{"$inc_file2"} .= "#ifdef FCODE\n";
        $data{"$inc_file2"} .= "      if (CCTK_IsThornActive(\"$thorn\").eq.1) then\n";
        $data{"$inc_file2"} .= "#else\n"; 
        $data{"$inc_file2"} .= "if (CCTK_IsThornActive(\"$thorn\")){\n";
        $data{"$inc_file2"} .= "#endif\n";           
        $data{"$inc_file2"} .= "$tmpline\n";
        $data{"$inc_file2"} .= "#ifdef FCODE\n";
        $data{"$inc_file2"} .= "      end if\n";
        $data{"$inc_file2"} .= "#else\n"; 
        $data{"$inc_file2"} .= "\n}\n";
        $data{"$inc_file2"} .= "#endif\n";           

        $data{"$inc_file2"} .= "/* End of include source file $inc_file1 from $thorn */\n";
      }
    }

  }

  foreach $thorn (split(" ",$interface_database{"THORNS"}))
  {
    foreach $inc_file1 (split(" ",$interface_database{"\U$thorn USES HEADER"}))
    {
      &WriteFile($inc_file1,\$data{"$inc_file1"});
    }
    foreach $inc_file1 (split(" ",$interface_database{"\U$thorn USES SOURCE"}))
    {
      &WriteFile($inc_file1,\$data{"$inc_file1"});
    }
  }

  chdir $start_dir;
  return;

}

1;