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