Skip to content

Commit b395efd

Browse files
committed
Makefile: create build & packaging targets
Signed-off-by: Victoria Dye <[email protected]>
1 parent ee21643 commit b395efd

File tree

10 files changed

+521
-2
lines changed

10 files changed

+521
-2
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/git-bundle-server
2-
/git-bundle-web-server
1+
/_dist
2+
/bin

Makefile

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Default target
2+
build:
3+
4+
# Project metadata (note: to package, VERSION *must* be set by the caller)
5+
NAME := git-bundle-server
6+
VERSION :=
7+
PACKAGE_REVISION := 1
8+
9+
# Helpful paths
10+
BINDIR := $(CURDIR)/bin
11+
DISTDIR := $(CURDIR)/_dist
12+
PKGDIR := $(DISTDIR)/pkg
13+
DEBDIR := $(DISTDIR)/deb
14+
15+
# Platform information
16+
GOOS := $(shell go env GOOS)
17+
GOARCH := $(shell go env GOARCH)
18+
19+
# Packaging information
20+
SUPPORTED_PACKAGE_GOARCHES := amd64 arm64
21+
PACKAGE_ARCH := $(GOARCH)
22+
DEB_FILENAME := $(DISTDIR)/$(NAME)_$(VERSION)-$(PACKAGE_REVISION)_$(PACKAGE_ARCH).deb
23+
PKG_FILENAME := $(DISTDIR)/$(NAME)_$(VERSION)-$(PACKAGE_REVISION)_$(PACKAGE_ARCH).pkg
24+
ifeq ($(GOOS),linux)
25+
PACKAGE := $(DEB_FILENAME)
26+
else ifeq ($(GOOS),darwin)
27+
PACKAGE := $(PKG_FILENAME)
28+
else
29+
PACKAGE :=
30+
endif
31+
32+
# Build targets
33+
.PHONY: build
34+
build:
35+
$(RM) -r $(BINDIR)
36+
@mkdir -p $(BINDIR)
37+
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go build -o $(BINDIR) ./...
38+
39+
# Package targets
40+
define check_arch
41+
$(if $(filter $(GOARCH),$(SUPPORTED_PACKAGE_GOARCHES)), ,
42+
$(error cannot create package for GOARCH "$(GOARCH)", \
43+
supported architectures are: $(SUPPORTED_PACKAGE_GOARCHES)"))
44+
endef
45+
46+
.PHONY: check-arch
47+
check-arch:
48+
$(check_arch)
49+
50+
define check_version
51+
$(if $(VERSION), ,
52+
$(error version is undefined"))
53+
endef
54+
55+
.PHONY: check-version
56+
check-version:
57+
$(check_version)
58+
59+
# MacOS .pkg file
60+
# Steps:
61+
# 1. Layout files in _dist/pkg/payload/ as they'll be installed (including
62+
# uninstall.sh script).
63+
# 2. (Optional) Codesign the package contents in place.
64+
# 3. Create the component package in _dist/<name>.component.pkg.
65+
# 4. Create the product archive in _dist/pkg/.
66+
$(PKGDIR)/payload: GOOS := darwin
67+
$(PKGDIR)/payload: check-arch build
68+
@echo
69+
@echo "======== Formatting package contents ========"
70+
@build/package/layout-unix.sh --bindir="$(BINDIR)" \
71+
--uninstaller="$(CURDIR)/build/package/pkg/uninstall.sh" \
72+
--output="$(PKGDIR)/payload"
73+
74+
.PHONY: codesign
75+
codesign: $(PKGDIR)/payload
76+
@echo
77+
@echo "======== Codesigning package contents ========"
78+
@build/package/pkg/codesign.sh --payload="$(PKGDIR)/payload" \
79+
--developer-id="$(DEVELOPER_ID)" \
80+
--entitlements="$(CURDIR)/build/package/pkg/entitlements.xml"
81+
82+
ifdef NEED_CODESIGN
83+
$(PKG_FILENAME): codesign
84+
endif
85+
86+
$(PKG_FILENAME): check-version $(PKGDIR)/payload
87+
@echo
88+
@echo "======== Creating product archive package ========"
89+
@build/package/pkg/pack.sh --version="$(VERSION)" \
90+
--payload="$(PKGDIR)/payload" \
91+
--output="$(PKG_FILENAME)"
92+
93+
# Linux binary .deb file
94+
# Steps:
95+
# 1. Layout files in _dist/deb/root/ as they'll be installed (unlike MacOS
96+
# .pkg packages, symlinks created in the payload are preserved, so we
97+
# create them here to avoid doing so in a post-install step).
98+
# 2. Create the binary deb package in _dist/deb/.
99+
$(DEBDIR)/root: GOOS := linux
100+
$(DEBDIR)/root: check-arch build
101+
@echo
102+
@echo "======== Formatting package contents ========"
103+
@build/package/layout-unix.sh --bindir="$(BINDIR)" \
104+
--include-symlinks \
105+
--output="$(DEBDIR)/root"
106+
107+
$(DEB_FILENAME): check-version $(DEBDIR)/root
108+
@echo
109+
@echo "======== Creating binary Debian package ========"
110+
@build/package/deb/pack.sh --payload="$(DEBDIR)/root" \
111+
--arch="$(PACKAGE_ARCH)" \
112+
--version="$(VERSION)" \
113+
--distdir="$(DEBDIR)" \
114+
--output="$(DEB_FILENAME)"
115+
116+
.PHONY: package
117+
package: $(PACKAGE)
118+
119+
# Cleanup targets
120+
.PHONY: clean
121+
clean:
122+
go clean ./...
123+
$(RM) -r $(BINDIR)
124+
$(RM) -r $(DISTDIR)

build/package/deb/pack.sh

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
die () {
3+
echo "$*" >&2
4+
exit 1
5+
}
6+
7+
# Directories
8+
THISDIR="$( cd "$(dirname "$0")" ; pwd -P )"
9+
10+
# Local paths
11+
UNINSTALLER="$THISDIR/uninstall.sh"
12+
13+
# Parse script arguments
14+
for i in "$@"
15+
do
16+
case "$i" in
17+
--payload=*)
18+
DEBROOT="${i#*=}"
19+
shift # past argument=value
20+
;;
21+
--arch=*)
22+
ARCH="${i#*=}"
23+
shift # past argument=value
24+
;;
25+
--version=*)
26+
VERSION="${i#*=}"
27+
shift # past argument=value
28+
;;
29+
--output=*)
30+
DEBOUT="${i#*=}"
31+
shift # past argument=value
32+
;;
33+
*)
34+
# unknown option
35+
;;
36+
esac
37+
done
38+
39+
# Perform pre-execution checks
40+
if [ -z "$DEBROOT" ]; then
41+
die "--payload was not set"
42+
elif [ ! -d "$DEBROOT" ]; then
43+
die "Could not find '$DEBROOT'. Did you run layout-unix.sh first?"
44+
fi
45+
if [ -z "$ARCH" ]; then
46+
die "--arch was not set"
47+
fi
48+
if [ -z "$VERSION" ]; then
49+
die "--version was not set"
50+
fi
51+
if [ -z "$DEBOUT" ]; then
52+
die "--output was not set"
53+
fi
54+
55+
# Cleanup old package
56+
if [ -e "$DEBOUT" ]; then
57+
echo "Deleting old package '$DEBOUT'..."
58+
rm -f "$DEBOUT"
59+
fi
60+
61+
# Ensure the parent directory for the component exists
62+
mkdir -p "$(dirname "$DEBOUT")"
63+
64+
# Build .deb
65+
mkdir -p "$DEBROOT/DEBIAN" || exit 1
66+
67+
# Create the debian control file
68+
cat >"$DEBROOT/DEBIAN/control" <<EOF
69+
Package: git-bundle-server
70+
Version: $VERSION
71+
Section: vcs
72+
Priority: optional
73+
Architecture: $ARCH
74+
Depends:
75+
Maintainer: Git Bundle Server <[email protected]>
76+
Description: A self-hostable Git bundle server.
77+
EOF
78+
79+
dpkg-deb -Zxz --build "$DEBROOT" "$DEBOUT" || exit 1

