]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/unit-cc.pl
Merge tag 'v2.0.26' into master.
[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 File::Spec::Functions qw(abs2rel);
31
32 use make::console;
33 use make::directive;
34
35 chdir $ENV{BUILDPATH};
36
37 my $type = shift;
38 my $out = shift;
39
40 if ($type eq 'gen-ld') {
41         do_static_find(@ARGV);
42 } elsif ($type eq 'static-ld') {
43         do_static_link(@ARGV);
44 } elsif ($type eq 'core-ld') {
45         do_core_link(@ARGV);
46 } elsif ($type eq 'link-dir') {
47         do_link_dir(@ARGV);
48 } elsif ($type eq 'gen-o') {
49         do_compile(1, 0, @ARGV);
50 } elsif ($type eq 'gen-so') {
51         do_compile(1, 1, @ARGV);
52 } elsif ($type eq 'link-so') {
53         do_compile(0, 1, @ARGV);
54 } else {
55         print STDERR "Unknown unit-cc subcommand $type!\n";
56 }
57 exit 1;
58
59 sub message($$$) {
60         my ($type, $file, $command) = @_;
61         if ($ENV{INSPIRCD_VERBOSE}) {
62                 print "$command\n";
63         } else {
64                 print_format "\t<|GREEN $type:|>\t\t$file\n";
65         }
66 }
67
68 sub rpath($) {
69         my $message = shift;
70         $message =~ s/-L(\S+)/-Wl,-rpath,$1 -L$1/g unless defined $ENV{INSPIRCD_DISABLE_RPATH};
71         return $message;
72 }
73
74 sub do_static_find {
75         my @flags;
76         for my $file (@ARGV) {
77                 push @flags, rpath(get_directive($file, 'LinkerFlags', ''));
78         }
79         open F, '>', $out;
80         print F join ' ', @flags;
81         close F;
82         exit 0;
83 }
84
85 sub do_static_link {
86         my $execstr = "$ENV{CXX} -o $out $ENV{CORELDFLAGS}";
87         my $link_flags = '';
88         for (@ARGV) {
89                 if (/\.cmd$/) {
90                         open F, '<', $_;
91                         my $libs = <F>;
92                         chomp $libs;
93                         $link_flags .= ' '.$libs;
94                         close F;
95                 } else {
96                         $execstr .= ' '.$_;
97                 }
98         }
99         $execstr .= ' '.$ENV{LDLIBS}.' '.$link_flags;
100         message 'LINK', $out, $execstr;
101         exec $execstr;
102 }
103
104 sub do_core_link {
105         my $execstr = "$ENV{CXX} -o $out $ENV{CORELDFLAGS} @_ $ENV{LDLIBS}";
106         message 'LINK', $out, $execstr;
107         exec $execstr;
108 }
109
110 sub do_link_dir {
111         my ($dir, $link_flags) = (shift, '');
112         for my $file (<$dir/*.cpp>) {
113                 $link_flags .= rpath(get_directive($file, 'LinkerFlags', '')) . ' ';
114         }
115         my $execstr = "$ENV{CXX} -o $out $ENV{PICLDFLAGS} $link_flags @_";
116         message 'LINK', $out, $execstr;
117         exec $execstr;
118 }
119
120 sub do_compile {
121         my ($do_compile, $do_link, $file) = @_;
122
123         my $flags = '';
124         my $libs = '';
125         if ($do_compile) {
126                 $flags = $ENV{CORECXXFLAGS} . ' ' . get_directive($file, 'CompilerFlags', '');
127
128                 if ($file =~ m#(?:^|/)((?:m|core)_[^/. ]+)(?:\.cpp|/.*\.cpp)$#) {
129                         $flags .= ' -DMODNAME=\\"'.$1.'\\"';
130                 }
131         }
132
133         if ($do_link) {
134                 $flags = join ' ', $flags, $ENV{PICLDFLAGS};
135                 $libs = rpath(get_directive($file, 'LinkerFlags', ''));
136         } else {
137                 $flags .= ' -c';
138         }
139
140         my $execstr = "$ENV{CXX} -o $out $flags $file $libs";
141         message 'BUILD', abs2rel($file, "$ENV{SOURCEPATH}/src"), $execstr;
142         exec $execstr;
143 }