summaryrefslogtreecommitdiff
path: root/infra/nncc/CMakeLists.txt
blob: 12b84063681c369548a24611a8da2c9a4f1f70ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
cmake_minimum_required(VERSION 3.1)

project(nncc)

enable_testing()

set(CMAKE_CXX_STANDARD 11)

# This feature works with CMake 3.5.2 or later. However, using previous versions does not produce
# an error. We are still officially using CMake 3.1.0, but put this code for the sake of semantic
# support in various development tools.
# Todo: Someday, CMake needs to be updated to 3.7.2 or later to take advantage of improvements
#       such as `cmake-server`.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(NNCC_PROJECT_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../.." CACHE
  INTERNAL "Where to find nncc top-level source directory"
)

set(NNCC_EXTERNALS_DIR
  "${NNCC_PROJECT_SOURCE_DIR}/externals" CACHE
  INTERNAL "Where to download external dependencies"
)
set(NNCC_OVERLAY_DIR "${CMAKE_BINARY_DIR}/overlay" CACHE
    INTERNAL "Where locally built external dependencies are installed")

# This allows find_package to access configurations installed inside overlay
list(APPEND CMAKE_PREFIX_PATH "${NNCC_OVERLAY_DIR}")

macro(nncc_include PREFIX)
  include("${NNCC_PROJECT_SOURCE_DIR}/infra/nncc/cmake/modules/${PREFIX}.cmake")
endmacro(nncc_include)

macro(nncc_find_package PREFIX)
  find_package(${PREFIX} CONFIG NO_DEFAULT_PATH
    PATHS ${NNCC_PROJECT_SOURCE_DIR}/infra/nncc/cmake/packages
    ${ARGN}
  )
endmacro(nncc_find_package)

# nncc_find_resource(NAME) will update the following variables
#
#   NAME_FOUND
#   NAME_DIR
#
# TODO Explain how to add a resource in README.md
function(nncc_find_resource NAME)
  set(RESOURCE_DIR "${NNCC_PROJECT_SOURCE_DIR}/res/${NAME}")

  if(NOT IS_DIRECTORY ${RESOURCE_DIR})
    set(${NAME}_FOUND FALSE PARENT_SCOPE)
    return()
  endif(NOT IS_DIRECTORY ${RESOURCE_DIR})

  set(${NAME}_DIR ${RESOURCE_DIR} PARENT_SCOPE)
  set(${NAME}_FOUND TRUE PARENT_SCOPE)
endfunction(nncc_find_resource)

###
### CMake configuration
###
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Type of build" FORCE)
endif(NOT CMAKE_BUILD_TYPE)
message(STATUS "Use '${CMAKE_BUILD_TYPE}' configuration")

# Prefer -pthread to -lpthread for find_package(Threads ...)
#
# std::thread code compiled only with -lpthread emits the following runtime error (on GCC 4.8.4)
#
#   terminate called after throwing an instance of 'std::system_error'
#     what():  Enable multithreading to use std::thread: Operation not permitted
#
set(THREADS_PREFER_PTHREAD_FLAG TRUE)

###
### Configuration
###
option(DOWNLOAD_PROTOBUF "Download Protocol Buffer source" ON)
option(BUILD_PROTOBUF "Locally build Protocol Buffer from the downloaded source" ON)
option(DOWNLOAD_EIGEN "Download Eigen source" ON)
option(DOWNLOAD_FARMHASH "Download farmhash source" ON)
option(DOWNLOAD_GEMMLOWP "Download GEMM low precesion library source" ON)
option(DOWNLOAD_NEON2SSE "Download NEON2SSE library source" ON)
option(DOWNLOAD_GFLAGS "Download GFlags source" OFF)
option(DOWNLOAD_FLATBUFFERS "Download FlatBuffers source" ON)
option(BUILD_FLATBUFFERS "Locally build Flatbuffers from the downloaded source" ON)
option(DOWNLOAD_TENSORFLOW "Download TensorFlow source" ON)
option(DOWNLOAD_CAFFE "Download Caffe source" ON)
option(DOWNLOAD_PYTORCH "Download Pytorch source" ON)
option(DOWNLOAD_ONNX "Download ONNX source" ON)
option(DOWNLOAD_ABSEIL "Download Abseil-cpp source" ON)

option(DOWNLOAD_GTEST "Download Google Test source" ON)
option(BUILD_GTEST "Build Google Test from the downloaded source" ON)

nncc_find_package(GTest QUIET)

option(ENABLE_TEST "Build Tests using Google Test" ${GTest_FOUND})

if(${ENABLE_TEST} AND NOT ${GTest_FOUND})
  message(FATAL_ERROR "Google Test is required to enable test")
endif(${ENABLE_TEST} AND NOT ${GTest_FOUND})

option(ENABLE_COVERAGE "Build for coverage test" OFF)
if(${ENABLE_COVERAGE} AND NOT ${ENABLE_TEST})
  message(FATAL_ERROR "Test should be enabled to measure test coverage")
endif(${ENABLE_COVERAGE} AND NOT ${ENABLE_TEST})

if(${ENABLE_TEST})
  include(CTest)
endif(${ENABLE_TEST})

option(ENABLE_CONTRIB_BUILD "Build incubating projects under contrib/" ON)
option(ENABLE_STRICT_BUILD "Treat warning as error" OFF)

###
### Target
###
add_library(nncc_common INTERFACE)
if(ENABLE_STRICT_BUILD)
  # TODO Remove -Wno-reoder
  target_compile_options(nncc_common INTERFACE -Werror -Wall -Wextra -Wno-reorder)
endif(ENABLE_STRICT_BUILD)

add_library(nncc_coverage INTERFACE)
if(ENABLE_COVERAGE)
  target_compile_options(nncc_coverage INTERFACE -g -O0 -fprofile-arcs -ftest-coverage)
  target_link_libraries(nncc_coverage INTERFACE gcov)
endif(ENABLE_COVERAGE)

###
### Function
###
# TODO Remove this nncc_include
nncc_include(OptionalTargetTools)
nncc_include(add_subdirectories)

###
### Components
###
if(ENABLE_CONTRIB_BUILD)
  add_subdirectory("${NNCC_PROJECT_SOURCE_DIR}/compiler" "${CMAKE_BINARY_DIR}/compiler")
endif(ENABLE_CONTRIB_BUILD)