]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/unit-cc.pl
Merge branch 'insp20' 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 'core-ld') {
41         do_core_link(@ARGV);
42 } elsif ($type eq 'link-dir') {
43         do_link_dir(@ARGV);
44 } elsif ($type eq 'gen-o') {
45         do_compile(1, 0, @ARGV);
46 } elsif ($type eq 'gen-so') {
47         do_compile(1, 1, @ARGV);
48 } elsif ($type eq 'link-so') {
49         do_compile(0, 1, @ARGV);
50 } else {
51         print STDERR "Unknown unit-cc subcommand $type!\n";
52 }
53 exit 1;
54
55 sub message($$$) {
56         my ($type, $file, $command) = @_;
57         if ($ENV{INSPIRCD_VERBOSE}) {
58                 print "$command\n";
59         } else {
60                 print_format "\t<|GREEN $type:|>\t\t$file\n";
61         }
62 }
63
64 sub rpath($) {
65         my $message = shift;
66         $message =~ s/-L(\S+)/-Wl,-rpath,$1 -L$1/g unless defined $ENV{INSPIRCD_DISABLE_RPATH};
67         return $message;
68 }
69
70 sub do_core_link {
71         my $execstr = "$ENV{CXX} -o $out $ENV{CORELDFLAGS} @_ $ENV{LDLIBS}";
72         message 'LINK', $out, $execstr;
73         exec $execstr;
74 }
75
76 sub do_link_dir {
77         my ($dir, $link_flags) = (shift, '');
78         for my $file (<$dir/*.cpp>) {
79                 $link_flags .= rpath(get_directive($file, 'LinkerFlags', '')) . ' ';
80         }
81         my $execstr = "$ENV{CXX} -o $out $ENV{PICLDFLAGS} $link_flags @_";
82         message 'LINK', $out, $execstr;
83         exec $execstr;
84 }
85
86 sub do_compile {
87         my ($do_compile, $do_link, $file) = @_;
88
89         my $flags = '';
90         my $libs = '';
91         if ($do_compile) {
92                 $flags = $ENV{CORECXXFLAGS} . ' ' . get_directive($file, 'CompilerFlags', '');
93
94                 if ($file =~ m#(?:^|/)((?:m|core)_[^/. ]+)(?:\.cpp|/.*\.cpp)$#) {
95                         $flags .= ' -DMODNAME=\\"'.$1.'\\"';
96                 }
97         }
98
99         if ($do_link) {
100                 $flags = join ' ', $flags, $ENV{PICLDFLAGS};
101                 $libs = rpath(get_directive($file, 'LinkerFlags', ''));
102         } else {
103                 $flags .= ' -c';
104         }
105
106         my $execstr = "$ENV{CXX} -o $out $flags $file $libs";
107         message 'BUILD', abs2rel($file, "$ENV{SOURCEPATH}/src"), $execstr;
108         exec $execstr;
109 }