cmake: generate macros.h with configure_file()

Summary:
Using file(WRITE) caused the file to be rewritten for every CMake
reconfigure, which was causing unnecessary full rebuilds of the project
even when no source files changed.

The new strategy has the added benefit of enforcing that the macros.h file
is always generated correctly. When the main project relies on this
header for macro definitions (instead of relying on add_definitions()),
we can be more confident that the project will build correctly when used
as a library (which is the whole point of the macros.h file).

Upsides:
* No more unnecessary rebuilds
* Higher confidence that the project will compile properly as a third-party library

Downsides:
* Developers need to add an entry to `macros.h.in` whenever they would have added a new definition with `add_definitions()`
Closes https://github.com/caffe2/caffe2/pull/1103

Differential Revision: D5680367

Pulled By: Yangqing

fbshipit-source-id: 4db29c28589efda1b6a3f5f88752e3984260a0f2
This commit is contained in:
Luke Yeager
2017-08-22 14:09:53 -07:00
committed by Facebook Github Bot
parent 5748e7140f
commit c1356216a2
6 changed files with 41 additions and 24 deletions

View File

@ -91,20 +91,9 @@ if (Caffe2_EXTERNAL_DEPENDENCIES)
endif()
# Write the macros file.
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/caffe2/core)
file(WRITE ${PROJECT_BINARY_DIR}/caffe2/core/macros.h
"// Automatically generated header file for caffe2 macros. These \n"
"// macros are used to build the Caffe2 binary, and if you are \n"
"// building a dependent library, they will need to be set as well \n"
"// for your program to link correctly.\n\n"
"#pragma once\n\n")
get_directory_property(tmp DIRECTORY ${PROJECT_SOURCE_DIR} COMPILE_DEFINITIONS)
foreach(item ${tmp})
if (${item} MATCHES "CAFFE2.*")
file(APPEND ${PROJECT_BINARY_DIR}/caffe2/core/macros.h
"\n#ifndef ${item}\n#define ${item}\n#endif // ${item}\n")
endif()
endforeach()
configure_file(
${PROJECT_SOURCE_DIR}/caffe2/core/macros.h.in
${PROJECT_BINARY_DIR}/caffe2/core/macros.h)
# Installing the header files
install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}

24
caffe2/core/macros.h.in Normal file
View File

@ -0,0 +1,24 @@
// Automatically generated header file for caffe2 macros. These
// macros are used to build the Caffe2 binary, and if you are
// building a dependent library, they will need to be set as well
// for your program to link correctly.
#pragma once
#cmakedefine CAFFE2_ANDROID
#cmakedefine CAFFE2_FORCE_FALLBACK_CUDA_MPI
#cmakedefine CAFFE2_HAS_MKL_DNN
#cmakedefine CAFFE2_HAS_MKL_SGEMM_PACK
#cmakedefine CAFFE2_PERF_WITH_AVX
#cmakedefine CAFFE2_PERF_WITH_AVX2
#cmakedefine CAFFE2_THREADPOOL_MAIN_IMBALANCE
#cmakedefine CAFFE2_THREADPOOL_STATS
#cmakedefine CAFFE2_UNIQUE_LONG_TYPEMETA
#cmakedefine CAFFE2_USE_ACCELERATE
#cmakedefine CAFFE2_USE_EIGEN_FOR_BLAS
#cmakedefine CAFFE2_USE_FBCODE
#cmakedefine CAFFE2_USE_GFLAGS
#cmakedefine CAFFE2_USE_GOOGLE_GLOG
#cmakedefine CAFFE2_USE_LITE_PROTO
#cmakedefine CAFFE2_USE_MKL
#cmakedefine CAFFE2_USE_NVTX

View File

@ -2,6 +2,8 @@
// example, if your compiler did not specify -mavx, you should not provide
// the CAFFE2_PERF_WITH_AVX macro.
#include "caffe2/core/common.h"
#ifdef CAFFE2_PERF_WITH_AVX
#ifndef __AVX__
#error( \

View File

@ -2,6 +2,8 @@
// example, if your compiler did not specify -mavx2, you should not provide
// the CAFFE2_PERF_WITH_AVX2 macro.
#include "caffe2/core/common.h"
#ifdef CAFFE2_PERF_WITH_AVX2
#ifndef __AVX2__
#error( \

