]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/unit-cc.pl
Merge pull request #1186 from SaberUK/master+build-system-cleanup
[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::configure;
33 use make::console;
34
35 chdir $ENV{BUILDPATH};
36
37 my $type = shift;
38 my $out = shift;
39
40 our %config = read_configure_cache();
41
42 if ($type eq 'gen-ld') {
43         do_static_find(@ARGV);
44 } elsif ($type eq 'static-ld') {
45         do_static_link(@ARGV);
46 } elsif ($type eq 'core-ld') {
47         do_core_link(@ARGV);
48 } elsif ($type eq 'link-dir') {
49         do_link_dir(@ARGV);
50 } elsif ($type eq 'gen-o') {
51         do_compile(1, 0, @ARGV);
52 } elsif ($type eq 'gen-so') {
53         do_compile(1, 1, @ARGV);
54 } elsif ($type eq 'link-so') {
55         do_compile(0, 1, @ARGV);
56 } else {
57         print STDERR "Unknown unit-cc subcommand $type!\n";
58 }
59 exit 1;
60
61 sub message($$$) {
62         my ($type, $file, $command) = @_;
63         if ($ENV{INSPIRCD_VERBOSE}) {
64                 print "$command\n";
65         } else {
66                 print_format "\t<|GREEN $type:|>\t\t$file\n";
67         }
68 }
69
70 sub do_static_find {
71         my @flags;
72         for my $file (@ARGV) {
73                 push @flags, get_property($file, 'LinkerFlags');
74         }
75         open F, '>', $out;
76         print F join ' ', @flags;
77         close F;
78         exit 0;
79 }
80
81 sub do_static_link {
82         my $execstr = "$ENV{CXX} -o $out $ENV{CORELDFLAGS}";
83         for (@ARGV) {
84                 if (/\.cmd$/) {
85                         open F, '<', $_;
86                         my $libs = <F>;
87                         chomp $libs;
88                         $execstr .= ' '.$libs;
89                         close F;
90                 } else {
91                         $execstr .= ' '.$_;
92                 }
93         }
94         $execstr .= ' '.$ENV{LDLIBS};
95         message 'LINK', $out, $execstr;
96         exec $execstr;
97 }
98
99 sub do_core_link {
100         my $execstr = "$ENV{CXX} -o $out $ENV{CORELDFLAGS} @_ $ENV{LDLIBS}";
101         message 'LINK', $out, $execstr;
102         exec $execstr;
103 }
104
105 sub do_link_dir {
106         my ($dir, $link_flags) = (shift, '');
107         for my $file (<$dir/*.cpp>) {
108                 $link_flags .= get_property($file, 'LinkerFlags') . ' ';
109         }
110         my $execstr = "$ENV{CXX} -o $out $ENV{PICLDFLAGS} $link_flags @_";
111         message 'LINK', $out, $execstr;
112         exec $execstr;
113 }
114
115 sub do_compile {
116         my ($do_compile, $do_link, $file) = @_;
117
118         my $flags = '';
119         my $libs = '';
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         }
127
128         if ($do_link) {
129                 $flags = join ' ', $flags, $ENV{PICLDFLAGS};
130                 $libs = get_property($file, 'LinkerFlags');
131         } else {
132                 $flags .= ' -c';
133         }
134
135         my $execstr = "$ENV{CXX} -o $out $flags $file $libs";
136         message 'BUILD', abs2rel($file, "$ENV{SOURCEPATH}/src"), $execstr;
137         exec $execstr;
138 }