]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/unit-cc.pl
Update copyright headers.
[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) 2019 iwalkalone <iwalkalone69@gmail.com>
7 #   Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
8 #   Copyright (C) 2013, 2015-2016, 2018 Sadie Powell <sadie@witchery.services>
9 #   Copyright (C) 2012 Robby <robby@chatbelgie.be>
10 #   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11 #
12 # This file is part of InspIRCd.  InspIRCd is free software: you can
13 # redistribute it and/or modify it under the terms of the GNU General Public
14 # License as published by the Free Software Foundation, version 2.
15 #
16 # This program is distributed in the hope that it will be useful, but WITHOUT
17 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19 # details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 #
24
25
26 BEGIN {
27         push @INC, $ENV{SOURCEPATH};
28         require 5.10.0;
29 }
30
31 use strict;
32 use warnings FATAL => qw(all);
33
34 use File::Spec::Functions qw(abs2rel);
35
36 use make::console;
37 use make::directive;
38
39 chdir $ENV{BUILDPATH};
40
41 my $type = shift;
42 my $out = shift;
43
44 if ($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_core_link {
75         my $execstr = "$ENV{CXX} -o $out $ENV{CORELDFLAGS} @_ $ENV{LDLIBS}";
76         message 'LINK', $out, $execstr;
77         exec $execstr;
78 }
79
80 sub do_link_dir {
81         my ($dir, $link_flags) = (shift, '');
82         for my $file (<$dir/*.cpp>) {
83                 $link_flags .= rpath(get_directive($file, 'LinkerFlags', '')) . ' ';
84         }
85         my $execstr = "$ENV{CXX} -o $out $ENV{PICLDFLAGS} @_ $link_flags";
86         message 'LINK', $out, $execstr;
87         exec $execstr;
88 }
89
90 sub do_compile {
91         my ($do_compile, $do_link, $file) = @_;
92
93         my $flags = '';
94         my $libs = '';
95         if ($do_compile) {
96                 $flags = $ENV{CORECXXFLAGS} . ' ' . get_directive($file, 'CompilerFlags', '');
97
98                 if ($file =~ m#(?:^|/)((?:m|core)_[^/. ]+)(?:\.cpp|/.*\.cpp)$#) {
99                         $flags .= ' -DMODNAME=\\"'.$1.'\\"';
100                 }
101         }
102
103         if ($do_link) {
104                 $flags = join ' ', $flags, $ENV{PICLDFLAGS};
105                 $libs = rpath(get_directive($file, 'LinkerFlags', ''));
106         } else {
107                 $flags .= ' -c';
108         }
109
110         my $execstr = "$ENV{CXX} -o $out $flags $file $libs";
111         message 'BUILD', abs2rel($file, "$ENV{SOURCEPATH}/src"), $execstr;
112         exec $execstr;
113 }