View File

@ -14,7 +14,7 @@ endif()
# ---[ protobuf
if(USE_LITE_PROTO)
add_definitions(-DCAFFE2_USE_LITE_PROTO)
set(CAFFE2_USE_LITE_PROTO 1)
endif()
# ---[ BLAS
@ -24,7 +24,7 @@ message(STATUS "The BLAS backend of choice:" ${BLAS})
if(BLAS STREQUAL "Eigen")
# Eigen is header-only and we do not have any dependent libraries
add_definitions(-DCAFFE2_USE_EIGEN_FOR_BLAS)
set(CAFFE2_USE_EIGEN_FOR_BLAS 1)
elseif(BLAS STREQUAL "ATLAS")
find_package(Atlas REQUIRED)
include_directories(SYSTEM ${ATLAS_INCLUDE_DIRS})
@ -39,7 +39,7 @@ elseif(BLAS STREQUAL "MKL")
find_package(MKL REQUIRED)
include_directories(SYSTEM ${MKL_INCLUDE_DIR})
list(APPEND Caffe2_DEPENDENCY_LIBS ${MKL_LIBRARIES})
add_definitions(-DCAFFE2_USE_MKL)
set(CAFFE2_USE_MKL 1)
elseif(BLAS STREQUAL "vecLib")
find_package(vecLib REQUIRED)
include_directories(SYSTEM ${vecLib_INCLUDE_DIR})
@ -64,7 +64,7 @@ endif()
if(USE_GLOG)
include("cmake/External/glog.cmake")
if(GLOG_FOUND)
add_definitions(-DCAFFE2_USE_GOOGLE_GLOG)
set(CAFFE2_USE_GOOGLE_GLOG 1)
include_directories(SYSTEM ${GLOG_INCLUDE_DIRS})
list(APPEND Caffe2_DEPENDENCY_LIBS ${GLOG_LIBRARIES})
else()
@ -77,7 +77,7 @@ endif()
if(USE_GFLAGS)
include("cmake/External/gflags.cmake")
if(GFLAGS_FOUND)
add_definitions(-DCAFFE2_USE_GFLAGS)
set(CAFFE2_USE_GFLAGS 1)
include_directories(SYSTEM ${GFLAGS_INCLUDE_DIRS})
list(APPEND Caffe2_DEPENDENCY_LIBS ${GFLAGS_LIBRARIES})
else()
@ -198,7 +198,7 @@ if(USE_FFMPEG)
endif()
# ---[ EIGEN
add_definitions(-DEIGEN_MPL2_ONLY)
set(EIGEN_MPL2_ONLY 1)
find_package(Eigen3 QUIET)
if(EIGEN3_FOUND)
include_directories(SYSTEM ${EIGEN3_INCLUDE_DIRS})
@ -251,7 +251,7 @@ if(USE_MPI)
message(STATUS "Found OpenMPI with CUDA support built.")
else()
message(WARNING "OpenMPI found, but it is not built with CUDA support.")
add_definitions(-DCAFFE2_FORCE_FALLBACK_CUDA_MPI)
set(CAFFE2_FORCE_FALLBACK_CUDA_MPI 1)
endif()
endif()
else()

View File

@ -17,7 +17,7 @@ if (CAFFE2_LONG_IS_INT32_OR_64)
message(STATUS "Does not need to define long separately.")
else()
message(STATUS "Need to define long as a separate typeid.")
add_definitions(-DCAFFE2_UNIQUE_LONG_TYPEMETA)
set(CAFFE2_UNIQUE_LONG_TYPEMETA 1)
endif()
@ -62,8 +62,8 @@ if (CAFFE2_COMPILER_SUPPORTS_AVX2_EXTENSIONS)
# in msvc.
# Also see CMakeLists.txt under caffe2/perfkernels.
if (NOT MSVC)
add_definitions(-DCAFFE2_PERF_WITH_AVX)
add_definitions(-DCAFFE2_PERF_WITH_AVX2)
set(CAFFE2_PERF_WITH_AVX 1)
set(CAFFE2_PERF_WITH_AVX2 1)
endif()
endif()