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