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