#!/bin/bash # # compile-theoria - Linux build script for Theoria Chess Engine # For use on London server (Debian) # # Builds Linux executables for Theoria16, Theoria17, Theoria17-1024, and Theoria17-3072 # # Hash file location HASH_FILE="/home/user/theoria/.nnue-hashes" # Lines per page for pagination LINES_PER_PAGE=20 # Architecture selection (set during runtime) # 1 = Both AVX2+BMI2, 2 = AVX2 only, 3 = BMI2 only ARCH_CHOICE=1 # Function to display text with pagination paginate() { local line_count=0 local total_lines=0 local lines=() while IFS= read -r line; do lines+=("$line") done total_lines=${#lines[@]} if [ "$total_lines" -le "$LINES_PER_PAGE" ]; then printf '%s\n' "${lines[@]}" return fi for i in "${!lines[@]}"; do echo "${lines[$i]}" ((line_count++)) if [ "$line_count" -ge "$LINES_PER_PAGE" ] && [ "$((i + 1))" -lt "$total_lines" ]; then local remaining=$((total_lines - i - 1)) echo "" read -p "-- Press Enter for more ($remaining lines remaining) --" &2 local menu_content="" menu_content+="========================================\n" menu_content+="NNUE Selection for $PROJECT_LABEL\n" menu_content+="========================================\n" menu_content+="\n" menu_content+="Available NNUE files:\n" menu_content+="\n" local NNUE_FILES=("$NNUE_DIR"/*.nnue) for i in "${!NNUE_FILES[@]}"; do local nnue_file="${NNUE_FILES[$i]}" local exists_flag="" if [ "$(check_build_exists "$nnue_file" "$BUILD_DIR" "$PROJECT_NAME" "$VERSION")" == "exists" ]; then exists_flag=" <-- Build Exists" fi menu_content+="$((i+1))) $(basename "$nnue_file")$exists_flag\n" done echo -e "$menu_content" | paginate >&2 echo "" >&2 echo "Enter selection (examples: 5 or 2,5,8 or 4-8 or 2,4-7,11):" >&2 read -p "Selection: " selection &2 echo "Invalid selection. Please try again." >&2 sleep 2 select_nnues "$NNUE_DIR" "$PROJECT_LABEL" "$BUILD_DIR" "$PROJECT_NAME" "$VERSION" return fi echo "$parsed" } # Function to select architecture select_architecture() { echo "" >&2 echo "========================================" >&2 echo "Architecture Selection" >&2 echo "========================================" >&2 echo "" >&2 echo "Select architecture(s):" >&2 echo "" >&2 echo "1) Both AVX2 and BMI2 [default]" >&2 echo "2) AVX2 only" >&2 echo "3) BMI2 only" >&2 echo "" >&2 read -p "Enter choice (1, 2, or 3) [1]: " arch_input &2 echo "Invalid selection. Please try again." >&2 sleep 2 select_architecture return fi } # Function to get NNUE details from indices get_nnue_details() { local NNUE_DIR="$1" local indices="$2" local -a selected_nnues=() local NNUE_FILES=("$NNUE_DIR"/*.nnue) for idx in $indices; do selected_nnues+=("${NNUE_FILES[$((idx-1))]}") done printf '%s\n' "${selected_nnues[@]}" } # Function to get architecture label get_arch_label() { local arch_choice="$1" case "$arch_choice" in 1) echo "AVX2 + BMI2" ;; 2) echo "AVX2 only" ;; 3) echo "BMI2 only" ;; esac } # Function to build summary text build_summary_text() { local project_label="$1" local build_dir="$2" local project_name="$3" local version="$4" shift 4 local nnue_files=("$@") local summary="" summary+="\n" summary+="----------------------------------------\n" summary+="$project_label Builds:\n" summary+="----------------------------------------\n" summary+="Build directory: $build_dir\n" summary+="\n" for nnue_file in "${nnue_files[@]}"; do local nnue_ver=$(basename "$nnue_file" .nnue | grep -o 'epoch_[0-9]\+\|epoch-[0-9]\+\|epoch[0-9]\+') if [ -z "$nnue_ver" ]; then nnue_ver=$(basename "$nnue_file" .nnue) fi summary+=" NNUE: $(basename "$nnue_file")\n" if [ "$ARCH_CHOICE" == "1" ] || [ "$ARCH_CHOICE" == "2" ]; then summary+=" -> ${project_name}-${version}-${nnue_ver}-avx2\n" fi if [ "$ARCH_CHOICE" == "1" ] || [ "$ARCH_CHOICE" == "3" ]; then summary+=" -> ${project_name}-${version}-${nnue_ver}-bmi2\n" fi done echo -e "$summary" } # Function to calculate total builds calc_total_builds() { local nnue_count="$1" local builds_per_nnue=0 if [ "$ARCH_CHOICE" == "1" ]; then builds_per_nnue=2 else builds_per_nnue=1 fi echo $((nnue_count * builds_per_nnue)) } # Function to display summary with pagination display_summary_paginated() { local summary_text="$1" local total_builds="$2" clear echo "========================================" echo "Build Summary" echo "========================================" echo "" echo "Architecture: $(get_arch_label $ARCH_CHOICE)" echo "Total binaries to build: $total_builds" echo -e "$summary_text" | paginate } # Function to compile a single NNUE compile_single_nnue() { local project_dir="$1" local nnue_file="$2" local target_nnue="$3" local build_dir="$4" local project_name="$5" local version="$6" local project_label="$7" local nnue_ver=$(basename "$nnue_file" .nnue | grep -o 'epoch_[0-9]\+\|epoch-[0-9]\+\|epoch[0-9]\+') if [ -z "$nnue_ver" ]; then nnue_ver=$(basename "$nnue_file" .nnue) fi echo "" echo "----------------------------------------" echo "Compiling: $(basename "$nnue_file")" echo "Project: $project_label" echo "----------------------------------------" cd "$project_dir/src" || exit 1 cp "$nnue_file" "$target_nnue" if [ $? -ne 0 ]; then echo "Error: Failed to copy NNUE file" return 1 fi local nnue_hash=$(sha256sum "$target_nnue" | awk '{print $1}') echo "$project_label - $(basename "$nnue_file"): $nnue_hash" >> "$HASH_FILE" if [ "$ARCH_CHOICE" == "1" ] || [ "$ARCH_CHOICE" == "2" ]; then echo "" echo "Building AVX2 version..." make -j clean > /dev/null 2>&1 make -j profile-build ARCH=x86-64-avx2 COMP=gcc > /dev/null 2>&1 if [ $? -eq 0 ]; then local output_name="${project_name}-${version}-${nnue_ver}-avx2" cp stockfish "$build_dir/$output_name" echo " ✓ AVX2 build complete: $output_name" else echo " ✗ AVX2 build failed" return 1 fi fi if [ "$ARCH_CHOICE" == "1" ] || [ "$ARCH_CHOICE" == "3" ]; then echo "" echo "Building BMI2 version..." make -j clean > /dev/null 2>&1 make -j profile-build ARCH=x86-64-bmi2 COMP=gcc > /dev/null 2>&1 if [ $? -eq 0 ]; then local output_name="${project_name}-${version}-${nnue_ver}-bmi2" cp stockfish "$build_dir/$output_name" echo " ✓ BMI2 build complete: $output_name" else echo " ✗ BMI2 build failed" return 1 fi fi rm -f "$project_dir/src/$target_nnue" rm -f "$project_dir/src/run-stockfish"* rm -f "$project_dir/src/"*.lto_wrapper_args rm -f "$project_dir/src/"*.ltrans.out rm -f "$project_dir/src/"*.ltrans_args rm -f "$project_dir/src/"*.res rm -f "$project_dir/src/"*.wpa.args.* return 0 } # Function to compile all selected NNUEs for a project compile_project() { local project_dir="$1" local target_nnue="$2" local build_dir="$3" local project_name="$4" local version="$5" local project_label="$6" shift 6 local nnue_files=("$@") mkdir -p "$build_dir" echo "" >> "$HASH_FILE" echo "========================================" >> "$HASH_FILE" echo "$project_label Compilation Started" >> "$HASH_FILE" echo "Platform: Linux only" >> "$HASH_FILE" echo "Architecture: $(get_arch_label $ARCH_CHOICE)" >> "$HASH_FILE" echo "========================================" >> "$HASH_FILE" for nnue_file in "${nnue_files[@]}"; do if ! compile_single_nnue "$project_dir" "$nnue_file" "$target_nnue" \ "$build_dir" "$project_name" "$version" "$project_label"; then echo "" echo "Compilation stopped due to error." exit 1 fi done cd "$project_dir/src" || exit 1 make clean > /dev/null 2>&1 } # ============================================================================ # Main script starts here # ============================================================================ clear echo "========================================" echo "Theoria Compilation Script (Linux)" echo "========================================" echo "" echo "Select compilation mode:" echo "1) Theoria16 (HalfKAv2_hm - SF16)" echo "2) Theoria17 (HalfKAv2_hm - SF17)" echo "3) Theoria17-1024 (Full_Threats L1=1024)" echo "4) Theoria17-3072 (Full_Threats L1=3072)" echo "5) All four" echo "" read -p "Enter choice (1, 2, 3, 4, or 5): " project_choice # Initialize hash file echo "Theoria Compilation Run - $(date)" > "$HASH_FILE" echo "========================================" >> "$HASH_FILE" # Variables for Theoria16 T16_DIR="/home/user/theoria/sf16" T16_NNUE_DIR="/home/user/nnue/theoria16" T16_TARGET="nn-b1a57edbea57.nnue" T16_BUILD="/home/user/theoria/build/theoria16" T16_NAME="theoria16" # Variables for Theoria17 T17_DIR="/home/user/theoria/sf17" T17_NNUE_DIR="/home/user/nnue/theoria17" T17_TARGET="nn-1c0000000000.nnue" T17_BUILD="/home/user/theoria/build/theoria17" T17_NAME="theoria17" # Variables for Theoria17-1024 T17_1024_DIR="/home/user/theoria/sf17-threats-1024" T17_1024_NNUE_DIR="/home/user/nnue/theoria17-threat-1024" T17_1024_TARGET="nn-c288c895ea92.nnue" T17_1024_BUILD="/home/user/theoria/build/theoria17-1024" T17_1024_NAME="theoria17-1024" # Variables for Theoria17-3072 T17_3072_DIR="/home/user/theoria/sf17-threats-3072" T17_3072_NNUE_DIR="/home/user/nnue/theoria17-threat-3072" T17_3072_TARGET="nn-c288c895ea92.nnue" T17_3072_BUILD="/home/user/theoria/build/theoria17-3072" T17_3072_NAME="theoria17-3072" VERSION="0.1" # Create build directories mkdir -p "$T16_BUILD" mkdir -p "$T17_BUILD" mkdir -p "$T17_1024_BUILD" mkdir -p "$T17_3072_BUILD" # Selection loop while true; do if [ "$project_choice" == "1" ]; then t16_indices=$(select_nnues "$T16_NNUE_DIR" "Theoria16 (HalfKAv2_hm)" "$T16_BUILD" "$T16_NAME" "$VERSION") t16_nnues=($(get_nnue_details "$T16_NNUE_DIR" "$t16_indices")) ARCH_CHOICE=$(select_architecture) summary_text=$(build_summary_text "Theoria16" "$T16_BUILD" "$T16_NAME" "$VERSION" "${t16_nnues[@]}") total_builds=$(calc_total_builds "${#t16_nnues[@]}") display_summary_paginated "$summary_text" "$total_builds" echo "" read -p "Proceed with compilation? (y/n/b to go back): " confirm if [ "$confirm" == "b" ] || [ "$confirm" == "B" ]; then continue elif [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then echo "Compilation cancelled." exit 0 fi break elif [ "$project_choice" == "2" ]; then t17_indices=$(select_nnues "$T17_NNUE_DIR" "Theoria17 (HalfKAv2_hm - SF17)" "$T17_BUILD" "$T17_NAME" "$VERSION") t17_nnues=($(get_nnue_details "$T17_NNUE_DIR" "$t17_indices")) ARCH_CHOICE=$(select_architecture) summary_text=$(build_summary_text "Theoria17" "$T17_BUILD" "$T17_NAME" "$VERSION" "${t17_nnues[@]}") total_builds=$(calc_total_builds "${#t17_nnues[@]}") display_summary_paginated "$summary_text" "$total_builds" echo "" read -p "Proceed with compilation? (y/n/b to go back): " confirm if [ "$confirm" == "b" ] || [ "$confirm" == "B" ]; then continue elif [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then echo "Compilation cancelled." exit 0 fi break elif [ "$project_choice" == "3" ]; then t17_1024_indices=$(select_nnues "$T17_1024_NNUE_DIR" "Theoria17-1024 (Full_Threats L1=1024)" "$T17_1024_BUILD" "$T17_1024_NAME" "$VERSION") t17_1024_nnues=($(get_nnue_details "$T17_1024_NNUE_DIR" "$t17_1024_indices")) ARCH_CHOICE=$(select_architecture) summary_text=$(build_summary_text "Theoria17-1024" "$T17_1024_BUILD" "$T17_1024_NAME" "$VERSION" "${t17_1024_nnues[@]}") total_builds=$(calc_total_builds "${#t17_1024_nnues[@]}") display_summary_paginated "$summary_text" "$total_builds" echo "" read -p "Proceed with compilation? (y/n/b to go back): " confirm if [ "$confirm" == "b" ] || [ "$confirm" == "B" ]; then continue elif [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then echo "Compilation cancelled." exit 0 fi break elif [ "$project_choice" == "4" ]; then t17_3072_indices=$(select_nnues "$T17_3072_NNUE_DIR" "Theoria17-3072 (Full_Threats L1=3072)" "$T17_3072_BUILD" "$T17_3072_NAME" "$VERSION") t17_3072_nnues=($(get_nnue_details "$T17_3072_NNUE_DIR" "$t17_3072_indices")) ARCH_CHOICE=$(select_architecture) summary_text=$(build_summary_text "Theoria17-3072" "$T17_3072_BUILD" "$T17_3072_NAME" "$VERSION" "${t17_3072_nnues[@]}") total_builds=$(calc_total_builds "${#t17_3072_nnues[@]}") display_summary_paginated "$summary_text" "$total_builds" echo "" read -p "Proceed with compilation? (y/n/b to go back): " confirm if [ "$confirm" == "b" ] || [ "$confirm" == "B" ]; then continue elif [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then echo "Compilation cancelled." exit 0 fi break elif [ "$project_choice" == "5" ]; then t16_indices=$(select_nnues "$T16_NNUE_DIR" "Theoria16 (HalfKAv2_hm)" "$T16_BUILD" "$T16_NAME" "$VERSION") t16_nnues=($(get_nnue_details "$T16_NNUE_DIR" "$t16_indices")) t17_indices=$(select_nnues "$T17_NNUE_DIR" "Theoria17 (HalfKAv2_hm - SF17)" "$T17_BUILD" "$T17_NAME" "$VERSION") t17_nnues=($(get_nnue_details "$T17_NNUE_DIR" "$t17_indices")) t17_1024_indices=$(select_nnues "$T17_1024_NNUE_DIR" "Theoria17-1024 (Full_Threats L1=1024)" "$T17_1024_BUILD" "$T17_1024_NAME" "$VERSION") t17_1024_nnues=($(get_nnue_details "$T17_1024_NNUE_DIR" "$t17_1024_indices")) t17_3072_indices=$(select_nnues "$T17_3072_NNUE_DIR" "Theoria17-3072 (Full_Threats L1=3072)" "$T17_3072_BUILD" "$T17_3072_NAME" "$VERSION") t17_3072_nnues=($(get_nnue_details "$T17_3072_NNUE_DIR" "$t17_3072_indices")) ARCH_CHOICE=$(select_architecture) summary_text=$(build_summary_text "Theoria16" "$T16_BUILD" "$T16_NAME" "$VERSION" "${t16_nnues[@]}") summary_text+=$(build_summary_text "Theoria17" "$T17_BUILD" "$T17_NAME" "$VERSION" "${t17_nnues[@]}") summary_text+=$(build_summary_text "Theoria17-1024" "$T17_1024_BUILD" "$T17_1024_NAME" "$VERSION" "${t17_1024_nnues[@]}") summary_text+=$(build_summary_text "Theoria17-3072" "$T17_3072_BUILD" "$T17_3072_NAME" "$VERSION" "${t17_3072_nnues[@]}") total_builds=$(calc_total_builds "$((${#t16_nnues[@]} + ${#t17_nnues[@]} + ${#t17_1024_nnues[@]} + ${#t17_3072_nnues[@]}))") display_summary_paginated "$summary_text" "$total_builds" echo "" read -p "Proceed with compilation? (y/n/b to go back): " confirm if [ "$confirm" == "b" ] || [ "$confirm" == "B" ]; then continue elif [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then echo "Compilation cancelled." exit 0 fi break else echo "Invalid choice. Exiting." exit 1 fi done # Start compilation clear echo "========================================" echo "Starting Compilation..." echo "========================================" echo "Platform: Linux only" echo "Architecture: $(get_arch_label $ARCH_CHOICE)" if [ "$project_choice" == "1" ]; then compile_project "$T16_DIR" "$T16_TARGET" "$T16_BUILD" "$T16_NAME" "$VERSION" "Theoria16" "${t16_nnues[@]}" elif [ "$project_choice" == "2" ]; then compile_project "$T17_DIR" "$T17_TARGET" "$T17_BUILD" "$T17_NAME" "$VERSION" "Theoria17" "${t17_nnues[@]}" elif [ "$project_choice" == "3" ]; then compile_project "$T17_1024_DIR" "$T17_1024_TARGET" "$T17_1024_BUILD" "$T17_1024_NAME" "$VERSION" "Theoria17-1024" "${t17_1024_nnues[@]}" elif [ "$project_choice" == "4" ]; then compile_project "$T17_3072_DIR" "$T17_3072_TARGET" "$T17_3072_BUILD" "$T17_3072_NAME" "$VERSION" "Theoria17-3072" "${t17_3072_nnues[@]}" elif [ "$project_choice" == "5" ]; then compile_project "$T16_DIR" "$T16_TARGET" "$T16_BUILD" "$T16_NAME" "$VERSION" "Theoria16" "${t16_nnues[@]}" compile_project "$T17_DIR" "$T17_TARGET" "$T17_BUILD" "$T17_NAME" "$VERSION" "Theoria17" "${t17_nnues[@]}" compile_project "$T17_1024_DIR" "$T17_1024_TARGET" "$T17_1024_BUILD" "$T17_1024_NAME" "$VERSION" "Theoria17-1024" "${t17_1024_nnues[@]}" compile_project "$T17_3072_DIR" "$T17_3072_TARGET" "$T17_3072_BUILD" "$T17_3072_NAME" "$VERSION" "Theoria17-3072" "${t17_3072_nnues[@]}" fi # Display final summary clear echo "========================================" echo "COMPILATION COMPLETE!" echo "========================================" echo "" echo "Build summary saved to: $HASH_FILE" echo "" echo "Displaying summary:" echo "" cat "$HASH_FILE" | paginate echo "" echo "========================================" echo "All builds completed successfully!" echo "========================================" echo "" exit 0