| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
# Copyright (c), ETH Zurich and UNC Chapel Hill. |
| 4 |
# All rights reserved. |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# * Redistributions of source code must retain the above copyright |
| 10 |
# notice, this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# * Redistributions in binary form must reproduce the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer in the |
| 14 |
# documentation and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of |
| 17 |
# its contributors may be used to endorse or promote products derived |
| 18 |
# from this software without specific prior written permission. |
| 19 |
# |
| 20 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 24 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 |
# POSSIBILITY OF SUCH DAMAGE. |
| 31 |
|
| 32 |
|
| 33 |
# This script creates a deployable package of COLMAP for macOS. |
| 34 |
|
| 35 |
set -euo pipefail |
| 36 |
|
| 37 |
if [[ $# -ne 1 ]]; then |
| 38 |
echo "Usage: $0 /path/to/colmap" >&2 |
| 39 |
exit 1 |
| 40 |
fi |
| 41 |
|
| 42 |
BINARY_PATH=$1 |
| 43 |
BASE_PATH=$(dirname "$BINARY_PATH") |
| 44 |
APP_PATH="$BASE_PATH/COLMAP.app" |
| 45 |
APP_BINARY="$APP_PATH/Contents/MacOS/colmap" |
| 46 |
APP_LAUNCHER="$APP_PATH/Contents/MacOS/colmap_gui.sh" |
| 47 |
ARCHIVE_PATH="$BASE_PATH/COLMAP-mac.zip" |
| 48 |
|
| 49 |
rm -rf "$APP_PATH" "$ARCHIVE_PATH" |
| 50 |
|
| 51 |
echo "Creating bundle directory" |
| 52 |
mkdir -p "$APP_PATH/Contents/MacOS" |
| 53 |
|
| 54 |
echo "Copying binary" |
| 55 |
cp "$BINARY_PATH" "$APP_BINARY" |
| 56 |
|
| 57 |
echo "Writing Info.plist" |
| 58 |
cat <<EOM >"$APP_PATH/Contents/Info.plist" |
| 59 |
<?xml version="1.0" encoding="UTF-8"?> |
| 60 |
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 61 |
<plist version="1.0"> |
| 62 |
<dict> |
| 63 |
<key>CFBundlePackageType</key> |
| 64 |
<string>APPL</string> |
| 65 |
<key>CFBundleExecutable</key> |
| 66 |
<string>colmap</string> |
| 67 |
<key>CFBundleIdentifier</key> |
| 68 |
<string>COLMAP</string> |
| 69 |
<key>CFBundleName</key> |
| 70 |
<string>COLMAP</string> |
| 71 |
<key>CFBundleDisplayName</key> |
| 72 |
<string>COLMAP</string> |
| 73 |
<key>NSHighResolutionCapable</key> |
| 74 |
<true/> |
| 75 |
<key>NSAppSleepDisabled</key> |
| 76 |
<true/> |
| 77 |
</dict> |
| 78 |
</plist> |
| 79 |
EOM |
| 80 |
|
| 81 |
echo "Linking dynamic libraries" |
| 82 |
"$(brew --prefix qt)/bin/macdeployqt" "$APP_PATH" -no-codesign |
| 83 |
|
| 84 |
echo "Wrapping binary" |
| 85 |
cat <<'EOM' >"$APP_LAUNCHER" |
| 86 |
#!/bin/bash |
| 87 |
script_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 88 |
exec "$script_path/colmap" gui |
| 89 |
EOM |
| 90 |
chmod +x "$APP_LAUNCHER" |
| 91 |
sed -i '' 's#<string>colmap</string>#<string>colmap_gui.sh</string>#g' "$APP_PATH/Contents/Info.plist" |
| 92 |
|
| 93 |
echo "Signing application binaries" |
| 94 |
BREW_PREFIX=$(brew --prefix) |
| 95 |
remove_homebrew_rpaths() { |
| 96 |
local binary_path=$1 |
| 97 |
while IFS= read -r rpath; do |
| 98 |
if [[ $rpath == "$BREW_PREFIX/"* ]]; then |
| 99 |
install_name_tool -delete_rpath "$rpath" "$binary_path" |
| 100 |
fi |
| 101 |
done < <(otool -l "$binary_path" | awk '/LC_RPATH/{getline; getline; print $2}') |
| 102 |
} |
| 103 |
|
| 104 |
while IFS= read -r file_path; do |
| 105 |
if file "$file_path" | grep -q "Mach-O"; then |
| 106 |
remove_homebrew_rpaths "$file_path" |
| 107 |
codesign --force --sign - "$file_path" >/dev/null 2>&1 |
| 108 |
fi |
| 109 |
done < <(find "$APP_PATH/Contents/Frameworks" "$APP_PATH/Contents/PlugIns" -type f) |
| 110 |
remove_homebrew_rpaths "$APP_BINARY" |
| 111 |
codesign --force --sign - "$APP_BINARY" >/dev/null 2>&1 |
| 112 |
codesign --force --sign - "$APP_PATH" >/dev/null 2>&1 |
| 113 |
|
| 114 |
echo "Checking packaged binary" |
| 115 |
"$APP_BINARY" help >/dev/null |
| 116 |
codesign --verify --deep --strict "$APP_PATH" |
| 117 |
|
| 118 |
echo "Compressing application" |
| 119 |
ditto -c -k --sequesterRsrc --keepParent "$APP_PATH" "$ARCHIVE_PATH" |