File: docker-build.sh

package info (click to toggle)
js8call 2.5.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,720 kB
  • sloc: cpp: 562,651; sh: 898; python: 132; ansic: 102; makefile: 4
file content (211 lines) | stat: -rwxr-xr-x 6,505 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
# Optimized Docker build script for JS8Call with caching support

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to print colored output
print_status() {
    echo -e "${GREEN}[*]${NC} $1"
}

print_error() {
    echo -e "${RED}[!]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[!]${NC} $1"
}

print_info() {
    echo -e "${BLUE}[i]${NC} $1"
}

# Check if Docker is installed
if ! command -v docker &> /dev/null; then
    print_error "Docker is not installed. Please install Docker first."
    exit 1
fi

# Check if docker-compose is installed
if ! command -v docker-compose &> /dev/null; then
    print_warning "docker-compose not found, trying 'docker compose'"
    COMPOSE_CMD="docker compose"
else
    COMPOSE_CMD="docker-compose"
fi

# Enable BuildKit for better caching
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1

# Create output directory
mkdir -p output

print_status "JS8Call Docker build script with caching support"

# Parse command line arguments
BUILD_TYPE="release"
TARGET="all"
USE_CACHE=true
BUILD_BASE=false

while [[ $# -gt 0 ]]; do
    case $1 in
        --dev)
            BUILD_TYPE="dev"
            shift
            ;;
        --base)
            BUILD_BASE=true
            shift
            ;;
        --hamlib-only)
            TARGET="hamlib"
            shift
            ;;
        --cache)
            USE_CACHE=true
            shift
            ;;
        --no-cache)
            USE_CACHE=false
            shift
            ;;
        --clean)
            print_status "Cleaning previous builds..."
            rm -rf output/*
            docker rmi js8call-base:ubuntu-24.04 js8call-hamlib:latest js8call-builder:ubuntu-24.04 js8call-dev:ubuntu-24.04 2>/dev/null || true
            docker volume rm docker_ccache 2>/dev/null || true
            print_status "Clean complete"
            exit 0
            ;;
        --rebuild)
            print_status "Quick rebuild using cached images..."
            TARGET="rebuild"
            shift
            ;;
        --help)
            echo "Usage: $0 [OPTIONS]"
            echo ""
            echo "Options:"
            echo "  --dev          Start development container with shell"
            echo "  --base         Build/rebuild base image with all dependencies"
            echo "  --hamlib-only  Build only Hamlib"
            echo "  --cache        Use Docker build cache (default)"
            echo "  --no-cache     Build without cache"
            echo "  --clean        Clean all images and caches"
            echo "  --rebuild      Quick rebuild using existing base images"
            echo "  --help         Show this help message"
            echo ""
            echo "Examples:"
            echo "  $0                    # Normal build with cache"
            echo "  $0 --base             # Rebuild base image first"
            echo "  $0 --no-cache         # Full rebuild without cache"
            echo "  $0 --rebuild          # Quick rebuild for code changes"
            echo "  $0 --dev              # Development mode with shell"
            exit 0
            ;;
        *)
            print_error "Unknown option: $1"
            exit 1
            ;;
    esac
done

# Build base image if requested or if it doesn't exist
if [ "$BUILD_BASE" = true ] || ! docker images | grep -q "js8call-base.*ubuntu-24.04"; then
    print_status "Building base image with all dependencies..."
    if [ "$USE_CACHE" = false ]; then
        $COMPOSE_CMD build --no-cache js8call-base
    else
        $COMPOSE_CMD build js8call-base
    fi
fi

# Check if Hamlib image exists
if ! docker images | grep -q "js8call-hamlib.*latest"; then
    print_status "Hamlib image not found. Building Hamlib..."
    if [ "$USE_CACHE" = false ]; then
        $COMPOSE_CMD build --no-cache hamlib-builder
    else
        $COMPOSE_CMD build hamlib-builder
    fi
fi

# Build based on target
case $TARGET in
    hamlib)
        print_status "Building Hamlib only..."
        if [ "$USE_CACHE" = false ]; then
            $COMPOSE_CMD build --no-cache hamlib-builder
        else
            $COMPOSE_CMD build hamlib-builder
        fi
        print_status "Hamlib build complete!"
        ;;
    rebuild)
        print_status "Performing quick rebuild..."
        $COMPOSE_CMD build js8call-rebuild

        # Extract artifacts
        print_status "Extracting build artifacts..."
        docker run --rm -v "$(pwd)/output":/output js8call-rebuild:latest \
            sh -c "find /js8call-prefix/build -name '*.deb' -exec cp {} /output/ \;"

        print_info "Quick rebuild complete!"
        ls -la output/
        ;;
    all)
        if [ "$BUILD_TYPE" = "dev" ]; then
            print_status "Starting development container..."
            if [ "$USE_CACHE" = false ]; then
                $COMPOSE_CMD build --no-cache js8call-dev
            else
                $COMPOSE_CMD build js8call-dev
            fi
            print_info "Entering development shell. Source is mounted at /js8call-prefix/src"
            print_info "To build: cd /js8call-prefix/build && cmake ../src && make"
            $COMPOSE_CMD run --rm js8call-dev
        else
            print_status "Building JS8Call..."

            # Show ccache stats if available
            if docker volume ls | grep -q docker_ccache; then
                print_info "Using ccache for faster compilation"
            fi

            # Build using docker-compose
            if [ "$USE_CACHE" = false ]; then
                if ! $COMPOSE_CMD build --no-cache js8call-build; then
                    print_error "Build failed!"
                    exit 1
                fi
            else
                if ! $COMPOSE_CMD build js8call-build; then
                    print_error "Build failed!"
                    exit 1
                fi
            fi

            print_status "Extracting build artifacts..."
            $COMPOSE_CMD run --rm js8call-build

            print_status "Build complete! Output files:"
            ls -la output/

            # Check if we got the expected outputs
            if [ -z "$(ls output/*.deb 2>/dev/null)" ] && [ -z "$(ls output/*.AppImage 2>/dev/null)" ]; then
                print_warning "No output files found. Build may have failed."
            else
                print_status "Success! Build artifacts are in the 'output' directory."
            fi
        fi
        ;;
esac