[Conf] add flag to load path from env-var
authorJaeyun <jy1210.jung@samsung.com>
Tue, 7 May 2019 08:17:39 +0000 (17:17 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Fri, 17 May 2019 06:06:57 +0000 (15:06 +0900)
Parse ini file and add sub-plugins path from env-var. (default false)
For unittest, add temorary configuration to read env variables.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
debian/rules
gst/nnstreamer/nnstreamer_conf.c
gst/nnstreamer/nnstreamer_conf.h
meson.build
meson_options.txt
nnstreamer.ini.in
packaging/nnstreamer.spec
tizen-api/meson.build
tools/development/nnstreamerCodeGenCustomFilter.py

index 46eddf2..2ca2352 100755 (executable)
@@ -17,6 +17,7 @@ ROOT_DIR:=$(shell pwd)
 export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
 export DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
 export GST_PLUGIN_PATH=${ROOT_DIR}/build/gst/nnstreamer
+export NNSTREAMER_CONF=${ROOT_DIR}/build/nnstreamer-test.ini
 export NNSTREAMER_FILTERS=${ROOT_DIR}/build/ext/nnstreamer/tensor_filter
 export NNSTREAMER_DECODERS=${ROOT_DIR}/build/ext/nnstreamer/tensor_decoder
 
index 7bf7d1a..6e0690a 100644 (file)
@@ -38,6 +38,7 @@ static const gchar *subplugin_prefixes[] = {
 typedef struct
 {
   gboolean loaded;            /**< TRUE if loaded at least once */
+  gboolean enable_envvar;     /**< TRUE to parse env variables */
 
   gchar *conffile;            /**< Location of conf file. */
 
@@ -262,22 +263,24 @@ nnsconf_loadconf (gboolean force_reload)
     memset (&conf, 0, sizeof (confdata));
   }
 
-  /* Read from Environmental Variables */
-  conf.conffile = _strdup_getenv (NNSTREAMER_ENVVAR_CONF_FILE);
-  if (!conf.conffile || !g_file_test (conf.conffile, G_FILE_TEST_IS_REGULAR)) {
+  /* Read from the conf file first */
+  conf.conffile = g_strdup (NNSTREAMER_CONF_FILE);
+  if (!g_file_test (conf.conffile, G_FILE_TEST_IS_REGULAR)) {
     /* File not found or not configured */
-    if (NULL != conf.conffile)
-      g_free (conf.conffile);
-    conf.conffile = g_strdup (NNSTREAMER_DEFAULT_CONF_FILE);
-  }
+    g_free (conf.conffile);
 
-  conf.pathFILTERS[0] = _strdup_getenv (NNSTREAMER_ENVVAR_FILTERS);
-  conf.pathDECODERS[0] = _strdup_getenv (NNSTREAMER_ENVVAR_DECODERS);
-  conf.pathCUSTOM_FILTERS[0] = _strdup_getenv (NNSTREAMER_ENVVAR_CUSTOMFILTERS);
+    /* Try to read from Environmental Variables */
+    conf.conffile = _strdup_getenv (NNSTREAMER_ENVVAR_CONF_FILE);
+  }
 
   /* Read the conf file. It's ok even if we cannot load it. */
   if (g_key_file_load_from_file (key_file, conf.conffile, G_KEY_FILE_NONE,
           NULL)) {
+    gchar *value;
+
+    value = g_key_file_get_string (key_file, "common", "enable_envvar", NULL);
+    conf.enable_envvar = _parse_bool_string (value, FALSE);
+    g_free (value);
 
     conf.pathFILTERS[1] =
         g_key_file_get_string (key_file, "filter", "filters", NULL);
@@ -287,6 +290,14 @@ nnsconf_loadconf (gboolean force_reload)
         g_key_file_get_string (key_file, "filter", "customfilters", NULL);
   }
 
+  /* Read from env variables. */
+  if (conf.enable_envvar) {
+    conf.pathFILTERS[0] = _strdup_getenv (NNSTREAMER_ENVVAR_FILTERS);
+    conf.pathDECODERS[0] = _strdup_getenv (NNSTREAMER_ENVVAR_DECODERS);
+    conf.pathCUSTOM_FILTERS[0] =
+        _strdup_getenv (NNSTREAMER_ENVVAR_CUSTOMFILTERS);
+  }
+
   /* Strdup the hardcoded */
   conf.pathFILTERS[2] = g_strdup (NNSTREAMER_FILTERS);
   conf.pathDECODERS[2] = g_strdup (NNSTREAMER_DECODERS);
@@ -429,11 +440,13 @@ nnsconf_get_custom_value_string (const gchar * group, const gchar * key)
   value = g_hash_table_lookup (custom_table, hashkey);
 
   if (NULL == value) {
-    gchar *envkey = g_strdup_printf ("NNSTREAMER_%s_%s", group, key);
-
     /* 1. Read envvar */
-    value = _strdup_getenv (envkey);
-    g_free (envkey);
+    if (conf.enable_envvar) {
+      gchar *envkey = g_strdup_printf ("NNSTREAMER_%s_%s", group, key);
+
+      value = _strdup_getenv (envkey);
+      g_free (envkey);
+    }
 
     /* 2. Read ini */
     if (NULL == value && conf.conffile) {
index 552ffcf..a78d3f4 100644 (file)
@@ -44,6 +44,9 @@ G_BEGIN_DECLS
 
 /* Internal Hardcoded Values */
 #define NNSTREAMER_DEFAULT_CONF_FILE    "/etc/nnstreamer.ini"
+#ifndef NNSTREAMER_CONF_FILE
+#define NNSTREAMER_CONF_FILE NNSTREAMER_DEFAULT_CONF_FILE
+#endif
 #define NNSTREAMER_FILTERS              "/usr/lib/nnstreamer/filters/"
 #define NNSTREAMER_DECODERS             "/usr/lib/nnstreamer/decoders/"
 #define NNSTREAMER_CUSTOM_FILTERS       "/usr/lib/nnstreamer/customfilters/"
index 301896d..0e92526 100644 (file)
@@ -4,7 +4,7 @@
 project('nnstreamer', 'c', 'cpp',
   version: '0.1.3',
   license: ['LGPL'],
-  meson_version: '>=0.40.0',
+  meson_version: '>=0.42.0',
   default_options: [
     'werror=true',
     'warning_level=1',
@@ -13,7 +13,7 @@ project('nnstreamer', 'c', 'cpp',
   ]
 )
 
-add_project_arguments('-DVERSION="'+meson.project_version()+'"', language: ['c', 'cpp'])
+add_project_arguments('-DVERSION="' + meson.project_version() + '"', language: ['c', 'cpp'])
 
 cc = meson.get_compiler('c')
 cxx = meson.get_compiler('cpp')
@@ -58,10 +58,6 @@ endforeach
 
 gst_api_verision = '1.0'
 
-# Set configuration
-nnstreamer_conf = configuration_data()
-nnstreamer_conf.set('VERSION', meson.project_version())
-
 # Set install path
 nnstreamer_prefix = get_option('prefix')
 nnstreamer_libdir = join_paths(nnstreamer_prefix, get_option('libdir'))
@@ -79,6 +75,9 @@ decoder_subplugin_install_dir = join_paths(subplugin_install_prefix, 'decoders')
 customfilter_install_dir = join_paths(subplugin_install_prefix, 'customfilters')
 unittest_install_dir = join_paths(subplugin_install_prefix, 'unittest')
 
+# Set default configuration
+nnstreamer_conf = configuration_data()
+nnstreamer_conf.set('VERSION', meson.project_version())
 nnstreamer_conf.set('PREFIX', nnstreamer_prefix)
 nnstreamer_conf.set('EXEC_PREFIX', nnstreamer_bindir)
 nnstreamer_conf.set('LIB_INSTALL_DIR', nnstreamer_libdir)
@@ -86,6 +85,9 @@ nnstreamer_conf.set('GST_INSTALL_DIR', plugins_install_dir)
 nnstreamer_conf.set('INCLUDE_INSTALL_DIR', nnstreamer_includedir)
 nnstreamer_conf.set('SUBPLUGIN_INSTALL_PREFIX', subplugin_install_prefix)
 
+# Define default conf file
+add_project_arguments('-DNNSTREAMER_CONF_FILE="' + join_paths(nnstreamer_inidir, 'nnstreamer.ini') + '"', language: 'c')
+
 # Dependencies
 glib_dep = dependency('glib-2.0')
 gst_dep = dependency('gstreamer-' + gst_api_verision)
@@ -136,11 +138,6 @@ if get_option('enable-tensorflow')
   else
     error('Cannot find tensorflow')
   endif
-
-  nnstreamer_conf.set('TF_MEM_OPTMZ', get_option('enable-tensorflow-mem-optmz'))
-else
-  message('Tensorflow is disabled')
-  nnstreamer_conf.set('TF_MEM_OPTMZ', false)
 endif
 
 # Tensorflow-lite
@@ -202,16 +199,42 @@ endif
 # Build unittests
 if get_option('enable-test')
   subdir('tests')
+
+  # temporary ini file for test, enable env variables.
+  nnstreamer_test_conf = configuration_data()
+  nnstreamer_test_conf.merge_from(nnstreamer_conf)
+
+  nnstreamer_test_conf.set('ENABLE_ENV_VAR', true)
+  nnstreamer_test_conf.set('TF_MEM_OPTMZ', false)
+
+  # meson 0.50 supports install argument in configure_file()
+  if get_option('install-test')
+    configure_file(input: 'nnstreamer.ini.in', output: 'nnstreamer-test.ini',
+      install_dir: unittest_install_dir,
+      configuration: nnstreamer_test_conf
+    )
+  else
+    configure_file(input: 'nnstreamer.ini.in', output: 'nnstreamer-test.ini',
+      configuration: nnstreamer_test_conf
+    )
+  endif
 endif
 
+# Set configuration to install .ini
+nnstreamer_install_conf = configuration_data()
+nnstreamer_install_conf.merge_from(nnstreamer_conf)
+
+nnstreamer_install_conf.set('ENABLE_ENV_VAR', get_option('enable-env-var'))
+nnstreamer_install_conf.set('TF_MEM_OPTMZ', get_option('enable-tensorflow-mem-optmz'))
+
 # Install .ini
 configure_file(input: 'nnstreamer.ini.in', output: 'nnstreamer.ini',
   install_dir: nnstreamer_inidir,
-  configuration: nnstreamer_conf
+  configuration: nnstreamer_install_conf
 )
 
 # Install .pc
 configure_file(input: 'nnstreamer.pc.in', output: 'nnstreamer.pc',
   install_dir: join_paths(nnstreamer_libdir, 'pkgconfig'),
-  configuration: nnstreamer_conf
+  configuration: nnstreamer_install_conf
 )
index 020dada..ba25735 100644 (file)
@@ -8,5 +8,6 @@ option('enable-tensorflow-mem-optmz', type: 'boolean', value: true)
 option('install-example', type: 'boolean', value: false)
 option('disable-video-support', type: 'boolean', value: false)
 option('disable-audio-support', type: 'boolean', value: false)
+option('enable-env-var', type: 'boolean', value: true)
 option('enable-tizen-capi', type: 'boolean', value: false)
 option('enable-python', type: 'boolean', value: true)
index 310b08f..34a957a 100644 (file)
@@ -1,3 +1,8 @@
+# Set 1 or True if you want to set your custom sub-plugins' path with env variables.
+# Please be informed that, configured ini file should be in RO partition for the release binary.
+[common]
+enable_envvar=@ENABLE_ENV_VAR@
+
 [filter]
 filters=@SUBPLUGIN_INSTALL_PREFIX@/filters/
 customfilters=@SUBPLUGIN_INSTALL_PREFIX@/customfilters/
index 6364b3f..275aee6 100644 (file)
@@ -179,13 +179,14 @@ enable_tf=true
 enable_tf=false
 %endif
 
-meson --buildtype=plain --prefix=%{_prefix} --sysconfdir=%{_sysconfdir} --libdir=%{_libdir} --bindir=%{nnstexampledir} --includedir=%{_includedir} -Dinstall-example=true -Denable-tensorflow=${enable_tf} %{api} build
+meson --buildtype=plain --prefix=%{_prefix} --sysconfdir=%{_sysconfdir} --libdir=%{_libdir} --bindir=%{nnstexampledir} --includedir=%{_includedir} -Dinstall-example=true -Denable-tensorflow=${enable_tf} %{api} -Denable-env-var=false build
 
 ninja -C build %{?_smp_mflags}
 
 %if 0%{?unit_test}
     pushd build
     export GST_PLUGIN_PATH=$(pwd)/gst/nnstreamer
+    export NNSTREAMER_CONF=$(pwd)/nnstreamer-test.ini
     export NNSTREAMER_FILTERS=$(pwd)/ext/nnstreamer/tensor_filter
     export NNSTREAMER_DECODERS=$(pwd)/ext/nnstreamer/tensor_decoder
     ./tests/unittest_common
index 4015e55..d32cda7 100644 (file)
@@ -3,7 +3,7 @@ if meson.project_name() != 'nnstreamer'
   project('capi-nnstreamer', 'c',
     version: '0.1',
     license: ['LGPL'],
-    meson_version: '>=0.40.0',
+    meson_version: '>=0.42.0',
     default_options: [
       'werror=true',
       'warning_level=1',
index db02d08..355c978 100644 (file)
@@ -353,7 +353,7 @@ meson_script = """
 project('{fname}', 'c',
   version: '1.0',
   license: ['LGPL'],
-  meson_version: '>=0.40.0',
+  meson_version: '>=0.42.0',
   default_options: [
     'warning_level=1',
   ]