Skip to content

Commit

Permalink
Add build script for optimized perl interpreter
Browse files Browse the repository at this point in the history
Cleanup Makefile a bit
Make built in perl the default for linux x86
  • Loading branch information
ariselseng committed Aug 1, 2020
1 parent bc840e9 commit 379e8af
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 30 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/vendor/
/build/
/build/artifacts
/build/*tar.gz
/build/tools/perl-build/build/exiftool.bin
/build/tools/perl-build/build/staticperl
35 changes: 11 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@

app_name=$(notdir $(CURDIR))
build_tools_directory=$(CURDIR)/build/tools
vendor_directory=$(CURDIR)/vendor
source_build_directory=$(CURDIR)/build/artifacts/source
source_package_name=$(source_build_directory)/$(app_name)
appstore_build_directory=$(CURDIR)/build/artifacts/appstore
appstore_package_name=$(appstore_build_directory)/$(app_name)
npm=$(shell which npm 2> /dev/null)
composer=$(shell which composer 2> /dev/null)

all: build
Expand All @@ -61,9 +61,6 @@ endif
ifneq (,$(wildcard $(CURDIR)/package.json))
make npm
endif
ifneq (,$(wildcard $(CURDIR)/js/package.json))
make npm
endif

# Installs and updates the composer dependencies. If composer is not installed
# a copy is fetched from the web
Expand All @@ -81,35 +78,25 @@ else
composer update --prefer-dist
endif

# Installs npm dependencies
.PHONY: npm
npm:
ifeq (,$(wildcard $(CURDIR)/package.json))
cd js && $(npm) run build
else
npm run build
endif

# Removes the appstore build
.PHONY: clean
clean:
rm -rf ./build

# Same as clean but also removes dependencies installed by composer, bower and
# npm
.PHONY: distclean
distclean: clean
rm -rf vendor
rm -rf node_modules
rm -rf js/vendor
rm -rf js/node_modules
rm -rf ./build/artifacts
rm -rf ./build/camerarawpreviews*tar.gz

# Builds the source and appstore package
.PHONY: dist
dist:
make source
composer install --prefer-dist
test -s $vendor_directory/exiftool/exiftool/exiftool.bin || make perl
make tests
make appstore

# Builds the source package
.PHONY: perl
perl:
$(build_tools_directory)/perl-build/build.sh

# Builds the source package
.PHONY: source
source:
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ This app also gives you preview of Adobe Indesign files (.INDD).
* Probably **memory_limit** quite high.
* **imagick** or **gd** module. If imagick is available, it will use that for performance.
* For files with a TIFF preview (at least some DNG files), **imagick** is required
* Perl installed. ~~A deprecated fallback is used now that will be removed in a future version.~~

## Installation
Install in Nextcloud App store.
Expand All @@ -21,3 +20,7 @@ https://marketplace.owncloud.com/apps/camerarawpreviews
## Building locally
- Run "make"
- Place this app in **./apps/**

## Information about the perl binary
- To avoid lots of issues and problems for users I am bundling a static build of perl for x86_64
- The binary is built using an isolated docker container with this: http://software.schmorp.de/pkg/App-Staticperl.html
1 change: 0 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<bugs>https://github.com/ariselseng/camerarawpreviews/issues</bugs>
<dependencies>
<php min-version="7.2"></php>
<command>perl</command>
<owncloud min-version="10.0.9" max-version="10"/>
<nextcloud min-version="17" max-version="19"/>
</dependencies>
Expand Down
17 changes: 17 additions & 0 deletions build/tools/perl-build/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
docker_build_mount="$(realpath "$(dirname "$0")"/build)"
app_dir="$(realpath "$docker_build_mount"/../../../..)"
exiftool_dir="$app_dir"/vendor/exiftool/exiftool

docker run -ti --workdir /build --rm -v "$docker_build_mount":/build i386/alpine:3.9 ./build-perl.sh

if [ -e "$docker_build_mount"/exiftool.bin ];then
sudo chown $(id -u):$(id -g) "$docker_build_mount"/exiftool.bin

chmod +x "$docker_build_mount"/exiftool.bin
if [ -e "$exiftool_dir"/exiftool.bin ];then
rm "$exiftool_dir"/exiftool.bin
fi

mv "$docker_build_mount"/exiftool.bin "$exiftool_dir"/
fi
11 changes: 11 additions & 0 deletions build/tools/perl-build/build/build-perl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

[ -e ./exiftool.bin ] && rm ./exiftool.bin
[ -e ./staticperl ] && rm ./staticperl

apk -U add upx curl wget alpine-sdk &&
curl http://cvs.schmorp.de/App-Staticperl/bin/staticperl -o ./staticperl && chmod +x ./staticperl && \
./staticperl mkapp exiftool.bin --boot exiftool_wrapper.pl perl.bundle && \
upx exiftool.bin

[ -e ./staticperl ] && rm ./staticperl
2 changes: 2 additions & 0 deletions build/tools/perl-build/build/exiftool_wrapper.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
use FindBin;
require "$FindBin::Bin/exiftool";
16 changes: 16 additions & 0 deletions build/tools/perl-build/build/perl.bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
static
usepacklists
strip pod
use FindBin
use File::Glob
use Archive::Zip
use Compress::Zlib
use Digest::MD5
use Digest::SHA
use IO::Compress::Bzip2
use Time::HiRes
use Time::Piece
use IO::Compress::RawDeflate
use IO::Uncompress::RawInflate
use IO::String
use Encode
25 changes: 22 additions & 3 deletions lib/RawPreviewBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public function __construct(ILogger $logger, string $appName)

try {
$perlBin = $this->getPerlExecutable();
$this->converter = $perlBin . ' ' . realpath(__DIR__ . '/../vendor/exiftool/exiftool/exiftool');
if (strpos($perlBin, 'exiftool/exiftool.bin') !== false) {
$this->converter = $perlBin;
} else {
$this->converter = $perlBin . ' ' . realpath(__DIR__ . '/../vendor/exiftool/exiftool/exiftool');
}
$this->perlFound = true;
} catch (Exception $e) {
$this->logger->logException($e, ['app' => $this->appName]);
Expand All @@ -58,8 +62,10 @@ public function getMimeType()
*/
protected function getBestPreviewTag($tmpPath)
{
$cmd = $this->converter . " -json -preview:all -FileType " . escapeshellarg($tmpPath);
$json = shell_exec($cmd);
// get all available previews and the file type
$previewData = json_decode(shell_exec($this->converter . " -json -preview:all -FileType " . escapeshellarg($tmpPath)), true);
$previewData = json_decode($json, true);
$fileType = $previewData[0]['FileType'] ?? 'n/a';

// potential tags in priority
Expand Down Expand Up @@ -101,7 +107,7 @@ protected function getBestPreviewTag($tmpPath)
}
return $tag;
}
throw new Exception('Unable to find preview data: debug ' . json_encode($previewData));
throw new Exception('Unable to find preview data: ' . $json);
}

/**
Expand All @@ -110,6 +116,18 @@ protected function getBestPreviewTag($tmpPath)
*/
private function getPerlExecutable()
{
if (strpos(php_uname("m"), 'x86') === 0 && php_uname("s") === "Linux") {
$perlBin = realpath(__DIR__ . '/../vendor/exiftool/exiftool/exiftool.bin');
$perlBinIsExecutable = is_executable($perlBin);

if (!$perlBinIsExecutable && is_writable($perlBin)) {
$perlBinIsExecutable = chmod($perlBin, 0744);
}
if ($perlBinIsExecutable) {
return $perlBin;
}
}

$perlBin = \OC_Helper::findBinaryPath('perl');
if (!is_null($perlBin)) {
return $perlBin;
Expand Down Expand Up @@ -196,6 +214,7 @@ protected function getThumbnailInternal(File $file, int $maxX, int $maxY): ?IIma
return $image;
} catch (Exception $e) {
$this->logger->logException($e, ['app' => $this->appName]);

$this->cleanTmpFiles();
return null;
}
Expand Down

0 comments on commit 379e8af

Please sign in to comment.