doc: read version from Makefile

Change-Id: I6edb5ee53cdff1c687e97663c93ddaa3b09a9288
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
diff --git a/doc/conf.py b/doc/conf.py
index f2dc610..9f7f6b5 100644
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -52,14 +52,32 @@
 copyright = u'2015, Intel Corporation, Wind River Systems, Inc'
 author = u'many'
 
-# The version info for the project you're documenting, acts as replacement for
-# |version| and |release|, also used in various other places throughout the
-# built documents.
-#
-# The short X.Y version.
-version = os.getenv('KERNELVERSION','0.1.0')
-# The full version, including alpha/beta/rc tags.
-release = os.getenv('KERNELVERSION','0.1.0')
+
+# The following code tries to extract the information by reading the Makefile,
+# when Sphinx is run directly (e.g. by Read the Docs).
+try:
+    makefile_version_major = None
+    makefile_version_minor = None
+    makefile_patchlevel = None
+    for line in open('../Makefile'):
+        key, val = [x.strip() for x in line.split('=', 2)]
+        if key == 'VERSION_MAJOR':
+            makefile_version_major = val
+        if key == 'VERSION_MINOR':
+            makefile_version_minor = val
+        elif key == 'PATCHLEVEL':
+            makefile_patchlevel = val
+        if makefile_version_major and makefile_version_minor and makefile_patchlevel:
+            break
+except:
+    pass
+finally:
+    if makefile_version_major and makefile_version_minor and makefile_patchlevel:
+        version = release = makefile_version_major + '.' + makefile_version_minor + '.' + makefile_patchlevel
+    else:
+        sys.stderr.write('Warning: Could not extract kernel version\n')
+        version = release = "unknown version"
+        version = release = os.getenv('KERNELVERSION','0.1.0')
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.
diff --git a/doc/index.rst b/doc/index.rst
index 69eaecc..f512eaa 100644
--- a/doc/index.rst
+++ b/doc/index.rst
@@ -14,7 +14,7 @@
 .. only:: (development or daily)
 
    Welcome to the Zephyr Project's documentation. This is the documentation of the
-   master tree under development.
+   master tree under development (version |version|).
 
    Documentation for released versions of Zephyr can be found at
    ``https://www.zephyrproject.org/doc/<version>``. The following documentation