Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / infra / nncc / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.1)
2
3 project(nncc)
4
5 enable_testing()
6
7 set(CMAKE_CXX_STANDARD 14)
8
9 set(CMAKE_SKIP_BUILD_RPATH FALSE)
10 set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
11 set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib:$ORIGIN/")
12 set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
13
14 # This feature works with CMake 3.5.2 or later. However, using previous versions does not produce
15 # an error. We are still officially using CMake 3.1.0, but put this code for the sake of semantic
16 # support in various development tools.
17 # Todo: Someday, CMake needs to be updated to 3.7.2 or later to take advantage of improvements
18 #       such as `cmake-server`.
19 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
20
21 set(NNAS_PROJECT_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../.." CACHE
22   INTERNAL "Where to find nnas top-level source directory"
23 )
24
25 set(NNAS_EXTERNALS_DIR
26   "${NNAS_PROJECT_SOURCE_DIR}/externals" CACHE
27   INTERNAL "Where to download external dependencies"
28 )
29 set(NNCC_OVERLAY_DIR "${CMAKE_BINARY_DIR}/overlay" CACHE
30     INTERNAL "Where locally built external dependencies are installed")
31
32 # Share package build script with runtime
33 set(EXT_OVERLAY_DIR ${NNCC_OVERLAY_DIR})
34
35 # This allows find_package to access configurations installed inside overlay
36 list(APPEND CMAKE_PREFIX_PATH "${EXT_OVERLAY_DIR}")
37
38 macro(nnas_include PREFIX)
39   include("${NNAS_PROJECT_SOURCE_DIR}/infra/cmake/modules/${PREFIX}.cmake")
40 endmacro(nnas_include)
41
42 macro(nnas_find_package PREFIX)
43   find_package(${PREFIX} CONFIG NO_DEFAULT_PATH
44     PATHS ${NNAS_PROJECT_SOURCE_DIR}/infra/cmake/packages
45     ${ARGN}
46   )
47 endmacro(nnas_find_package)
48
49 # nncc_find_resource(NAME) will update the following variables
50 #
51 #   NAME_FOUND
52 #   NAME_DIR
53 #
54 # TODO Explain how to add a resource in README.md
55 function(nncc_find_resource NAME)
56   set(RESOURCE_DIR "${NNAS_PROJECT_SOURCE_DIR}/res/${NAME}")
57
58   if(NOT IS_DIRECTORY ${RESOURCE_DIR})
59     set(${NAME}_FOUND FALSE PARENT_SCOPE)
60     return()
61   endif(NOT IS_DIRECTORY ${RESOURCE_DIR})
62
63   set(${NAME}_DIR ${RESOURCE_DIR} PARENT_SCOPE)
64   set(${NAME}_FOUND TRUE PARENT_SCOPE)
65 endfunction(nncc_find_resource)
66
67 ###
68 ### CMake configuration
69 ###
70 if(NOT CMAKE_BUILD_TYPE)
71   set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Type of build" FORCE)
72 endif(NOT CMAKE_BUILD_TYPE)
73 message(STATUS "Use '${CMAKE_BUILD_TYPE}' configuration")
74
75 # Prefer -pthread to -lpthread for find_package(Threads ...)
76 #
77 # std::thread code compiled only with -lpthread emits the following runtime error (on GCC 4.8.4)
78 #
79 #   terminate called after throwing an instance of 'std::system_error'
80 #     what():  Enable multithreading to use std::thread: Operation not permitted
81 #
82 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
83
84 ###
85 ### Configuration
86 ###
87 option(DOWNLOAD_PROTOBUF "Download Protocol Buffer source" ON)
88 option(BUILD_PROTOBUF "Locally build Protocol Buffer from the downloaded source" ON)
89 option(DOWNLOAD_EIGEN "Download Eigen source" ON)
90 option(DOWNLOAD_FARMHASH "Download farmhash source" ON)
91 option(DOWNLOAD_GEMMLOWP "Download GEMM low precesion library source" ON)
92 option(DOWNLOAD_RUY "Download ruy source" ON)
93 option(DOWNLOAD_NEON2SSE "Download NEON2SSE library source" ON)
94 option(DOWNLOAD_GFLAGS "Download GFlags source" OFF)
95 option(DOWNLOAD_FLATBUFFERS "Download FlatBuffers source" ON)
96 option(BUILD_FLATBUFFERS "Locally build Flatbuffers from the downloaded source" ON)
97 option(DOWNLOAD_TENSORFLOW "Download TensorFlow source" ON)
98 option(DOWNLOAD_CAFFE "Download Caffe source" ON)
99 option(DOWNLOAD_PYTORCH "Download Pytorch source" ON)
100 option(DOWNLOAD_ONNX "Download ONNX source" ON)
101 option(DOWNLOAD_ABSEIL "Download Abseil-cpp source" ON)
102 option(DOWNLOAD_PYBIND11 "Download Pybind11 source" ON)
103
104 option(DOWNLOAD_GTEST "Download Google Test source" ON)
105 option(BUILD_GTEST "Build Google Test from the downloaded source" ON)
106 option(DOWNLOAD_HDF5 "Download HDF5 source" ON)
107 option(BUILD_HDF5 "Build HDF5 from the downloaded source" ON)
108
109 nnas_find_package(GTest QUIET)
110
111 option(ENABLE_TEST "Build Tests using Google Test" ${GTest_FOUND})
112
113 if(${ENABLE_TEST} AND NOT ${GTest_FOUND})
114   message(FATAL_ERROR "Google Test is required to enable test")
115 endif(${ENABLE_TEST} AND NOT ${GTest_FOUND})
116
117 option(ENABLE_COVERAGE "Build for coverage test" OFF)
118 if(${ENABLE_COVERAGE} AND NOT ${ENABLE_TEST})
119   message(FATAL_ERROR "Test should be enabled to measure test coverage")
120 endif(${ENABLE_COVERAGE} AND NOT ${ENABLE_TEST})
121
122 if(${ENABLE_TEST})
123   include(CTest)
124 endif(${ENABLE_TEST})
125
126 option(ENABLE_STRICT_BUILD "Treat warning as error" OFF)
127
128 # This option might be turned ON for Windows native build.
129 # Check our ProtobufConfig.cmake for its usage.
130 option(USE_PROTOBUF_LEGACY_IMPORT "Use legacy MODULE mode import rather than CONFIG mode" OFF)
131
132 ###
133 ### Target
134 ###
135 add_library(nncc_common INTERFACE)
136 if(ENABLE_STRICT_BUILD)
137   # TODO Remove -Wno-reoder
138   target_compile_options(nncc_common INTERFACE -Werror -Wall -Wextra -Wno-reorder)
139 endif(ENABLE_STRICT_BUILD)
140
141 add_library(nncc_coverage INTERFACE)
142 if(ENABLE_COVERAGE)
143   target_compile_options(nncc_coverage INTERFACE -g -O0 -fprofile-arcs -ftest-coverage)
144   target_link_libraries(nncc_coverage INTERFACE gcov)
145 endif(ENABLE_COVERAGE)
146
147 ###
148 ### Function
149 ###
150 # TODO Remove this nnas_include
151 nnas_include(OptionalTargetTools)
152 nnas_include(AddSubdirectories)
153
154 add_subdirectory("${NNAS_PROJECT_SOURCE_DIR}/compiler" "${CMAKE_BINARY_DIR}/compiler")