Skip to content

Commit

Permalink
Merge swift-CPerl back to swiftperl repo (thanks to Swift 5)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksey-mashanov committed Apr 16, 2019
1 parent b48c441 commit f82f703
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 132 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.DS_Store
/.build
/.vscode
/Packages
/*.xcodeproj
/Sources/CPerl/include/module.modulemap
/Sources/CPerl/module.modulemap
/Sources/Perl/Call.swift
/Sources/Perl/Subroutine.swift
9 changes: 5 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ import PackageDescription

#if os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || CYGWIN
import Glibc
let pkgConfig = false
#elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
let pkgConfig = true
#endif

let buildBenchmark = false

let package = Package(
name: "Perl",
products: [
.library(name: "CPerl", targets: ["CPerl"]),
.library(name: "Perl", targets: ["Perl"]),
],
dependencies: [
.package(url: "https://github.com/my-mail-ru/swift-CPerl.git", from: "1.0.1"),
],
targets: [
.target(name: "Perl"),
.systemLibrary(name: "CPerl", pkgConfig: pkgConfig ? "perl" : nil),
.target(name: "Perl", dependencies: ["CPerl"]),
.testTarget(name: "PerlTests", dependencies: ["Perl"]),
]
)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Swift and Perl Interoperability

![Swift: 4.0](https://img.shields.io/badge/Swift-4.0-orange.svg)
![Swift: 5.0](https://img.shields.io/badge/Swift-5.0-orange.svg)
![OS: Linux | macOS](https://img.shields.io/badge/OS-Linux%20%7C%20macOS-brightgreen.svg)
![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)

Expand All @@ -10,15 +10,15 @@ though running Perl Interpreter in Swift environment is also possible.

## Prerequisites

* Swift 4.0
* Swift 5.0
* Perl 5 (>=5.10)

## Getting Started

### Linux

```sh
swift test -Xcc -D_GNU_SOURCE
swift test
```

### macOS
Expand Down
5 changes: 0 additions & 5 deletions Sources/CPerl/.gitignore

This file was deleted.

13 changes: 0 additions & 13 deletions Sources/CPerl/README.md

This file was deleted.

51 changes: 0 additions & 51 deletions Sources/CPerl/prepare

This file was deleted.

5 changes: 5 additions & 0 deletions Sources/CPerl/shim.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include <termios.h>
#include <fcntl.h>
#define PERL_NO_GET_CONTEXT

// workaround for "-Xcc -D_GNU_SOURCE" on Linux
#if defined(__linux__) && !defined(_GNU_SOURCE)
typedef __off64_t off64_t;
#endif
51 changes: 0 additions & 51 deletions Sources/CPerl/swift-CPerl.spec

This file was deleted.

56 changes: 56 additions & 0 deletions prepare
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

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") {
Expand All @@ -27,6 +30,50 @@ sub expand_gyb {
}
}

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) {
Expand All @@ -36,3 +83,12 @@ sub commit_file {
}
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);
}
10 changes: 6 additions & 4 deletions swiftperl.spec
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ URL: https://github.com/my-mail-ru/%{name}
Source0: https://github.com/my-mail-ru/%{name}/archive/%{version}.tar.gz#/%{name}-%{version}.tar.gz
BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)

BuildRequires: swift >= 4
BuildRequires: swift-packaging >= 0.9
BuildRequires: swiftpm(https://github.com/my-mail-ru/swift-CPerl.git) >= 1.0.1
BuildRequires: swift >= 5
BuildRequires: swift-packaging >= 0.10

%undefine _missing_build_ids_terminate_build
%swift_find_provides_and_requires

%description
Expand All @@ -25,7 +25,6 @@ in Swift environment is also possible.

%prep
%setup -q
%swift_patch_package


%build
Expand All @@ -36,6 +35,8 @@ in Swift environment is also possible.
rm -rf %{buildroot}
%swift_install
%swift_install_devel
mkdir -p %{buildroot}%{swift_clangmoduleroot}/CPerl/
cp Sources/CPerl/{module.modulemap,*.h} %{buildroot}%{swift_clangmoduleroot}/CPerl/


%clean
Expand Down Expand Up @@ -64,3 +65,4 @@ in Swift environment is also possible.
%defattr(-,root,root,-)
%{swift_moduledir}/*.swiftmodule
%{swift_moduledir}/*.swiftdoc
%{swift_clangmoduleroot}/CPerl

0 comments on commit f82f703

Please sign in to comment.