build/package/deb/scripts/prerm

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Stop & cleanup the web server
5+
/usr/local/git-bundle-server/bin/git-bundle-server web-server stop --remove || exit 1
6+
7+
exit 0

build/package/layout-unix.sh

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
die () {
3+
echo "$*" >&2
4+
exit 1
5+
}
6+
7+
# Directories
8+
THISDIR="$( cd "$(dirname "$0")" ; pwd -P )"
9+
10+
# Parse script arguments
11+
for i in "$@"
12+
do
13+
case "$i" in
14+
--bindir=*)
15+
BINDIR="${i#*=}"
16+
shift # past argument=value
17+
;;
18+
--uninstaller=*)
19+
UNINSTALLER="${i#*=}"
20+
shift # past argument=value
21+
;;
22+
--include-symlinks)
23+
INCLUDE_SYMLINKS=1
24+
shift # past argument=value
25+
;;
26+
--output=*)
27+
PAYLOAD="${i#*=}"
28+
shift # past argument=value
29+
;;
30+
*)
31+
# unknown option
32+
;;
33+
esac
34+
done
35+
36+
# Perform pre-execution checks
37+
if [ -z "$BINDIR" ]; then
38+
die "--bindir was not set"
39+
fi
40+
if [ -z "$PAYLOAD" ]; then
41+
die "--output was not set"
42+
fi
43+
44+
# Cleanup any old payload directory
45+
if [ -d "$PAYLOAD" ]; then
46+
echo "Cleaning old output directory '$PAYLOAD'..."
47+
rm -rf "$PAYLOAD"
48+
fi
49+
50+
# Ensure payload directory exists
51+
INSTALL_TO="$PAYLOAD/usr/local/git-bundle-server/"
52+
mkdir -p "$INSTALL_TO" || exit 1
53+
54+
# Copy built binaries
55+
echo "Copying binaries..."
56+
cp -R "$BINDIR/." "$INSTALL_TO/bin" || exit 1
57+
58+
# Copy uninstaller script
59+
if [ -n "$UNINSTALLER" ]; then
60+
echo "Copying uninstall script..."
61+
cp "$UNINSTALLER" "$INSTALL_TO" || exit 1
62+
fi
63+
64+
# Create symlinks
65+
if [ -n "$INCLUDE_SYMLINKS" ]; then
66+
LINK_TO="$DEBROOT/usr/local/bin/"
67+
mkdir -p "$LINK_TO" || exit 1
68+
69+
echo "Creating symlinks..."
70+
for program in "$INSTALL_TO"/bin/*
71+
do
72+
ln -s -r "$program" "$LINK_TO/$(basename $program)" || exit 1
73+
done
74+
fi
75+
76+
# Remove any unwanted .DS_Store files
77+
echo "Removing unnecessary files..."
78+
find "$PAYLOAD" -name '*.DS_Store' -type f -delete || exit 1
79+
80+
echo "Layout complete."

build/package/pkg/codesign.sh

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
die () {
3+
echo "$*" >&2
4+
exit 1
5+
}
6+
7+
# Parse script arguments
8+
for i in "$@"
9+
do
10+
case "$i" in
11+
--payload=*)
12+
SIGN_DIR="${i#*=}"
13+
shift # past argument=value
14+
;;
15+
--developer-id=*)
16+
DEVELOPER_ID="${i#*=}"
17+
shift # past argument=value
18+
;;
19+
--entitlements=*)
20+
ENTITLEMENTS_FILE="${i#*=}"
21+
shift # past argument=value
22+
;;
23+
*)
24+
# unknown option
25+
;;
26+
esac
27+
done
28+
29+
if [ -z "$SIGN_DIR" ]; then
30+
die "--payload was not set"
31+
elif [ -z "$DEVELOPER_ID" ]; then
32+
die "--developer-id was not set"
33+
elif [ -z "$ENTITLEMENTS_FILE" ]; then
34+
die "--entitlements was not set"
35+
fi
36+
37+
echo "======== INPUTS ========"
38+
echo "Directory: $SIGN_DIR"
39+
echo "Developer ID: $DEVELOPER_ID"
40+
echo "Entitlements: $ENTITLEMENTS_FILE"
41+
echo "======== END INPUTS ========"
42+
43+
cd $SIGN_DIR
44+
for f in *
45+
do
46+
macho=$(file --mime $f | grep mach)
47+
# Runtime sign dylibs and Mach-O binaries
48+
if [[ $f == *.dylib ]] || [ ! -z "$macho" ];
49+
then
50+
echo "Runtime Signing $f"
51+
codesign -s "$DEVELOPER_ID" $f --timestamp --force --options=runtime --entitlements $ENTITLEMENTS_FILE
52+
elif [ -d "$f" ];
53+
then
54+
echo "Signing files in subdirectory $f"
55+
cd $f
56+
for i in *
57+
do
58+
codesign -s "$DEVELOPER_ID" $i --timestamp --force
59+
done
60+
cd ..
61+
else
62+
echo "Signing $f"
63+
codesign -s "$DEVELOPER_ID" $f --timestamp --force
64+
fi
65+
done

build/package/pkg/entitlements.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>com.apple.security.cs.allow-jit</key>
6+
<true/>
7+
<key>com.apple.security.cs.allow-unsigned-executable-memory</key>
8+
<true/>
9+
<key>com.apple.security.cs.disable-library-validation</key>
10+
<true/>
11+
</dict>
12+
</plist>

0 commit comments

Comments
 (0)