Repository: pytorch
Issue: ATen codegen is always re-running for Makefile generator
Labels: module: build,triaged

## 🐛 Bug

codgen is always rerunning (even if no changes to templates). I.e. I get lines like this every time I hit build/run/debug etc

```
[ 69%] Built target ATEN_CPU_FILES_GEN_TARGET
[ 69%] Generating ../aten/src/ATen/CPUFunctions.h, ../aten/src/ATen/CPUFunctions_inl.h, ../aten/src/ATen/CompositeExplicitAutogradFunctions.h, ../aten/src/ATen/CompositeExplicitAutogradFunctions_inl.h, ../aten/src/ATen/CompositeImplicitAutogradFunctions.h, ../aten/src/ATen/CompositeImplicitAutogradFunctions_inl.h, ../aten/src/ATen/Declarations.yaml, ../aten/src/ATen/Functions.cpp, ../aten/src/ATen/Functions.h, ../aten/src/ATen/MetaFunctions.h, ../aten/src/ATen/MetaFunctions_inl.h, ../aten/src/ATen/NativeFunctions.h, ../aten/src/ATen/NativeMetaFunctions.h, ../aten/src/ATen/Operators.cpp, ../aten/src/ATen/Operators.h, ../aten/src/ATen/RedispatchFunctions.h, ../aten/src/ATen/RegisterBackendSelect.cpp, ../aten/src/ATen/RegisterCPU.cpp, ../aten/src/ATen/RegisterCompositeExplicitAutograd.cpp, ../aten/src/ATen/RegisterCompositeImplicitAutograd.cpp, ../aten/src/ATen/RegisterMeta.cpp, ../aten/src/ATen/RegisterMkldnnCPU.cpp, ../aten/src/ATen/RegisterQuantizedCPU.cpp, ../aten/src/ATen/RegisterSchema.cpp, ../aten/src/ATen/RegisterSparseCPU.cpp, ../aten/src/ATen/RegisterSparseCsrCPU.cpp, ../aten/src/ATen/RegistrationDeclarations.h, ../aten/src/ATen/CUDAFunctions.h, ../aten/src/ATen/CUDAFunctions_inl.h, ../aten/src/ATen/RegisterCUDA.cpp, ../aten/src/ATen/RegisterQuantizedCUDA.cpp, ../aten/src/ATen/RegisterSparseCUDA.cpp, ../aten/src/ATen/RegisterSparseCsrCUDA.cpp, ../aten/src/ATen/core/ATenOpList.cpp, ../aten/src/ATen/core/TensorBody.h, ../aten/src/ATen/core/TensorMethods.cpp
[ 70%] Built target __aten_op_header_gen
[ 71%] Generating ../aten/src/ATen/CPUFunctions.h, ../aten/src/ATen/CPUFunctions_inl.h, ../aten/src/ATen/CompositeExplicitAutogradFunctions.h, ../aten/src/ATen/CompositeExplicitAutogradFunctions_inl.h, ../aten/src/ATen/CompositeImplicitAutogradFunctions.h, ../aten/src/ATen/CompositeImplicitAutogradFunctions_inl.h, ../aten/src/ATen/Declarations.yaml, ../aten/src/ATen/Functions.cpp, ../aten/src/ATen/Functions.h, ../aten/src/ATen/MetaFunctions.h, ../aten/src/ATen/MetaFunctions_inl.h, ../aten/src/ATen/NativeFunctions.h, ../aten/src/ATen/NativeMetaFunctions.h, ../aten/src/ATen/Operators.cpp, ../aten/src/ATen/Operators.h, ../aten/src/ATen/RedispatchFunctions.h, ../aten/src/ATen/RegisterBackendSelect.cpp, ../aten/src/ATen/RegisterCPU.cpp, ../aten/src/ATen/RegisterCompositeExplicitAutograd.cpp, ../aten/src/ATen/RegisterCompositeImplicitAutograd.cpp, ../aten/src/ATen/RegisterMeta.cpp, ../aten/src/ATen/RegisterMkldnnCPU.cpp, ../aten/src/ATen/RegisterQuantizedCPU.cpp, ../aten/src/ATen/RegisterSchema.cpp, ../aten/src/ATen/RegisterSparseCPU.cpp, ../aten/src/ATen/RegisterSparseCsrCPU.cpp, ../aten/src/ATen/RegistrationDeclarations.h, ../aten/src/ATen/CUDAFunctions.h, ../aten/src/ATen/CUDAFunctions_inl.h, ../aten/src/ATen/RegisterCUDA.cpp, ../aten/src/ATen/RegisterQuantizedCUDA.cpp, ../aten/src/ATen/RegisterSparseCUDA.cpp, ../aten/src/ATen/RegisterSparseCsrCUDA.cpp, ../aten/src/ATen/core/ATenOpList.cpp, ../aten/src/ATen/core/TensorBody.h, ../aten/src/ATen/core/TensorMethods.cpp
[ 71%] Generating ../aten/src/ATen/CPUFunctions.h, ../aten/src/ATen/CPUFunctions_inl.h, ../aten/src/ATen/CompositeExplicitAutogradFunctions.h, ../aten/src/ATen/CompositeExplicitAutogradFunctions_inl.h, ../aten/src/ATen/CompositeImplicitAutogradFunctions.h, ../aten/src/ATen/CompositeImplicitAutogradFunctions_inl.h, ../aten/src/ATen/Declarations.yaml, ../aten/src/ATen/Functions.cpp, ../aten/src/ATen/Functions.h, ../aten/src/ATen/MetaFunctions.h, ../aten/src/ATen/MetaFunctions_inl.h, ../aten/src/ATen/NativeFunctions.h, ../aten/src/ATen/NativeMetaFunctions.h, ../aten/src/ATen/Operators.cpp, ../aten/src/ATen/Operators.h, ../aten/src/ATen/RedispatchFunctions.h, ../aten/src/ATen/RegisterBackendSelect.cpp, ../aten/src/ATen/RegisterCPU.cpp, ../aten/src/ATen/RegisterCompositeExplicitAutograd.cpp, ../aten/src/ATen/RegisterCompositeImplicitAutograd.cpp, ../aten/src/ATen/RegisterMeta.cpp, ../aten/src/ATen/RegisterMkldnnCPU.cpp, ../aten/src/ATen/RegisterQuantizedCPU.cpp, ../aten/src/ATen/RegisterSchema.cpp, ../aten/src/ATen/RegisterSparseCPU.cpp, ../aten/src/ATen/RegisterSparseCsrCPU.cpp, ../aten/src/ATen/RegistrationDeclarations.h, ../aten/src/ATen/CUDAFunctions.h, ../aten/src/ATen/CUDAFunctions_inl.h, ../aten/src/ATen/RegisterCUDA.cpp, ../aten/src/ATen/RegisterQuantizedCUDA.cpp, ../aten/src/ATen/RegisterSparseCUDA.cpp, ../aten/src/ATen/RegisterSparseCsrCUDA.cpp, ../aten/src/ATen/core/ATenOpList.cpp, ../aten/src/ATen/core/TensorBody.h, ../aten/src/ATen/core/TensorMethods.cpp
[100%] Built target torch_cpu
[100%] Built target torch
```

