blob: 04b0db53aa36aa2470ccbdda81d812d2afd710ab [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001cmake_minimum_required (VERSION 3.4)
2
Matthew Sloyanba5fad32022-09-26 13:31:43 +01003# Copyright (c) 2020-2022, ARM Limited.
Eric Kunzee5e26762020-10-13 16:11:07 -07004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Eric Kunzee5e26762020-10-13 16:11:07 -070017project(tosa_reference_model LANGUAGES CXX)
18
19set(CMAKE_CXX_STANDARD 17)
20set(CMAKE_CXX_STANDARD_REQUIRED ON)
21
22if(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
23 set(CMAKE_CXX_FLAGS "-Wall -Wno-ignored-attributes -Wno-format-truncation")
24else()
25 set(CMAKE_CXX_FLAGS "-Wall -Wno-ignored-attributes")
26endif()
27
Matthew Sloyanba5fad32022-09-26 13:31:43 +010028# If Serialization Library path is specified, look for library so it doesn't have to be built again.
29# Otherwise, set the Serialization Library related paths to thirdparty directory.
30if(SERIALIZATION_DIR)
31 find_library(SERIALIZATION_LIB
32 NAMES libtosa_serialization_lib.a tosa_serialization_lib
33 NO_DEFAULT_PATH
34 HINTS ${SERIALIZATION_DIR}
35 PATH_SUFFIXES lib)
Eric Kunzee5e26762020-10-13 16:11:07 -070036
Matthew Sloyanba5fad32022-09-26 13:31:43 +010037 if(NOT SERIALIZATION_LIB)
38 message(FATAL_ERROR "TOSA Serialization Library location was specified but not found at: ${SERIALIZATION_LIB_DIR}")
39 endif()
40else()
41 # Build from third party directory if not found.
42 set(SERIALIZATION_LIB tosa_serialization_lib)
43 set(SERIALIZATION_DIR "../thirdparty/serialization_lib/")
44endif()
45
James Ward8b390432022-08-12 20:48:56 +010046# If Flatbuffers, Eigen, Half path isn't specified, set to thirdparty directory.
Matthew Sloyanba5fad32022-09-26 13:31:43 +010047if(NOT FLATBUFFERS_DIR)
48 set(FLATBUFFERS_DIR "../thirdparty/serialization_lib/third_party/flatbuffers/")
49endif()
50
51if(NOT EIGEN_DIR)
52 set(EIGEN_DIR "../thirdparty/eigen/")
53endif()
54
James Ward8b390432022-08-12 20:48:56 +010055if(NOT HALF_DIR)
56 set(HALF_DIR "../thirdparty/serialization_lib/third_party/half")
57endif()
58
Matthew Sloyanba5fad32022-09-26 13:31:43 +010059include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
60
61# Common sources required for TOSA Reference Model library, executable and unit tests
62set(CXX_SOURCE
63 src/model_runner.cc
64 src/model_runner_impl.cc
65 src/tensor.cc
66 src/graph_node.cc
67 src/subgraph_traverser.cc
68 src/func_debug.cc
69 src/ops/op_factory.cc
70 src/ops/tensor_ops.cc
71 src/ops/activation_funcs.cc
72 src/ops/ewise_binary.cc
73 src/ops/ewise_unary.cc
74 src/ops/ewise_ternary.cc
75 src/ops/comparison.cc
76 src/ops/reduction.cc
77 src/ops/data_layout.cc
78 src/ops/scatter_gather.cc
79 src/ops/image.cc
80 src/ops/type_conversion.cc
81 src/ops/data_nodes.cc
82 src/ops/custom.cc
83 src/ops/control_flow.cc
Eric Kunzee5e26762020-10-13 16:11:07 -070084)
85
Matthew Sloyanba5fad32022-09-26 13:31:43 +010086# Build TOSA Reference Model library
87add_library(tosa_reference_model_lib ${CXX_SOURCE})
Eric Kunzee5e26762020-10-13 16:11:07 -070088
Matthew Sloyanba5fad32022-09-26 13:31:43 +010089target_include_directories(tosa_reference_model_lib
Eric Kunzee5e26762020-10-13 16:11:07 -070090 PUBLIC
91 $<INSTALL_INTERFACE:include>
92 $<BUILD_INTERFACE:${CMAKE_CURRENT_SRC_DIR}/include>
93 PRIVATE
94 ${CMAKE_CURRENT_SOURCE_DIR}/src
95 ${FLATBUFFERS_DIR}/include
Matthew Sloyanba5fad32022-09-26 13:31:43 +010096 ${EIGEN_DIR}
97 ${EIGEN_DIR}/unsupported/
Kevin Cheng550ccc52021-03-03 11:21:43 -080098 ${SERIALIZATION_DIR}/include
James Ward8b390432022-08-12 20:48:56 +010099 ${HALF_DIR}/include
Eric Kunzee5e26762020-10-13 16:11:07 -0700100)
101
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100102target_link_libraries(tosa_reference_model_lib
Eric Kunzee5e26762020-10-13 16:11:07 -0700103 PRIVATE
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100104 ${SERIALIZATION_LIB}
Eric Kunzee5e26762020-10-13 16:11:07 -0700105)
106
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100107set(PUBLIC_HEADERS)
108list(APPEND PUBLIC_HEADERS
109 include/debug_modes.def
110 include/debug_types.h
111 include/func_config.h
112 include/func_debug.h
113 include/graph_status.h
114 include/model_common.h
115 include/model_runner.h
116 include/version.h
117)
118
119set_target_properties(tosa_reference_model_lib PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}")
120
121# Build TOSA Refererence Model executable
122if(BUILD_TOSA_REFERENCE_MODEL_EXECUTABLE)
123 set(CXX_SOURCE_EX src/main.cpp)
124 list(APPEND CXX_SOURCE_EX ${CXX_SOURCE})
125
126 add_executable(tosa_reference_model ${CXX_SOURCE_EX})
127
128 target_include_directories(tosa_reference_model
129 PUBLIC
130 $<INSTALL_INTERFACE:include>
131 $<BUILD_INTERFACE:${CMAKE_CURRENT_SRC_DIR}/include>
132 PRIVATE
133 ${CMAKE_CURRENT_SOURCE_DIR}/src
134 ${FLATBUFFERS_DIR}/include
135 ${EIGEN_DIR}
136 ${EIGEN_DIR}/unsupported/
137 ${SERIALIZATION_DIR}/include
James Ward8b390432022-08-12 20:48:56 +0100138 ${HALF_DIR}/include
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100139 )
140
141 target_link_libraries(tosa_reference_model
142 PRIVATE
143 ${SERIALIZATION_LIB}
144 nlohmann_json::nlohmann_json
145 cxxopts
146 )
147
148 install(TARGETS tosa_reference_model DESTINATION bin)
149endif()
150
151if(BUILD_TOSA_REFERENCE_MODEL_TESTS)
152 # Set definition so unit tests can find examples directory.
153 add_definitions(-DPROJECT_ROOT=\"${CMAKE_CURRENT_SOURCE_DIR}/\")
154
155 # Set doctest location if not specified.
156 if(NOT DOCTEST_DIR)
157 set(DOCTEST_DIR "../thirdparty/doctest/doctest")
158 endif()
159
160 # Sources only required for unit tests.
161 set(CXX_SOURCE_TESTS
162 test/model_runner_tests.cpp
163 ${DOCTEST_DIR}/doctest.h
164 )
165
166 list(APPEND CXX_SOURCE_TESTS ${CXX_SOURCE})
167
168 add_executable(unit_tests ${CXX_SOURCE_TESTS})
169
170 target_include_directories(unit_tests
171 PUBLIC
172 $<INSTALL_INTERFACE:include>
173 $<BUILD_INTERFACE:${CMAKE_CURRENT_SRC_DIR}/include>
174 PRIVATE
175 ${CMAKE_CURRENT_SOURCE_DIR}/src
176 ${FLATBUFFERS_DIR}/include
177 ${EIGEN_DIR}
178 ${EIGEN_DIR}/unsupported/
179 ${SERIALIZATION_DIR}/include
James Ward8b390432022-08-12 20:48:56 +0100180 ${HALF_DIR}/include
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100181 ${DOCTEST_DIR}
182 )
183
184 target_link_libraries(unit_tests
185 PRIVATE
186 ${SERIALIZATION_LIB}
187 )
188endif()
189
190if(BUILD_MODEL_RUNNER_SAMPLE)
191 # Set definition so sample executable can find examples directory.
192 add_definitions(-DPROJECT_ROOT=\"${CMAKE_CURRENT_SOURCE_DIR}/\")
193
194 # Sources only required for example executable.
195 set(CXX_SOURCE_SAMPLE
196 samples/model_runner_simple_sample.cpp
197 )
198
199 list(APPEND CXX_SOURCE_SAMPLE ${CXX_SOURCE})
200
201 add_executable(model_runner_sample ${CXX_SOURCE_SAMPLE})
202
203 target_include_directories(model_runner_sample
204 PUBLIC
205 $<INSTALL_INTERFACE:include>
206 $<BUILD_INTERFACE:${CMAKE_CURRENT_SRC_DIR}/include>
207 PRIVATE
208 ${CMAKE_CURRENT_SOURCE_DIR}/src
209 ${FLATBUFFERS_DIR}/include
210 ${EIGEN_DIR}
211 ${EIGEN_DIR}/unsupported/
212 ${SERIALIZATION_DIR}/include
James Ward8b390432022-08-12 20:48:56 +0100213 ${HALF_DIR}/include
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100214 )
215
216 target_link_libraries(model_runner_sample
217 PRIVATE
218 ${SERIALIZATION_LIB}
219 )
220endif()
221
222# Follow GNU packaging norms for installation directory structure.
223include(GNUInstallDirs)
224install(
225 TARGETS tosa_reference_model_lib EXPORT TosaReferenceModelLibTargets
226 PUBLIC_HEADER
227 ARCHIVE
228)
229
230install(EXPORT TosaReferenceModelLibTargets
231 FILE TosaReferenceModelLibTargets.cmake
232 NAMESPACE TosaReference::
233 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/tosa_reference_model_lib"
234)