blob: 5a0195c14e9cd0d1dc1affda205e721a1c9b9cae [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001cmake_minimum_required (VERSION 3.4)
2
Tai Lya4d748b2023-03-28 22:06:56 +00003# Copyright (c) 2020-2023, 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
Matthew Sloyanba5fad32022-09-26 13:31:43 +010063 src/func_debug.cc
Georgios Pinitas41df4282023-05-30 12:20:31 +010064 src/graph_node.cc
65 src/model_runner_impl.cc
66 src/model_runner.cc
Grant Watson64285a12022-11-16 15:32:39 +000067 src/operators.cc
Georgios Pinitas41df4282023-05-30 12:20:31 +010068 src/subgraph_traverser.cc
69 src/tensor.cc
70 src/verify.cc
Matthew Sloyanba5fad32022-09-26 13:31:43 +010071 src/ops/op_factory.cc
72 src/ops/tensor_ops.cc
73 src/ops/activation_funcs.cc
74 src/ops/ewise_binary.cc
75 src/ops/ewise_unary.cc
76 src/ops/ewise_ternary.cc
77 src/ops/comparison.cc
78 src/ops/reduction.cc
79 src/ops/data_layout.cc
80 src/ops/scatter_gather.cc
81 src/ops/image.cc
82 src/ops/type_conversion.cc
83 src/ops/data_nodes.cc
84 src/ops/custom.cc
85 src/ops/control_flow.cc
Eric Kunzee5e26762020-10-13 16:11:07 -070086)
87
Matthew Sloyanba5fad32022-09-26 13:31:43 +010088# Build TOSA Reference Model library
89add_library(tosa_reference_model_lib ${CXX_SOURCE})
Eric Kunzee5e26762020-10-13 16:11:07 -070090
Matthew Sloyanba5fad32022-09-26 13:31:43 +010091target_include_directories(tosa_reference_model_lib
Eric Kunzee5e26762020-10-13 16:11:07 -070092 PUBLIC
93 $<INSTALL_INTERFACE:include>
94 $<BUILD_INTERFACE:${CMAKE_CURRENT_SRC_DIR}/include>
95 PRIVATE
96 ${CMAKE_CURRENT_SOURCE_DIR}/src
97 ${FLATBUFFERS_DIR}/include
Matthew Sloyanba5fad32022-09-26 13:31:43 +010098 ${EIGEN_DIR}
99 ${EIGEN_DIR}/unsupported/
Kevin Cheng550ccc52021-03-03 11:21:43 -0800100 ${SERIALIZATION_DIR}/include
James Ward8b390432022-08-12 20:48:56 +0100101 ${HALF_DIR}/include
Eric Kunzee5e26762020-10-13 16:11:07 -0700102)
103
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100104target_link_libraries(tosa_reference_model_lib
Eric Kunzee5e26762020-10-13 16:11:07 -0700105 PRIVATE
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100106 ${SERIALIZATION_LIB}
Eric Kunzee5e26762020-10-13 16:11:07 -0700107)
108
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100109set(PUBLIC_HEADERS)
110list(APPEND PUBLIC_HEADERS
111 include/debug_modes.def
112 include/debug_types.h
Tai Lya4d748b2023-03-28 22:06:56 +0000113 include/dtype.h
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100114 include/func_config.h
115 include/func_debug.h
116 include/graph_status.h
117 include/model_common.h
118 include/model_runner.h
Georgios Pinitas41df4282023-05-30 12:20:31 +0100119 include/verify.h
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100120 include/version.h
121)
122
123set_target_properties(tosa_reference_model_lib PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}")
124
125# Build TOSA Refererence Model executable
126if(BUILD_TOSA_REFERENCE_MODEL_EXECUTABLE)
127 set(CXX_SOURCE_EX src/main.cpp)
128 list(APPEND CXX_SOURCE_EX ${CXX_SOURCE})
129
130 add_executable(tosa_reference_model ${CXX_SOURCE_EX})
131
132 target_include_directories(tosa_reference_model
133 PUBLIC
134 $<INSTALL_INTERFACE:include>
135 $<BUILD_INTERFACE:${CMAKE_CURRENT_SRC_DIR}/include>
136 PRIVATE
137 ${CMAKE_CURRENT_SOURCE_DIR}/src
138 ${FLATBUFFERS_DIR}/include
139 ${EIGEN_DIR}
140 ${EIGEN_DIR}/unsupported/
141 ${SERIALIZATION_DIR}/include
James Ward8b390432022-08-12 20:48:56 +0100142 ${HALF_DIR}/include
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100143 )
144
145 target_link_libraries(tosa_reference_model
146 PRIVATE
147 ${SERIALIZATION_LIB}
148 nlohmann_json::nlohmann_json
149 cxxopts
150 )
151
152 install(TARGETS tosa_reference_model DESTINATION bin)
153endif()
154
155if(BUILD_TOSA_REFERENCE_MODEL_TESTS)
156 # Set definition so unit tests can find examples directory.
157 add_definitions(-DPROJECT_ROOT=\"${CMAKE_CURRENT_SOURCE_DIR}/\")
158
159 # Set doctest location if not specified.
160 if(NOT DOCTEST_DIR)
161 set(DOCTEST_DIR "../thirdparty/doctest/doctest")
162 endif()
163
164 # Sources only required for unit tests.
165 set(CXX_SOURCE_TESTS
166 test/model_runner_tests.cpp
Georgios Pinitas41df4282023-05-30 12:20:31 +0100167 test/verify_tests.cpp
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100168 ${DOCTEST_DIR}/doctest.h
169 )
170
171 list(APPEND CXX_SOURCE_TESTS ${CXX_SOURCE})
172
173 add_executable(unit_tests ${CXX_SOURCE_TESTS})
174
175 target_include_directories(unit_tests
176 PUBLIC
177 $<INSTALL_INTERFACE:include>
178 $<BUILD_INTERFACE:${CMAKE_CURRENT_SRC_DIR}/include>
179 PRIVATE
180 ${CMAKE_CURRENT_SOURCE_DIR}/src
181 ${FLATBUFFERS_DIR}/include
182 ${EIGEN_DIR}
183 ${EIGEN_DIR}/unsupported/
184 ${SERIALIZATION_DIR}/include
James Ward8b390432022-08-12 20:48:56 +0100185 ${HALF_DIR}/include
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100186 ${DOCTEST_DIR}
187 )
188
189 target_link_libraries(unit_tests
190 PRIVATE
191 ${SERIALIZATION_LIB}
192 )
193endif()
194
195if(BUILD_MODEL_RUNNER_SAMPLE)
196 # Set definition so sample executable can find examples directory.
197 add_definitions(-DPROJECT_ROOT=\"${CMAKE_CURRENT_SOURCE_DIR}/\")
198
199 # Sources only required for example executable.
200 set(CXX_SOURCE_SAMPLE
201 samples/model_runner_simple_sample.cpp
202 )
203
204 list(APPEND CXX_SOURCE_SAMPLE ${CXX_SOURCE})
205
206 add_executable(model_runner_sample ${CXX_SOURCE_SAMPLE})
207
208 target_include_directories(model_runner_sample
209 PUBLIC
210 $<INSTALL_INTERFACE:include>
211 $<BUILD_INTERFACE:${CMAKE_CURRENT_SRC_DIR}/include>
212 PRIVATE
213 ${CMAKE_CURRENT_SOURCE_DIR}/src
214 ${FLATBUFFERS_DIR}/include
215 ${EIGEN_DIR}
216 ${EIGEN_DIR}/unsupported/
217 ${SERIALIZATION_DIR}/include
James Ward8b390432022-08-12 20:48:56 +0100218 ${HALF_DIR}/include
Matthew Sloyanba5fad32022-09-26 13:31:43 +0100219 )
220
221 target_link_libraries(model_runner_sample
222 PRIVATE
223 ${SERIALIZATION_LIB}
224 )
225endif()
226
227# Follow GNU packaging norms for installation directory structure.
228include(GNUInstallDirs)
229install(
230 TARGETS tosa_reference_model_lib EXPORT TosaReferenceModelLibTargets
231 PUBLIC_HEADER
232 ARCHIVE
233)
234
235install(EXPORT TosaReferenceModelLibTargets
236 FILE TosaReferenceModelLibTargets.cmake
237 NAMESPACE TosaReference::
238 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/tosa_reference_model_lib"
239)