Repository: pytorch
Issue: Build torch as a submodule with static linking doesn't work (CAFFE2_PERF_WITH_AVX2 is not defined)
Labels: caffe2,module: static linking

## Issue description

Hi,

I'm trying to compile a simple C++ program using the Caffe2 library. I include the pytorch repository as a git submodule to my project because I want to make this project as self-conteined as I can (the systems I want to target doesn't have the caffe2 lib installed).

The file layout of my project is the following:
```
.
├── build
├── CMakeLists.txt
├── pytorch
│   ├── aten
│   ├── binaries
│   ├── caffe
│   ├── caffe2
│   ├── CITATION
│   ├── cmake
│   ├── CMakeLists.txt
│   ├── CODEOWNERS
│   ├── conda
│   ├── CONTRIBUTING.md
│   ├── docker
│   ├── docs
│   ├── LICENSE
│   ├── Makefile
│   ├── modules
│   ├── mypy-files.txt
│   ├── mypy.ini
│   ├── mypy-README.md
│   ├── NOTICE
│   ├── README.md
│   ├── requirements.txt
│   ├── scripts
│   ├── setup_caffe2.py
│   ├── setup.py
│   ├── test
│   ├── third_party
│   ├── tools
│   ├── torch
│   └── tox.ini
├── README.md
├── src
│   └── test.cpp
```

I'm using the following CMakeLists.txt file:

```
cmake_minimum_required(VERSION 2.6)

project (caffe2_cpp_tutorial)

find_package(Protobuf REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Threads)

set(ALL_LIBRARIES)

set(BUILD_PYTHON OFF)
add_subdirectory(pytorch caffe2_build)
list(APPEND ALL_LIBRARIES caffe2)

find_library(GLOG_LIB glog)
find_library(GFLAGS_LIB gflags)

if(OpenCV_LIBS)
  include_directories(${OpenCV_INCLUDE_DIRS})
  list(APPEND ALL_LIBRARIES ${OpenCV_LIBS})
  add_definitions(-DWITH_OPENCV)
endif()

set(CMAKE_CXX_STANDARD 11)

add_executable(test src/test.cpp)

target_link_libraries(test ${ALL_LIBRARIES} ${GLOG_LIB} ${GFLAGS_LIB} ${PROTOBUF_LIBRARY})
```

I couldn't find any CMakeLists.txt file example to do this, nor any documentation about it, so I cannibalized different CMakeLists.txt files hopping something functional would form from it.

I can now successfully generate the build scripts with these cmake arguments:

```
$ mkdir build
$ cd build
$ cmake .. -DUSE_CUDA=OFF -DUSE_NATIVE_ARCH=ON
```

But when compiling I get this build error:
```
/home/giachero/repos/livdet-caffe2/pytorch/caffe2/perfkernels/common_avx.cc:17:2: error: #error ( "You found a build system error: __AVX__ is defined (via e.g. -mavx) " "but CAFFE2_PERF_WITH_AVX is not defined.");
 #error( \
  ^~~~~
/home/giachero/repos/livdet-caffe2/pytorch/caffe2/perfkernels/common_avx2.cc:17:2: error: #error ( "You found a build system error: __AVX2__ is defined (via e.g. -mavx2) " "but CAFFE2_PERF_WITH_AVX2 is not defined.");
 #error( \
  ^~~~~
caffe2_build/caffe2/perfkernels/CMakeFiles/Caffe2_perfkernels_avx.dir/build.make:62: recipe for target 'caffe2_build/caffe2/perfkernels/CMakeFiles/Caffe2_perfkernels_avx.dir/common_avx.cc.o' failed
make[2]: *** [caffe2_build/caffe2/perfkernels/CMakeFiles/Caffe2_perfkernels_avx.dir/common_avx.cc.o] Error 1
make[2]: *** Waiting for unfinished jobs....
caffe2_build/caffe2/perfkernels/CMakeFiles/Caffe2_perfkernels_avx2.dir/build.make:62: recipe for target 'caffe2_build/caffe2/perfkernels/CMakeFiles/Caffe2_perfkernels_avx2.dir/common_avx2.cc.o' failed
make[2]: *** [caffe2_build/caffe2/perfkernels/CMakeFiles/Caffe2_perfkernels_avx2.dir/common_avx2.cc.o] Error 1
make[2]: *** Waiting for unfinished jobs....
[ 47%] Linking CXX static library ../../../lib/libonnx.a
[ 47%] Built target onnx
CMakeFiles/Makefile2:2105: recipe for target 'caffe2_build/caffe2/perfkernels/CMakeFiles/Caffe2_perfkernels_avx.dir/all' failed
make[1]: *** [caffe2_build/caffe2/perfkernels/CMakeFiles/Caffe2_perfkernels_avx.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 47%] Linking CXX static library ../../../../lib/libnomnigraph.a
[ 47%] Built target nomnigraph
CMakeFiles/Makefile2:2143: recipe for target 'caffe2_build/caffe2/perfkernels/CMakeFiles/Caffe2_perfkernels_avx2.dir/all' failed
make[1]: *** [caffe2_build/caffe2/perfkernels/CMakeFiles/Caffe2_perfkernels_avx2.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
```

My searches return nothing useful, any hints?


Thanks!
Augusto.

## System Info

- Ubuntu 17.04 amd64
- gcc (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406:
- CMake version: 3.7.2
- pytorch: 3efdece9daade24630c72ebb7b17502134995196