-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·126 lines (106 loc) · 3.91 KB
/
entrypoint.sh
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
#!/bin/bash
# Pico Build Action - Entrypoint script, this builds the code.
# Copyright 2024 Samyar Sadat Akhavi
# Written by Samyar Sadat Akhavi, 2024.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https: www.gnu.org/licenses/>.
set -e
# Define binary file extensions and build directories to ignore
DEFAULT_IGNORED_BUILD_DIRS=("CMakeFiles" "elf2uf2" "pico-sdk" "pioasm")
# Get arguments
SOURCE_DIR="$1"
OUTPUT_DIR="$2"
OUTPUT_EXT="$3"
BOARD_NAME="$4"
CMAKE_ARGS=$5
OUTPUT_IGNORED_DIRS="$6"
CMAKE_CONFIG_ONLY="$7"
MAKEFILES_GENERATOR="$8"
# Split output extensions and into array
IFS=" " read -r -a BINARY_EXTENSIONS <<< "$OUTPUT_EXT"
# Split ignored build directories into array and append to IGNORED_BUILD_DIRS
IFS=" " read -r -a IGNORED_BUILD_DIRS <<< "$OUTPUT_IGNORED_DIRS"
IGNORED_BUILD_DIRS=("${DEFAULT_IGNORED_BUILD_DIRS[@]}" "${IGNORED_BUILD_DIRS[@]}")
# Validate arguments
if [ -z "$SOURCE_DIR" ]; then
echo "ERROR: Source directory not provided."
exit 1
fi
if [ -z "$OUTPUT_DIR" ]; then
echo "ERROR: Output directory not provided."
exit 1
fi
if [ -z "$BOARD_NAME" ]; then
BOARD_NAME="pico"
fi
if [ -z "$MAKEFILES_GENERATOR" ]; then
MAKEFILES_GENERATOR="Ninja"
fi
if [ -z "$BINARY_EXTENSIONS" ]; then
BINARY_EXTENSIONS=("*.uf2" "*.elf" "*.elf.map")
fi
# Check if the source directory exists
if [ ! -d "$SOURCE_DIR" ]; then
echo "ERROR: Source directory does not exist."
exit 1
fi
# Make paths absolute
OUTPUT_DIR_RELATIVE="$SOURCE_DIR/$OUTPUT_DIR"
SOURCE_DIR="$GITHUB_WORKSPACE/$SOURCE_DIR"
OUTPUT_DIR="$SOURCE_DIR/$OUTPUT_DIR"
# Echo arguments
echo "Configuration:"
echo "SOURCE_DIR=$SOURCE_DIR"
echo "OUTPUT_DIR=$OUTPUT_DIR"
echo "BINARY_EXTENSIONS=${BINARY_EXTENSIONS[@]}"
echo "BOARD_NAME=$BOARD_NAME"
echo "CMAKE_ARGS=$CMAKE_ARGS"
echo "IGNORED_BUILD_DIRS=${IGNORED_BUILD_DIRS[@]}"
echo "CMAKE_CONFIG_ONLY=$CMAKE_CONFIG_ONLY"
echo "MAKEFILES_GENERATOR=$MAKEFILES_GENERATOR"
# Build the project
echo "Generating build files..."
mkdir "$OUTPUT_DIR" && cd "$OUTPUT_DIR"
cmake -DPICO_BOARD="$BOARD_NAME" -S "$SOURCE_DIR" -B "$OUTPUT_DIR" -G "$MAKEFILES_GENERATOR" $CMAKE_ARGS
if [ "$CMAKE_CONFIG_ONLY" = "false" ]; then
echo "Building project..."
cd "$OUTPUT_DIR" && make -j$(nproc)
# Remove ignored build directories
echo "Removing unnecessary build directories..."
for dir in "${IGNORED_BUILD_DIRS[@]}"; do
if [ -d "$OUTPUT_DIR/$dir" ]; then
echo "Removing $dir..."
rm -rf "$OUTPUT_DIR/$dir"
fi
done
# Move the build artifacts to temporary directory
echo "Moving build artifacts to temporary directory..."
COPY_DEST_DIR="/tmp/make_build"
for ext in "${BINARY_EXTENSIONS[@]}"; do
find "$OUTPUT_DIR" -name "$ext" -print0 | while IFS= read -r -d '' file; do
echo "Copying $file..."
relative_path="${file#$OUTPUT_DIR/}"
mkdir -p "$COPY_DEST_DIR/$(dirname "$relative_path")"
cp "$file" "$COPY_DEST_DIR/$relative_path"
done
done
# Clear the build directory
echo "Clearing the build directory..."
rm -rf "$OUTPUT_DIR" && mkdir "$OUTPUT_DIR"
# Move the build artifacts back to the output directory
echo "Moving build artifacts back to the output directory..."
mv /tmp/make_build/* "$OUTPUT_DIR"
fi
# Add output directory path to GITHUB_OUTPUT
echo "output_dir=$OUTPUT_DIR_RELATIVE" >> $GITHUB_OUTPUT