summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Schulte <mark@mjs.pw>2017-04-12 23:04:13 -0700
committerPaolo Bonzini <pbonzini@redhat.com>2019-03-15 11:42:36 +0100
commit647bd2839e83267bf92fc5909f1b86e630bf395f (patch)
tree20b7224d00bbe161ee245c88e4db47628654feab
parent66db1af4dd4905426cc7e11e335e1e4788d267d4 (diff)
downloadmeson-647bd2839e83267bf92fc5909f1b86e630bf395f.tar.gz
[modules] Add kconfig module
Add a kconfig module to allow meson to integrate with existing projects that use kconfig.
-rw-r--r--docs/markdown/Kconfig-module.md44
-rw-r--r--docs/markdown/snippets/kconfig.md5
-rw-r--r--docs/sitemap.txt1
-rw-r--r--mesonbuild/modules/unstable_kconfig.py72
-rwxr-xr-xrun_project_tests.py1
-rw-r--r--test cases/kconfig/1 basic/.config3
-rw-r--r--test cases/kconfig/1 basic/meson.build16
-rw-r--r--test cases/kconfig/2 subdir/.config2
-rw-r--r--test cases/kconfig/2 subdir/dir/meson.build13
-rw-r--r--test cases/kconfig/2 subdir/meson.build4
-rw-r--r--test cases/kconfig/3 load_config files/dir/config2
-rw-r--r--test cases/kconfig/3 load_config files/dir/meson.build13
-rw-r--r--test cases/kconfig/3 load_config files/meson.build4
13 files changed, 180 insertions, 0 deletions
diff --git a/docs/markdown/Kconfig-module.md b/docs/markdown/Kconfig-module.md
new file mode 100644
index 000000000..b13f7ec49
--- /dev/null
+++ b/docs/markdown/Kconfig-module.md
@@ -0,0 +1,44 @@
+---
+short-description: Unstable kconfig module
+authors:
+ - name: Mark Schulte, Paolo Bonzini
+ years: [2017, 2019]
+ has-copyright: false
+...
+
+# Unstable kconfig module
+
+This module parses Kconfig output files to allow use of kconfig
+configurations in meson projects.
+
+**Note**: this does not provide kconfig frontend tooling to generate a
+configuration. You still need something such as kconfig frontends (see
+link below) to parse your Kconfig files, and then (after you've
+choosen the configuration options), output a ".config" file.
+
+ [kconfig-frontends]: http://ymorin.is-a-geek.org/projects/kconfig-frontends
+
+## Usage
+
+The module may be imported as follows:
+
+``` meson
+kconfig = import('unstable-kconfig')
+```
+
+The following functions will then be available as methods on the object
+with the name `kconfig`. You can, of course, replace the name
+`kconfig` with anything else.
+
+### kconfig.load()
+
+This function loads a kconfig output file and returns a dictionary object.
+
+`kconfig.load()` makes no attempt at parsing the values in the
+file. Therefore, true boolean values will be represented as the string "y"
+and integer values will have to be converted with `.to_int()`.
+
+* The first (and only) argument is the path to the configuration file to
+ load (usually ".config").
+
+**Returns**: a [dictionary object](Reference-manual.md#dictionary-object).
diff --git a/docs/markdown/snippets/kconfig.md b/docs/markdown/snippets/kconfig.md
new file mode 100644
index 000000000..d4d5c9bdf
--- /dev/null
+++ b/docs/markdown/snippets/kconfig.md
@@ -0,0 +1,5 @@
+## New module to parse kconfig output files
+
+The new module `unstable-kconfig` adds the ability to parse and use kconfig output
+files from `meson.build`.
+
diff --git a/docs/sitemap.txt b/docs/sitemap.txt
index bea2a316c..2e6eb6847 100644
--- a/docs/sitemap.txt
+++ b/docs/sitemap.txt
@@ -45,6 +45,7 @@ index.md
Simd-module.md
Windows-module.md
Cuda-module.md
+ Kconfig-module.md
Java.md
Vala.md
D.md
diff --git a/mesonbuild/modules/unstable_kconfig.py b/mesonbuild/modules/unstable_kconfig.py
new file mode 100644
index 000000000..1639eed8e
--- /dev/null
+++ b/mesonbuild/modules/unstable_kconfig.py
@@ -0,0 +1,72 @@
+# Copyright 2017, 2019 The Meson development team
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from . import ExtensionModule
+
+from .. import mesonlib
+from ..mesonlib import typeslistify
+from ..interpreterbase import FeatureNew, noKwargs
+from ..interpreter import InvalidCode
+
+import os
+
+class KconfigModule(ExtensionModule):
+
+ @FeatureNew('Kconfig Module', '0.51.0')
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.snippets.add('load')
+
+ def _load_file(self, path_to_config):
+ result = dict()
+ try:
+ with open(path_to_config) as f:
+ for line in f:
+ if '#' in line:
+ comment_idx = line.index('#')
+ line = line[:comment_idx]
+ line = line.strip()
+ try:
+ name, val = line.split('=', 1)
+ except ValueError:
+ continue
+ result[name.strip()] = val.strip()
+ except IOError as e:
+ raise mesonlib.MesonException('Failed to load {}: {}'.format(path_to_config, e))
+
+ return result
+
+ @noKwargs
+ def load(self, interpreter, state, args, kwargs):
+ sources = typeslistify(args, (str, mesonlib.File))
+ if len(sources) != 1:
+ raise InvalidCode('load takes only one file input.')
+
+ s = sources[0]
+ if isinstance(s, mesonlib.File):
+ # kconfig input is processed at "meson setup" time, not during
+ # the build, so it cannot reside in the build directory.
+ if s.is_built:
+ raise InvalidCode('kconfig input must be a source file.')
+ s = s.relative_name()
+
+ s = os.path.join(interpreter.environment.source_dir, s)
+ if s not in interpreter.build_def_files:
+ interpreter.build_def_files.append(s)
+
+ return self._load_file(s)
+
+
+def initialize(*args, **kwargs):
+ return KconfigModule(*args, **kwargs)
diff --git a/run_project_tests.py b/run_project_tests.py
index 89f11d382..00fca6cda 100755
--- a/run_project_tests.py
+++ b/run_project_tests.py
@@ -550,6 +550,7 @@ def detect_tests_to_run():
('failing-meson', 'failing', False),
('failing-build', 'failing build', False),
('failing-test', 'failing test', False),
+ ('kconfig', 'kconfig', False),
('platform-osx', 'osx', not mesonlib.is_osx()),
('platform-windows', 'windows', not mesonlib.is_windows() and not mesonlib.is_cygwin()),
diff --git a/test cases/kconfig/1 basic/.config b/test cases/kconfig/1 basic/.config
new file mode 100644
index 000000000..071d1854d
--- /dev/null
+++ b/test cases/kconfig/1 basic/.config
@@ -0,0 +1,3 @@
+CONFIG_VAL1=y
+# CONFIG_VAL2 is not set
+CONFIG_VAL_VAL=4
diff --git a/test cases/kconfig/1 basic/meson.build b/test cases/kconfig/1 basic/meson.build
new file mode 100644
index 000000000..5dc8d194a
--- /dev/null
+++ b/test cases/kconfig/1 basic/meson.build
@@ -0,0 +1,16 @@
+project('kconfig basic test')
+
+k = import('unstable-kconfig')
+conf = k.load('.config')
+
+if not conf.has_key('CONFIG_VAL1')
+ error('Expected CONFIG_VAL1 to be set, but it wasn\'t')
+endif
+
+if conf.has_key('CONFIG_VAL2')
+ error('Expected CONFIG_VAL2 not be set, but it was')
+endif
+
+if conf.get('CONFIG_VAL_VAL').to_int() != 4
+ error('Expected CONFIG_VAL_VAL to be 4')
+endif
diff --git a/test cases/kconfig/2 subdir/.config b/test cases/kconfig/2 subdir/.config
new file mode 100644
index 000000000..0599d4616
--- /dev/null
+++ b/test cases/kconfig/2 subdir/.config
@@ -0,0 +1,2 @@
+CONFIG_IS_SET=y
+# CONFIG_NOT_IS_SET is not set
diff --git a/test cases/kconfig/2 subdir/dir/meson.build b/test cases/kconfig/2 subdir/dir/meson.build
new file mode 100644
index 000000000..12f150297
--- /dev/null
+++ b/test cases/kconfig/2 subdir/dir/meson.build
@@ -0,0 +1,13 @@
+
+k = import('unstable-kconfig')
+
+conf = k.load(meson.source_root() / '.config')
+
+if not conf.has_key('CONFIG_IS_SET')
+ error('Expected CONFIG_IS_SET to be set, but it wasn\'t')
+endif
+
+if conf.has_key('CONFIG_NOT_IS_SET')
+ error('Expected CONFIG_NOT_IS_SET not be set, but it was')
+endif
+
diff --git a/test cases/kconfig/2 subdir/meson.build b/test cases/kconfig/2 subdir/meson.build
new file mode 100644
index 000000000..1245b1802
--- /dev/null
+++ b/test cases/kconfig/2 subdir/meson.build
@@ -0,0 +1,4 @@
+project('kconfig subdir test')
+
+# Test into sub directory
+subdir('dir')
diff --git a/test cases/kconfig/3 load_config files/dir/config b/test cases/kconfig/3 load_config files/dir/config
new file mode 100644
index 000000000..0599d4616
--- /dev/null
+++ b/test cases/kconfig/3 load_config files/dir/config
@@ -0,0 +1,2 @@
+CONFIG_IS_SET=y
+# CONFIG_NOT_IS_SET is not set
diff --git a/test cases/kconfig/3 load_config files/dir/meson.build b/test cases/kconfig/3 load_config files/dir/meson.build
new file mode 100644
index 000000000..d7b8d4477
--- /dev/null
+++ b/test cases/kconfig/3 load_config files/dir/meson.build
@@ -0,0 +1,13 @@
+
+k = import('unstable-kconfig')
+
+conf = k.load(files('config'))
+
+if not conf.has_key('CONFIG_IS_SET')
+ error('Expected CONFIG_IS_SET to be set, but it wasn\'t')
+endif
+
+if conf.has_key('CONFIG_NOT_IS_SET')
+ error('Expected CONFIG_NOT_IS_SET not be set, but it was')
+endif
+
diff --git a/test cases/kconfig/3 load_config files/meson.build b/test cases/kconfig/3 load_config files/meson.build
new file mode 100644
index 000000000..1245b1802
--- /dev/null
+++ b/test cases/kconfig/3 load_config files/meson.build
@@ -0,0 +1,4 @@
+project('kconfig subdir test')
+
+# Test into sub directory
+subdir('dir')