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