Skip to content
Merged
138 changes: 79 additions & 59 deletions script/tools/ck-build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier: MIT

# CK Build - Build Composable Kernel targets in Docker
# CK Build - Build Composable Kernel targets
# Environment-agnostic: works natively on ROCm hosts or inside containers

set -e
set -o pipefail
Expand All @@ -12,68 +13,87 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/common.sh"

# Initialize configuration
PROJECT_ROOT=$(get_project_root "${SCRIPT_DIR}")
CONTAINER_NAME=$(get_container_name "${PROJECT_ROOT}")
PROJECT_ROOT=$(find_project_root "${SCRIPT_DIR}" || get_project_root "${SCRIPT_DIR}")
BUILD_DIR=$(get_build_dir "${PROJECT_ROOT}")

# Help message
show_help() {
cat << EOF
CK Build - Build Composable Kernel targets in Docker
CK Build - Build Composable Kernel targets

Usage: ck-build [options] [target...]

Options:
-h, --help Show this help message
--name <name> Specify container name
--reconfigure Reconfigure CMake before building
-j <N> Parallel jobs (passed to ninja)
-v, --verbose Verbose output
--build-dir <dir> Build directory (default: ./build)
--clean Clean before building
--configure Auto-configure if build.ninja missing
--list List available targets

Arguments:
target Target(s) to build (default: all)

Environment:
CK_CONTAINER_NAME - Override default container name
GPU_TARGET - Override GPU target detection (e.g., gfx950, gfx942)
CK_BUILD_DIR - Override build directory
CK_GPU_TARGET - Override GPU target for auto-configure

Examples:
ck-build # Build all targets
ck-build test_amdgcn_mma # Build specific target
ck-build test_amdgcn_mma test_gemm # Build multiple targets
ck-build --reconfigure # Reconfigure CMake and build all
ck-build --configure # Auto-configure and build all
ck-build --clean test_amdgcn_mma # Clean and build target
ck-build -j 8 test_amdgcn_mma # Build with 8 parallel jobs
ck-build --list # List available targets

EOF
}

# Parse arguments
targets=()
reconfigure=false
clean=false
parallel_jobs=""
verbose=false
clean=false
auto_configure=false
list_targets=false

while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
--name)
CONTAINER_NAME="$2"
-j)
require_arg "$1" "${2:-}"
parallel_jobs="$2"
shift 2
;;
--reconfigure)
reconfigure=true
-j*)
parallel_jobs="${1#-j}"
shift
;;
-v|--verbose)
verbose=true
shift
;;
--build-dir)
require_arg "$1" "${2:-}"
BUILD_DIR="$2"
shift 2
;;
--clean)
clean=true
shift
;;
-j)
parallel_jobs="-j $2"
shift 2
--configure)
auto_configure=true
shift
;;
--list)
list_targets=true
shift
;;
*)
targets+=("$1")
Expand All @@ -82,62 +102,62 @@ while [[ $# -gt 0 ]]; do
esac
done

# Ensure container is running
if ! container_is_running "${CONTAINER_NAME}"; then
echo "Container '${CONTAINER_NAME}' not running. Starting..."
"${SCRIPT_DIR}/ck-start" "${CONTAINER_NAME}"
# Handle --list
if [ "$list_targets" = true ]; then
if ! is_build_configured "${BUILD_DIR}"; then
error "Build not configured. Run 'ck-configure' first or use --configure"
exit 1
fi
info "Available targets:"
cd "${BUILD_DIR}"
ninja -t targets 2>/dev/null | grep -E '^[a-zA-Z_][a-zA-Z0-9_-]*:' | cut -d: -f1 | sort | head -100
echo ""
echo "(Showing first 100 targets. Use 'ninja -t targets' for full list)"
exit 0
fi

# Configure CMake if needed or requested
if [ "$reconfigure" = true ] || ! docker exec "${CONTAINER_NAME}" test -f /workspace/build/build.ninja 2>/dev/null; then
echo "Detecting GPU target..."
GPU_TARGET_DETECTED=$(detect_gpu_target "${CONTAINER_NAME}")

if [ "$reconfigure" = true ]; then
echo "Reconfiguring CMake from scratch for GPU target: ${GPU_TARGET_DETECTED}"
# Auto-configure if needed
if ! is_build_configured "${BUILD_DIR}"; then
if [ "$auto_configure" = true ]; then
info "Build not configured. Running ck-configure..."
"${SCRIPT_DIR}/ck-configure" --build-dir "${BUILD_DIR}"
echo ""
else
echo "Configuring build with CMake for GPU target: ${GPU_TARGET_DETECTED}"
error "Build not configured. Run 'ck-configure' first or use --configure"
exit 1
fi

docker exec "${CONTAINER_NAME}" bash -c "
cd /workspace || exit 1
rm -rf /workspace/build
mkdir /workspace/build
cd /workspace/build || exit 1
cmake .. -GNinja \
-DGPU_TARGETS=${GPU_TARGET_DETECTED} \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_COMPILER=/opt/rocm/llvm/bin/clang++ \
-DBUILD_TESTING=ON 2>&1 | tail -30
"
echo ""
fi

# Clean if requested
if [ "$clean" = true ]; then
echo "Cleaning build directory..."
docker exec "${CONTAINER_NAME}" bash -c "
cd /workspace/build || exit 1
ninja clean
"
info "Cleaning build directory..."
cd "${BUILD_DIR}"
ninja clean
echo ""
fi

# Build ninja command
ninja_cmd=(ninja -C "${BUILD_DIR}")

if [ -n "$parallel_jobs" ]; then
ninja_cmd+=("-j" "$parallel_jobs")
fi

if [ "$verbose" = true ]; then
ninja_cmd+=(-v)
fi

# Add targets
ninja_cmd+=("${targets[@]}")

# Build targets
if [ ${#targets[@]} -eq 0 ]; then
echo "Building all configured targets..."
docker exec "${CONTAINER_NAME}" bash -c "
cd /workspace/build || exit 1
ninja ${parallel_jobs} 2>&1
"
info "Building all configured targets..."
else
echo "Building targets: ${targets[*]}"
docker exec "${CONTAINER_NAME}" bash -c "
cd /workspace/build || exit 1
ninja ${parallel_jobs} ${targets[*]} 2>&1
"
info "Building targets: ${targets[*]}"
fi

"${ninja_cmd[@]}"

echo ""
echo "Build complete"
info "Build complete"
Loading