Skip to content

Commit f6e9c77

Browse files
authored
fix: build script for macOS development (#1491)
`build.sh --lit` as well as `build.sh --package` should now work with this PR on MacOS (ARM)
1 parent a2e304b commit f6e9c77

File tree

1 file changed

+63
-52
lines changed

1 file changed

+63
-52
lines changed

scripts/build.sh

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -208,85 +208,96 @@ function set_offline() {
208208
fi
209209
}
210210

211+
# Create shared library from static library with platform-specific flags
212+
function create_shared_library() {
213+
local cc=$1
214+
local lib_dir=$2
215+
local target=$3
216+
217+
log "Creating a shared library from the compiled static library"
218+
219+
# Check if we're on macOS and adjust linker flags accordingly
220+
case "$(uname -s)" in
221+
Darwin*)
222+
local cmd_args=("--shared" "-L$lib_dir" "-Wl,-force_load,$lib_dir/libiec61131std.a" "-o" "$lib_dir/libiec61131std.so" "-lm" "-framework" "CoreFoundation")
223+
if [[ -n "$target" ]]; then
224+
cmd_args+=("--target=$target")
225+
fi
226+
log "Running: $cc ${cmd_args[*]}"
227+
"$cc" "${cmd_args[@]}"
228+
;;
229+
*)
230+
local cmd_args=("--shared" "-L$lib_dir" "-Wl,--whole-archive" "-liec61131std" "-o" "$lib_dir/libiec61131std.so" "-Wl,--no-whole-archive" "-lm" "-fuse-ld=lld")
231+
if [[ -n "$target" ]]; then
232+
cmd_args+=("--target=$target")
233+
fi
234+
log "Running: $cc ${cmd_args[*]}"
235+
"$cc" "${cmd_args[@]}"
236+
;;
237+
esac
238+
}
239+
211240
function run_package_std() {
241+
local cc
212242
cc=$(get_compiler)
213-
OUTPUT_DIR=$project_location/output
243+
local OUTPUT_DIR=$project_location/output
244+
local target_dir="$project_location/target"
245+
local include_dir=$OUTPUT_DIR/include
246+
214247
make_dir "$OUTPUT_DIR"
215248
log "Packaging Standard functions"
216249
log "Removing previous output folder"
217-
rm -rf $OUTPUT_DIR
218-
target_dir="$project_location/target"
219-
include_dir=$OUTPUT_DIR/include
220-
make_dir $include_dir
221-
#Copy the iec61131-st folder
250+
rm -rf "$OUTPUT_DIR"
251+
make_dir "$include_dir"
252+
253+
# Copy the iec61131-st folder
222254
cp -r "$project_location"/libs/stdlib/iec61131-st/*.st "$include_dir"
223255

224-
if [[ ! -z $target ]]; then
225-
for val in ${target//,/ }
226-
do
227-
lib_dir=$OUTPUT_DIR/$val/lib
228-
make_dir $lib_dir
256+
if [[ -n "$target" ]]; then
257+
for val in ${target//,/ }; do
258+
local lib_dir=$OUTPUT_DIR/$val/lib
259+
make_dir "$lib_dir"
229260

230-
# if the target ends with -linux-gnu but does not have unknown, add unknown
261+
# Normalize target name for rustc
262+
local rustc_target=$val
231263
if [[ $val == *"-linux-gnu" && $val != *"unknown-linux-gnu" ]]; then
232264
rustc_target="${val/-linux-gnu/-unknown-linux-gnu}"
233-
else
234-
rustc_target=$val
235265
fi
236-
rel_dir="$target_dir/$rustc_target"
266+
267+
# Determine release or debug directory
268+
local rel_dir="$target_dir/$rustc_target"
237269
if [[ $release -ne 0 ]]; then
238270
rel_dir="$rel_dir/release"
239271
else
240272
rel_dir="$rel_dir/debug"
241273
fi
274+
242275
if [[ ! -d "$rel_dir" ]]; then
243276
echo "Compilation directory $rel_dir not found"
244277
exit 1
245278
fi
246-
cp "$rel_dir/"*.a "$lib_dir" 2>/dev/null || log "$rel_dir does not contain *.a files"
247-
# Create an SO file from the copied a file
248-
log "Creating a shared library from the compiled static library"
249-
log "Running : $cc --shared -L$lib_dir \
250-
-Wl,--whole-archive -liec61131std \
251-
-o $lib_dir/out.so -Wl,--no-whole-archive \
252-
-lm \
253-
-fuse-ld=lld \
254-
--target=$val"
255-
$cc --shared -L"$lib_dir" \
256-
-Wl,--whole-archive -liec61131std \
257-
-o "$lib_dir/out.so" -Wl,--no-whole-archive \
258-
-lm \
259-
-fuse-ld=lld \
260-
--target="$val"
261-
262-
mv "$lib_dir/out.so" "$lib_dir/libiec61131std.so"
279+
280+
cp "$rel_dir/"*.a "$lib_dir" 2>/dev/null || log "$rel_dir does not contain *.a files"
281+
create_shared_library "$cc" "$lib_dir" "$val"
263282
done
264283
else
265-
lib_dir=$OUTPUT_DIR/lib
266-
make_dir $lib_dir
284+
local lib_dir=$OUTPUT_DIR/lib
285+
make_dir "$lib_dir"
286+
287+
# Determine release or debug directory
288+
local rel_dir="$target_dir"
267289
if [[ $release -ne 0 ]]; then
268-
rel_dir="$target_dir/release"
290+
rel_dir="$rel_dir/release"
269291
else
270-
rel_dir="$target_dir/debug"
292+
rel_dir="$rel_dir/debug"
271293
fi
294+
272295
cp "$rel_dir/"*.a "$lib_dir" 2>/dev/null || log "$rel_dir does not contain *.a files"
273-
# Create an SO file from the copied a file
274-
log "Creating a shared library from the compiled static library"
275-
log "Running : $cc --shared -L"$lib_dir" \
276-
-Wl,--whole-archive -liec61131std \
277-
-o "$lib_dir/out.so" -Wl,--no-whole-archive \
278-
-lm \
279-
-fuse-ld=lld "
280-
$cc --shared -L"$lib_dir" \
281-
-Wl,--whole-archive -liec61131std \
282-
-o "$lib_dir/out.so" -Wl,--no-whole-archive \
283-
-lm \
284-
-fuse-ld=lld
285-
mv "$lib_dir/out.so" "$lib_dir/libiec61131std.so"
296+
create_shared_library "$cc" "$lib_dir" ""
286297
fi
287298

288299
log "Enabling read/write on the output folder"
289-
chmod a+rw $OUTPUT_DIR -R
300+
chmod -R a+rw "$OUTPUT_DIR"
290301

291302
}
292303

@@ -504,9 +515,9 @@ if [[ $coverage -ne 0 ]]; then
504515
run_coverage
505516
fi
506517

507-
if [[ -d $project_location/target/ ]]; then
518+
if [[ -d "$project_location/target/" ]]; then
508519
log "Allow access to target folders"
509-
chmod a+rw -R $project_location/target/
520+
chmod -R a+rw "$project_location/target/"
510521
fi
511522

512523
if [[ $offline -ne 0 ]]; then

0 commit comments

Comments
 (0)