Repository navigation
Expand file tree
/
Copy pathbuild_libe3
More file actions
executable file
·928 lines (822 loc) · 29.1 KB
/
Copy pathbuild_libe3
File metadata and controls
executable file
·928 lines (822 loc) · 29.1 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
#!/bin/bash
#
# libe3 - Vendor-Neutral E3AP C++ Library
#
# SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
# SPDX-License-Identifier: Apache-2.0
#
# Build script for libe3 library
# Inspired by OpenAirInterface's build_oai script
set -e
# ============================================================================
# Configuration
# ============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Single source of truth for the version: the VERSION file (same as CMake reads).
LIBE3_VERSION="$(tr -d '[:space:]' < "$SCRIPT_DIR/VERSION" 2>/dev/null)"
LIBE3_VERSION="${LIBE3_VERSION:-unknown}"
BUILD_DIR="build"
CMAKE_BUILD_TYPE="Release"
CMAKE_GENERATOR=""
CMAKE_OPTIONS=""
CLEAN=0
CLEAN_ALL=0
INSTALL=0
UNINSTALL=0
INSTALL_PREFIX=""
INSTALL_DEPS=0
BUILD_TESTS=1
BUILD_EXAMPLES=1
ENABLE_ZMQ=1
ENABLE_ASN1=1
ENABLE_JSON=1
ENABLE_PROTOBUF=0
ENABLE_SCTP=0
ENABLE_ASAN=0
ENABLE_TSAN=0
ENABLE_SWIG=0
ENABLE_UBSAN=0
ENABLE_MSAN=0
VERBOSE=0
JOBS=$(nproc)
ARCH_NATIVE=0
RUN_TESTS=0
FATAL_ERRORS=0
BUILD_DOCS=0
BUILD_DOCS_STRICT=0
CREATE_DEB=0
DEB_ARCH=""
# Terminal colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# ============================================================================
# Helper Functions
# ============================================================================
echo_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
echo_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
echo_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
echo_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_help() {
cat << EOF
libe3 - A Vendor-Neutral E3AP C++ Library, v${LIBE3_VERSION}
Copyright (c) 2026 Northeastern University.
==================
This script builds the libe3 E3AP library and its documentation.
Usage: $(basename "$0") [OPTIONS]
Build Options:
--------------
-c, --clean Clean build directory before building
-C, --clean-all Remove all build artifacts and start fresh
-d, --build-dir DIR Set build directory (default: build)
-j, --jobs N Number of parallel jobs (default: $(nproc))
-v, --verbose Enable verbose build output
--ninja Use Ninja build system instead of Make
Build Types:
------------
-g, --debug Build with debug symbols (Debug)
-r, --release Build optimized release (Release, default)
--relwithdebinfo Build optimized with debug info (RelWithDebInfo)
--minsizerel Build for minimum size (MinSizeRel)
Features:
---------
--enable-zmq Enable ZeroMQ transport (default: ON)
--disable-zmq Disable ZeroMQ transport
--enable-asn1 Enable ASN.1 encoding support (default: ON)
--disable-asn1 Disable ASN.1 encoding
--enable-json Enable JSON encoding support (default: ON)
--disable-json Disable JSON encoding
--enable-protobuf Enable Protocol Buffers encoding support (default: OFF)
--all-encodings Enable ASN.1, JSON and Protocol Buffers (what a gNB that has to serve
any dApp needs; same as the three --enable-* options)
--disable-protobuf Disable Protocol Buffers encoding
--enable-sctp Enable the SCTP POSIX transport (default: OFF).
Needs the SCTP headers: libsctp-dev (Debian/Ubuntu),
lksctp-tools-devel (Fedora/RHEL), lksctp-tools (Arch).
--install-deps does NOT install them; SCTP is optional
and the deployed E3 links are IPC or TCP.
--disable-sctp Disable the SCTP transport
--enable-swig Build the Python (SWIG) binding, libe3py, in build/swig to develop and
test it (default: OFF). It is not installed with the library: install
it with pip (pip install libe3py, or pip install . in this checkout)
after installing libe3.
--disable-swig Disable the Python (SWIG) bindings
--enable-tests Build unit tests (default: ON)
--disable-tests Disable unit tests
--enable-examples Build examples (default: ON)
--disable-examples Disable examples
--docs Build documentation (via CMake 'docs' target)
--docs-strict Build documentation, warning on every undocumented member, parameter and return value
Sanitizers:
-----------
--sanitize Enable address and undefined sanitizers
--sanitize-address Enable AddressSanitizer
--sanitize-thread Enable ThreadSanitizer
--sanitize-undefined Enable UndefinedBehaviorSanitizer
--sanitize-memory Enable MemorySanitizer (requires clang)
Installation:
-------------
-I, --install-deps Install required dependencies (requires sudo)
--install Install library after building (uses sudo only if the prefix is not
writable by you, then refreshes the linker cache with ldconfig)
--uninstall Uninstall library using install manifest
--prefix DIR Installation prefix (default: /usr/local). A prefix in your home, such as
~/.local/libe3, needs no sudo; put its lib/pkgconfig on PKG_CONFIG_PATH.
Testing:
--------
-t, --test Run tests after building
Packaging:
----------
--deb Create .deb package(s) after build
--deb-arch ARCH Package arch: amd64, arm64, or all (default: host)
Other:
------
--arch-native Enable -march=native compiler flag
--fatal-errors Stop compilation on first error (-Wfatal-errors)
--cmake-opt "OPTS" Pass additional options to CMake
-h, --help Show this help message
Examples:
---------
# Basic release build
./$(basename "$0")
# Debug build with tests
./$(basename "$0") -g -t
# Clean release build with ZMQ disabled
./$(basename "$0") -c --disable-zmq
# Install dependencies and build
./$(basename "$0") -I
# Uninstall files installed from this build directory
./$(basename "$0") --uninstall
# Build and create Debian package for host architecture
./$(basename "$0") --deb
# Build and create Debian packages for amd64 and arm64
./$(basename "$0") --deb --deb-arch all
# Build with sanitizers for debugging
./$(basename "$0") -g --sanitize
# Build with Ninja and verbose output
./$(basename "$0") --ninja -v
EOF
}
# Install ASN.1 compiler from source (used on Debian where package may be outdated)
install_asn1c_from_source() {
local pinned_commit="940dd5fa9f3917913fd487b13dfddfacd0ded06e"
local build_id="$pinned_commit+nsa"
local stamp="/opt/asn1c/.installed-commit"
# Already installed for this exact pinned commit (e.g. restored from a CI
# cache) - nothing to do. Checking the stamp, not just the binary's
# presence, matters for commit-trailers.yml's per-commit-build job: it
# checks out several commits in the same container, and an earlier commit
# could pin a different asn1c revision than the one already on disk.
if [[ -x /opt/asn1c/bin/asn1c ]] && [[ "$(cat "$stamp" 2>/dev/null)" == "$build_id" ]]; then
echo_info "asn1c already installed at /opt/asn1c for commit $pinned_commit, skipping rebuild"
return 0
fi
local asn1_install_log="$SCRIPT_DIR/build/log/asn1c_install_log.txt"
mkdir -p "$SCRIPT_DIR/build/log"
echo_info "\nInstalling ASN1 from source. Log: $asn1_install_log"
# set +e around the subshell: without it, this function's inherited `set -e`
# would abort here on a non-zero subshell exit before `status=$?` below ever
# runs, skipping the diagnostics entirely. The inner `set -e` is still needed
# so a failing step (e.g. checkout) stops the sequence instead of silently
# continuing to build an unpinned tree.
set +e
(
set -e
sudo $INSTALLER -y install bison flex autoconf automake libtool m4 || true
sudo rm -rf /tmp/asn1c || true
git clone https://github.com/mouse07410/asn1c /tmp/asn1c
cd /tmp/asn1c
git checkout "$pinned_commit"
git log -n1 || true
autoreconf -iv
# asn1c breaks C99 aliasing rules; gcc >= 15 at -O2 miscompiles it (vlm/asn1c#509)
CFLAGS="-O2 -fno-strict-aliasing" ./configure --prefix /opt/asn1c/
make -j"$(nproc)"
sudo make install
) > "$asn1_install_log" 2>&1
local status=$?
set -e
if [[ -v CI_ENV ]]; then
sudo rm -rf /tmp/asn1c || true
fi
if [[ $status -ne 0 ]]; then
echo_error "asn1c build from source failed (exit $status). Last 200 lines of $asn1_install_log:"
tail -n 200 "$asn1_install_log"
return "$status"
fi
echo "$build_id" | sudo tee "$stamp" > /dev/null
}
check_command() {
if ! command -v "$1" &> /dev/null; then
return 1
fi
return 0
}
# apt retries a file itself and gives up on a connection silent for 30 s, so a slow mirror is
# waited for. DPkg::Lock::Timeout waits for a dpkg that is still running.
APT_OPTS=(-o Acquire::Retries=5 -o Acquire::http::Timeout=30 -o Acquire::https::Timeout=30
-o DPkg::Lock::Timeout=300)
# Only commands that download may run under a limit: a killed download is resumed, but a killed
# dpkg stays alive, keeps its lock and fails every later attempt.
retry_apt() {
local limit="$1"; shift
local attempt
for attempt in 1 2 3; do
if sudo timeout "$limit" apt-get "${APT_OPTS[@]}" "$@"; then
return 0
fi
echo_warning "apt-get $* did not finish (attempt $attempt/3, limit ${limit}s)"
done
echo_error "apt-get $* failed after 3 attempts"
return 1
}
# Download under a limit and retries, then unpack with no limit.
apt_install() {
retry_apt "${APT_UPDATE_LIMIT:-300}" update || return 1
retry_apt "${APT_DOWNLOAD_LIMIT:-600}" install -y --download-only "$@" || return 1
sudo apt-get "${APT_OPTS[@]}" install -y "$@"
}
install_dependencies() {
echo_info "Installing dependencies..."
# Detect package manager
if check_command apt-get; then
echo_info "Detected Debian/Ubuntu system"
apt_install \
build-essential \
cmake \
pkg-config \
libzmq3-dev \
ninja-build \
git \
autoconf \
automake \
libtool \
m4 \
bison \
flex \
nlohmann-json3-dev \
protobuf-compiler \
libprotobuf-dev \
swig \
python3-dev
# Build and install asn1c from source (some distros ship old versions)
INSTALLER="apt-get"
install_asn1c_from_source
elif check_command dnf; then
echo_info "Detected Fedora/RHEL system"
sudo dnf install -y \
gcc-c++ \
cmake \
pkg-config \
zeromq-devel \
ninja-build \
git \
asn1c \
nlohmann-json-devel \
protobuf-compiler \
protobuf-devel \
swig \
python3-devel
elif check_command pacman; then
echo_info "Detected Arch Linux system"
sudo pacman -Sy --noconfirm \
base-devel \
cmake \
pkg-config \
zeromq \
ninja \
git \
asn1c \
nlohmann-json \
protobuf \
swig \
python
elif check_command brew; then
echo_info "Detected macOS with Homebrew"
brew install cmake pkg-config zeromq ninja asn1c nlohmann-json protobuf swig python
else
echo_error "Unsupported package manager. Please install dependencies manually:"
echo " - cmake (>= 3.16)"
echo " - C++17 compiler (gcc >= 7, clang >= 5)"
echo " - pkg-config"
echo " - libzmq (optional, for ZMQ transport)"
echo " - ninja (optional, for faster builds)"
exit 1
fi
echo_success "Dependencies installed successfully"
}
clean_build() {
if [ -d "$SCRIPT_DIR/$BUILD_DIR" ]; then
echo_info "Cleaning build directory: $BUILD_DIR"
rm -rf "$SCRIPT_DIR/$BUILD_DIR"
fi
}
clean_all() {
echo_info "Cleaning all build artifacts..."
rm -rf "$SCRIPT_DIR/build"*
rm -rf "$SCRIPT_DIR/cmake-build-"*
rm -rf "$SCRIPT_DIR/CMakeCache.txt"
rm -rf "$SCRIPT_DIR/CMakeFiles"
echo_success "All build artifacts cleaned"
}
configure_cmake() {
local cmake_cmd="cmake"
local build_path="$SCRIPT_DIR/$BUILD_DIR"
mkdir -p "$build_path"
cd "$build_path"
# Build CMake command
local cmake_args=(
"-DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE"
"-DLIBE3_BUILD_TESTS=$([ $BUILD_TESTS -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_BUILD_EXAMPLES=$([ $BUILD_EXAMPLES -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_ENABLE_ZMQ=$([ $ENABLE_ZMQ -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_ENABLE_ASN1=$([ $ENABLE_ASN1 -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_ENABLE_JSON=$([ $ENABLE_JSON -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_ENABLE_PROTOBUF=$([ $ENABLE_PROTOBUF -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_ENABLE_SCTP=$([ $ENABLE_SCTP -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_ENABLE_ASAN=$([ $ENABLE_ASAN -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_ENABLE_TSAN=$([ $ENABLE_TSAN -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_ENABLE_SWIG=$([ $ENABLE_SWIG -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_BUILD_DOCS=$([ $BUILD_DOCS -eq 1 ] && echo ON || echo OFF)"
"-DLIBE3_DOCS_STRICT=$([ $BUILD_DOCS_STRICT -eq 1 ] && echo ON || echo OFF)"
)
# Generator
if [ -n "$CMAKE_GENERATOR" ]; then
cmake_args+=("-G" "$CMAKE_GENERATOR")
fi
# Arch native
if [ $ARCH_NATIVE -eq 1 ]; then
cmake_args+=("-DCMAKE_CXX_FLAGS=-march=native")
fi
# Fatal errors - stop on first error
if [ $FATAL_ERRORS -eq 1 ]; then
cmake_args+=("-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS:+$CMAKE_CXX_FLAGS }-Wfatal-errors")
cmake_args+=("-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS:+$CMAKE_C_FLAGS }-Wfatal-errors")
fi
# Install prefix
if [ -n "$INSTALL_PREFIX" ]; then
cmake_args+=("-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX")
fi
# Additional sanitizers (need to add to CMake)
if [ $ENABLE_UBSAN -eq 1 ]; then
cmake_args+=("-DCMAKE_CXX_FLAGS=-fsanitize=undefined -fno-omit-frame-pointer")
cmake_args+=("-DCMAKE_EXE_LINKER_FLAGS=-fsanitize=undefined")
fi
if [ $ENABLE_MSAN -eq 1 ]; then
cmake_args+=("-DCMAKE_CXX_FLAGS=-fsanitize=memory -fno-omit-frame-pointer")
cmake_args+=("-DCMAKE_EXE_LINKER_FLAGS=-fsanitize=memory")
fi
# Additional CMake options
if [ -n "$CMAKE_OPTIONS" ]; then
cmake_args+=($CMAKE_OPTIONS)
fi
echo_info "Configuring CMake..."
echo_info "Build type: $CMAKE_BUILD_TYPE"
echo_info "Build directory: $build_path"
if [ $VERBOSE -eq 1 ]; then
echo_info "CMake arguments: ${cmake_args[*]}"
fi
$cmake_cmd "${cmake_args[@]}" "$SCRIPT_DIR"
}
build_library() {
local build_path="$SCRIPT_DIR/$BUILD_DIR"
cd "$build_path"
local build_cmd="cmake --build ."
local build_args=("--parallel" "$JOBS")
if [ $VERBOSE -eq 1 ]; then
build_args+=("--verbose")
fi
echo_info "Building libe3 with $JOBS parallel jobs..."
$build_cmd "${build_args[@]}"
echo_success "Build completed successfully!"
}
build_docs() {
local build_path="$SCRIPT_DIR/$BUILD_DIR"
cd "$build_path"
echo_info "Building documentation (target: docs)..."
# Attempt to build a CMake 'docs' target. Many projects expose docs via a target.
cmake --build . --target docs --parallel "$JOBS"
echo_success "Documentation build completed!"
}
run_tests() {
local build_path="$SCRIPT_DIR/$BUILD_DIR"
cd "$build_path"
echo_info "Running tests..."
ctest --output-on-failure --parallel "$JOBS"
echo_success "All tests passed!"
}
install_library() {
local build_path="$SCRIPT_DIR/$BUILD_DIR"
cd "$build_path"
local prefix
prefix="$(sed -n 's/^CMAKE_INSTALL_PREFIX:PATH=//p' CMakeCache.txt | head -n1)"
prefix="${prefix:-/usr/local}"
# sudo only when the prefix (or the closest existing parent) is not writable by this user
local probe="$prefix"
while [ ! -e "$probe" ] && [ "$probe" != "/" ]; do probe="$(dirname "$probe")"; done
local runner=()
if [ ! -w "$probe" ]; then
runner=(sudo)
fi
echo_info "Installing libe3 into $prefix${runner:+ (with sudo)}..."
"${runner[@]}" cmake --install .
# A library in a directory the dynamic linker searches (/usr/local/lib) is only found at run
# time after the linker cache is refreshed.
if [ "$(uname -s)" = "Linux" ] && command -v ldconfig >/dev/null 2>&1 \
&& { [ "$prefix" = "/usr" ] || [ "$prefix" = "/usr/local" ]; }; then
"${runner[@]}" ldconfig || echo_warning "ldconfig failed; run it once by hand (sudo ldconfig)"
fi
echo_success "Installation completed!"
case "$prefix" in
/usr|/usr/local) ;;
*)
echo_info "libe3 is not in a system path. Before building something against it, run:"
echo " export PKG_CONFIG_PATH=$prefix/lib/pkgconfig:\$PKG_CONFIG_PATH"
;;
esac
if [ "$ENABLE_SWIG" -eq 1 ]; then
echo_info "The Python binding is not installed with the library. In your virtual environment run:"
echo " pip install libe3py==$(cat "$SCRIPT_DIR/VERSION") # or: pip install $SCRIPT_DIR"
fi
}
uninstall_library() {
local build_path="$SCRIPT_DIR/$BUILD_DIR"
local manifest_path="$build_path/install_manifest.txt"
local cache_path="$build_path/CMakeCache.txt"
local cache_prefix=""
local -a uninstall_prefixes=()
local prefix
# Include explicit --prefix when provided.
if [ -n "$INSTALL_PREFIX" ]; then
uninstall_prefixes+=("$INSTALL_PREFIX")
fi
# Include prefix from CMake cache when available.
if [ -f "$cache_path" ]; then
cache_prefix="$(sed -n 's/^CMAKE_INSTALL_PREFIX:PATH=//p' "$cache_path" | head -n1)"
if [ -n "$cache_prefix" ]; then
uninstall_prefixes+=("$cache_prefix")
fi
fi
# /usr/local is the default for CMake install.
uninstall_prefixes+=("/usr/local")
if [ -f "$manifest_path" ]; then
echo_info "Uninstalling files listed in manifest: $manifest_path"
sudo sh -c 'while IFS= read -r path; do rm -f "$path"; done' < "$manifest_path"
else
echo_warning "Install manifest not found at $manifest_path, using prefix cleanup only."
fi
# Clean known libe3 install paths from candidate prefixes.
for prefix in "${uninstall_prefixes[@]}"; do
# Skip unsafe empty/root prefixes.
if [ -z "$prefix" ] || [ "$prefix" = "/" ]; then
continue
fi
echo_info "Removing libe3 install paths under prefix: $prefix"
sudo rm -rf \
"$prefix/include/libe3" \
"$prefix/lib/liblibe3.a" \
"$prefix/lib/liblibe3.so" \
"$prefix/lib/liblibe3.so.0" \
"$prefix/lib/liblibe3.so.$LIBE3_VERSION" \
"$prefix/lib/libasn1_e3ap.a" \
"$prefix/lib/pkgconfig/libe3.pc" \
"$prefix/lib/cmake/libe3" \
"$prefix/lib64/liblibe3.a" \
"$prefix/lib64/liblibe3.so" \
"$prefix/lib64/liblibe3.so.0" \
"$prefix/lib64/liblibe3.so.$LIBE3_VERSION" \
"$prefix/lib64/libasn1_e3ap.a" \
"$prefix/lib64/pkgconfig/libe3.pc" \
"$prefix/lib64/cmake/libe3"
done
echo_success "Uninstallation completed. Re-run with --verbose and check the prefix if files remain."
}
create_deb_packages() {
local deb_script="$SCRIPT_DIR/scripts/create_deb.sh"
local build_path="$SCRIPT_DIR/$BUILD_DIR"
if [ ! -f "$deb_script" ]; then
echo_error "Packaging script not found: $deb_script"
exit 1
fi
if [ ! -x "$deb_script" ]; then
chmod +x "$deb_script"
fi
echo_info "Creating Debian package(s)..."
if [ -n "$DEB_ARCH" ]; then
"$deb_script" --use-existing-build --build-dir "$build_path" "$DEB_ARCH"
else
"$deb_script" --use-existing-build --build-dir "$build_path"
fi
echo_success "Debian package creation completed!"
}
print_summary() {
echo ""
echo "======================================"
echo " libe3 Build Summary"
echo "======================================"
echo " Build Type: $CMAKE_BUILD_TYPE"
echo " Build Dir: $BUILD_DIR"
echo " Tests: $([ $BUILD_TESTS -eq 1 ] && echo 'enabled' || echo 'disabled')"
echo " Examples: $([ $BUILD_EXAMPLES -eq 1 ] && echo 'enabled' || echo 'disabled')"
echo " ZMQ Support: $([ $ENABLE_ZMQ -eq 1 ] && echo 'enabled' || echo 'disabled')"
echo " ASN.1 Encoding: $([ $ENABLE_ASN1 -eq 1 ] && echo 'enabled' || echo 'disabled')"
echo " AddressSan: $([ $ENABLE_ASAN -eq 1 ] && echo 'enabled' || echo 'disabled')"
echo " ThreadSan: $([ $ENABLE_TSAN -eq 1 ] && echo 'enabled' || echo 'disabled')"
echo " Fatal Errors: $([ $FATAL_ERRORS -eq 1 ] && echo 'enabled' || echo 'disabled')"
echo " Documentation: $([ $BUILD_DOCS -eq 1 ] && echo 'build requested' || echo 'not built')"
echo "======================================"
echo ""
}
# ============================================================================
# Main
# ============================================================================
main() {
# Parse arguments
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
print_help
exit 0
;;
-c|--clean)
CLEAN=1
shift
;;
-C|--clean-all)
CLEAN_ALL=1
shift
;;
-d|--build-dir)
BUILD_DIR="$2"
shift 2
;;
-j|--jobs)
JOBS="$2"
shift 2
;;
-v|--verbose)
VERBOSE=1
shift
;;
--ninja)
CMAKE_GENERATOR="Ninja"
shift
;;
-g|--debug)
CMAKE_BUILD_TYPE="Debug"
shift
;;
-r|--release)
CMAKE_BUILD_TYPE="Release"
shift
;;
--relwithdebinfo)
CMAKE_BUILD_TYPE="RelWithDebInfo"
shift
;;
--minsizerel)
CMAKE_BUILD_TYPE="MinSizeRel"
shift
;;
--enable-zmq)
ENABLE_ZMQ=1
shift
;;
--disable-zmq)
ENABLE_ZMQ=0
shift
;;
--enable-asn1)
ENABLE_ASN1=1
shift
;;
--disable-asn1)
ENABLE_ASN1=0
shift
;;
--enable-json)
ENABLE_JSON=1
shift
;;
--disable-json)
ENABLE_JSON=0
shift
;;
--enable-protobuf)
ENABLE_PROTOBUF=1
shift
;;
--all-encodings)
ENABLE_ASN1=1
ENABLE_JSON=1
ENABLE_PROTOBUF=1
shift
;;
--enable-sctp)
ENABLE_SCTP=1
shift
;;
--disable-sctp)
ENABLE_SCTP=0
shift
;;
--disable-protobuf)
ENABLE_PROTOBUF=0
shift
;;
--enable-swig)
ENABLE_SWIG=1
shift
;;
--disable-swig)
ENABLE_SWIG=0
shift
;;
--enable-tests)
BUILD_TESTS=1
shift
;;
--disable-tests)
BUILD_TESTS=0
shift
;;
--enable-examples)
BUILD_EXAMPLES=1
shift
;;
--disable-examples)
BUILD_EXAMPLES=0
shift
;;
--sanitize)
ENABLE_ASAN=1
ENABLE_UBSAN=1
shift
;;
--sanitize-address)
ENABLE_ASAN=1
shift
;;
--sanitize-thread)
ENABLE_TSAN=1
shift
;;
--sanitize-undefined)
ENABLE_UBSAN=1
shift
;;
--sanitize-memory)
ENABLE_MSAN=1
shift
;;
--docs)
BUILD_DOCS=1
shift
;;
--docs-strict)
BUILD_DOCS=1
BUILD_DOCS_STRICT=1
shift
;;
-I|--install-deps)
INSTALL_DEPS=1
shift
;;
--install)
INSTALL=1
shift
;;
--uninstall)
UNINSTALL=1
shift
;;
--prefix)
INSTALL_PREFIX="$2"
shift 2
;;
--deb)
CREATE_DEB=1
shift
;;
--deb-arch)
if [ -z "${2:-}" ]; then
echo_error "--deb-arch requires a value"
exit 1
fi
CREATE_DEB=1
DEB_ARCH="$2"
shift 2
;;
-t|--test)
RUN_TESTS=1
shift
;;
--arch-native)
ARCH_NATIVE=1
shift
;;
--fatal-errors)
FATAL_ERRORS=1
shift
;;
--cmake-opt)
CMAKE_OPTIONS="$CMAKE_OPTIONS $2"
shift 2
;;
*)
echo_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Validate sanitizer combinations
if [ $ENABLE_ASAN -eq 1 ] && [ $ENABLE_TSAN -eq 1 ]; then
echo_error "AddressSanitizer and ThreadSanitizer cannot be used together"
exit 1
fi
if [ $ENABLE_MSAN -eq 1 ] && [ $ENABLE_ASAN -eq 1 ]; then
echo_error "MemorySanitizer and AddressSanitizer cannot be used together"
exit 1
fi
if [ $INSTALL -eq 1 ] && [ $UNINSTALL -eq 1 ]; then
echo_error "--install and --uninstall cannot be used together"
exit 1
fi
if [ $UNINSTALL -eq 1 ]; then
uninstall_library
exit 0
fi
if [ -n "$DEB_ARCH" ]; then
case "$DEB_ARCH" in
amd64|arm64|all|x86_64|x64|aarch64)
;;
*)
echo_error "Invalid value for --deb-arch: $DEB_ARCH"
echo "Valid values: amd64, arm64, all"
exit 1
;;
esac
fi
# Install dependencies if requested
if [ $INSTALL_DEPS -eq 1 ]; then
install_dependencies
# When invoked with -I/--install-deps we only install dependencies and exit.
# This prevents the script from proceeding to configure/build twice when
# CI pipelines call it for dependency setup before running the actual build.
echo_info "Dependencies installed. Please try to build libe3."
exit 0
fi
# Checked after --install-deps, which is what installs it on a fresh machine
if ! check_command cmake; then
echo_error "CMake is required but not found. Install with: --install-deps"
exit 1
fi
# Clean if requested
if [ $CLEAN_ALL -eq 1 ]; then
clean_all
elif [ $CLEAN -eq 1 ]; then
clean_build
fi
# Print build summary
print_summary
# Configure and build
configure_cmake
build_library
# Run tests if requested
if [ $RUN_TESTS -eq 1 ] && [ $BUILD_TESTS -eq 1 ]; then
run_tests
fi
# Build documentation if requested
if [ $BUILD_DOCS -eq 1 ]; then
build_docs
fi
# Install if requested
if [ $INSTALL -eq 1 ]; then
install_library
fi
# Create .deb package(s) if requested
if [ $CREATE_DEB -eq 1 ]; then
create_deb_packages
fi
echo_success "libe3 build completed successfully!"
echo ""
echo "Build artifacts are in: $SCRIPT_DIR/$BUILD_DIR"
if [ $BUILD_TESTS -eq 1 ]; then
echo "Run tests with: cd $BUILD_DIR && ctest"
fi
if [ $BUILD_EXAMPLES -eq 1 ]; then
echo "Examples are in: $BUILD_DIR/examples/"
fi
}
main "$@"