]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/unit-cc.pl
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / make / unit-cc.pl
1 #!/usr/bin/env perl
2 #
3 # InspIRCd -- Internet Relay Chat Daemon
4 #
5 #   Copyright (C) 2019 iwalkalone <iwalkalone69@gmail.com>
6 #   Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
7 #   Copyright (C) 2013, 2015-2016, 2018, 2021 Sadie Powell <sadie@witchery.services>
8 #   Copyright (C) 2012 Robby <robby@chatbelgie.be>
9 #   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10 #
11 # This file is part of InspIRCd.  InspIRCd is free software: you can
12 # redistribute it and/or modify it under the terms of the GNU General Public
13 # License as published by the Free Software Foundation, version 2.
14 #
15 # This program is distributed in the hope that it will be useful, but WITHOUT
16 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18 # details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23
24
25 use v5.10.0;
26 use strict;
27 use warnings FATAL => qw(all);
28
29 use File::Basename        qw(dirname);
30 use File::Spec::Functions qw(abs2rel);
31 use FindBin               qw($RealDir);
32
33 use lib dirname $RealDir;
34 use make::console;
35 use make::directive;
36
37 chdir $ENV{BUILDPATH};
38
39 my $type = shift;
40 my $out = shift;
41
42 if ($type eq 'core-ld') {
43         do_core_link(@ARGV);
44 } elsif ($type eq 'link-dir') {
45         do_link_dir(@ARGV);
46 } elsif ($type eq 'gen-o') {
47         do_compile(1, 0, @ARGV);
48 } elsif ($type eq 'gen-so') {
49         do_compile(1, 1, @ARGV);
50 } elsif ($type eq 'link-so') {
51         do_compile(0, 1, @ARGV);
52 } else {
53         print STDERR "Unknown unit-cc subcommand $type!\n";
54 }
55 exit 1;
56
57 sub message($$$) {
58         my ($type, $file, $command) = @_;
59         if ($ENV{INSPIRCD_VERBOSE}) {
60                 say $command;
61         } else {
62                 say console_format "\t<|GREEN $type:|>\t\t$file";
63         }
64 }
65
66 sub rpath($) {
67         my $message = shift;
68         $message =~ s/-L(\S+)/-Wl,-rpath,$1 -L$1/g unless defined $ENV{INSPIRCD_DISABLE_RPATH};
69         return $message;
70 }
71
72 sub do_core_link {
73         my $execstr = "$ENV{CXX} -o $out $ENV{CORELDFLAGS} @_ $ENV{LDLIBS}";
74         message 'LINK', $out, $execstr;
75         exec $execstr;
76 }
77
78 sub do_link_dir {
79         my ($dir, $link_flags) = (shift, '');
80         for my $file (<$dir/*.cpp>) {
81                 $link_flags .= rpath(get_directive($file, 'LinkerFlags', '')) . ' ';
82         }
83         my $execstr = "$ENV{CXX} -o $out $ENV{PICLDFLAGS} @_ $link_flags";
84         message 'LINK', $out, $execstr;
85         exec $execstr;
86 }
87
88 sub do_compile {
89         my ($do_compile, $do_link, $file) = @_;
90
91         my $flags = '';
92         my $libs = '';
93         if ($do_compile) {
94                 $flags = $ENV{CORECXXFLAGS} . ' ' . get_directive($file, 'CompilerFlags', '');
95
96                 if ($file =~ m#(?:^|/)((?:m|core)_[^/. ]+)(?:\.cpp|/.*\.cpp)$#) {
97                         $flags .= ' -DMODNAME=\\"'.$1.'\\"';
98                 }
99         }
100
101         if ($do_link) {
102                 $flags = join ' ', $flags, $ENV{PICLDFLAGS};
103                 $libs = rpath(get_directive($file, 'LinkerFlags', ''));
104         } else {
105                 $flags .= ' -c';
106         }
107
108         my $execstr = "$ENV{CXX} -o $out $flags $file $libs";
109         message 'BUILD', abs2rel($file, "$ENV{SOURCEPATH}/src"), $execstr;
110         exec $execstr;
111 }