From 449c93e05d72db871045342a25af360df30759ff Mon Sep 17 00:00:00 2001 From: blozano-tt Date: Fri, 27 Sep 2024 18:09:56 +0000 Subject: [PATCH] #13128: Optimize cmake switches, add more support to build script --- .github/workflows/build-artifact.yaml | 2 +- .github/workflows/build.yaml | 2 +- CMakeLists.txt | 7 +- build_metal.sh | 94 +++++++++++---------------- tests/CMakeLists.txt | 3 - 5 files changed, 44 insertions(+), 64 deletions(-) diff --git a/.github/workflows/build-artifact.yaml b/.github/workflows/build-artifact.yaml index e61f89bc5cb..ad5b5dd9082 100644 --- a/.github/workflows/build-artifact.yaml +++ b/.github/workflows/build-artifact.yaml @@ -111,7 +111,7 @@ jobs: -e ARCH_NAME=${{ matrix.arch }} -w ${{ github.workspace }} run: | - nice -n 19 cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=${{ inputs.build-type }} -DCMAKE_INSTALL_PREFIX=build -DENABLE_TRACY=${{ inputs.tracy }} -DTT_METALLIUM_BUILD_TESTS=ON -DTT_METAL_BUILD_TESTS=ON -DTTNN_BUILD_TESTS=ON -DTT_UMD_BUILD_TESTS=ON + nice -n 19 cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=${{ inputs.build-type }} -DCMAKE_INSTALL_PREFIX=build -DENABLE_TRACY=${{ inputs.tracy }} -DTT_METAL_BUILD_TESTS=ON -DTTNN_BUILD_TESTS=ON -DTT_UMD_BUILD_TESTS=ON nice -n 19 cmake --build build --target install - name: 'Tar files' diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index a5963c9dd9b..f405f464b5d 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -43,7 +43,7 @@ jobs: -e ARCH_NAME=${{ matrix.arch }} docker_os_arch: ${{ matrix.build.os }}-amd64 run_args: | - nice -n 19 cmake -B build -DCMAKE_BUILD_TYPE=${{ matrix.build.type }} -DCMAKE_CXX_COMPILER=${{ matrix.build.cxx_compiler }} -DCMAKE_C_COMPILER=${{ matrix.build.c_compiler }} -G Ninja -DTT_METALLIUM_BUILD_TESTS=ON -DTT_METAL_BUILD_TESTS=ON -DTTNN_BUILD_TESTS=ON -DTT_UMD_BUILD_TESTS=ON + nice -n 19 cmake -B build -DCMAKE_BUILD_TYPE=${{ matrix.build.type }} -DCMAKE_CXX_COMPILER=${{ matrix.build.cxx_compiler }} -DCMAKE_C_COMPILER=${{ matrix.build.c_compiler }} -G Ninja -DTT_METAL_BUILD_TESTS=ON -DTTNN_BUILD_TESTS=ON -DTT_UMD_BUILD_TESTS=ON nice -n 19 cmake --build build - name: Check disk space diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f1cf92db1e..e4185699611 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -252,10 +252,11 @@ endif() add_subdirectory(tt_metal) add_subdirectory(ttnn) -option(TT_METALLIUM_BUILD_TESTS "Enables build of Tests" OFF) -if(TT_METALLIUM_BUILD_TESTS) +option(TT_METAL_BUILD_TESTS "Enables build of tt_metal tests" OFF) +option(TTNN_BUILD_TESTS "Enables build of ttnn tests" OFF) +if(TT_METAL_BUILD_TESTS OR TTNN_BUILD_TESTS) add_subdirectory(tests) -endif(TT_METALLIUM_BUILD_TESTS) +endif(TT_METAL_BUILD_TESTS OR TTNN_BUILD_TESTS) ############################################################################################################################ # Install targets for build artifacts and pybinds diff --git a/build_metal.sh b/build_metal.sh index 2659bdb112a..5a3b5a3f071 100755 --- a/build_metal.sh +++ b/build_metal.sh @@ -1,55 +1,7 @@ #!/bin/bash -: ' -TLDR: Follow the steps outlined below to build metal. For more in-depth information, keep reading - -Steps: - 1. Create python_env (you only need to do this ONCE): ./create_venv.sh - - The env will by default be created in $TT_METAL_HOME/python_env. If you want to change it, set the PYTHON_ENV_DIR. - - This step is not dependent on any of the other steps; you only need to run it ONCE and in any order. - 2. Configure and generate build files: `cmake -B build -G Ninja` - - The `-B` indicates where the build folder is; you can change the folder name to whatever you want. - - The `-G Ninja` specifies to cmake to use the Ninja build system, which is faster and more reliable than make. - 3. Build metal: `ninja install -C build` - - The general command would be: `cmake --build build --target install` - - The `-C` indicates where to run the command; in this case, it will be your build folder(s). - - We are targeting `install` since that will also just build src. - - The install target will install pybinding .so (_C.so & _ttnn.so) into the src files, so pybinds can be used. - 4. Build cpp tests: `ninja tests -C build` - - Building tests will also automatically build src. - -Notes: - - YOU ONLY NEED TO BUILD THE PYTHON_ENV ONCE!!!!! (unless you touch the dev python dependencies) - - ALWAYS INSTALL, i.e., just run `ninja install -C build` as the new make build. - - `cmake --build build --target install` is the EXACT same as running `ninja install -C build`. You would use the cmake command if you want to be agnostic of the build system (Ninja or Make). - -Different configs: To change build type or use tracy, you have to change the configuration cmake step (step #2). - - Changing build types: `cmake -B build -DCMAKE_BUILD_TYPE= -G Ninja` - - Valid CMAKE_BUILD_TYPE values: `Release`, `Debug`, `RelWithDebInfo`, `CI` - - Release is the default if you do not set CMAKE_BUILD_TYPE. - - Tracy: `cmake -B build -G Ninja -DENABLE_TRACY=ON` - -Now you can have multiple build folders with different configs. If you want to switch, just run `ninja install -C ` to install different pybinds. - - Caveats: - - At least one of these folders has to be named `build`, and if using tracy config, it has to be named `build`.. pending issue #8767. - - They have to be built with the original build folder name, i.e., you cannot change the build folder name after building because it will mess up the RPATHs and linking. - -Example: - ./create_venv.sh - cmake -B build -G Ninja && ninja -C build # <- Build in Release, inside folder called `build`. - cmake -DCMAKE_BUILD_TYPE=Debug -B build_debug -G Ninja && ninja -C build_debug # <- Build in Debug, inside folder called `build_debug`. - source python_env/bin/activate # <- You cannot run pytests yet since pybinds have not been installed. - ninja install -C build # <- Install Release pybinds. - # <- This test ran in Release config. - ninja install -C build_debug # <- Install Debug pybinds. - # <- This test ran in Debug config. - -NOTE ON DEBUGGING!: - GDB/LLDB is not stable right now. Recommend using GCC11 or higher for debugging or Clang-17 with GDB 14+. -' set -eo pipefail -# Function to display help show_help() { echo "Usage: $0 [options]..." echo " -h, --help Show this help message." @@ -65,6 +17,15 @@ show_help() { echo " --build-tests Build Testcases." echo " --install-prefix Where to install build artifacts." echo " --cmake-options Supply additional options to cmake." + echo " --release Set the build type as Release." + echo " --development Set the build type as RelWithDebInfo." + echo " --debug Set the build type as Debug." + echo " --clean Remove build workspaces." +} + +clean() { + echo "Removing build artifacts!" + rm -rf build_Release* build_Debug* build_RelWithDebInfo* build built } @@ -82,10 +43,8 @@ build_tests="OFF" declare -a cmake_args -#!/bin/bash - -OPTIONS=h,e,c,t,a,m,s,u,b:,p,build-tests,install-prefix:,cmake-options:: -LONGOPTS=help,export_compile_commands,enable_ccache,enable_time_trace,enable_asan,enable_msan,enable_tsan,enable_ubsan,build-type:,enable_profiler,build-tests,install-prefix:,cmake-options:: +OPTIONS=h,e,c,t,a,m,s,u,b:,p +LONGOPTS=help,export_compile_commands,enable_ccache,enable_time_trace,enable_asan,enable_msan,enable_tsan,enable_ubsan,build-type:,release,development,debug,enable_profiler,build-tests,install-prefix:,cmake-options:,clean # Parse the options PARSED=$(getopt -n 'build_metal' --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") @@ -109,8 +68,7 @@ while true; do -c|--enable-ccache) enable_ccache="ON";; -t|--enable-time-trace) - enable_time_trace="ON" - ;; + enable_time_trace="ON";; -a|--enable-asan) enable_asan="ON";; -m|--enable-msan) @@ -120,20 +78,45 @@ while true; do -u|--enable-ubsan) enable_ubsan="ON";; -b|--build-type) + echo "Manually specifying build type" build_type="$2";shift;; + -rel|--release) + build_type="Release";; + -dev|--development) + build_type="RelWithDebInfo";; + -dbg|--debug) + build_type="Debug";; -p|--enable-profiler) enable_profiler="ON";; --build-tests) build_tests="ON";; - --install-prefix) shift; install_prefix=$1;; + --install-prefix) + install_prefix="$2";shift;; --cmake-options) cmake_args+=($2);shift;; + --clean) + clean; exit 0;; --) shift;break;; esac shift done +# Check if there are unrecognized positional arguments left +if [[ $# -gt 0 ]]; then + echo "ERROR: Unrecognized positional argument(s): $@" + show_help + exit 1 +fi + +# Validate the build_type +VALID_BUILD_TYPES=("Release" "Debug" "RelWithDebInfo") +if [[ ! " ${VALID_BUILD_TYPES[@]} " =~ " ${build_type} " ]]; then + echo "ERROR: Invalid build type '$build_type'. Allowed values are Release, Debug, RelWithDebInfo." + show_help + exit 1 +fi + build_dir="build_$build_type" @@ -196,7 +179,6 @@ if [ "$export_compile_commands" = "ON" ]; then fi if [ "$build_tests" = "ON" ]; then - cmake_args+=("-DTT_METALLIUM_BUILD_TESTS=ON") cmake_args+=("-DTTNN_BUILD_TESTS=ON") cmake_args+=("-DTT_METAL_BUILD_TESTS=ON") cmake_args+=("-DTT_UMD_BUILD_TESTS=ON") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b8753b72389..24088c4ba89 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,13 +4,10 @@ include(GoogleTest) add_library(test_common_libs INTERFACE) target_link_libraries(test_common_libs INTERFACE pthread gtest gtest_main) - -option(TT_METAL_BUILD_TESTS "Enables build of tt_metal tests" OFF) if(TT_METAL_BUILD_TESTS) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tt_metal/tt_metal) endif(TT_METAL_BUILD_TESTS) -option(TTNN_BUILD_TESTS "Enables build of ttnn tests" OFF) if(TTNN_BUILD_TESTS) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tt_eager) # this should go away and be replaced with link to ttnn add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/ttnn/unit_tests/gtests)