Skip to content

Commit 24b5f77

Browse files
authored
refactor: consolidate duplicated code across build scripts and Makefiles (#21)
* refactor: consolidate duplicated code across build scripts and Makefiles - Create scripts/lib/logging.sh with centralized logging functions - Create scripts/lib/common.sh with shared build utilities (normalize_license, verify_binary_arch, install_cmake_3x) - Create shared/ffmpeg.mk with common FFmpeg codec options (BSD, LGPL, GPL) - Refactor all platform build.sh scripts to use shared libraries - Update docker/build.sh to use shared libraries - Add STRIP variable to darwin config.mk files for consistency with linux - Fix darwin-x64 terminal detection (was missing [[ -t 1 ]] check) - Remove unused variables: CACHE_VERSION, AUTOCONF_DARWIN_ARGS, PARALLEL_CODECS, validate_versions * style: fix SC2015 shellcheck warning in release.yml Replace `A && B && C || D` pattern with proper if-then-else to avoid ambiguous control flow that shellcheck warns about.
1 parent 96ee0de commit 24b5f77

18 files changed

Lines changed: 386 additions & 593 deletions

File tree

.github/workflows/release.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ jobs:
161161
cd artifacts
162162
# Flatten subdirectories
163163
for dir in */; do
164-
[[ -d "$dir" ]] && mv "$dir"* . 2>/dev/null && rmdir "$dir" 2>/dev/null || true
164+
if [[ -d "$dir" ]]; then
165+
mv "$dir"* . 2>/dev/null || true
166+
rmdir "$dir" 2>/dev/null || true
167+
fi
165168
done
166169
167170
# Verify count

docker/build.sh

Lines changed: 13 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,11 @@ set -euo pipefail
2323
# Resolve script directory
2424
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2525
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
26+
readonly SCRIPT_DIR PROJECT_ROOT
2627

27-
# =============================================================================
28-
# Colors
29-
# =============================================================================
30-
31-
if [[ -t 1 ]]; then
32-
readonly RED='\033[0;31m'
33-
readonly GREEN='\033[0;32m'
34-
readonly YELLOW='\033[1;33m'
35-
readonly BLUE='\033[0;34m'
36-
readonly NC='\033[0m'
37-
else
38-
readonly RED=''
39-
readonly GREEN=''
40-
readonly YELLOW=''
41-
readonly BLUE=''
42-
readonly NC=''
43-
fi
44-
45-
log_info() { echo -e "${GREEN}[INFO]${NC} $*"; }
46-
log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
47-
log_error() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
48-
log_step() { echo -e "${BLUE}[STEP]${NC} $*"; }
28+
# Source shared libraries
29+
source "${PROJECT_ROOT}/scripts/lib/logging.sh"
30+
source "${PROJECT_ROOT}/scripts/lib/common.sh"
4931

5032
# =============================================================================
5133
# Configuration
@@ -65,9 +47,11 @@ fi
6547
case "$PLATFORM" in
6648
linux-x64)
6749
DOCKER_TARGET="builder-x64"
50+
EXPECTED_ARCH="x86-64"
6851
;;
6952
linux-arm64)
7053
DOCKER_TARGET="builder-arm64"
54+
EXPECTED_ARCH="aarch64"
7155
;;
7256
*)
7357
log_error "Unknown platform: $PLATFORM"
@@ -76,31 +60,13 @@ case "$PLATFORM" in
7660
;;
7761
esac
7862

79-
# Backwards compatibility: map old values to new
80-
case "$LICENSE" in
81-
bsd|lgpl)
82-
log_warn "DEPRECATION: LICENSE=$LICENSE is deprecated. Use LICENSE=free instead."
83-
LICENSE="free"
84-
;;
85-
gpl)
86-
log_warn "DEPRECATION: LICENSE=gpl is deprecated. Use LICENSE=non-free instead."
87-
LICENSE="non-free"
88-
;;
89-
esac
90-
91-
# Validate license
92-
if [[ ! "$LICENSE" =~ ^(free|non-free)$ ]]; then
93-
log_error "Invalid LICENSE=$LICENSE. Must be: free, non-free"
94-
exit 1
95-
fi
63+
# Normalize license
64+
LICENSE="$(normalize_license "$LICENSE")" || exit 1
9665

9766
# =============================================================================
9867
# Docker Functions
9968
# =============================================================================
10069

