You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nudt-compiler-cpp/build.sh

117 lines
3.8 KiB

#!/usr/bin/env bash
# ============================================================================
# SysY Compiler - Competition Build Script
# ============================================================================
# This script compiles the SysY compiler for the 2026 Compiler Design Competition.
# Usage:
# ./build.sh - Build the compiler
# ./build.sh clean - Clean build artifacts
# ============================================================================
set -euo pipefail
PROJ_DIR="$(cd "$(dirname "$0")" && pwd)"
BUILD_DIR="${PROJ_DIR}/build"
BIN_DIR="${BUILD_DIR}/bin"
ANTLR4_GENERATED="${BUILD_DIR}/generated/antlr4/src/antlr4"
# Compiler settings
CXX="${CXX:-clang++}"
CXXSTD="-std=c++17"
CXXFLAGS="${CXXFLAGS:--O2 -Wall -Wextra -Wpedantic}"
LDFLAGS="-lpthread"
# Include paths (all relative to project root)
INCLUDES=(
"-I${PROJ_DIR}/include"
"-I${PROJ_DIR}/src"
"-I${PROJ_DIR}/third_party/antlr4-runtime-4.13.2/runtime/src"
"-I${PROJ_DIR}/third_party/antlr4-runtime-4.13.2/runtime/src/atn"
"-I${PROJ_DIR}/third_party/antlr4-runtime-4.13.2/runtime/src/dfa"
"-I${PROJ_DIR}/third_party/antlr4-runtime-4.13.2/runtime/src/internal"
"-I${PROJ_DIR}/third_party/antlr4-runtime-4.13.2/runtime/src/misc"
"-I${PROJ_DIR}/third_party/antlr4-runtime-4.13.2/runtime/src/support"
"-I${PROJ_DIR}/third_party/antlr4-runtime-4.13.2/runtime/src/tree"
"-I${PROJ_DIR}/third_party/antlr4-runtime-4.13.2/runtime/src/tree/pattern"
"-I${PROJ_DIR}/third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath"
"-I${ANTLR4_GENERATED}"
)
# ============================================================================
# Functions
# ============================================================================
generate_antlr() {
echo "[ANTLR4] Generating parser code..."
mkdir -p "${ANTLR4_GENERATED}"
if [ ! -f "${ANTLR4_GENERATED}/SysYParser.h" ]; then
java -jar "${PROJ_DIR}/third_party/antlr-4.13.2-complete.jar" \
-Dlanguage=Cpp \
-o "${BUILD_DIR}/generated/antlr4" \
-visitor \
"${PROJ_DIR}/src/frontend/SysY.g4" 2>/dev/null || {
echo "Warning: ANTLR4 generation failed, checking for existing files..."
if [ ! -f "${ANTLR4_GENERATED}/SysYParser.h" ]; then
echo "Error: No generated parser code found."
exit 1
fi
}
else
echo "[ANTLR4] Parser code already exists, skipping."
fi
}
build() {
echo "[CXX] Compiling compiler..."
mkdir -p "${BIN_DIR}"
# Collect source files
SRC_FILES=()
while IFS= read -r -d '' file; do
SRC_FILES+=("$file")
done < <(find "${PROJ_DIR}/src" -name '*.cpp' -print0)
ANTLR_RUNTIME_FILES=()
while IFS= read -r -d '' file; do
ANTLR_RUNTIME_FILES+=("$file")
done < <(find "${PROJ_DIR}/third_party/antlr4-runtime-4.13.2/runtime/src" -name '*.cpp' -print0)
GENERATED_FILES=()
if [ -d "${ANTLR4_GENERATED}" ]; then
while IFS= read -r -d '' file; do
GENERATED_FILES+=("$file")
done < <(find "${ANTLR4_GENERATED}" -name '*.cpp' -print0 2>/dev/null || true)
fi
# Compile
"${CXX}" ${CXXSTD} ${CXXFLAGS} \
"${INCLUDES[@]}" \
"${SRC_FILES[@]}" \
"${ANTLR_RUNTIME_FILES[@]}" \
"${GENERATED_FILES[@]}" \
${LDFLAGS} \
-o "${BIN_DIR}/compiler"
echo "[INFO] Build successful: ${BIN_DIR}/compiler"
}
clean() {
echo "[CLEAN] Removing build directory..."
rm -rf "${BUILD_DIR}"
}
# ============================================================================
# Main
# ============================================================================
case "${1:-}" in
clean)
clean
;;
*)
generate_antlr
build
;;
esac