]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/unit-cc.pl
unit-cc: Use the same preamble as other Perl tools.
[user/henk/code/inspircd.git] / make / unit-cc.pl
1 #!/usr/bin/env perl
2
3 #
4 # InspIRCd -- Internet Relay Chat Daemon
5 #
6 #   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
7 #
8 # This file is part of InspIRCd.  InspIRCd is free software: you can
9 # redistribute it and/or modify it under the terms of the GNU General Public
10 # License as published by the Free Software Foundation, version 2.
11 #
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15 # details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21
22 BEGIN {
23         push @INC, $ENV{SOURCEPATH};
24         require 5.10.0;
25 }
26
27 use strict;
28 use warnings FATAL => qw(all);
29
30 use make::configure;
31
32 chdir $ENV{BUILDPATH};
33
34 my $type = shift;
35 my $out = shift;
36 my $verbose = ($type =~ s/-v$//);
37
38 ## BEGIN HACK: REMOVE IN 2.2!
39 sub read_config_cache {
40         my %cfg = ();
41         open(CACHE, '../.config.cache') or return %cfg;
42         while (my $line = <CACHE>) {
43                 next if $line =~ /^\s*($|\#)/;
44                 my ($key, $value) = ($line =~ /^(\S+)="(.*)"$/);
45                 $cfg{$key} = $value;
46         }
47         close(CACHE);
48         return %cfg;
49 }
50
51 our %config = read_config_cache();
52 ## END HACK
53
54 if ($type eq 'gen-ld') {
55         do_static_find(@ARGV);
56 } elsif ($type eq 'static-ld') {
57         do_static_link(@ARGV);
58 } elsif ($type eq 'core-ld') {
59         do_core_link(@ARGV);
60 } elsif ($type eq 'link-dir') {
61         do_link_dir(@ARGV);
62 } elsif ($type eq 'gen-o') {
63         do_compile(1, 0, @ARGV);
64 } elsif ($type eq 'gen-so') {
65         do_compile(1, 1, @ARGV);
66 } elsif ($type eq 'link-so') {
67         do_compile(0, 1, @ARGV);
68 } else {
69         print STDERR "Unknown unit-cc subcommand $type!\n";
70 }
71 exit 1;
72
73 sub do_static_find {
74         my @flags;
75         for my $file (@ARGV) {
76                 push @flags, get_property($file, 'LinkerFlags');
77         }
78         open F, '>', $out;
79         print F join ' ', @flags;
80         close F;
81         exit 0;
82 }
83
84 sub do_static_link {
85         my $execstr = "$ENV{RUNLD} -o $out $ENV{CORELDFLAGS}";
86         for (@ARGV) {
87                 if (/\.cmd$/) {
88                         open F, '<', $_;
89                         my $libs = <F>;
90                         chomp $libs;
91                         $execstr .= ' '.$libs;
92                         close F;
93                 } else {
94                         $execstr .= ' '.$_;
95                 }
96         }
97         $execstr .= ' '.$ENV{LDLIBS};
98         print "$execstr\n" if $verbose;
99         exec $execstr;
100 }
101
102 sub do_core_link {
103         my $execstr = "$ENV{RUNLD} -o $out $ENV{CORELDFLAGS} @_ $ENV{LDLIBS}";
104         print "$execstr\n" if $verbose;
105         exec $execstr;
106 }
107
108 sub do_link_dir {
109         my $execstr = "$ENV{RUNLD} -o $out $ENV{PICLDFLAGS} @_";
110         print "$execstr\n" if $verbose;
111         exec $execstr;
112 }
113
114 sub do_compile {
115         my ($do_compile, $do_link, $file) = @_;
116
117         my $flags = '';
118         my $libs = '';
119         my $binary = $ENV{RUNCC};
120         if ($do_compile) {
121                 $flags = $ENV{CORECXXFLAGS} . ' ' . get_property($file, 'CompileFlags');
122
123                 if ($file =~ m#(?:^|/)((?:m|core)_[^/. ]+)(?:\.cpp|/.*\.cpp)$#) {
124                         $flags .= ' -DMODNAME=\\"'.$1.'\\"';
125                 }
126         } else {
127                 $binary = $ENV{RUNLD};
128         }
129
130         if ($do_link) {
131                 $flags = join ' ', $flags, $ENV{PICLDFLAGS};
132                 $libs = get_property($file, 'LinkerFlags');
133         } else {
134                 $flags .= ' -c';
135         }
136
137         my $execstr = "$binary -o $out $flags $file $libs";
138         print "$execstr\n" if $verbose;
139         exec $execstr;
140 }