[mesonbuild] update meson build
[platform/core/ml/nntrainer.git] / meson.build
1 project('nntrainer', 'c', 'cpp',
2   version: '0.0.1',
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 add_project_arguments('-DMIN_CPP_VERSION=201703L', language:['c','cpp'])
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   '-Wno-error=deprecated-declarations',
40   '-Wdefaulted-function-deleted',
41   '-ftree-vectorize'
42 ]
43
44 warning_c_flags = [
45   '-Wmissing-declarations',
46   '-Wmissing-include-dirs',
47   '-Wmissing-prototypes',
48   '-Wnested-externs',
49   '-Waggregate-return',
50   '-Wold-style-definition',
51   '-Wdeclaration-after-statement',
52   '-Wno-error=varargs'
53 ]
54
55 foreach extra_arg : warning_flags
56   if cc.has_argument (extra_arg)
57     add_project_arguments([extra_arg], language: 'c')
58   endif
59   if cxx.has_argument (extra_arg)
60     add_project_arguments([extra_arg], language: 'cpp')
61   endif
62 endforeach
63
64 foreach extra_arg : warning_c_flags
65   if cc.has_argument (extra_arg)
66     add_project_arguments([extra_arg], language: 'c')
67   endif
68 endforeach
69
70 # Set install path
71 nntrainer_prefix = get_option('prefix')
72 nntrainer_libdir = nntrainer_prefix / get_option('libdir')
73 nntrainer_bindir = nntrainer_prefix / get_option('bindir')
74 nntrainer_includedir = nntrainer_prefix / get_option('includedir')
75 nntrainer_confdir = get_option('sysconfdir')
76 application_install_dir = nntrainer_bindir / 'applications'
77
78 # handle resources
79 nntrainer_resdir = meson.build_root() / 'res'
80 run_command('mkdir', '-p', nntrainer_resdir)
81
82 if get_option('install-app')
83 # add a script to install resources from installs to application_install_dir
84 meson.add_install_script(
85   'sh', '-c', 'cp -r @0@ ${DESTDIR}@1@'.format(nntrainer_resdir, application_install_dir)
86 )
87 endif
88
89 # Set default configuration
90 nntrainer_conf = configuration_data()
91 nntrainer_conf.set('VERSION', meson.project_version())
92 nntrainer_conf.set('PREFIX', nntrainer_prefix)
93 nntrainer_conf.set('EXEC_PREFIX', nntrainer_bindir)
94 nntrainer_conf.set('LIB_INSTALL_DIR', nntrainer_libdir)
95 nntrainer_conf.set('PLUGIN_INSTALL_PREFIX', nntrainer_libdir / 'nntrainer')
96 nntrainer_conf.set('INCLUDE_INSTALL_DIR', nntrainer_includedir)
97 nntrainer_conf.set('CAPI_ML_COMMON_DEP', get_option('capi-ml-common-actual'))
98
99 dummy_dep = dependency('', required: false)
100
101 blas_dep = dummy_dep
102 # Dependencies
103 if get_option('enable-cublas')
104    add_project_arguments('-DUSE_CUBLAS=1', language:['c','cpp'])
105 endif
106
107 if get_option('enable-blas')
108   add_project_arguments('-DUSE_BLAS=1', language:['c','cpp'])
109   if get_option('platform') == 'tizen' or get_option('platform') == 'yocto'
110     blas_dep = dependency('openblas')
111   else
112     blas_dep = dependency('blas-openblas', required:false)
113     # for Ubuntu 20.04
114     if not blas_dep.found()
115       blas_dep = dependency('openblas')
116     endif
117   endif
118 endif
119
120 if get_option('enable-profile')
121   add_project_arguments('-DPROFILE=1', language:['c', 'cpp'])
122 endif
123
124 if get_option('use_gym')
125    add_project_arguments('-DUSE_GYM=1', language:['c','cpp'])
126 endif
127
128 if get_option('enable-logging')
129    add_project_arguments('-D__LOGGING__=1', language:['c','cpp'])
130 endif
131
132 if get_option('enable-test')
133   add_project_arguments('-DENABLE_TEST=1', language:['c','cpp'])
134 endif
135
136 if get_option('reduce-tolerance')
137   add_project_arguments('-DREDUCE_TOLERANCE=1', language:['c', 'cpp'])
138 endif
139
140 libm_dep = cxx.find_library('m') # cmath library
141 libdl_dep = cxx.find_library('dl') # DL library
142 thread_dep = dependency('threads') # pthread for tensorflow-lite
143
144 iniparser_dep = dependency('iniparser', required : false, version : '>=4.1') # iniparser
145 if not iniparser_dep.found()
146   message('falling back to find libiniparser library and header files')
147   libiniparser_dep = cxx.find_library('iniparser')
148   sysroot = run_command(
149     cxx.cmd_array() + ['-print-sysroot']
150     ).stdout().split('\n')[0]
151
152   if sysroot.startswith('/')
153     sysroot_inc_cflags_template = '-I@0@/usr/include@1@'
154     sysroot_inc = sysroot_inc_cflags_template.format(sysroot, '')
155     add_project_arguments(sysroot_inc, language: ['c', 'cpp'])
156     sysroot_inc_cflags_iniparser = sysroot_inc_cflags_template.format(sysroot,
157       '/iniparser')
158   else
159     sysroot_inc_cflags_iniparser = '-I/usr/include/iniparser'
160   endif
161
162   if libiniparser_dep.found() and cxx.has_header('iniparser.h', \
163         args : sysroot_inc_cflags_iniparser)
164     iniparser_dep = declare_dependency (dependencies : libiniparser_dep,
165       compile_args : sysroot_inc_cflags_iniparser)
166   else
167     error('Failed to resolve dependency on iniparser')
168   endif
169 endif
170
171 nnstreamer_capi_dep = dependency(get_option('capi-ml-inference-actual'), required:false)
172 if nnstreamer_capi_dep.found()
173   add_project_arguments('-DNNSTREAMER_AVAILABLE=1', language:['c','cpp'])
174   # accessing this variable when dep_.not_found() remains hard error on purpose
175   supported_nnstreamer_capi = nnstreamer_capi_dep.version().version_compare('>=1.7.0')
176   if not supported_nnstreamer_capi
177     add_project_arguments('-DUNSUPPORTED_NNSTREAMER=1', language:['c','cpp'])
178     warning('capi-nnstreamer version is too old, we do not know if it works with older nnstreamer version')
179   endif
180 endif
181
182 ml_api_common_dep = dependency(get_option('capi-ml-common-actual'), required: true)
183
184 if get_option('enable-nnstreamer-backbone')
185   add_project_arguments('-DENABLE_NNSTREAMER_BACKBONE=1', language:['c','cpp'])
186 endif
187
188 tflite_dep = dependency('tensorflow2-lite', required: false)
189 if get_option('enable-tflite-backbone')
190   add_project_arguments('-DENABLE_TFLITE_BACKBONE=1', language:['c','cpp'])
191 endif
192
193 gtest_dep = dependency('gtest', static: true, main: false, required: false)
194 gtest_main_dep = dependency('gtest', static: true, main: true, required: false)
195
196 opencv_dep = dependency('opencv', required: false)
197 if not opencv_dep.found()
198   opencv_dep = dependency('opencv4', required: false)
199   if not opencv_dep.found()
200     opencv_dep = dependency('opencv3', required: false)
201   endif
202 endif
203 if opencv_dep.found()
204   add_project_arguments('-DENABLE_DATA_AUGMENTATION_OPENCV=1', language:['c','cpp'])
205 endif
206 flatc_prog = find_program('flatc', required: false)
207
208 # Install .pc
209 configure_file(input: 'nntrainer.pc.in', output: 'nntrainer.pc',
210   install_dir: nntrainer_libdir / 'pkgconfig',
211   configuration: nntrainer_conf
212 )
213
214 # Install conf
215 configure_file(
216   input: 'nntrainer.ini.in',
217   output: 'nntrainer.ini',
218   install_dir: nntrainer_confdir,
219   configuration: nntrainer_conf
220 )
221 nntrainer_conf_abs_path = get_option('prefix') / nntrainer_confdir / 'nntrainer.ini'
222 message('NNTRAINER_CONF_PATH=@0@'.format(nntrainer_conf_abs_path))
223
224 add_project_arguments(
225   '-DNNTRAINER_CONF_PATH="@0@"'.format(nntrainer_conf_abs_path),
226   language: ['c', 'cpp']
227 )
228
229 # Build nntrainer
230 subdir('nntrainer')
231
232 # Build api
233 subdir('api')
234
235 if get_option('enable-test')
236   if gtest_dep.found()
237     subdir('test')
238   else
239     error('test enabled but gtest not found')
240   endif
241 endif
242
243 if get_option('enable-app')
244   jsoncpp_dep = dependency('jsoncpp') # jsoncpp
245   libcurl_dep = dependency('libcurl')
246   if not tflite_dep.found()
247     error('Tensorflow-Lite dependency not found')
248   endif
249   subdir('Applications')
250 endif
251
252 if get_option('enable-nnstreamer-tensor-filter')
253   nnstreamer_dep = dependency('nnstreamer', required: true)
254   subdir('nnstreamer/tensor_filter')
255 endif
256
257 if get_option('enable-android')
258
259   ndk_build = find_program('ndk-build', required : true)
260   jni_root = meson.current_source_dir() / 'jni'
261   jni_build_root = meson.current_build_dir() / 'jni'
262
263   ndk_args = {
264     'NDK_PROJECT_PATH': jni_root,
265     'APP_BUILD_SCRIPT': jni_root / 'Android.mk',
266     'NDK_APPLICATION_MK': jni_root / 'Application.mk',
267     'NDK_LIBS_OUT': jni_build_root / 'libs',
268     'NDK_OUT': jni_build_root / 'objs',
269     'ENABLE_PROFILE': get_option('enable-profile') ? 1 : 0,
270     'ENABLE_BLAS': get_option('enable-blas') ? 1 : 0,
271   }
272
273   num_threads = run_command('grep', '-c', '^processor', '/proc/cpuinfo').stdout().strip()
274   message('num processor are: ' + num_threads)
275
276   thread_opt_flag = '-j' + num_threads
277
278   ndk_additional_flags = [thread_opt_flag]
279
280   ndk_build_command = [ndk_build]
281   foreach key, val : ndk_args
282     ndk_build_command += '@0@=@1@'.format(key, val)
283   endforeach
284   ndk_build_command += ndk_additional_flags
285
286   android_build_target = custom_target('android',
287     output: 'jni',
288     build_by_default: true,
289     command: ndk_build_command
290   )
291
292 endif