# Using separate cmake build directories when building from source

**URL:** <https://dev-discuss.pytorch.org/t/using-separate-cmake-build-directories-when-building-from-source/2285>\
**Category:** Uncategorized\
**Created:** [July 17, 2024, 4:29pm UTC](https://dev-discuss.pytorch.org/t/using-separate-cmake-build-directories-when-building-from-source/2285 "2024-07-17T16:29:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![galv](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/galv/32/2133_2.png) [@galv](https://dev-discuss.pytorch.org/u/galv)\
**Post date:** [July 17, 2024, 4:29pm UTC](https://dev-discuss.pytorch.org/t/using-separate-cmake-build-directories-when-building-from-source/2285/1 "2024-07-17T16:29:54Z")

</div>

Hi all,

One thing that has bothered me about building pytorch from source is that I could not use multiple build directories, even though it is a cmake project. I dug around a little bit, and found a workaround for this that I wanted to share with anyone else in my situation.

If you apply this diff:

```auto
modified tools/setup_helpers/env.py                                                                                                                                                                       
@@ -21,7 +21,7 @@ CONDA_DIR = os.path.join(os.path.dirname(sys.executable), "..")                                                                                                                           
                                                                                                                                                                                                            
 IS_64BIT = struct.calcsize("P") == 8                                                                                                                                                                       
                                                                                                                                                                                                            
-BUILD_DIR = "build"                                                                                                                                                                                        
+BUILD_DIR = os.environ.get("TORCH_CMAKE_BUILD_DIR", "build")                                                                                                                                               
                                                                                                                                                                                                            
                                                                                                                                                                                                            
 def check_env_flag(name: str, default: str = "") -> bool:                                                                                                                                                  

```

and then set the TORCH\_CMAKE\_BUILD\_DIR environment variable to something other than “build”, you can share multiple build directories in the same repository. I have found this useful for testing out the same changes against multiple different versions of CUDA for my PR [Add support for conditional nodes in cuda graphs. by galv · Pull Request #130386 · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/pull/130386), since it depends upon CUDA 12.4 features.

Here are my two compile command lines:

```auto
TORCH_CUDA_ARCH_LIST="8.0" CUDA_HOME=/home/dgalvez/scratch/cuda/cuda-12.4 CMAKE_C_COMPILER_LAUNCHER=ccache CMAKE_CXX_COMPILER_LAUNCHER=ccache CMAKE_CUDA_COMPILER_LAUNCHER=ccache REL_WITH_DEB_INFO=1 python setup.py develop 2>&1 | tee develop_cudagraphs_12.4.

```

```auto
TORCH_CMAKE_BUILD_DIR=build_cuda12.1 TORCH_CUDA_ARCH_LIST="8.0" CUDA_HOME=/home/dgalvez/scratch/cuda/cuda-12.1.1 CMAKE_C_COMPILER_LAUNCHER=ccache CMAKE_CXX_COMPILER_LAUNCHER=ccache CMAKE_CUDA_COMPILER_LAUNCHER=ccache python setup.py develop 2>&1 | tee develop_cudagraphs_12.1.1_2.log

```

I have no idea if this works in all cases, but it works for me.
