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