[neuralnet] check stop_cb by every layer
[platform/core/ml/nntrainer.git] / meson.build
1 project('nntrainer', 'c', 'cpp',
2   version: '0.4.0',
3   license: ['apache-2.0'],
4   meson_version: '>=0.50.0',
5   default_options: [
6     'werror=true',
7     'warning_level=1',
8     'c_std=gnu89',
9     'cpp_std=c++17',
10     'buildtype=release'
11   ]
12 )
13 extra_defines = ['-DMIN_CPP_VERSION=201703L']
14
15 cc = meson.get_compiler('c')
16 cxx = meson.get_compiler('cpp')
17
18 if get_option('platform') == 'tizen'
19   # Pass __TIZEN__ to the compiler
20   add_project_arguments('-D__TIZEN__=1', language:['c','cpp'])
21   add_project_arguments('-DTIZENVERSION=@0@'.format(get_option('tizen-version-major')), language: ['c', 'cpp'])
22   add_project_arguments('-DTIZENVERSIONMINOR=@0@'.format(get_option('tizen-version-minor')), language: ['c', 'cpp'])
23
24   if get_option('enable-tizen-feature-check')
25     add_project_arguments('-D__FEATURE_CHECK_SUPPORT__', language: ['c', 'cpp'])
26   endif
27 endif
28
29 warning_flags = [
30   '-Wredundant-decls',
31   '-Wwrite-strings',
32   '-Wformat',
33   '-Wformat-nonliteral',
34   '-Wformat-security',
35   '-Winit-self',
36   '-Waddress',
37   '-Wno-multichar',
38   '-Wvla',
39   '-Wpointer-arith',
40   '-Wno-error=varargs',
41   '-Wdefaulted-function-deleted',
42   '-ftree-vectorize',
43   '-Wno-unused-variable'
44 ]
45
46 warning_c_flags = [
47   '-Wmissing-declarations',
48   '-Wmissing-include-dirs',
49   '-Wmissing-prototypes',
50   '-Wnested-externs',
51   '-Waggregate-return',
52   '-Wold-style-definition',
53   '-Wdeclaration-after-statement',
54   '-Wno-error=varargs'
55 ]
56
57
58 foreach extra_arg : warning_flags
59   if cc.has_argument (extra_arg)
60     add_project_arguments([extra_arg], language: 'c')
61   endif
62   if cxx.has_argument (extra_arg)
63     add_project_arguments([extra_arg], language: 'cpp')
64   endif
65 endforeach
66
67 foreach extra_arg : warning_c_flags
68   if cc.has_argument (extra_arg)
69     add_project_arguments([extra_arg], language: 'c')
70   endif
71 endforeach
72
73 # Set install path
74 nntrainer_prefix = get_option('prefix')
75 if get_option('platform') != 'android'
76   nntrainer_libdir = nntrainer_prefix / get_option('libdir')
77   nntrainer_bindir = nntrainer_prefix / get_option('bindir')
78   nntrainer_includedir = nntrainer_prefix / get_option('includedir') / 'nntrainer'
79   nntrainer_confdir = get_option('sysconfdir')
80   application_install_dir = nntrainer_bindir / 'applications'
81 else
82   nntrainer_prefix = meson.build_root() / 'android_build_result'
83   # @todo arch has to be option
84   nntrainer_libdir = nntrainer_prefix / 'lib'
85   nntrainer_includedir = nntrainer_prefix / 'include' / 'nntrainer'
86   nntrainer_bindir = nntrainer_prefix / 'bin'
87   nntrainer_confdir = nntrainer_prefix / 'conf'
88   application_install_dir = nntrainer_prefix / 'examples'
89 endif
90
91 # handle resources
92 nntrainer_resdir = meson.build_root() / 'res'
93 run_command('mkdir', '-p', nntrainer_resdir)
94
95 if get_option('install-app')
96 # add a script to install resources from installs to application_install_dir
97 meson.add_install_script(
98   'sh', '-c', 'cp -r @0@ ${DESTDIR}@1@'.format(nntrainer_resdir, application_install_dir)
99 )
100 endif
101
102 # Set default configuration
103 nntrainer_conf = configuration_data()
104 nntrainer_conf.set('VERSION', meson.project_version())
105 nntrainer_conf.set('PREFIX', nntrainer_prefix)
106 nntrainer_conf.set('EXEC_PREFIX', nntrainer_bindir)
107 nntrainer_conf.set('LIB_INSTALL_DIR', nntrainer_libdir)
108 nntrainer_conf.set('PLUGIN_INSTALL_PREFIX', nntrainer_libdir / 'nntrainer')
109 nntrainer_conf.set('INCLUDE_INSTALL_DIR', nntrainer_includedir / '..')
110
111 dummy_dep = dependency('', required: false)
112 found_dummy_dep = declare_dependency() # dummy dep to use if found
113
114 # if ml-api-support is disabled, enable dummy common api interfaces and disable related dependencies.
115 ml_api_common_dep = dependency(get_option('capi-ml-common-actual'), required : get_option('ml-api-support').enabled())
116 nnstreamer_capi_dep = dummy_dep
117 if (ml_api_common_dep.found())
118   nntrainer_conf.set('CAPI_ML_COMMON_DEP', get_option('capi-ml-common-actual'))
119   extra_defines += '-DML_API_COMMON=1'
120
121   nnstreamer_capi_dep = dependency(get_option('capi-ml-inference-actual'), required : true)
122   extra_defines += '-DNNSTREAMER_AVAILABLE=1'
123   # accessing this variable when dep_.not_found() remains hard error on purpose
124   supported_nnstreamer_capi = nnstreamer_capi_dep.version().version_compare('>=1.7.0')
125   if not supported_nnstreamer_capi
126     extra_defines += '-DUNSUPPORTED_NNSTREAMER=1'
127     warning('capi-nnstreamer version is too old, we do not know if it works with older nnstreamer version')
128   endif
129 else
130   nntrainer_conf.set('CAPI_ML_COMMON_DEP', '')
131   extra_defines += '-DML_API_COMMON=0'
132 endif
133 blas_dep = dummy_dep
134 # Dependencies
135 if get_option('enable-cublas')
136   extra_defines += '-DUSE_CUBLAS=1'
137 endif
138
139 if get_option('enable-blas')
140   extra_defines += '-DUSE_BLAS=1'
141
142   if get_option('platform') == 'android'
143     message('preparing blas')
144     run_command(meson.source_root() / 'jni' / 'prepare_openblas.sh', meson.build_root(), check: true)
145     blas_dep = found_dummy_dep
146     blas_root = meson.build_root() / 'openblas'
147   else
148     blas_dep = dependency('openblas')
149   endif
150
151   if blas_dep.found()
152     if get_option('openblas-num-threads') > 0
153       extra_defines += '-DBLAS_NUM_THREADS=@0@'.format(get_option('openblas-num-threads'))
154       message('set openblas num threads=@0@'.format(get_option('openblas-num-threads')))
155     endif
156   endif
157 endif
158
159 extra_defines += '-DNNTR_NUM_THREADS=@0@'.format(get_option('nntr-num-threads'))
160 message('set nntrainer num threads=@0@'.format(get_option('nntr-num-threads')))
161
162 openmp_dep = dummy_dep
163 if get_option('enable-openmp')
164   openmp_dep = dependency('openmp')
165 endif
166
167 if get_option('enable-profile')
168   extra_defines += '-DPROFILE=1'
169 endif
170
171 if get_option('enable-debug')
172   extra_defines += '-DDEBUG=1'
173 endif
174
175 if get_option('use_gym')
176   extra_defines += '-DUSE_GYM=1'
177 endif
178
179 if get_option('enable-logging')
180   extra_defines += '-D__LOGGING__=1'
181 endif
182
183 gmock_dep = dependency('gmock', static: true, main: false, required: false)
184 gtest_dep = dependency('gtest', static: true, main: false, required: false)
185 gtest_main_dep = dependency('gtest', static: true, main: true, required: false)
186
187
188 if get_option('enable-test') # and get_option('platform') != 'android'
189   extra_defines += '-DENABLE_TEST=1'
190   if gtest_dep.version().version_compare('<1.10.0')
191      extra_defines += '-DGTEST_BACKPORT=1'
192   endif
193 endif
194
195 if get_option('reduce-tolerance')
196   extra_defines += '-DREDUCE_TOLERANCE=1'
197 endif
198
199 libm_dep = cxx.find_library('m') # cmath library
200 libdl_dep = cxx.find_library('dl') # DL library
201 thread_dep = dependency('threads') # pthread for tensorflow-lite
202
203 iniparser_dep = dependency('iniparser', required : false, version : '>=4.1') # iniparser
204 if get_option('platform') == 'android'
205   message('preparing iniparser')
206   run_command(meson.source_root() / 'jni' / 'prepare_iniparser.sh', meson.build_root(), check: true)
207   iniparser_root = meson.build_root() / 'iniparser'
208   iniparser_dep = found_dummy_dep
209 endif
210
211 if not iniparser_dep.found()
212   message('falling back to find libiniparser library and header files')
213   libiniparser_dep = cxx.find_library('iniparser')
214   sysroot = run_command(
215     cxx.cmd_array() + ['-print-sysroot']
216     ).stdout().split('\n')[0]
217
218   if sysroot.startswith('/')
219     sysroot_inc_cflags_template = '-I@0@/usr/include@1@'
220     sysroot_inc = sysroot_inc_cflags_template.format(sysroot, '')
221     add_project_arguments(sysroot_inc, language: ['c', 'cpp'])
222     sysroot_inc_cflags_iniparser = sysroot_inc_cflags_template.format(sysroot,
223       '/iniparser')
224   else
225     sysroot_inc_cflags_iniparser = '-I/usr/include/iniparser'
226   endif
227
228   if libiniparser_dep.found() and cxx.has_header('iniparser.h', \
229         args : sysroot_inc_cflags_iniparser)
230     iniparser_dep = declare_dependency (dependencies : libiniparser_dep,
231       compile_args : sysroot_inc_cflags_iniparser)
232   else
233     error('Failed to resolve dependency on iniparser')
234   endif
235 endif
236
237 if get_option('platform') == 'android'
238   message('preparing ml api')
239   run_command(meson.source_root() / 'jni' / 'prepare_ml-api.sh', meson.build_root() / 'ml-api-inference', check: true)
240   ml_api_common_root = meson.build_root() / 'ml-api-inference'
241   ml_api_inc = ml_api_common_root / 'include'
242   meson.add_install_script(
243     'sh', '-c', 'cp @0@ ${DESTDIR}@1@'.format(ml_api_inc / 'ml-api-common.h', nntrainer_includedir)
244   )
245   meson.add_install_script(
246     'sh', '-c', 'cp @0@ ${DESTDIR}@1@'.format(ml_api_inc / 'tizen_error.h', nntrainer_includedir)
247   )
248   ml_api_common_dep = found_dummy_dep
249 endif
250
251 if get_option('enable-nnstreamer-backbone') and get_option('platform') != 'android'
252   extra_defines += '-DENABLE_NNSTREAMER_BACKBONE=1'
253 endif
254
255 tflite_dep = dummy_dep
256
257 if get_option('platform') != 'android'
258   tflite_dep = dependency('tensorflow2-lite', required: false)
259 else
260   if get_option('enable-tflite-backbone') or get_option('enable-tflite-interpreter')
261     message('preparing tflite, because either tflite backbone or interpreter is enabled')
262     run_command(meson.source_root() / 'jni' / 'prepare_tflite.sh', '2.3.0', meson.build_root(), check: true)
263     tflite_root = meson.build_root() / 'tensorflow-2.3.0' / 'tensorflow-lite'
264     tflite_dep = found_dummy_dep
265   endif
266 endif
267
268 if get_option('enable-tflite-backbone')
269   extra_defines += '-DENABLE_TFLITE_BACKBONE=1'
270 endif
271
272 if get_option('enable-tflite-interpreter')
273   extra_defines += '-DENABLE_TFLITE_INTERPRETER=1'
274 endif
275
276 opencv_dep = dummy_dep
277
278 if get_option('platform') != 'android'
279   opencv_dep = dependency('opencv', required: false)
280   if not opencv_dep.found()
281     opencv_dep = dependency('opencv4', required: false)
282     if not opencv_dep.found()
283       opencv_dep = dependency('opencv3', required: false)
284     endif
285   endif
286   if opencv_dep.found()
287     extra_defines += '-DENABLE_DATA_AUGMENTATION_OPENCV=1'
288   endif
289 endif
290 flatc_prog = find_program('flatc', required: false)
291
292 # Install .pc
293 configure_file(input: 'nntrainer.pc.in', output: 'nntrainer.pc',
294   install_dir: nntrainer_libdir / 'pkgconfig',
295   configuration: nntrainer_conf
296 )
297
298 # Install conf
299 configure_file(
300   input: 'nntrainer.ini.in',
301   output: 'nntrainer.ini',
302   install_dir: nntrainer_confdir,
303   configuration: nntrainer_conf
304 )
305 nntrainer_conf_abs_path = get_option('prefix') / nntrainer_confdir / 'nntrainer.ini'
306 message('NNTRAINER_CONF_PATH=@0@'.format(nntrainer_conf_abs_path))
307
308 if get_option('platform') != 'android'
309   extra_defines += '-DNNTRAINER_CONF_PATH="@0@"'.format(nntrainer_conf_abs_path)
310 endif
311
312 message('extra defines are:' + ' '.join(extra_defines))
313 foreach defs: extra_defines
314   add_project_arguments(defs, language: ['c', 'cpp'])
315 endforeach
316
317 # Build nntrainer
318 subdir('nntrainer')
319
320 enable_capi = false
321 enable_ccapi = false
322 # Build api
323 subdir('api')
324
325 if get_option('enable-test')
326   if get_option('platform') == 'android'
327     warning('test is not supported in android build, test skipped')
328   else
329     if gtest_dep.found()
330       subdir('test')
331     else
332       error('test enabled but gtest not found')
333     endif
334   endif
335 endif
336
337 if get_option('enable-app')
338   if get_option('platform') == 'android'
339     warning('android app is not supported for now, building app skipped')
340   else
341     # this is needed for reinforcement application. We can move this to reinforecement app dependency
342     jsoncpp_dep = dependency('jsoncpp') # jsoncpp
343     libcurl_dep = dependency('libcurl')
344     if not tflite_dep.found()
345       error('Tensorflow-Lite dependency not found')
346     endif
347     subdir('Applications')
348   endif
349 endif
350
351 if get_option('enable-nnstreamer-tensor-filter')
352   if get_option('platform') == 'android'
353     warning('android nnstreamer-filter is not yet supported, building nnstreamer-filter skipped')
354   else
355     nnstreamer_dep = dependency('nnstreamer', required: true)
356     subdir('nnstreamer/tensor_filter')
357   endif
358 endif
359
360 if get_option('platform') == 'android'
361   subdir('jni')
362 endif
363
364 if get_option('platform') != 'none'
365   message('building for ' + get_option('platform'))
366 endif