Indeed (if I'm reading these log lines correctly) it was triggered thrice in this very build.

## To Reproduce

I'm not using `setup.py` but just CMake (through CLion...). My CMake options are

```
-DPYTHON_EXECUTABLE:FILEPATH=/Users/mlevental/opt/miniconda3/envs/pytorch/bin/python -DBUILD_TENSOREXPR_BENCHMARK:BOOL=ON -DBUILD_TEST:BOOL=ON
```
and env variables

```
CMAKE_PREFIX_PATH=/Users/mlevental/opt/miniconda3/envs/pytorch;USE_LLVM=/usr/local/opt/llvm/;LLVM_DIR=/usr/local/opt/llvm/lib/cmake/llvm
```

The only other differences from master is `add_subdirectory(memory_allocator)` at the bottom of `pytorch/CMakeLists.txt` (i.e. a dir with its own CMakeLists.txt where I'm doing some hacking).

## Expected behavior

no changes -> no code regen

## Environment

I'm doing dev (and I haven't installed in my venv) so this doesn't show much

```
(pytorch) mlevental@mlevental-mbp pytorch % python collect_env.py 
Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: macOS 11.4 (x86_64)
GCC version: Could not collect
Clang version: 12.0.0
CMake version: version 3.19.6
Libc version: N/A

Python version: 3.8.10 (default, May 19 2021, 11:01:55)  [Clang 10.0.0 ] (64-bit runtime)
Python platform: macOS-10.16-x86_64-i386-64bit
Is CUDA available: N/A
CUDA runtime version: Could not collect
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.20.2
[conda] blas                      1.0                         mkl  
[conda] mkl                       2021.2.0           hecd8cb5_269  
[conda] mkl-include               2021.2.0           hecd8cb5_269  
[conda] mkl-service               2.3.0            py38h9ed2024_1  
[conda] mkl_fft                   1.3.0            py38h4a7008c_2  
[conda] mkl_random                1.2.1            py38hb2f4e1b_2  
[conda] numpy                     1.20.2           py38h4b4dc7a_0  
[conda] numpy-base                1.20.2           py38he0bd621_0
```

## Additional context

Doing a little debugging I tracked it down to [here](https://github.com/pytorch/pytorch/blob/master/cmake/Codegen.cmake#L244)

```
  add_custom_target(ATEN_CPU_FILES_GEN_TARGET DEPENDS ${generated_cpp} ${core_generated_cpp})
```

The docs on `add_custom_target` claim

>Adds a target with the given name that executes the given commands. The target has no output file and is always considered out of date

but I'm not sure if I'm misunderstanding.

cc @malfet @seemethere @walterddr