Initial commit
This commit is contained in:
272
scripts/build.sh
Normal file
272
scripts/build.sh
Normal file
@@ -0,0 +1,272 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Azeron Linux Build Script
|
||||
# Builds the project and optionally installs it
|
||||
|
||||
set -e
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Default values
|
||||
BUILD_TYPE="Release"
|
||||
INSTALL_PREFIX="/usr/local"
|
||||
BUILD_DIR="build"
|
||||
INSTALL_UDEV="yes"
|
||||
RUN_TESTS="no"
|
||||
|
||||
# Print colored output
|
||||
print_info() {
|
||||
echo -e "${GREEN}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# Show usage
|
||||
usage() {
|
||||
cat << EOF
|
||||
Usage: $0 [options]
|
||||
|
||||
Options:
|
||||
-h, --help Show this help message
|
||||
-d, --debug Build in debug mode (default: Release)
|
||||
-p, --prefix PATH Installation prefix (default: /usr/local)
|
||||
-b, --build-dir DIR Build directory (default: build)
|
||||
-t, --test Run tests after building
|
||||
-n, --no-udev Skip udev rules installation
|
||||
-c, --clean Clean build directory before building
|
||||
-i, --install Install after building
|
||||
|
||||
Examples:
|
||||
$0 # Build only
|
||||
$0 -i # Build and install
|
||||
$0 -d -t # Debug build with tests
|
||||
$0 -c -i # Clean build and install
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
# Parse command line arguments
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
-d|--debug)
|
||||
BUILD_TYPE="Debug"
|
||||
shift
|
||||
;;
|
||||
-p|--prefix)
|
||||
INSTALL_PREFIX="$2"
|
||||
shift 2
|
||||
;;
|
||||
-b|--build-dir)
|
||||
BUILD_DIR="$2"
|
||||
shift 2
|
||||
;;
|
||||
-t|--test)
|
||||
RUN_TESTS="yes"
|
||||
shift
|
||||
;;
|
||||
-n|--no-udev)
|
||||
INSTALL_UDEV="no"
|
||||
shift
|
||||
;;
|
||||
-c|--clean)
|
||||
if [ -d "$BUILD_DIR" ]; then
|
||||
print_info "Cleaning build directory..."
|
||||
rm -rf "$BUILD_DIR"
|
||||
fi
|
||||
shift
|
||||
;;
|
||||
-i|--install)
|
||||
INSTALL="yes"
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
print_error "Unknown option: $1"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Check dependencies
|
||||
check_dependencies() {
|
||||
print_info "Checking dependencies..."
|
||||
|
||||
# Check for cmake
|
||||
if ! command -v cmake &> /dev/null; then
|
||||
print_error "cmake is required but not installed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check for C compiler
|
||||
if ! command -v gcc &> /dev/null && ! command -v clang &> /dev/null; then
|
||||
print_error "C compiler (gcc or clang) is required but not found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check for pkg-config
|
||||
if ! command -v pkg-config &> /dev/null; then
|
||||
print_error "pkg-config is required but not installed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check for libusb-1.0
|
||||
if ! pkg-config --exists libusb-1.0; then
|
||||
print_error "libusb-1.0 is required but not installed"
|
||||
print_info "Install it with:"
|
||||
print_info " Fedora: sudo dnf install libusb1-devel"
|
||||
print_info " Ubuntu: sudo apt-get install libusb-1.0-0-dev"
|
||||
print_info " Arch: sudo pacman -S libusb"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check for json-c
|
||||
if ! pkg-config --exists json-c; then
|
||||
print_error "json-c is required but not installed"
|
||||
print_info "Install it with:"
|
||||
print_info " Fedora: sudo dnf install json-c-devel"
|
||||
print_info " Ubuntu: sudo apt-get install libjson-c-dev"
|
||||
print_info " Arch: sudo pacman -S json-c"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
print_info "All dependencies found"
|
||||
}
|
||||
|
||||
# Build the project
|
||||
build_project() {
|
||||
print_info "Building project..."
|
||||
print_info "Build type: $BUILD_TYPE"
|
||||
print_info "Install prefix: $INSTALL_PREFIX"
|
||||
|
||||
# Create build directory
|
||||
mkdir -p "$BUILD_DIR"
|
||||
cd "$BUILD_DIR"
|
||||
|
||||
# Configure with cmake
|
||||
print_info "Configuring with cmake..."
|
||||
cmake .. -DCMAKE_BUILD_TYPE="$BUILD_TYPE" -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX"
|
||||
|
||||
# Build
|
||||
print_info "Building..."
|
||||
make -j$(nproc)
|
||||
|
||||
cd ..
|
||||
|
||||
print_info "Build completed successfully"
|
||||
}
|
||||
|
||||
# Run tests
|
||||
run_tests() {
|
||||
if [ "$RUN_TESTS" != "yes" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
print_info "Running tests..."
|
||||
|
||||
cd "$BUILD_DIR"
|
||||
|
||||
if [ -f "test_azeron" ]; then
|
||||
./test_azeron
|
||||
else
|
||||
print_warning "No test executable found"
|
||||
fi
|
||||
|
||||
cd ..
|
||||
}
|
||||
|
||||
# Install the project
|
||||
install_project() {
|
||||
if [ "$INSTALL" != "yes" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
print_info "Installing project..."
|
||||
|
||||
cd "$BUILD_DIR"
|
||||
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
print_warning "Installing without root privileges"
|
||||
print_warning "You may need to run: sudo $0 --install"
|
||||
fi
|
||||
|
||||
make install
|
||||
|
||||
# Install udev rules if requested
|
||||
if [ "$INSTALL_UDEV" = "yes" ]; then
|
||||
if [ "$EUID" -eq 0 ]; then
|
||||
print_info "Installing udev rules..."
|
||||
cp scripts/udev-rules/99-azeron.rules /etc/udev/rules.d/
|
||||
udevadm control --reload-rules
|
||||
udevadm trigger
|
||||
print_info "Udev rules installed. Reconnect your Azeron device."
|
||||
else
|
||||
print_warning "Skipping udev rules installation (requires root)"
|
||||
print_info "To install udev rules manually, run:"
|
||||
print_info " sudo cp scripts/udev-rules/99-azeron.rules /etc/udev/rules.d/"
|
||||
print_info " sudo udevadm control --reload-rules"
|
||||
print_info " sudo udevadm trigger"
|
||||
fi
|
||||
fi
|
||||
|
||||
cd ..
|
||||
|
||||
print_info "Installation completed"
|
||||
}
|
||||
|
||||
# Print build summary
|
||||
print_summary() {
|
||||
print_info "Build Summary:"
|
||||
echo " Build type: $BUILD_TYPE"
|
||||
echo " Build directory: $BUILD_DIR"
|
||||
echo " Install prefix: $INSTALL_PREFIX"
|
||||
echo " Udev rules: $INSTALL_UDEV"
|
||||
echo " Run tests: $RUN_TESTS"
|
||||
|
||||
if [ -d "$BUILD_DIR" ]; then
|
||||
echo ""
|
||||
print_info "Binaries built:"
|
||||
if [ -f "$BUILD_DIR/azeron-cli" ]; then
|
||||
echo " - azeron-cli (command-line tool)"
|
||||
fi
|
||||
if [ -f "$BUILD_DIR/libazeron/libazeron.so" ]; then
|
||||
echo " - libazeron.so (library)"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
print_info "Azeron Linux Build Script"
|
||||
echo ""
|
||||
|
||||
check_dependencies
|
||||
build_project
|
||||
run_tests
|
||||
install_project
|
||||
|
||||
echo ""
|
||||
print_summary
|
||||
|
||||
if [ "$INSTALL" != "yes" ]; then
|
||||
echo ""
|
||||
print_info "To install the software, run: $0 --install"
|
||||
print_info "Or cd $BUILD_DIR && sudo make install"
|
||||
fi
|
||||
}
|
||||
|
||||
# Run main function
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user