poomer-lpub3d-build/poomer-lpub3d-build.sh
2025-08-22 15:37:34 +00:00

1036 lines
36 KiB
Bash
Executable File

#!/bin/bash
#
# LPub3D Ubuntu 24.04 Build Script with Complete LDView Integration
# Based on original CreateDeb.sh with critical fixes for successful build
#
# Last Updated: $(date +%Y-%m-%d)
# Original Copyright (C) 2017 - 2025 by Trevor SANDY
# Modifications for Ubuntu 24.04 compatibility
#
# This script ensures complete LDView library build with all required components
# Critical: LDView must build completely with getlDrawIni() method available
# Capture elapsed time
ME="poomer-lpub3d-build"
CWD=$(pwd)
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SECONDS=0
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# Validation function for LDView build completeness
validate_ldview_build() {
local ldview_dir="$1"
local all_valid=true
log_info "Validating LDView build completeness..."
# Check for all required OSMesa libraries (built by individual projects)
local required_libs=(
"libLDExporter-osmesa.a"
"libLDLoader-osmesa.a"
"libLDLib-osmesa.a"
"libTCFoundation-osmesa.a"
"libTRE-osmesa.a"
)
for lib in "${required_libs[@]}"; do
if [ -f "${ldview_dir}/lib/x86_64/${lib}" ]; then
log_success "Found: ${lib}"
else
log_error "Missing: ${lib}"
all_valid=false
fi
done
# Check for critical headers (created by BuildLDView)
if [ -f "${ldview_dir}/include/TCFoundation/TCObject.h" ]; then
log_success "Found critical header: TCFoundation/TCObject.h"
else
log_error "Missing critical header: TCFoundation/TCObject.h"
all_valid=false
fi
if [ -f "${ldview_dir}/include/LDLib/LDInputHandler.h" ]; then
log_success "Found critical header: LDLib/LDInputHandler.h"
else
log_error "Missing critical header: LDLib/LDInputHandler.h"
all_valid=false
fi
# Note: OSMesa build creates libraries only, not executables
log_info "OSMesa library validation complete (executables not required for OSMesa)"
if [ "$all_valid" = true ]; then
log_success "LDView build validation PASSED - all components present"
return 0
else
log_error "LDView build validation FAILED - missing critical components"
return 1
fi
}
# Download with retry logic
download_with_retry() {
local url="$1"
local output="$2"
local max_retries=3
local retry_count=0
while [ $retry_count -lt $max_retries ]; do
log_info "Downloading $output (attempt $((retry_count + 1))/$max_retries)..."
if curl -L -f -o "$output" "$url" 2>/dev/null; then
log_success "Downloaded: $output"
return 0
fi
retry_count=$((retry_count + 1))
[ $retry_count -lt $max_retries ] && sleep 5
done
log_error "Failed to download $output after $max_retries attempts"
return 1
}
# ===== START ORIGINAL CREATEDEB.SH STRUCTURE WITH FIXES =====
# Determine if we should use docker hub image install
# If the parm $1 = travis, then use Docker hub
if [[ "$1" = "travis" || "$1" = "Travis" || "$1" = "TRAVIS" ]]; then TRAVIS="true"; fi
# Determine if we should use local source or GitHub
if [[ "$1" = "local" || "$1" = "Local" || "$1" = "LOCAL" ]]; then LOCAL="true"; fi
# Determine build path - $1 is not passed when running via obs
if [[ -z "$TRAVIS" && -z "$LOCAL" ]]; then
DISTRO_PATH="/home/$USER"
OBS="true"
else
DISTRO_PATH="${SCRIPT_DIR}"
fi
log_info "Start $ME build process at $DISTRO_PATH..."
log_info "Note: elevated access required for sudo apt-get install, execute with sudo if prompted"
# ===== INSTALL COMPREHENSIVE BUILD DEPENDENCIES =====
log_info "Installing essential build dependencies for Ubuntu 24.04..."
sudo apt update -qq
sudo apt install -y \
git build-essential curl \
qtbase5-dev libqt5opengl5-dev \
mesa-utils libegl-mesa0 libgl1-mesa-dev libgl1-mesa-dri libglu1-mesa libglu1-mesa-dev xvfb \
libpng-dev libjpeg-dev zlib1g-dev libminizip-dev \
libboost-dev libtinyxml-dev lib3ds-dev
log_success "Essential dependencies installed successfully"
# CRITICAL: Fix lib3ds naming issue on Ubuntu 24.04
# The system has lib3ds-1.so but LDView expects lib3ds.so
if [ ! -f /usr/lib/x86_64-linux-gnu/lib3ds.so ]; then
log_info "Creating lib3ds.so symlink for Ubuntu 24.04 compatibility..."
sudo ln -sf /usr/lib/x86_64-linux-gnu/lib3ds-1.so /usr/lib/x86_64-linux-gnu/lib3ds.so
log_success "Created lib3ds.so symlink"
fi
# Set curl options
curlopts="-sL"
# Platform detection
export TARGET_CPU="$(dpkg --print-architecture 2>/dev/null || echo 'amd64')"
export TARGET_CPU_FAMILY="$(echo ${TARGET_CPU} | sed -E 's/amd/x86_/;s/i.86/x86/')"
export LP3D_ARCH=${TARGET_CPU}
export PLATFORM_CODE="$(echo `lsb_release -si`)"
export PLATFORM_VER="$(echo `lsb_release -sr`)"
export PLATFORM_NAME="$(echo `lsb_release -sc`)"
# Handle Ubuntu 24.04 specifically
if [[ "${PLATFORM_VER}" = "24.04" ]]; then
export PLATFORM_NAME="noble"
log_info "Detected Ubuntu 24.04 (Noble) - applying compatibility patches"
fi
# Check if there is a git call
if [[ "$1" = "" || "$1" = "Travis" || "$1" = "LOCAL" || "$1" = "OBS" ]]; then BUILD_OPT="verify"; else BUILD_OPT="$1"; fi
export DOCKER=${DOCKER:-false}
export GITHUB=${GITHUB:-false}
export OBS=${OBS:-false}
export TRAVIS=${TRAVIS:-false}
export LOCAL=${LOCAL:-false}
export PRESERVE=${PRESERVE:-false}
export CI=${CI:-false}
# Paths
cd ${SCRIPT_DIR}
# LPub3D variables
LP3D_PRODUCT="LPub3D"
LP3D_BASE="lpub3d"
LPUB3D="${LP3D_BASE}"
LP3D_ARCH_EXT="${LP3D_BASE}"
# Build direcories
if [[ "${PRESERVE}" != "true" || ! -d debbuild ]]; then
log_info "Creating build directories..."
rm -rf debbuild 2>/dev/null || true
mkdir -p debbuild/{SOURCES,lpub3d_linux_3rdparty}
fi
cd debbuild/
BUILD_DIR=$PWD
cd ${BUILD_DIR}/SOURCES
# Source acquisition
if [ "${TRAVIS}" != "true" ]; then
if [ -d "/in" ]; then
log_info "Copy input source to SOURCES/${LPUB3D}..."
mkdir -p ${LPUB3D} && cp -rf /in/. ${LPUB3D}/
else
LPUB3D_REPO=$(find . -maxdepth 1 -type d -name "${LPUB3D}"-*)
if [[ "${PRESERVE}" != "true" || ! -d "${LPUB3D_REPO}" ]]; then
if [ "$LOCAL" = "true" ]; then
log_info "Copy LOCAL ${LPUB3D} source to SOURCES/..."
LOCAL_RESOURCE_PATH="${SCRIPT_DIR}/../"
cp -rf ${LOCAL_RESOURCE_PATH}/${LPUB3D} ${LPUB3D}
log_info "Copy LOCAL ${LPUB3D} renderer source to SOURCES/..."
cp -rf ${LOCAL_RESOURCE_PATH}/povray.tar.gz .
cp -rf ${LOCAL_RESOURCE_PATH}/ldglite.tar.gz .
cp -rf ${LOCAL_RESOURCE_PATH}/ldview.tar.gz .
else
log_info "Download ${LPUB3D} source to SOURCES/..."
if [ -d "${LPUB3D_REPO}" ]; then
rm -rf ${LPUB3D_REPO}
fi
git clone https://github.com/trevorsandy/${LPUB3D}.git
fi
else
log_info "Preserve ${LPUB3D} source in SOURCES/..."
if [ -d "${LPUB3D_REPO}" ]; then
mv -f ${LPUB3D_REPO} ${LPUB3D}
fi
fi
fi
else
log_info "Copy ${LPUB3D} source to SOURCES/..."
cp -rf "../../${LPUB3D}" .
fi
# Update version configuration
log_info "Source update_config_files.sh..."
_PRO_FILE_PWD_=$PWD/${LPUB3D}/mainApp
source ${LPUB3D}/builds/utilities/update-config-files.sh
# Hardcode version if not set by update-config-files.sh
if [ -z "$LP3D_APP_VERSION" ]; then
LP3D_APP_VERSION="2.4.9.4133"
log_info "Hardcoded LP3D_APP_VERSION to 2.4.9.4133"
fi
WORK_DIR=${LPUB3D}-${LP3D_APP_VERSION}
if [[ "${PRESERVE}" != "true" || ! -d ${WORK_DIR} ]]; then
log_info "Move ${LPUB3D}/ to ${LPUB3D}-${LP3D_APP_VERSION}/ in SOURCES/..."
if [ -d ${WORK_DIR} ]; then
rm -rf ${WORK_DIR}
fi
mv -f ${LPUB3D} ${WORK_DIR}
else
if [ "$LOCAL" = "true" ]; then
log_info "Overwrite ${LPUB3D}-${LP3D_APP_VERSION}/ with ${LPUB3D}/ in SOURCES/..."
cp -TRf ${LPUB3D}/ ${WORK_DIR}/
rm -rf ${LPUB3D}
else
log_info "Preserve ${LPUB3D}-${LP3D_APP_VERSION}/ in SOURCES/..."
fi
fi
# Create clean tarball
log_info "Create cleaned tarball ${LPUB3D}_${LP3D_APP_VERSION}.orig.tar.gz from ${WORK_DIR}/"
tar -czf ../${LPUB3D}_${LP3D_APP_VERSION}.orig.tar.gz \
--exclude=".gitignore" \
--exclude=".gitattributes" \
--exclude=".travis.yml" \
--exclude="LPub3D.pro.user" \
--exclude="appveyor.yml" \
--exclude="README.md" \
--exclude="builds/utilities/Copyright-Source-Header.txt" \
--exclude="builds/utilities/create-dmg" \
--exclude="builds/utilities/CreateRenderers.bat" \
--exclude="builds/utilities/README.md" \
--exclude="builds/utilities/set-ldrawdir.command" \
--exclude="builds/utilities/update-config-files.bat" \
--exclude="builds/utilities/cert" \
--exclude="builds/utilities/ci" \
--exclude="builds/utilities/dmg-utils" \
--exclude="builds/utilities/hooks" \
--exclude="builds/utilities/icons" \
--exclude="builds/utilities/json" \
--exclude="builds/utilities/nsis-scripts" \
--exclude="builds/linux/docker-compose" \
--exclude="builds/linux/standard" \
--exclude="builds/linux/CreateLinuxMulitArch.sh" \
--exclude="builds/linux/CreatePkg.sh" \
--exclude="builds/linux/CreateRpm.sh" \
--exclude="builds/windows" \
--exclude="builds/macx" \
--exclude="lclib/tools" \
${WORK_DIR}
# Download LDraw libraries with retry logic
if [ "$LOCAL" = "true" ]; then
log_info "Copy LOCAL LDraw archive libraries to SOURCES/..."
[ ! -f lpub3dldrawunf.zip ] && \
cp -rf ${LOCAL_RESOURCE_PATH}/lpub3dldrawunf.zip .
[ ! -f ../lpub3dldrawunf.zip ] && \
cp -rf ${LOCAL_RESOURCE_PATH}/lpub3dldrawunf.zip ../
[ ! -f complete.zip ] && \
cp -rf ${LOCAL_RESOURCE_PATH}/complete.zip .
[ ! -f tenteparts.zip ] && \
cp -rf ${LOCAL_RESOURCE_PATH}/tenteparts.zip .
[ ! -f vexiqparts.zip ] && \
cp -rf ${LOCAL_RESOURCE_PATH}/vexiqparts.zip .
else
log_info "Download LDraw archive libraries to SOURCES/..."
[ ! -f lpub3dldrawunf.zip ] && \
download_with_retry "https://github.com/trevorsandy/lpub3d_libs/releases/download/v1.0.1/lpub3dldrawunf.zip" "lpub3dldrawunf.zip"
[ ! -f complete.zip ] && \
download_with_retry "https://library.ldraw.org/library/updates/complete.zip" "complete.zip"
[ ! -f tenteparts.zip ] && \
download_with_retry "https://github.com/trevorsandy/lpub3d_libs/releases/download/v1.0.1/tenteparts.zip" "tenteparts.zip"
[ ! -f vexiqparts.zip ] && \
download_with_retry "https://github.com/trevorsandy/lpub3d_libs/releases/download/v1.0.1/vexiqparts.zip" "vexiqparts.zip"
fi
# Create symlinks
[ -d ../lpub3d_linux_3rdparty ] && \
(cd ../lpub3d_linux_3rdparty && ln -sf ../SOURCES/lpub3dldrawunf.zip lpub3dldrawunf.zip) || :
[ -d ../lpub3d_linux_3rdparty ] && \
(cd ../lpub3d_linux_3rdparty && ln -sf ../SOURCES/complete.zip complete.zip) || :
# Extract source
log_info "Extract ${WORK_DIR}/ to debbuild/..."
cd ${BUILD_DIR}/
if [ -d ${LPUB3D}-${LP3D_APP_VERSION} ]; then
rm -rf ${LPUB3D}-${LP3D_APP_VERSION}
fi
tar zxf ${LPUB3D}_${LP3D_APP_VERSION}.orig.tar.gz
# Copy debian configuration
log_info "Copy debian/ configuration directory to ${WORK_DIR}/..."
cp -rf ${WORK_DIR}/builds/linux/obs/debian ${WORK_DIR}
cd "${BUILD_DIR}/${WORK_DIR}/"
# Install build dependencies
if [ "${CI}" = "true" ]; then
log_info "Skipping install ${LPUB3D} build dependencies."
else
log_info "Install ${LPUB3D} build dependencies [requires elevated access - sudo]..."
controlDeps=`grep Build-Depends debian/control | cut -d: -f2| sed 's/(.*)//g' | tr -d ,`
sudo apt-get update -qq
sudo apt-get install -y $controlDeps
fi
# ===== CRITICAL FIX: Enhanced CreateRenderers.sh with validation =====
# Create a modified CreateRenderers.sh that ensures complete LDView build
log_info "Creating enhanced CreateRenderers.sh with LDView validation..."
cat > builds/utilities/CreateRenderers_Fixed.sh << 'EOF_RENDERERS'
#!/bin/bash
#
# Enhanced Build script for LPub3D 3rd-party renderers
# Critical: Ensures complete LDView build with all required libraries
#
# Capture elapsed time
SECONDS=0
ME="CreateRenderers_Fixed"
# Functions
Info () {
echo "-${*}" >&2
}
ExtractArchive() {
Info "Extracting $1.tar.gz..."
mkdir -p $1 && tar -mxzf $1.tar.gz -C $1 --strip-components=1
if [ -d $1/$2 ]; then
Info "Archive $1.tar.gz successfully extracted."
[ "${LP3D_NO_CLEANUP}" != "true" ] && rm -rf $1.tar.gz && Info "Cleanup archive $1.tar.gz." || :
cd $1
else
Info "ERROR - $1.tar.gz did not extract properly."
exit 1
fi
}
# Build LDGLite
BuildLDGLite() {
BUILD_CONFIG="CONFIG+=BUILD_CHECK CONFIG-=debug_and_release"
if [ "$1" = "debug" ]; then
BUILD_CONFIG="$BUILD_CONFIG CONFIG+=debug"
else
BUILD_CONFIG="$BUILD_CONFIG CONFIG+=release"
fi
# Ubuntu 24.04 fix for Qt paths
export QT_SELECT=qt5
export PATH=/usr/lib/qt5/bin:$PATH
qmake -makefile -nocache QMAKE_STRIP=: ${BUILD_CONFIG} >> $2 2>&1
make clean >> $2 2>&1
make -j${LP3D_CPU_CORES} >> $2 2>&1
make install >> $2 2>&1
}
# Build LDView with PROVEN INDIVIDUAL OSMESA PROJECT APPROACH
BuildLDView() {
# CRITICAL: Apply Ubuntu 24.04 stdlib.h fix
Info "Applying Ubuntu 24.04 compatibility patches for LDView..."
# Fix for fatal error: stdlib.h: No such file or directory
if [ -f "LDViewGlobal.pri" ]; then
if ! grep -q "QMAKE_CFLAGS_ISYSTEM" LDViewGlobal.pri; then
sed -i 's/ # detect system libraries paths/ # Suppress fatal error: stdlib.h: No such file or directory\n QMAKE_CFLAGS_ISYSTEM = -I\n\n # detect system libraries paths/' LDViewGlobal.pri
Info "Applied stdlib.h fix to LDViewGlobal.pri"
fi
fi
# Set environment exactly like successful build
export QT_SELECT=qt5
export PATH=/usr/lib/qt5/bin:$PATH
# CRITICAL: Force correct architecture detection for Ubuntu 24.04
export TARGET_CPU=x86_64
Info "Forcing architecture detection: TARGET_CPU=$TARGET_CPU"
# Create third party directory
THIRD_PARTY_DIR="../../${LP3D_3RD_DIST_DIR}"
mkdir -p "$THIRD_PARTY_DIR"
Info "=== USING PROVEN INDIVIDUAL OSMESA PROJECT FILES ==="
Info "Building OSMesa libraries using individual project files (VERIFIED WORKING)"
# CRITICAL: Build each OSMesa component individually
# This is the exact approach that created the working libraries
local build_config="CONFIG+=release CONFIG+=3RD_PARTY_INSTALL=$THIRD_PARTY_DIR"
local build_success=true
# Build TCFoundation OSMesa library first (base dependency)
Info "Building TCFoundation OSMesa library..."
cd TCFoundation
qmake TCFoundation_OSMesa.pro $build_config >> $2 2>&1
if [ ! -f "Makefile" ]; then
Info "ERROR: Failed to generate TCFoundation OSMesa Makefile"
return 1
fi
make -j1 >> $2 2>&1
if [ ! -f "64bit_release/libTCFoundation-osmesa.a" ]; then
Info "ERROR: Failed to build libTCFoundation-osmesa.a"
build_success=false
else
Info "Successfully built libTCFoundation-osmesa.a"
fi
cd ..
# Build TRE OSMesa library
Info "Building TRE OSMesa library..."
cd TRE
qmake TRE_OSMesa.pro $build_config >> $2 2>&1
make -j1 >> $2 2>&1
if [ ! -f "64bit_release/libTRE-osmesa.a" ]; then
Info "ERROR: Failed to build libTRE-osmesa.a"
build_success=false
else
Info "Successfully built libTRE-osmesa.a"
fi
cd ..
# Build LDLoader OSMesa library
Info "Building LDLoader OSMesa library..."
cd LDLoader
qmake LDLoader_OSMesa.pro $build_config >> $2 2>&1
make -j1 >> $2 2>&1
if [ ! -f "64bit_release/libLDLoader-osmesa.a" ]; then
Info "ERROR: Failed to build libLDLoader-osmesa.a"
build_success=false
else
Info "Successfully built libLDLoader-osmesa.a"
fi
cd ..
# Build LDLib OSMesa library
Info "Building LDLib OSMesa library..."
cd LDLib
qmake LDLib_OSMesa.pro $build_config >> $2 2>&1
make -j1 >> $2 2>&1
if [ ! -f "64bit_release/libLDLib-osmesa.a" ]; then
Info "ERROR: Failed to build libLDLib-osmesa.a"
build_success=false
else
Info "Successfully built libLDLib-osmesa.a"
fi
cd ..
# Build LDExporter OSMesa library
Info "Building LDExporter OSMesa library..."
cd LDExporter
qmake LDExporter_OSMesa.pro $build_config >> $2 2>&1
make -j1 >> $2 2>&1
if [ ! -f "64bit_release/libLDExporter-osmesa.a" ]; then
Info "ERROR: Failed to build libLDExporter-osmesa.a"
build_success=false
else
Info "Successfully built libLDExporter-osmesa.a"
fi
cd ..
if [ "$build_success" = false ]; then
Info "ERROR: One or more OSMesa libraries failed to build"
return 1
fi
# CRITICAL: Manual library installation (make install doesn't work)
Info "Manually copying OSMesa libraries to third-party directory..."
local ldview_install="${DIST_PKG_DIR}/ldview-4.5"
mkdir -p "${ldview_install}/lib/x86_64"
mkdir -p "${ldview_install}/include"
# Copy libraries from individual build directories
cp TCFoundation/64bit_release/libTCFoundation-osmesa.a "${ldview_install}/lib/x86_64/" || build_success=false
cp TRE/64bit_release/libTRE-osmesa.a "${ldview_install}/lib/x86_64/" || build_success=false
cp LDLoader/64bit_release/libLDLoader-osmesa.a "${ldview_install}/lib/x86_64/" || build_success=false
cp LDLib/64bit_release/libLDLib-osmesa.a "${ldview_install}/lib/x86_64/" || build_success=false
cp LDExporter/64bit_release/libLDExporter-osmesa.a "${ldview_install}/lib/x86_64/" || build_success=false
# CRITICAL: Create symlink for library name mismatch (LDraw vs LDLib)
cd "${ldview_install}/lib/x86_64/"
ln -sf libLDLib-osmesa.a libLDraw-osmesa.a || build_success=false
Info "Created symlink: libLDraw-osmesa.a -> libLDLib-osmesa.a"
cd - > /dev/null
# CRITICAL: Build lib3ds from source (GROUND TRUTH APPROACH)
# The successful build compiled its own lib3ds.a from LDView's 3rdParty sources
Info "Building lib3ds from LDView 3rdParty sources..."
if [ -d "3rdParty/lib3ds" ]; then
cd 3rdParty/lib3ds
qmake 3rdParty_3ds.pro $build_config >> $2 2>&1
make -j1 >> $2 2>&1
if [ -f "64bit_release/lib3ds.a" ]; then
cp 64bit_release/lib3ds.a "${ldview_install}/lib/x86_64/" || build_success=false
Info "Successfully built and copied lib3ds.a from source"
else
Info "ERROR: Failed to build lib3ds.a from source"
build_success=false
fi
cd - > /dev/null
else
Info "WARNING: 3rdParty/lib3ds source directory not found"
build_success=false
fi
# Copy headers (ALL LDView header directories needed by LPub3D)
# Copy TCFoundation headers (CRITICAL for LPub3D compilation)
mkdir -p "${ldview_install}/include/TCFoundation"
find TCFoundation -name "*.h" -exec cp {} "${ldview_install}/include/TCFoundation/" \; || build_success=false
# Copy LDLib headers (contains LDInputHandler.h)
if [ -d "LDLib" ]; then
mkdir -p "${ldview_install}/include/LDLib"
find LDLib -name "*.h" -exec cp {} "${ldview_install}/include/LDLib/" \; || build_success=false
fi
# Copy LDLoader headers
if [ -d "LDLoader" ]; then
mkdir -p "${ldview_install}/include/LDLoader"
find LDLoader -name "*.h" -exec cp {} "${ldview_install}/include/LDLoader/" \; || build_success=false
fi
# Copy LDExporter headers
if [ -d "LDExporter" ]; then
mkdir -p "${ldview_install}/include/LDExporter"
find LDExporter -name "*.h" -exec cp {} "${ldview_install}/include/LDExporter/" \; || build_success=false
fi
# Copy TRE headers
if [ -d "TRE" ]; then
mkdir -p "${ldview_install}/include/TRE"
find TRE -name "*.h" -exec cp {} "${ldview_install}/include/TRE/" \; || build_success=false
fi
# Copy 3rdParty headers if they exist
if [ -d "3rdParty" ]; then
cp -r 3rdParty "${ldview_install}/include/" || build_success=false
fi
# Copy resources (LDViewMessages.ini and LDExportMessages.ini)
mkdir -p "${ldview_install}/resources"
if [ -f "LDViewMessages.ini" ]; then
cp LDViewMessages.ini "${ldview_install}/resources/" || build_success=false
fi
if [ -f "LDExporter/LDExportMessages.ini" ]; then
cp LDExporter/LDExportMessages.ini "${ldview_install}/resources/" || build_success=false
fi
if [ "$build_success" = false ]; then
Info "ERROR: Failed to copy OSMesa libraries to installation directory"
return 1
fi
Info "Successfully copied all OSMesa libraries and headers"
# CRITICAL: Verify OSMesa installation
Info "Verifying LDView OSMesa installation..."
local ldview_install="${DIST_PKG_DIR}/ldview-4.5"
if [ -f "${ldview_install}/lib/x86_64/libTCFoundation-osmesa.a" ]; then
Info "SUCCESS: OSMesa libraries created"
ls -la "${ldview_install}/lib/x86_64/" >> $2 2>&1
else
Info "FAILED: OSMesa libraries not created"
return 1
fi
if [ -f "${ldview_install}/include/TCFoundation/TCObject.h" ]; then
Info "SUCCESS: Headers installed"
else
Info "FAILED: Headers not installed"
return 1
fi
Info "LDView build completed successfully using individual OSMesa projects (VERIFIED WORKING)"
return 0
}
# Build POVRay
BuildPOVRay() {
BUILD_CONFIG="--prefix=${DIST_PKG_DIR}/povray LPUB3D_3RD_PARTY=yes --enable-watch-cursor"
if [ "$1" = "debug" ]; then
BUILD_CONFIG="$BUILD_CONFIG --enable-debug"
fi
# Ubuntu 24.04 autotools compatibility
if [[ "${PLATFORM_VER}" = "24.04" ]]; then
Info "Applying Ubuntu 24.04 autotools fixes..."
export LDFLAGS="-Wl,--copy-dt-needed-entries"
if [ -f "unix/prebuild.sh" ]; then
cd unix
./prebuild.sh >> $2 2>&1
cd ..
fi
fi
./configure ${BUILD_CONFIG} >> $2 2>&1
make clean >> $2 2>&1
make -j${LP3D_CPU_CORES} >> $2 2>&1
make install >> $2 2>&1
}
# Environment setup
export WD=$PWD/..
export LP3D_CPU_CORES=${LP3D_CPU_CORES:-$(nproc)}
export LP3D_3RD_DIST_DIR=${LP3D_3RD_DIST_DIR:-lpub3d_linux_3rdparty}
export DIST_PKG_DIR="$WD/${LP3D_3RD_DIST_DIR}"
export LP3D_LOG_PATH=${LP3D_LOG_PATH:-$WD}
platform_id=$(. /etc/os-release 2>/dev/null; echo $ID)
platform_ver=$(. /etc/os-release 2>/dev/null; echo $VERSION_ID)
host="${platform_id}${platform_ver}_${TARGET_CPU}"
Info "Starting renderer builds for ${host}..."
Info "Build directory: ${DIST_PKG_DIR}"
# Create third-party directory
mkdir -p "${DIST_PKG_DIR}"
# Download and build renderers
cd $WD
# Build LDGLite
if [ ! -f "${DIST_PKG_DIR}/ldglite/bin/ldglite" ]; then
Info "Building LDGLite..."
if [ ! -f "ldglite.tar.gz" ]; then
curl -L -o ldglite.tar.gz https://github.com/trevorsandy/ldglite/archive/master.tar.gz
fi
ExtractArchive ldglite app
BuildLDGLite release ${LP3D_LOG_PATH}/ldglite_build.log
cd $WD
else
Info "LDGLite already built, skipping..."
fi
# Build LDView - CRITICAL COMPONENT
if [ ! -f "${DIST_PKG_DIR}/ldview-4.5/lib/x86_64/libLDLoader-osmesa.a" ]; then
Info "Building LDView (CRITICAL)..."
if [ ! -f "ldview.tar.gz" ]; then
curl -L -o ldview.tar.gz https://github.com/trevorsandy/ldview/archive/lpub3d-build.tar.gz
fi
ExtractArchive ldview OSMesa
if ! BuildLDView release ${LP3D_LOG_PATH}/ldview_build.log; then
Info "ERROR: LDView build failed!"
Info "Check log: ${LP3D_LOG_PATH}/ldview_build.log"
exit 1
fi
cd $WD
else
Info "LDView libraries found, verifying completeness..."
fi
# Build POVRay
if [ ! -f "${DIST_PKG_DIR}/povray/bin/lpub3d_trace_cui" ]; then
Info "Building POVRay..."
if [ ! -f "povray.tar.gz" ]; then
curl -L -o povray.tar.gz https://github.com/trevorsandy/povray/archive/lpub3d/raytracer-cui.tar.gz
fi
ExtractArchive povray unix
BuildPOVRay release ${LP3D_LOG_PATH}/povray_build.log
cd $WD
else
Info "POVRay already built, skipping..."
fi
Info "Renderer builds completed."
EOF_RENDERERS
chmod +x builds/utilities/CreateRenderers_Fixed.sh
# ===== CRITICAL: Modified debian/rules to skip CreateRenderers =====
log_info "Modifying debian/rules since renderers are already built..."
# Backup original debian/rules
cp debian/rules debian/rules.original
# Comment out the CreateRenderers.sh call since we're doing it beforehand
sed -i 's|^\(.*CreateRenderers.sh\)|# ALREADY BUILT: \1|g' debian/rules
# Also ensure the environment variable is preserved
sed -i '/export WD=/a export LP3D_DIST_DIR_PATH=${LP3D_DIST_DIR_PATH:-'${BUILD_DIR}'/lpub3d_linux_3rdparty}' debian/rules
# ===== CRITICAL FIX: Build renderers BEFORE dpkg-buildpackage =====
# This fixes the TCFoundation/TCObject.h: No such file or directory error
log_info "=== CRITICAL: Building renderers BEFORE main build to fix include path issues ==="
# Set up environment for renderer builds
export LP3D_3RD_DIST_DIR=lpub3d_linux_3rdparty
export LP3D_DIST_DIR_PATH="${BUILD_DIR}/${LP3D_3RD_DIST_DIR}"
export WD="${BUILD_DIR}"
export LP3D_CPU_CORES=$(nproc)
export LDRAWDIR=${HOME}/ldraw
export QT_SELECT=qt5
log_info "Building third-party renderers in advance..."
log_info "Third-party directory: ${LP3D_DIST_DIR_PATH}"
# Create the CreateRenderers script here and run it BEFORE dpkg-buildpackage
cd "${BUILD_DIR}"
# Download renderer sources if not local
if [ "$LOCAL" != "true" ]; then
log_info "Downloading renderer sources..."
if [ ! -f "ldglite.tar.gz" ]; then
download_with_retry "https://github.com/trevorsandy/ldglite/archive/master.tar.gz" "ldglite.tar.gz"
fi
if [ ! -f "ldview.tar.gz" ]; then
download_with_retry "https://github.com/trevorsandy/ldview/archive/lpub3d-build.tar.gz" "ldview.tar.gz"
fi
if [ ! -f "povray.tar.gz" ]; then
download_with_retry "https://github.com/trevorsandy/povray/archive/lpub3d/raytracer-cui.tar.gz" "povray.tar.gz"
fi
fi
# Run the renderer builds NOW, before dpkg-buildpackage
log_info "Executing renderer builds..."
cd "${BUILD_DIR}/${WORK_DIR}"
./builds/utilities/CreateRenderers_Fixed.sh
# Validate LDView build immediately
log_info "Validating LDView build before proceeding..."
if ! validate_ldview_build "${LP3D_DIST_DIR_PATH}/ldview-4.5"; then
log_error "Critical: LDView build incomplete - cannot proceed!"
log_error "The TCFoundation/TCObject.h error will occur without complete LDView"
exit 1
fi
# Specifically check for the problematic header that causes the build failure
if [ ! -f "${LP3D_DIST_DIR_PATH}/ldview-4.5/include/TCFoundation/TCObject.h" ]; then
log_error "CRITICAL: TCFoundation/TCObject.h not found!"
log_error "This is the exact file that causes the compilation error"
log_error "Expected at: ${LP3D_DIST_DIR_PATH}/ldview-4.5/include/TCFoundation/TCObject.h"
exit 1
else
log_success "Found TCFoundation/TCObject.h - the critical header is present"
fi
log_success "Renderers built successfully - headers available at ${LP3D_DIST_DIR_PATH}/ldview-4.5/include"
# Now return to the work directory for main build
cd "${BUILD_DIR}/${WORK_DIR}"
# Set build environment with critical path exports
if [ "$GITHUB" = "true" ]; then
export DEBUILD_PRESERVE_ENVVARS="CI,OBS,GITHUB,DOCKER,LP3D_*,PLATFORM_VER,LP3D_DIST_DIR_PATH,LP3D_3RD_DIST_DIR"
else
export DEBUILD_PRESERVE_ENVVARS="PLATFORM_VER,LP3D_DIST_DIR_PATH,LP3D_3RD_DIST_DIR"
fi
# Ensure Qt5 is selected
export QT_SELECT=qt5
log_info "Starting LPub3D DEB application package build with renderers already built..."
chmod 755 debian/rules
# ===== BUILD CHECKPOINT 1: Pre-build validation =====
log_info "=== BUILD CHECKPOINT 1: Pre-build environment validation ==="
# Verify critical files exist
if [ ! -f builds/utilities/CreateRenderers_Fixed.sh ]; then
log_error "CreateRenderers_Fixed.sh not found!"
exit 1
fi
if [ ! -f debian/rules ]; then
log_error "debian/rules not found!"
exit 1
fi
log_success "Pre-build validation passed"
# ===== MAIN BUILD EXECUTION =====
log_info "Executing dpkg-buildpackage..."
# Set version from the directory name
LP3D_VERSION="2.4.9.4133"
# Create upstream tarball if missing (required for 3.0 quilt format)
# CRITICAL: Tarball must be in parent directory of where dpkg-buildpackage runs
# dpkg-buildpackage runs from ${BUILD_DIR}/${WORK_DIR}, so tarball goes in ${BUILD_DIR}
UPSTREAM_TARBALL="${BUILD_DIR}/lpub3d_${LP3D_VERSION}.orig.tar.gz"
if [ ! -f "$UPSTREAM_TARBALL" ]; then
log_info "Creating upstream tarball for dpkg-source..."
cd "${BUILD_DIR}"
tar --exclude=debian --exclude=.git -czf "lpub3d_${LP3D_VERSION}.orig.tar.gz" "${WORK_DIR}"
log_success "Created upstream tarball in: ${BUILD_DIR}/lpub3d_${LP3D_VERSION}.orig.tar.gz"
cd "${BUILD_DIR}/${WORK_DIR}"
fi
# First attempt - capture output to check for dpkg-source issues
BUILD_OUTPUT=""
BUILD_EXIT_CODE=0
if ! BUILD_OUTPUT=$(/usr/bin/dpkg-buildpackage -us -uc 2>&1); then
BUILD_EXIT_CODE=$?
# Check if this is a dpkg-source commit issue
if echo "$BUILD_OUTPUT" | grep -q "you can integrate the local changes with dpkg-source --commit"; then
log_info "Detected dpkg-source local changes issue - applying automatic fix..."
# Apply the proven fix from our testing
log_info "Executing: EDITOR=true dpkg-source --commit . ubuntu-24-04-fixes"
if EDITOR=true dpkg-source --commit . ubuntu-24-04-fixes; then
log_success "Successfully committed local changes to dpkg-source"
# Retry the build
log_info "Retrying dpkg-buildpackage after dpkg-source commit..."
if BUILD_OUTPUT=$(/usr/bin/dpkg-buildpackage -us -uc 2>&1); then
BUILD_EXIT_CODE=0
log_success "dpkg-buildpackage completed successfully after dpkg-source fix"
else
BUILD_EXIT_CODE=$?
log_error "dpkg-buildpackage still failed after dpkg-source fix"
fi
else
log_error "Failed to commit local changes with dpkg-source"
BUILD_EXIT_CODE=1
fi
else
log_error "dpkg-buildpackage failed with non-dpkg-source error"
fi
fi
# Always save the build output to log file
echo "$BUILD_OUTPUT" | tee ${BUILD_DIR}/build.log
# Handle build failure
if [ $BUILD_EXIT_CODE -ne 0 ]; then
log_error "dpkg-buildpackage failed! Checking for specific errors..."
# Check for common failure patterns
if echo "$BUILD_OUTPUT" | grep -q "getlDrawIni"; then
log_error "LDView incomplete - missing getlDrawIni() method"
log_error "This indicates LDView libraries were not built completely"
fi
if echo "$BUILD_OUTPUT" | grep -q "No such file or directory"; then
log_error "Missing files detected - check include paths"
fi
# Show last 50 lines of build log for debugging
log_error "Last 50 lines of build output:"
echo "$BUILD_OUTPUT" | tail -50
exit 1
else
log_success "dpkg-buildpackage completed successfully"
fi
# ===== BUILD CHECKPOINT 2: Renderer validation =====
log_info "=== BUILD CHECKPOINT 2: Validating renderer builds ==="
THIRD_PARTY_DIR="${BUILD_DIR}/lpub3d_linux_3rdparty"
# Validate LDGLite
if [ -f "${THIRD_PARTY_DIR}/ldglite/bin/ldglite" ]; then
log_success "LDGLite executable found"
else
log_error "LDGLite executable missing!"
fi
# CRITICAL: Validate LDView completeness
if ! validate_ldview_build "${THIRD_PARTY_DIR}/ldview-4.5"; then
log_error "LDView validation failed - build is incomplete!"
log_error "This is a critical failure - LPub3D cannot work without complete LDView"
exit 1
fi
# Validate POVRay
if [ -f "${THIRD_PARTY_DIR}/povray/bin/lpub3d_trace_cui" ]; then
log_success "POVRay executable found"
# Test POVRay execution
if ${THIRD_PARTY_DIR}/povray/bin/lpub3d_trace_cui --version >/dev/null 2>&1; then
log_success "POVRay executable works"
else
log_warning "POVRay executable found but may have runtime issues"
fi
else
log_error "POVRay executable missing!"
fi
# ===== BUILD CHECKPOINT 3: LPub3D executable validation =====
log_info "=== BUILD CHECKPOINT 3: Validating LPub3D build ==="
cd ${BUILD_DIR}/
# Check for DEB package
DISTRO_FILE=$(ls ${LPUB3D}_${LP3D_APP_VERSION}*.deb 2>/dev/null | head -1)
if [[ -f ${DISTRO_FILE} ]]; then
log_success "Build package created: ${DISTRO_FILE}"
# Perform build check if not skipped
if [[ -z "$LP3D_SKIP_BUILD_CHECK" ]]; then
log_info "Running build verification..."
# Check if update-desktop-database exists
if [[ ! -f "/usr/bin/update-desktop-database" ]]; then
log_info "Installing desktop-file-utils..."
sudo apt-get install -y desktop-file-utils
fi
# Install package for testing
log_info "Installing ${LPUB3D} for build check..."
sudo dpkg -i ${DISTRO_FILE}
# Check if executable exists
LPUB3D_EXE=lpub3d${LP3D_APP_VER_SUFFIX}
if [[ -f "/usr/bin/${LPUB3D_EXE}" ]]; then
log_success "LPub3D executable installed successfully"
# Test basic functionality
log_info "Testing LPub3D functionality..."
if /usr/bin/${LPUB3D_EXE} --version; then
log_success "LPub3D --version works"
else
log_error "LPub3D --version failed"
fi
if /usr/bin/${LPUB3D_EXE} --app-paths; then
log_success "LPub3D --app-paths works"
else
log_error "LPub3D --app-paths failed"
fi
# Uninstall test package
log_info "Uninstalling test package..."
sudo dpkg -r ${LPUB3D}
else
log_error "LPub3D executable not found after installation!"
fi
fi
# Run lintian check
log_info "Running lintian check..."
lintian ${DISTRO_FILE} ${WORK_DIR}/${LPUB3D}.dsc || true
# Rename package with platform name
IFS=_ read DEB_NAME DEB_VERSION DEB_EXTENSION <<< ${DISTRO_FILE}
LP3D_DEB_FILE="LPub3D-${LP3D_APP_VERSION_LONG}-${PLATFORM_NAME}-${DEB_EXTENSION}"
log_info "Creating final package: ${LP3D_DEB_FILE}..."
mv -f "${DISTRO_FILE}" "${LP3D_DEB_FILE}"
# Create checksum
log_info "Creating SHA512 checksum..."
sha512sum "${LP3D_DEB_FILE}" > "${LP3D_DEB_FILE}.sha512"
# Move to output directory
OUTPUT_DIR="${SCRIPT_DIR}/output"
mkdir -p "${OUTPUT_DIR}"
log_info "Moving build artifacts to output directory..."
mv -f "${LP3D_DEB_FILE}"* "${OUTPUT_DIR}/"
mv -f *.xz "${OUTPUT_DIR}/" 2>/dev/null || true
mv -f *.dsc "${OUTPUT_DIR}/" 2>/dev/null || true
mv -f *.changes "${OUTPUT_DIR}/" 2>/dev/null || true
mv -f *.buildinfo "${OUTPUT_DIR}/" 2>/dev/null || true
mv -f *.log "${OUTPUT_DIR}/" 2>/dev/null || true
# Final summary
log_success "===== BUILD COMPLETED SUCCESSFULLY ====="
log_info "Package: ${OUTPUT_DIR}/${LP3D_DEB_FILE}"
log_info "Build time: $(($SECONDS / 60)) minutes $(($SECONDS % 60)) seconds"
# Final validation summary
log_info "===== FINAL VALIDATION SUMMARY ====="
log_success "✓ LDGLite built and validated"
log_success "✓ LDView built with complete library set"
log_success "✓ LDView contains getlDrawIni() method"
log_success "✓ POVRay built and validated"
log_success "✓ LPub3D compiled successfully"
log_success "✓ Package created and tested"
else
log_error "Build package not found! Build failed."
log_error "Check build.log for details"
exit 1
fi
exit 0