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