0be6885e2e7eafc17d0fe128a036d8aba49e5b05
[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_NEON2SSE "Download NEON2SSE library source" ON)
93 option(DOWNLOAD_GFLAGS "Download GFlags source" OFF)
94 option(DOWNLOAD_FLATBUFFERS "Download FlatBuffers source" ON)
95 option(BUILD_FLATBUFFERS "Locally build Flatbuffers from the downloaded source" ON)
96 option(DOWNLOAD_TENSORFLOW "Download TensorFlow source" ON)
97 option(DOWNLOAD_CAFFE "Download Caffe source" ON)
98 option(DOWNLOAD_PYTORCH "Download Pytorch source" ON)
99 option(DOWNLOAD_ONNX "Download ONNX source" ON)
100 option(DOWNLOAD_ABSEIL "Download Abseil-cpp source" ON)
101 option(DOWNLOAD_PYBIND11 "Download Pybind11 source" ON)
102
103 option(DOWNLOAD_GTEST "Download Google Test source" ON)
104 option(BUILD_GTEST "Build Google Test from the downloaded source" ON)
105 option(DOWNLOAD_HDF5 "Download HDF5 source" ON)
106 option(BUILD_HDF5 "Build HDF5 from the downloaded source" ON)
107
108 nnas_find_package(GTest QUIET)
109
110 option(ENABLE_TEST "Build Tests using Google Test" ${GTest_FOUND})
111
112 if(${ENABLE_TEST} AND NOT ${GTest_FOUND})
113   message(FATAL_ERROR "Google Test is required to enable test")
114 endif(${ENABLE_TEST} AND NOT ${GTest_FOUND})
115
116 option(ENABLE_COVERAGE "Build for coverage test" OFF)
117 if(${ENABLE_COVERAGE} AND NOT ${ENABLE_TEST})
118   message(FATAL_ERROR "Test should be enabled to measure test coverage")
119 endif(${ENABLE_COVERAGE} AND NOT ${ENABLE_TEST})
120
121 if(${ENABLE_TEST})
122   include(CTest)
123 endif(${ENABLE_TEST})
124
125 option(ENABLE_STRICT_BUILD "Treat warning as error" OFF)
126
127 # This option might be turned ON for Windows native build.
128 # Check our ProtobufConfig.cmake for its usage.
129 option(USE_PROTOBUF_LEGACY_IMPORT "Use legacy MODULE mode import rather than CONFIG mode" OFF)
130
131 ###
132 ### Target
133 ###
134 add_library(nncc_common INTERFACE)
135 if(ENABLE_STRICT_BUILD)
136   # TODO Remove -Wno-reoder
137   target_compile_options(nncc_common INTERFACE -Werror -Wall -Wextra -Wno-reorder)
138 endif(ENABLE_STRICT_BUILD)
139
140 add_library(nncc_coverage INTERFACE)
141 if(ENABLE_COVERAGE)
142   target_compile_options(nncc_coverage INTERFACE -g -O0 -fprofile-arcs -ftest-coverage)
143   target_link_libraries(nncc_coverage INTERFACE gcov)
144 endif(ENABLE_COVERAGE)
145
146 ###
147 ### Function
148 ###
149 # TODO Remove this nnas_include
150 nnas_include(OptionalTargetTools)
151 nnas_include(AddSubdirectories)
152
153 add_subdirectory("${NNAS_PROJECT_SOURCE_DIR}/compiler" "${CMAKE_BINARY_DIR}/compiler")