r22454: - let asn1_deps.pl calculate the dependencies depending on the IMPORT line...
[gd/samba/.git] / source4 / heimdal_build / asn1_deps.pl
1 #!/usr/bin/perl
2 # Generate make dependency rules for asn1 files
3 # Jelmer Vernooij <jelmer@samba.org> 2005
4 # Stefan Metzmacher <metze@samba.org> 2007
5 # GPL
6
7 use File::Basename;
8
9 my $file = shift;
10 my $prefix = shift;
11 my $dirname = shift;
12 my $options = join(' ', @ARGV);
13 my $x_file;
14 my @x_files = ();
15 my $c_file;
16 my @c_files = ();
17 my $o_file;
18 my @o_files = ();
19 my $import;
20 my @imports = ();
21 my $dep;
22 my @deps = ();
23
24 $basename = basename($file);
25 if (not defined $options) {
26     $options = "";
27 }
28
29 my $header = "$dirname/$prefix.h";
30
31 print "$header: $file bin/asn1_compile\n";
32 print "\t\@echo \"Compiling ASN1 file $file\"\n";
33 print "\t\@\$(builddir)/heimdal_build/asn1_compile_wrapper.sh \$(srcdir) \$(builddir) $dirname bin/asn1_compile $file $prefix $options\n\n";
34
35 open(IN,$file) or die("Can't open $file: $!");
36 my @lines = <IN>;
37 close(IN);
38 foreach my $line (@lines) {
39         if ($line =~ /^([\w]+[\w\-]+)(\s+OBJECT IDENTIFIER)?\s*::=/) {
40                 my $output = $1;
41                 $output =~ s/-/_/g;
42                 $c_file = "$dirname/asn1_$output.c";
43                 $x_file = "$dirname/asn1_$output.x";
44                 $o_file = "$dirname/asn1_$output.o";
45                 print "$x_file: $header\n";
46                 print "$c_file: $dirname/asn1_$output.x\n";
47                 print "\t\@cp $x_file $c_file\n\n";
48                 push @x_files, $x_file;
49                 push @c_files, $c_file;
50                 push @o_files, $o_file;
51         } elsif ($line =~ /^(\s*IMPORT)([\w\,\s])*(\s+FROM\s+)([\w]+[\w\-]+);/) {
52                 $import = $line;
53                 chomp $import;
54                 push @imports, $import;
55                 $import = undef;
56         } elsif ($line =~ /^(\s*IMPORT).*/) {
57                 $import = $line;
58                 chomp $import;
59         } elsif (defined($import) and ($line =~ /;/)) {
60                 $import .= $line;
61                 chomp $import;
62                 push @imports, $import;
63                 $import = undef;
64         } elsif (defined($import)) {
65                 $import .= $line;
66                 chomp $import;
67         }
68 }
69
70 foreach $import (@imports) {
71         next unless ($import =~ /^(\s*IMPORT)([\w\,\s])*(\s+FROM\s+)([\w]+[\w\-]+);/);
72
73         my @froms = split (/\s+FROM\s+/, $import);
74         foreach my $from (@froms) {
75                 next if ($from =~ /^(\s*IMPORT).*/);
76                 if ($from =~ /^(\w+)/) {
77                         my $f = $1;
78                         $dep = 'HEIMDAL_'.uc($f).'_ASN1';
79                         push @deps, $dep;
80                 }
81         }
82 }
83
84 unshift @deps, "HEIMDAL_HEIM_ASN1" unless grep /HEIMDAL_HEIM_ASN1/, @deps;
85 my $depstr = join(' ', @deps);
86
87 print '[SUBSYSTEM::HEIMDAL_'.uc($prefix).']'."\n";
88 print "CFLAGS = -Iheimdal_build -Iheimdal/lib/roken -I$dirname\n";
89 print "OBJ_FILES = ";
90 foreach $o_file (@o_files) {
91     print "\\\n\t$o_file";
92 }
93 print "\nPUBLIC_DEPENDENCIES = $depstr\n\n";
94
95 print "clean:: \n";
96 print "\t\@echo \"Deleting ASN1 output files generated from $file\"\n";
97 print "\t\@rm -f $header\n";
98 foreach $c_file (@c_files) {
99     print "\t\@rm -f $c_file\n";
100 }
101 foreach $x_file (@x_files) {
102     print "\t\@rm -f $x_file\n";
103 }
104 print "\t\@rm -f $dirname/$prefix\_files\n";
105 print "\t\@rm -f $dirname/$prefix\.h\n";
106 print "\n";