[Meson] Add android nntrainer build
[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
22   if get_option('enable-tizen-feature-check')
23     add_project_arguments('-D__FEATURE_CHECK_SUPPORT__', language: ['c', 'cpp'])
24   endif
25 endif
26
27 warning_flags = [
28   '-Wredundant-decls',
29   '-Wwrite-strings',
30   '-Wformat',
31   '-Wformat-nonliteral',
32   '-Wformat-security',
33   '-Winit-self',
34   '-Waddress',
35   '-Wno-multichar',
36   '-Wvla',
37   '-Wpointer-arith',
38   '-Wno-error=varargs',
39   '-Wdefaulted-function-deleted',
40   '-ftree-vectorize'
41 ]
42
43 warning_c_flags = [
44   '-Wmissing-declarations',
45   '-Wmissing-include-dirs',
46   '-Wmissing-prototypes',
47   '-Wnested-externs',
48   '-Waggregate-return',
49   '-Wold-style-definition',
50   '-Wdeclaration-after-statement',
51   '-Wno-error=varargs'
52 ]
53
54 foreach extra_arg : warning_flags
55   if cc.has_argument (extra_arg)
56     add_project_arguments([extra_arg], language: 'c')
57   endif
58   if cxx.has_argument (extra_arg)
59     add_project_arguments([extra_arg], language: 'cpp')
60   endif
61 endforeach
62
63 foreach extra_arg : warning_c_flags
64   if cc.has_argument (extra_arg)
65     add_project_arguments([extra_arg], language: 'c')
66   endif
67 endforeach
68
69 # Set install path
70 nntrainer_prefix = get_option('prefix')
71 nntrainer_libdir = nntrainer_prefix / get_option('libdir')
72 nntrainer_bindir = nntrainer_prefix / get_option('bindir')
73 nntrainer_includedir = nntrainer_prefix / get_option('includedir')
74 nntrainer_confdir = get_option('sysconfdir')
75 application_install_dir = nntrainer_bindir / 'applications'
76
77 # handle resources
78 nntrainer_resdir = meson.build_root() / 'res'
79 run_command('mkdir', '-p', nntrainer_resdir)
80
81 if get_option('install-app')
82 # add a script to install resources from installs to application_install_dir
83 meson.add_install_script(
84   'sh', '-c', 'cp -r @0@ ${DESTDIR}@1@'.format(nntrainer_resdir, application_install_dir)
85 )
86 endif
87
88 # Set default configuration
89 nntrainer_conf = configuration_data()
90 nntrainer_conf.set('VERSION', meson.project_version())
91 nntrainer_conf.set('PREFIX', nntrainer_prefix)
92 nntrainer_conf.set('EXEC_PREFIX', nntrainer_bindir)
93 nntrainer_conf.set('LIB_INSTALL_DIR', nntrainer_libdir)
94 nntrainer_conf.set('PLUGIN_INSTALL_PREFIX', nntrainer_libdir / 'nntrainer')
95 nntrainer_conf.set('INCLUDE_INSTALL_DIR', nntrainer_includedir)
96 nntrainer_conf.set('CAPI_ML_COMMON_DEP', get_option('capi-ml-common-actual'))
97
98 dummy_dep = dependency('', required: false)
99 found_dummy_dep = declare_dependency() # dummy dep to use if found
100
101 blas_dep = dummy_dep
102 # Dependencies
103 if get_option('enable-cublas')
104   extra_defines += '-DUSE_CUBLAS=1'
105 endif
106
107 if get_option('enable-blas')
108   extra_defines += '-DUSE_BLAS=1'
109   if get_option('platform') == 'tizen' or get_option('platform') == 'yocto'
110     blas_dep = dependency('openblas')
111   elif get_option('platform') == 'android'
112     message('preparing blas')
113     run_command(meson.source_root() / 'jni' / 'prepare_openblas.sh', meson.build_root(), check: true)
114     blas_dep = found_dummy_dep
115     blas_root = meson.build_root() / 'openblas'
116   else
117     blas_dep = dependency('blas-openblas', required:false)
118     # for Ubuntu 20.04
119     if not blas_dep.found()
120       blas_dep = dependency('openblas')
121     endif
122   endif
123 endif
124
125 openmp_dep = dummy_dep
126 if get_option('enable-openmp')
127   openmp_dep = dependency('openmp')
128 endif
129
130 if get_option('enable-profile')
131   extra_defines += '-DPROFILE=1'
132 endif
133
134 if get_option('enable-debug')
135   extra_defines += '-DDEBUG=1'
136 endif
137
138 if get_option('use_gym')
139   extra_defines += '-DUSE_GYM=1'
140 endif
141
142 if get_option('enable-logging')
143   extra_defines += '-D__LOGGING__=1'
144 endif
145
146 if get_option('enable-test') and get_option('platform') != 'android'
147   extra_defines += '-DENABLE_TEST=1'
148 endif
149
150 if get_option('reduce-tolerance')
151   extra_defines += '-DREDUCE_TOLERANCE=1'
152 endif
153
154 libm_dep = cxx.find_library('m') # cmath library
155 libdl_dep = cxx.find_library('dl') # DL library
156 thread_dep = dependency('threads') # pthread for tensorflow-lite
157
158 iniparser_dep = dependency('iniparser', required : false, version : '>=4.1') # iniparser
159 if get_option('platform') == 'android'
160   message('preparing iniparser')
161   run_command(meson.source_root() / 'jni' / 'prepare_iniparser.sh', meson.build_root(), check: true)
162   iniparser_root = meson.build_root() / 'iniparser'
163   iniparser_dep = found_dummy_dep
164 endif
165
166 if not iniparser_dep.found()
167   message('falling back to find libiniparser library and header files')
168   libiniparser_dep = cxx.find_library('iniparser')
169   sysroot = run_command(
170     cxx.cmd_array() + ['-print-sysroot']
171     ).stdout().split('\n')[0]
172
173   if sysroot.startswith('/')
174     sysroot_inc_cflags_template = '-I@0@/usr/include@1@'
175     sysroot_inc = sysroot_inc_cflags_template.format(sysroot, '')
176     add_project_arguments(sysroot_inc, language: ['c', 'cpp'])
177     sysroot_inc_cflags_iniparser = sysroot_inc_cflags_template.format(sysroot,
178       '/iniparser')
179   else
180     sysroot_inc_cflags_iniparser = '-I/usr/include/iniparser'
181   endif
182
183   if libiniparser_dep.found() and cxx.has_header('iniparser.h', \
184         args : sysroot_inc_cflags_iniparser)
185     iniparser_dep = declare_dependency (dependencies : libiniparser_dep,
186       compile_args : sysroot_inc_cflags_iniparser)
187   else
188     error('Failed to resolve dependency on iniparser')
189   endif
190 endif
191
192 nnstreamer_capi_dep = dependency(get_option('capi-ml-inference-actual'), required:false)
193 if nnstreamer_capi_dep.found()
194   extra_defines += '-DNNSTREAMER_AVAILABLE=1'
195   # accessing this variable when dep_.not_found() remains hard error on purpose
196   supported_nnstreamer_capi = nnstreamer_capi_dep.version().version_compare('>=1.7.0')
197   if not supported_nnstreamer_capi
198     extra_defines += '-DUNSUPPORTED_NNSTREAMER=1'
199     warning('capi-nnstreamer version is too old, we do not know if it works with older nnstreamer version')
200   endif
201 endif
202
203 ml_api_common_dep = dummy_dep
204
205 if get_option('platform') != 'android'
206   ml_api_common_dep = dependency(get_option('capi-ml-common-actual'), required: true)
207 else
208   message('preparing ml api')
209   run_command(meson.source_root() / 'jni' / 'prepare_ml-api.sh', meson.build_root() / 'ml-api-inference', check: true)
210   ml_api_common_root = meson.build_root() / 'ml-api-inference'
211   ml_api_common_dep = found_dummy_dep
212 endif
213
214 if get_option('enable-nnstreamer-backbone')
215   extra_defines += '-DENABLE_NNSTREAMER_BACKBONE=1'
216 endif
217
218 tflite_dep = dummy_dep
219
220 if get_option('platform') != 'android'
221   tflite_dep = dependency('tensorflow2-lite', required: false)
222 else
223   message('preparing tflite')
224   run_command(meson.source_root() / 'jni' / 'prepare_tflite.sh', '2.3.0', meson.build_root(), check: true)
225   tflite_root = meson.build_root() / 'tensorflow-2.3.0' / 'tensorflow-lite'
226   tflite_dep = found_dummy_dep
227 endif
228
229 if get_option('enable-tflite-backbone')
230   extra_defines += '-DENABLE_TFLITE_BACKBONE=1'
231 endif
232
233 if get_option('enable-tflite-interpreter')
234   extra_defines += '-DENABLE_TFLITE_INTERPRETER=1'
235 endif
236
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 opencv_dep = dummy_dep
241
242 if get_option('platform') != 'android'
243   opencv_dep = dependency('opencv', required: false)
244   if not opencv_dep.found()
245     opencv_dep = dependency('opencv4', required: false)
246     if not opencv_dep.found()
247       opencv_dep = dependency('opencv3', required: false)
248     endif
249   endif
250   if opencv_dep.found()
251     extra_defines += '-DENABLE_DATA_AUGMENTATION_OPENCV=1'
252   endif
253 endif
254 flatc_prog = find_program('flatc', required: false)
255
256 # Install .pc
257 configure_file(input: 'nntrainer.pc.in', output: 'nntrainer.pc',
258   install_dir: nntrainer_libdir / 'pkgconfig',
259   configuration: nntrainer_conf
260 )
261
262 # Install conf
263 configure_file(
264   input: 'nntrainer.ini.in',
265   output: 'nntrainer.ini',
266   install_dir: nntrainer_confdir,
267   configuration: nntrainer_conf
268 )
269 nntrainer_conf_abs_path = get_option('prefix') / nntrainer_confdir / 'nntrainer.ini'
270 message('NNTRAINER_CONF_PATH=@0@'.format(nntrainer_conf_abs_path))
271
272 if get_option('platform') != 'android'
273   extra_defines += '-DNNTRAINER_CONF_PATH="@0@"'.format(nntrainer_conf_abs_path)
274 endif
275
276 message('extra defines are:' + ' '.join(extra_defines))
277 foreach defs: extra_defines
278   add_project_arguments(defs, language: ['c', 'cpp'])
279 endforeach
280
281 # Build nntrainer
282 subdir('nntrainer')
283
284 # Build api
285 subdir('api')
286
287 if get_option('enable-test')
288   if get_option('platform') == 'android'
289     warning('test is not supported in android build, test skipped')
290   else
291     if gtest_dep.found()
292       subdir('test')
293     else
294       error('test enabled but gtest not found')
295     endif
296   endif
297 endif
298
299 if get_option('enable-app')
300   if get_option('platform') == 'android'
301     warning('android app is not supported for now, building app skipped')
302   else
303     jsoncpp_dep = dependency('jsoncpp') # jsoncpp
304     libcurl_dep = dependency('libcurl')
305     if not tflite_dep.found()
306       error('Tensorflow-Lite dependency not found')
307     endif
308     subdir('Applications')
309   endif
310 endif
311
312 if get_option('enable-nnstreamer-tensor-filter')
313   if get_option('platform') == 'android'
314     warning('android nnstreamer-filter is not yet supported, building nnstreamer-filter skipped')
315   else
316     nnstreamer_dep = dependency('nnstreamer', required: true)
317     subdir('nnstreamer/tensor_filter')
318   endif
319 endif
320
321 if get_option('enable-android')
322
323   ndk_build = find_program('ndk-build', required : true)
324   jni_root = meson.current_source_dir() / 'jni'
325   jni_build_root = meson.current_build_dir() / 'jni'
326
327   ndk_args = {
328     'NDK_PROJECT_PATH': jni_root,
329     'APP_BUILD_SCRIPT': jni_root / 'Android.mk',
330     'NDK_APPLICATION_MK': jni_root / 'Application.mk',
331     'NDK_LIBS_OUT': jni_build_root / 'libs',
332     'NDK_OUT': jni_build_root / 'objs',
333     'ENABLE_PROFILE': get_option('enable-profile') ? 1 : 0,
334     'ENABLE_BLAS': get_option('enable-blas') ? 1 : 0,
335   }
336
337   num_threads = run_command('grep', '-c', '^processor', '/proc/cpuinfo').stdout().strip()
338   message('num processor are: ' + num_threads)
339
340   thread_opt_flag = '-j' + num_threads
341
342   ndk_additional_flags = [thread_opt_flag]
343
344   ndk_build_command = [ndk_build]
345   foreach key, val : ndk_args
346     ndk_build_command += '@0@=@1@'.format(key, val)
347   endforeach
348   ndk_build_command += ndk_additional_flags
349
350   android_build_target = custom_target('android',
351     output: 'jni',
352     build_by_default: true,
353     command: ndk_build_command
354   )
355
356 endif
357
358 if get_option('platform') == 'android'
359   subdir('jni')
360 endif
361
362 if get_option('platform') != 'none'
363   message('building for ' + get_option('platform'))
364 endif