File: SigarBuild.pm

package info (click to toggle)
ruby-sigar 0.7.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,272 kB
  • sloc: ansic: 24,014; perl: 2,977; ruby: 99; makefile: 2
file content (301 lines) | stat: -rw-r--r-- 6,934 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#
# Copyright (c) 2009 Hyperic, Inc.
# Copyright (c) 2009 SpringSource, Inc.
# Copyright (c) 2009 VMware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

package SigarBuild;

use strict;
use Config;
use Exporter;
use File::Basename qw(basename);
use File::Copy qw(copy);
use File::Spec ();
use POSIX ();

use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(cppflags ldflags libs os src inline_src version_file resource_file);

sub archname {
    my $os = lc $^O;
    my $vers = $Config{osvers};
    my $arch = $Config{archname};

    if ($os =~ /win32/) {
        return 'x86-winnt';
    }
    elsif ($os =~ /linux/) {
        if ($arch =~ /_64/) {
            return 'amd64-linux';
        }
        else {
            return 'x86-linux';
        }
    }
    elsif ($os =~ /hpux/) {
        if ($vers =~ /11\./) {
            return 'pa-hpux-11';
        }
    }
    elsif ($os =~ /aix/) {
        return 'ppc-aix-5';
    }
    elsif ($os =~ /solaris/) {
        if ($arch =~ /sun4/) {
            return 'sparc-solaris';
        }
        elsif ($arch =~ /.86/) {
            return 'x86-solaris';
        }
    }
    elsif ($os =~ /darwin/) {
        return 'universal-macosx';
    }
    elsif ($os =~ /freebsd/) {
        if ($arch =~ /.86/) {
            if($vers =~ /6\../ ) {
                return 'x86-freebsd-6';
            }
        }
        elsif ($arch =~ /amd64/) {
            if ($vers =~ /6\../ ) {
                return 'amd64-freebsd-6';
            }
        }
    }

    return '';
}

sub flags {
    my $os = lc $^O;
    my $is_win32 = 0;
    my (@cppflags, @ldflags, @libs);
    if ($os =~ /(win32)/) {
        $os = $1;
        $is_win32 = 1;
        @cppflags = ('-DWIN32', '-D_CRT_SECURE_NO_DEPRECATE');
        @libs = qw(kernel32 user32 advapi32 ws2_32 netapi32 shell32 pdh version comsupp wbemuuid);
    }
    elsif ($os =~ /(linux)/) {
        $os = $1;
    }
    elsif ($os =~ /(hpux)/) {
        $os = $1;
        @libs = qw(nsl nm);
    }
    elsif ($os =~ /(aix)/) {
        $os = $1;
        @libs = qw(odm cfg perfstat);
    }
    elsif ($os =~ /(solaris)/) {
        $os = $1;
        @libs = qw(nsl socket kstat);
    }
    elsif ($os =~ /(darwin)/) {
        $os = $1;
        @cppflags = ('-DDARWIN');
        @ldflags = ('-framework CoreServices', '-framework IOKit');
        if (-e "/usr/local/libproc.h") {
            push @cppflags, '-DDARWIN_HAS_LIBPROC_H';
        }
    }
    elsif ($os =~ /bsd/) {
        $os = 'darwin';
        @libs = qw(kvm);
    }

    push @cppflags,
      '-I../../include',
      "-I../../src/os/$os";

    unless ($is_win32) {
        push @cppflags, '-U_FILE_OFFSET_BITS';
    }

    my(@src) = (<../../src/*.c>, <../../src/os/$os/*.c>, <../../src/os/$os/*.cpp>);

    return {
        is_win32 => $is_win32,
        os => $os,
        libs => \@libs,
        cppflags => \@cppflags,
        ldflags => \@ldflags,
        src => \@src,
    };
}

#perl -Mlib=.. -MSigarBuild -e cppflags
sub cppflags {
    print join ' ', @{ flags()->{cppflags} };
}

sub ldflags {
    print join ' ', @{ flags()->{ldflags} };
}

sub libs {
    print join ' ', @{ flags()->{libs} };
}

sub os {
    print flags()->{os};
}

sub src {
    print join ' ', @{ flags()->{src} };
}

sub inline_src {
    my $stdout = @_ ? 0 : 1;
    my $flags = shift || flags();
    my $src = $flags->{src};
    my $dir = $flags->{build_dir} || $ARGV[0];
    my(@files);
    #unlink symlinks incase of nfs shared dir...
    for my $file (grep { -l } <*.c>) {
        unlink $file;
    }
    for my $file (@$src) {
        my $cf = basename $file;
        #sigar.c -> libsigar.c else
        #sigar.o and perl Sigar.o clash on case insensitive filesystems
        $cf = 'libsigar.c' if $cf eq 'sigar.c';
        if ($dir) {
            $cf = join '/', $dir, $cf;
            $file = File::Spec->rel2abs($file);
        }
        push @files, $cf;
        if ($flags->{is_win32}) {
            copy($file, $cf);
        }
        else {
            symlink($file, $cf) unless -e $cf;
        }
    }
    if ($stdout) {
        print join ' ', @files;
    }
    else {
        return @files;
    }
}

sub scm_revision {
    my $rev;
    $rev = `git rev-parse --short HEAD`;
    if ($rev) {
        chomp $rev;
    }
    else {
        $rev = "exported";
    }
    return $rev;
}

sub build_date {
    return POSIX::strftime("%m/%d/%Y %I:%M %p", localtime);
}

sub find_file {
    my $file = shift;
    for my $dir (qw(../.. .. .)) {
        my $pfile = "$dir/$file";
        return $pfile if -e $pfile;
    }
    return $file;
}

sub version_properties {
    my $props = {};
    my $file = $_[0] || find_file('version.properties');
    open my $fh, $file or die "open $file: $!";
    while (<$fh>) {
        chomp;
        my($key,$val) = split '=';
        next unless $key and defined $val;
        $props->{$key} = $val;
    }
    close $fh;

    $props->{'scm.revision'} = scm_revision();

    $props->{'build.date'} = build_date();

    $props->{'version'} =
        join '.', map $props->{"version.$_"}, qw(major minor maint);

    $props->{'version.build'} = $ENV{BUILD_NUMBER} || '0';

    $props->{'version.string'} =
        join '.', $props->{'version'}, $props->{'version.build'};

    return $props;
}

sub resource_file {
    my(@args) = @_ ? @_ : @ARGV;
    version_file(find_file("src/os/win32/sigar.rc.in"), "sigar.rc", @args);
}

sub version_file {
    local $_;
    my($source, $dest, %filters);
    my(@args) = @_ ? @_ : @ARGV;
    for (@args) {
        if (/=/) {
            my($key,$val) = split '=', $_, 2;
            $filters{$key} = $val;
        }
        else {
            if ($source) {
                $dest = $_;
            }
            else {
                $source = $_;
            }
        }
    }
    unless ($source) {
        $dest = 'sigar_version.c';
        $source = find_file("src/$dest.in");
    }
    my $props = version_properties();
    while (my($key,$val) = each %$props) {
        $key = uc $key;
        $key =~ s/\./_/;
        $filters{$key} = $val;
    }
    my $re = join '|', keys %filters;
    open my $in, $source or die "open $source: $!";
    my $out;
    if ($dest) {
        open $out, '>', $dest or die "open $dest: $!";
    }
    else {
        $out = \*STDOUT;
    }
    while (<$in>) {
        s/\@\@($re)\@\@/$filters{$1}/go;
        print $out $_;
    }
    close $in;
    close $out if $dest;
}

1;
__END__