101-
#######################################
102-
# Check if Docker is available and running.
103-
#######################################
10470
check_docker() {
10571
if ! command -v docker &>/dev/null; then
10672
log_error "Docker is not installed"
@@ -114,9 +80,6 @@ check_docker() {
11480
fi
11581
}
11682

117-
#######################################
118-
# Build the Docker image for the platform.
119-
#######################################
12083
build_image() {
12184
local image_name="ffmpeg-build:${PLATFORM}"
12285

@@ -129,22 +92,14 @@ build_image() {
12992
"${PROJECT_ROOT}"
13093
}
13194

132-
#######################################
133-
# Run the build inside Docker container.
134-
#######################################
135-
run_build() {
95+
run_docker_build() {
13696
local image_name="ffmpeg-build:${PLATFORM}"
13797

13898
log_step "Running build in container..."
13999
log_info "Platform: ${PLATFORM}"
140100
log_info "Target: ${TARGET}"
141101
log_info "License: ${LICENSE}"
142102

143-
# Run build with project mounted
144-
# --rm: Remove container after exit
145-
# -v: Mount project directory
146-
# -e: Pass environment variables
147-
# -w: Set working directory
148103
docker run --rm \
149104
-v "${PROJECT_ROOT}:/build:rw" \
150105
-e "LICENSE=${LICENSE}" \
@@ -154,10 +109,7 @@ run_build() {
154109
make -C "/build/platforms/${PLATFORM}" LICENSE="${LICENSE}" "${TARGET}"
155110
}
156111

157-
#######################################
158-
# Verify the build output.
159-
#######################################
160-
verify_build() {
112+
verify_docker_build() {
161113
local artifacts_dir="${PROJECT_ROOT}/artifacts/${PLATFORM}-${LICENSE}"
162114
local ffmpeg_bin="${artifacts_dir}/bin/ffmpeg"
163115

@@ -168,29 +120,10 @@ verify_build() {
168120

169121
log_step "Verifying build..."
170122

171-
# Verify architecture
172-
local expected_arch
173-
case "$PLATFORM" in
174-
linux-x64)
175-
expected_arch="x86-64"
176-
;;
177-
linux-arm64)
178-
expected_arch="aarch64"
179-
;;
180-
esac
181-
182-
local file_output
183-
file_output="$(file "$ffmpeg_bin")"
184-
185-
if ! echo "$file_output" | grep -qi "$expected_arch"; then
186-
log_error "Architecture mismatch!"
187-
log_error "Expected: $expected_arch"
188-
log_error "Got: $file_output"
123+
if ! verify_binary_arch "$ffmpeg_bin" "$EXPECTED_ARCH"; then
189124
exit 1
190125
fi
191126

192-
log_info "Architecture verified: $expected_arch"
193-
194127
# Verify static linking (only libc, libm, libpthread should be dynamic)
195128
log_step "Checking dynamic dependencies..."
196129
if command -v ldd &>/dev/null && [[ "$PLATFORM" == "linux-x64" ]]; then
@@ -214,10 +147,10 @@ main() {
214147

215148
check_docker
216149
build_image
217-
run_build
150+
run_docker_build
218151

219152
if [[ "$TARGET" == "all" ]] || [[ "$TARGET" == "package" ]]; then
220-
verify_build
153+
verify_docker_build
221154

222155
local artifacts_dir="${PROJECT_ROOT}/artifacts/${PLATFORM}-${LICENSE}"
223156
echo ""

openspec/project.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,4 @@ Each platform's `config.mk` sets: `LIBVPX_TARGET`, `X264_HOST`, `AOM_TARGET_CPU`
192192
- npm registry (@pproenca scope)
193193

194194
**Version Management:**
195-
All versions, URLs, and checksums centralized in `shared/versions.mk`. Bump `CACHE_VERSION` to invalidate CI cache.
195+
All versions, URLs, and checksums centralized in `shared/versions.mk`.

platforms/darwin-arm64/Makefile

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ include $(ROOT_DIR)/config.mk
5252
include $(PROJECT_ROOT)/shared/verify.mk
5353
include $(PROJECT_ROOT)/shared/codecs/pkgconfig.mk
5454
include $(PROJECT_ROOT)/shared/codecs/codec.mk
55+
include $(PROJECT_ROOT)/shared/ffmpeg.mk
5556

5657
# =============================================================================
5758
# Include Individual Codec Build Rules (from shared)
@@ -124,29 +125,7 @@ FFMPEG_BASE_OPTS := \
124125
--disable-podpages \
125126
--disable-txtpages
126127

127-
FFMPEG_BSD_OPTS := \
128-
--enable-libvpx \
129-
--enable-libaom \
130-
--enable-libsvtav1 \
131-
--enable-libdav1d \
132-
--enable-libopus \
133-
--enable-libvorbis
134-
135-
FFMPEG_LGPL_OPTS := \
136-
--enable-libmp3lame
137-
138-
FFMPEG_GPL_OPTS := \
139-
--enable-gpl \
140-
--enable-libx264 \
141-
--enable-libx265
142-
143-
ifeq ($(LICENSE),free)
144-
FFMPEG_LICENSE_OPTS := $(FFMPEG_BSD_OPTS) $(FFMPEG_LGPL_OPTS)
145-
else
146-
# non-free: includes GPL codecs
147-
FFMPEG_LICENSE_OPTS := $(FFMPEG_BSD_OPTS) $(FFMPEG_LGPL_OPTS) $(FFMPEG_GPL_OPTS)
148-
endif
149-
128+
# FFmpeg codec options (BSD, LGPL, GPL) defined in shared/ffmpeg.mk
150129
FFMPEG_CONFIGURE_OPTS := $(FFMPEG_BASE_OPTS) $(FFMPEG_LICENSE_OPTS)
151130

152131
# -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)