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