-
Notifications
You must be signed in to change notification settings - Fork 6
/
prepare
executable file
·94 lines (83 loc) · 2.32 KB
/
prepare
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
#!/usr/bin/perl
use strict;
use warnings;
use Config;
use FindBin;
my $root = $FindBin::Bin;
expand_gyb();
write_modulemap();
write_pkgconfig();
sub expand_gyb {
unless (-d ".build/gyb") {
system("mkdir -p .build/gyb") == 0
or die "Cannot mdkir .build/gyb";
foreach my $f (qw/ gyb gyb.py /) {
system("curl -f -s https://raw.githubusercontent.com/apple/swift/master/utils/$f -o .build/gyb/$f") == 0
or die "Failed to download gyb";
}
chmod 0755, ".build/gyb/gyb" or die $!;
}
foreach my $f (qw# Sources/Perl/Call.swift Sources/Perl/Subroutine.swift #) {
my $filename = "$root/$f";
system(".build/gyb/gyb $filename.gyb > $filename.tmp") == 0
or die "Failed to expand gyb template\n";
commit_file($filename);
}
}
sub write_modulemap {
my $archlib = $Config{archlib};
my $perl = 'perl';
if ($Config{osname} ne 'darwin' && system('swiftc -o /dev/null -Xlinker -lperl - </dev/null 2>/dev/null') != 0) {
die "Cannot find libperl.so\n" unless -f "$archlib/CORE/libperl.so";
$perl = "perl -Xlinker -rpath=$archlib/CORE -Xlinker -L$archlib/CORE";
}
if ($Config{osname} eq 'darwin' && ! -f "$archlib/CORE/perl.h") {
my $sdk_path = `xcrun --show-sdk-path`;
chomp $sdk_path;
$archlib = $sdk_path . $archlib;
}
write_file("$root/Sources/CPerl/module.modulemap", <<EOF);
module CPerl [system] {
header "shim.h"
header "$archlib/CORE/EXTERN.h"
header "$archlib/CORE/perl.h"
header "$archlib/CORE/XSUB.h"
header "custom.h"
header "macro.h"
header "func.h"
link "$perl"
use SwiftGlibc
}
EOF
}
sub write_pkgconfig {
return unless $Config{osname} eq 'darwin';
my $sdk_path = `xcrun --show-sdk-path`;
chomp $sdk_path;
my $archlib = $Config{archlib};
-d '.build' or mkdir '.build'
or die "Failed to mkdir .build: $!";
-d '.build/pkgconfig' or mkdir '.build/pkgconfig'
or die "Failed to mkdir .build/pkgconfig: $!";
write_file(".build/pkgconfig/perl.pc", <<EOF);
Cflags: -I$sdk_path$archlib/CORE
Libs: -L$archlib/CORE -lperl
EOF
}
sub commit_file {
my ($filename) = @_;
if (system("cmp -s $filename.tmp $filename") == 0) {
unlink "$filename.tmp" or die $!;
} else {
rename "$filename.tmp", $filename or die $!;
}
return;
}
sub write_file {
my ($filename, $content) = @_;
open my $file, '>', "$filename.tmp"
or die "Cannot write $filename.tmp\n";
print $file $content;
close $file;
commit_file($filename);
}