[Coverity] Fix coverity issue
[platform/core/ml/nntrainer.git] / meson.build
1 project('nntrainer', 'c', 'cpp',
2   version: '0.5.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   '-Wvla',
38   '-Wpointer-arith',
39   '-Wno-error=varargs',
40   '-Wdefaulted-function-deleted',
41   '-ftree-vectorize',
42   '-Wno-unused-variable',
43   '-Wno-deprecated-declarations',
44   '-Wno-array-bounds',
45   '-Wno-maybe-uninitialized',
46   '-Wno-unused-result'
47 ]
48
49 warning_c_flags = [
50   '-Wmissing-declarations',
51   '-Wmissing-include-dirs',
52   '-Wmissing-prototypes',
53   '-Wnested-externs',
54   '-Waggregate-return',
55   '-Wold-style-definition',
56   '-Wdeclaration-after-statement',
57   '-Wno-error=varargs'
58 ]
59
60
61 if get_option('enable-fp16')
62    arch = target_machine.cpu_family()
63    if get_option('platform') == 'android'
64      add_project_arguments('-mfp16-format=ieee', language: ['c', 'cpp'])
65      extra_defines += '-DENABLE_FP16=1'
66      extra_defines += '-DUSE__FP16=1'
67    elif arch == 'aarch64' or arch =='arm'
68      extra_defines += '-DENABLE_FP16=1'
69      extra_defines += '-DUSE__FP16=1'
70    else
71      has_avx512fp16 = cc.has_argument('-mavx512fp16')
72      if (has_avx512fp16)
73        # add_project_arguments(['-mavx512fp16'], language: ['c','cpp'])
74        message ('Float16 for x86_64 enabled. Modern gcc-x64 genrally supports float16 with _Float16. -mavx512fp16 added for hardware acceleration')
75        extra_defines += '-DENABLE_FP16=1'
76      else
77        warning ('Float16 for x86_64 enabled. However, software emulation is applied for fp16, making it slower and inconsistent. Use GCC 12+ for AVX512 FP16 support. This build will probably fail unless you bring a compiler that supports fp16 for x64.')
78      endif
79    endif  
80 endif
81     
82 foreach extra_arg : warning_flags
83   if cc.has_argument (extra_arg)
84     add_project_arguments([extra_arg], language: 'c')
85   endif
86   if cxx.has_argument (extra_arg)
87     add_project_arguments([extra_arg], language: 'cpp')
88   endif
89 endforeach
90
91 foreach extra_arg : warning_c_flags
92   if cc.has_argument (extra_arg)
93     add_project_arguments([extra_arg], language: 'c')
94   endif
95 endforeach
96
97 # Set install path
98 nntrainer_prefix = get_option('prefix')
99 if get_option('platform') != 'android'
100   nntrainer_libdir = nntrainer_prefix / get_option('libdir')
101   nntrainer_bindir = nntrainer_prefix / get_option('bindir')
102   nntrainer_includedir = nntrainer_prefix / get_option('includedir') / 'nntrainer'
103   nntrainer_confdir = get_option('sysconfdir')
104   application_install_dir = nntrainer_bindir / 'applications'
105   nntrainer_swapdir = '/tmp'
106 else
107   nntrainer_prefix = meson.build_root() / 'android_build_result'
108   # @todo arch has to be option
109   nntrainer_libdir = nntrainer_prefix / 'lib'
110   nntrainer_includedir = nntrainer_prefix / 'include' / 'nntrainer'
111   nntrainer_bindir = nntrainer_prefix / 'bin'
112   nntrainer_confdir = nntrainer_prefix / 'conf'
113   application_install_dir = nntrainer_prefix / 'examples'
114   nntrainer_swapdir = '/data/local/tmp'
115 endif
116
117 # handle swap options
118 if get_option('enable-memory-swap')
119   nntrainer_enable_swap = 'true'
120 else
121   nntrainer_enable_swap = 'false'
122 endif
123
124 if get_option('memory-swap-path') != ''
125   nntrainer_swapdir = get_option('memory-swap-path')
126 endif
127
128 # handle resources
129 nntrainer_resdir = meson.build_root() / 'res'
130 run_command('mkdir', '-p', nntrainer_resdir)
131
132 if get_option('install-app')
133 # add a script to install resources from installs to application_install_dir
134 meson.add_install_script(
135   'sh', '-c', 'cp -r @0@ ${DESTDIR}@1@'.format(nntrainer_resdir, application_install_dir)
136 )
137 endif
138
139 # Set default configuration
140 nntrainer_conf = configuration_data()
141 nntrainer_conf.set('VERSION', meson.project_version())
142 nntrainer_conf.set('PREFIX', nntrainer_prefix)
143 nntrainer_conf.set('EXEC_PREFIX', nntrainer_bindir)
144 nntrainer_conf.set('LIB_INSTALL_DIR', nntrainer_libdir)
145 nntrainer_conf.set('PLUGIN_INSTALL_PREFIX', nntrainer_libdir / 'nntrainer')
146 nntrainer_conf.set('INCLUDE_INSTALL_DIR', nntrainer_includedir / '..')
147 nntrainer_conf.set('MEMORY_SWAP', nntrainer_enable_swap)
148 nntrainer_conf.set('MEMORY_SWAP_PATH', nntrainer_swapdir)
149
150 dummy_dep = dependency('', required: false)
151 found_dummy_dep = declare_dependency() # dummy dep to use if found
152
153 # if ml-api-support is disabled, enable dummy common api interfaces and disable related dependencies.
154 ml_api_common_dep = dependency(get_option('capi-ml-common-actual'), required : get_option('ml-api-support').enabled())
155 nnstreamer_capi_dep = dummy_dep
156 if (ml_api_common_dep.found())
157   nntrainer_conf.set('CAPI_ML_COMMON_DEP', get_option('capi-ml-common-actual'))
158   extra_defines += '-DML_API_COMMON=1'
159
160   nnstreamer_capi_dep = dependency(get_option('capi-ml-inference-actual'), required : true)
161   extra_defines += '-DNNSTREAMER_AVAILABLE=1'
162   # accessing this variable when dep_.not_found() remains hard error on purpose
163   supported_nnstreamer_capi = nnstreamer_capi_dep.version().version_compare('>=1.7.0')
164   if not supported_nnstreamer_capi
165     extra_defines += '-DUNSUPPORTED_NNSTREAMER=1'
166     warning('capi-nnstreamer version is too old, we do not know if it works with older nnstreamer version')
167   endif
168 else
169   nntrainer_conf.set('CAPI_ML_COMMON_DEP', '')
170   extra_defines += '-DML_API_COMMON=0'
171 endif
172 blas_dep = dummy_dep
173 # Dependencies
174 if get_option('enable-cublas')
175   extra_defines += '-DUSE_CUBLAS=1'
176 endif
177
178 if get_option('enable-blas')
179   extra_defines += '-DUSE_BLAS=1'
180
181   if get_option('platform') == 'android'
182     message('preparing blas')
183     run_command(meson.source_root() / 'jni' / 'prepare_openblas.sh', meson.build_root(), check: true)
184     blas_dep = found_dummy_dep
185     blas_root = meson.build_root() / 'openblas'
186   else
187     blas_dep = dependency('openblas')
188   endif
189
190   if blas_dep.found()
191     if get_option('openblas-num-threads') > 0
192       extra_defines += '-DBLAS_NUM_THREADS=@0@'.format(get_option('openblas-num-threads'))
193       message('set openblas num threads=@0@'.format(get_option('openblas-num-threads')))
194     endif
195   endif
196 endif
197
198 extra_defines += '-DNNTR_NUM_THREADS=@0@'.format(get_option('nntr-num-threads'))
199 message('set nntrainer num threads=@0@'.format(get_option('nntr-num-threads')))
200
201 openmp_dep = dummy_dep
202 if get_option('enable-openmp')
203   openmp_dep = dependency('openmp')
204 endif
205
206 if get_option('enable-profile')
207   extra_defines += '-DPROFILE=1'
208 endif
209
210 if get_option('enable-trace')
211   extra_defines += '-DTRACE=1'
212 endif
213
214 if get_option('enable-debug')
215   extra_defines += '-DDEBUG=1'
216 endif
217
218 if get_option('use_gym')
219   extra_defines += '-DUSE_GYM=1'
220 endif
221
222 if get_option('enable-logging')
223   extra_defines += '-D__LOGGING__=1'
224 endif
225
226 gmock_dep = dependency('gmock', static: true, main: false, required: false)
227 gtest_dep = dependency('gtest', static: true, main: false, required: false)
228 gtest_main_dep = dependency('gtest', static: true, main: true, required: false)
229
230
231 if get_option('enable-test') # and get_option('platform') != 'android'
232   extra_defines += '-DENABLE_TEST=1'
233   if gtest_dep.version().version_compare('<1.10.0')
234      extra_defines += '-DGTEST_BACKPORT=1'
235   endif
236   test_timeout = get_option('test-timeout')
237 endif
238
239 if get_option('reduce-tolerance')
240   extra_defines += '-DREDUCE_TOLERANCE=1'
241 endif
242
243 libm_dep = cxx.find_library('m') # cmath library
244 libdl_dep = cxx.find_library('dl') # DL library
245 thread_dep = dependency('threads') # pthread for tensorflow-lite
246
247 iniparser_dep = dependency('iniparser', required : false, version : '>=4.1') # iniparser
248 if get_option('platform') == 'android'
249   message('preparing iniparser')
250   run_command(meson.source_root() / 'jni' / 'prepare_iniparser.sh', meson.build_root(), check: true)
251   iniparser_root = meson.build_root() / 'iniparser'
252   iniparser_dep = found_dummy_dep
253 endif
254
255 if not iniparser_dep.found()
256   message('falling back to find libiniparser library and header files')
257   libiniparser_dep = cxx.find_library('iniparser')
258   sysroot = run_command(
259     cxx.cmd_array() + ['-print-sysroot']
260     ).stdout().split('\n')[0]
261
262   if sysroot.startswith('/')
263     sysroot_inc_cflags_template = '-I@0@/usr/include@1@'
264     sysroot_inc = sysroot_inc_cflags_template.format(sysroot, '')
265     add_project_arguments(sysroot_inc, language: ['c', 'cpp'])
266     sysroot_inc_cflags_iniparser = sysroot_inc_cflags_template.format(sysroot,
267       '/iniparser')
268   else
269     sysroot_inc_cflags_iniparser = '-I/usr/include/iniparser'
270   endif
271
272   if libiniparser_dep.found() and cxx.has_header('iniparser.h', \
273         args : sysroot_inc_cflags_iniparser)
274     iniparser_dep = declare_dependency (dependencies : libiniparser_dep,
275       compile_args : sysroot_inc_cflags_iniparser)
276   else
277     error('Failed to resolve dependency on iniparser')
278   endif
279 endif
280
281 if get_option('platform') == 'android'
282   message('preparing ml api')
283   run_command(meson.source_root() / 'jni' / 'prepare_ml-api.sh', meson.build_root() / 'ml-api-inference', check: true)
284   ml_api_common_root = meson.build_root() / 'ml-api-inference'
285   ml_api_inc = ml_api_common_root / 'include'
286   meson.add_install_script(
287     'sh', '-c', 'cp @0@ ${DESTDIR}@1@'.format(ml_api_inc / 'ml-api-common.h', nntrainer_includedir)
288   )
289   meson.add_install_script(
290     'sh', '-c', 'cp @0@ ${DESTDIR}@1@'.format(ml_api_inc / 'tizen_error.h', nntrainer_includedir)
291   )
292   ml_api_common_dep = found_dummy_dep
293 endif
294
295 if get_option('enable-nnstreamer-backbone') and get_option('platform') != 'android'
296   extra_defines += '-DENABLE_NNSTREAMER_BACKBONE=1'
297 endif
298
299 tflite_dep = dummy_dep
300
301 if get_option('platform') != 'android'
302   tflite_dep = dependency('tensorflow2-lite', required: false)
303 else
304   if get_option('enable-tflite-backbone') or get_option('enable-tflite-interpreter')
305     message('preparing tflite, because either tflite backbone or interpreter is enabled')
306     run_command(meson.source_root() / 'jni' / 'prepare_tflite.sh', '2.3.0', meson.build_root(), check: true)
307     tflite_root = meson.build_root() / 'tensorflow-2.3.0' / 'tensorflow-lite'
308     tflite_dep = found_dummy_dep
309   endif
310 endif
311
312 if get_option('enable-tflite-backbone')
313   extra_defines += '-DENABLE_TFLITE_BACKBONE=1'
314 endif
315
316 if get_option('enable-tflite-interpreter')
317   extra_defines += '-DENABLE_TFLITE_INTERPRETER=1'
318 endif
319
320 opencv_dep = dummy_dep
321
322 if get_option('platform') != 'android'
323   opencv_dep = dependency('opencv', required: false)
324   if not opencv_dep.found()
325     opencv_dep = dependency('opencv4', required: false)
326     if not opencv_dep.found()
327       opencv_dep = dependency('opencv3', required: false)
328     endif
329   endif
330   if opencv_dep.found()
331     extra_defines += '-DENABLE_DATA_AUGMENTATION_OPENCV=1'
332   endif
333 endif
334 flatc_prog = find_program('flatc', required: false)
335
336 # Install .pc
337 configure_file(input: 'nntrainer.pc.in', output: 'nntrainer.pc',
338   install_dir: nntrainer_libdir / 'pkgconfig',
339   configuration: nntrainer_conf
340 )
341
342 # Install conf
343 configure_file(
344   input: 'nntrainer.ini.in',
345   output: 'nntrainer.ini',
346   install_dir: nntrainer_confdir,
347   configuration: nntrainer_conf
348 )
349 nntrainer_conf_abs_path = get_option('prefix') / nntrainer_confdir / 'nntrainer.ini'
350 message('NNTRAINER_CONF_PATH=@0@'.format(nntrainer_conf_abs_path))
351
352 if get_option('platform') != 'android'
353   extra_defines += '-DNNTRAINER_CONF_PATH="@0@"'.format(nntrainer_conf_abs_path)
354 endif
355
356 message('extra defines are:' + ' '.join(extra_defines))
357 foreach defs: extra_defines
358   add_project_arguments(defs, language: ['c', 'cpp'])
359 endforeach
360
361 # Build nntrainer
362 subdir('nntrainer')
363
364 enable_capi = false
365 enable_ccapi = false
366 # Build api
367 subdir('api')
368
369 if get_option('enable-test')
370   if get_option('platform') == 'android'
371     warning('test is not supported in android build, test skipped')
372   else
373     if gtest_dep.found()
374       subdir('test')
375     else
376       error('test enabled but gtest not found')
377     endif
378   endif
379 endif
380
381 if get_option('enable-app')
382   if get_option('platform') == 'android'
383     warning('android app is not supported for now, building app skipped')
384   else
385     # this is needed for reinforcement application. We can move this to reinforecement app dependency
386     jsoncpp_dep = dependency('jsoncpp') # jsoncpp
387     libcurl_dep = dependency('libcurl')
388     if not tflite_dep.found()
389       error('Tensorflow-Lite dependency not found')
390     endif
391     subdir('Applications')
392   endif
393 endif
394
395 if get_option('platform') != 'android'
396   nnstreamer_dep = dependency('nnstreamer')
397   message('building nnstreamer')
398   subdir('nnstreamer')
399 else
400   warning('android nnstreamer-filter and nnstreamer-trainer are not yet supported, building them is skipped')
401 endif
402
403 if get_option('platform') == 'android'
404   subdir('jni')
405 endif
406
407 if get_option('platform') != 'none'
408   message('building for ' + get_option('platform'))
409 endif