blob: 160da333a8ba671fae2788b6e7351b092aadcdcf [file] [log] [blame]
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -08001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -08002
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -07003from waflib import Logs, Utils, Context
4import os
Alexander Afanasyeva06fdda2014-04-29 19:15:00 -07005
Alexander Afanasyev45312f52015-09-27 12:06:50 -07006VERSION = "0.3.91"
Alexander Afanasyev766cea72014-04-24 19:16:42 -07007APPNAME = "ndn-cxx"
8PACKAGE_BUGREPORT = "http://redmine.named-data.net/projects/ndn-cxx"
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -07009PACKAGE_URL = "http://named-data.net/doc/ndn-cxx/"
Alexander Afanasyev6c632302014-10-31 12:34:11 -070010GIT_TAG_PREFIX = "ndn-cxx-"
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070011
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080012def options(opt):
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070013 opt.load(['compiler_cxx', 'gnu_dirs', 'c_osx'])
14 opt.load(['default-compiler-flags', 'coverage', 'osx-security', 'pch',
Alexander Afanasyev01515792014-12-16 14:20:01 -080015 'boost', 'cryptopp', 'sqlite3',
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070016 'doxygen', 'sphinx_build', 'type_traits', 'compiler-features'],
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070017 tooldir=['.waf-tools'])
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080018
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070019 opt = opt.add_option_group('Library Options')
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080020
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070021 opt.add_option('--with-tests', action='store_true', default=False, dest='with_tests',
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080022 help='''build unit tests''')
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080023
Yingdi Yue6bfab22014-02-06 16:01:19 -080024 opt.add_option('--without-tools', action='store_false', default=True, dest='with_tools',
25 help='''Do not build tools''')
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080026
Alexander Afanasyevc8bcd452014-05-12 17:58:47 -070027 opt.add_option('--with-examples', action='store_true', default=False, dest='with_examples',
28 help='''Build examples''')
29
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070030 opt.add_option('--without-sqlite-locking', action='store_false', default=True,
31 dest='with_sqlite_locking',
32 help='''Disable filesystem locking in sqlite3 database '''
33 '''(use unix-dot locking mechanism instead). '''
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -080034 '''This option may be necessary if home directory is hosted on NFS.''')
Alexander Afanasyev5519cc72015-03-01 14:25:03 -080035
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070036 opt.add_option('--without-osx-keychain', action='store_false', default=True,
37 dest='with_osx_keychain',
Alexander Afanasyev3e08d5d2014-02-12 19:24:28 -080038 help='''On Darwin, do not use OSX keychain as a default TPM''')
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080039
Spyridon Mastorakis5ebfda62015-07-21 20:57:40 -070040 opt.add_option('--enable-static', action='store_true', default=False,
41 dest='enable_static', help='''Build static library (disabled by default)''')
42 opt.add_option('--disable-static', action='store_false', default=False,
43 dest='enable_static', help='''Do not build static library (disabled by default)''')
Alexander Afanasyev5519cc72015-03-01 14:25:03 -080044
Spyridon Mastorakis5ebfda62015-07-21 20:57:40 -070045 opt.add_option('--enable-shared', action='store_true', default=True,
46 dest='enable_shared', help='''Build shared library (enabled by default)''')
47 opt.add_option('--disable-shared', action='store_false', default=True,
48 dest='enable_shared', help='''Do not build shared library (enabled by default)''')
Alexander Afanasyev5519cc72015-03-01 14:25:03 -080049
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080050def configure(conf):
Alexander Afanasyev5519cc72015-03-01 14:25:03 -080051 conf.start_msg('Building static library')
52 if conf.options.enable_static:
53 conf.end_msg('yes')
54 else:
55 conf.end_msg('no', color='YELLOW')
56 conf.env.enable_static = conf.options.enable_static
57
58 conf.start_msg('Building shared library')
59 if conf.options.enable_shared:
60 conf.end_msg('yes')
61 else:
62 conf.end_msg('no', color='YELLOW')
63 conf.env.enable_shared = conf.options.enable_shared
64
65 if not conf.options.enable_shared and not conf.options.enable_static:
66 conf.fatal("Either static library or shared library must be enabled")
67
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070068 conf.load(['compiler_cxx', 'gnu_dirs', 'c_osx',
69 'default-compiler-flags', 'osx-security', 'pch',
Alexander Afanasyev01515792014-12-16 14:20:01 -080070 'boost', 'cryptopp', 'sqlite3',
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070071 'doxygen', 'sphinx_build', 'type_traits', 'compiler-features'])
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080072
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070073 conf.env['WITH_TESTS'] = conf.options.with_tests
74 conf.env['WITH_TOOLS'] = conf.options.with_tools
Alexander Afanasyevc8bcd452014-05-12 17:58:47 -070075 conf.env['WITH_EXAMPLES'] = conf.options.with_examples
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -080076
Alexander Afanasyev95de62e2014-04-11 18:26:33 -070077 conf.find_program('sh', var='SH', mandatory=True)
78
Alexander Afanasyevfff47d62014-05-11 19:24:46 -070079 conf.check_cxx(lib='pthread', uselib_store='PTHREAD', define_name='HAVE_PTHREAD',
80 mandatory=False)
81 conf.check_cxx(lib='rt', uselib_store='RT', define_name='HAVE_RT', mandatory=False)
Alexander Afanasyev3188c40f2015-01-28 14:49:42 -080082 conf.check_cxx(msg='Checking for function getpass', mandatory=False,
83 define_name='HAVE_GETPASS', fragment='''
84#include <unistd.h>
85int
86main(int, char**)
87{
88 char* pass = getpass("test prompt");
89 (void)(pass);
90 return 0;
91}
92''')
Alexander Afanasyevfff47d62014-05-11 19:24:46 -070093
Alexander Afanasyev7b3080f2015-01-28 21:21:01 -080094 conf.check_cxx(msg='Checking for rtnetlink', mandatory=False,
95 define_name='HAVE_RTNETLINK',
96 header_name=['netinet/in.h', 'linux/netlink.h', 'linux/rtnetlink.h', 'net/if.h'])
97
Alexander Afanasyev1160baa2014-04-10 18:50:29 -070098 conf.check_osx_security(mandatory=False)
Yingdi Yue6bfab22014-02-06 16:01:19 -080099
Alexander Afanasyev1160baa2014-04-10 18:50:29 -0700100 conf.check_sqlite3(mandatory=True)
Alexander Afanasyevfff47d62014-05-11 19:24:46 -0700101 conf.check_cryptopp(mandatory=True, use='PTHREAD')
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800102
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700103 USED_BOOST_LIBS = ['system', 'filesystem', 'date_time', 'iostreams',
Alexander Afanasyev75088672014-07-14 11:58:30 -0700104 'regex', 'program_options', 'chrono', 'random']
Alexander Afanasyevdafdc372014-03-03 15:58:44 +0000105 if conf.env['WITH_TESTS']:
106 USED_BOOST_LIBS += ['unit_test_framework']
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700107 conf.define('HAVE_TESTS', 1)
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800108
Alexander Afanasyevdafdc372014-03-03 15:58:44 +0000109 conf.check_boost(lib=USED_BOOST_LIBS, mandatory=True)
110 if conf.env.BOOST_VERSION_NUMBER < 104800:
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700111 Logs.error("Minimum required boost version is 1.48.0")
112 Logs.error("Please upgrade your distribution or install custom boost libraries" +
Alexander Afanasyevdafdc372014-03-03 15:58:44 +0000113 " (http://redmine.named-data.net/projects/nfd/wiki/Boost_FAQ)")
114 return
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800115
Alexander Afanasyev5b60f702014-02-07 12:55:24 -0800116 if not conf.options.with_sqlite_locking:
117 conf.define('DISABLE_SQLITE3_FS_LOCKING', 1)
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -0800118
Alexander Afanasyev59d67a52014-04-03 16:09:31 -0700119 if conf.env['HAVE_OSX_SECURITY']:
Alexander Afanasyev3e08d5d2014-02-12 19:24:28 -0800120 conf.env['WITH_OSX_KEYCHAIN'] = conf.options.with_osx_keychain
121 if conf.options.with_osx_keychain:
122 conf.define('WITH_OSX_KEYCHAIN', 1)
123 else:
124 conf.env['WITH_OSX_KEYCHAIN'] = False
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -0800125
Alexander Afanasyev1160baa2014-04-10 18:50:29 -0700126 # Loading "late" to prevent tests to be compiled with profiling flags
127 conf.load('coverage')
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -0800128
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600129 conf.define('SYSCONFDIR', conf.env['SYSCONFDIR'])
130
Alexander Afanasyev3e7d2ac2015-02-10 19:39:28 -0800131 # config file will contain all defines that were added using conf.define('xxx'...)
132 # Everything that was added directly to conf.env['DEFINES'] will not appear in the
133 # config file and will be added using compiler directives in the command line.
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700134 conf.write_config_header('src/ndn-cxx-config.hpp', define_prefix='NDN_CXX_')
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800135
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700136def build(bld):
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -0700137 version(bld)
138
139 bld(features="subst",
140 name='version',
141 source='src/version.hpp.in',
142 target='src/version.hpp',
143 install_path=None,
144 VERSION_STRING=VERSION_BASE,
145 VERSION_BUILD=VERSION,
146 VERSION=int(VERSION_SPLIT[0]) * 1000000 +
147 int(VERSION_SPLIT[1]) * 1000 +
148 int(VERSION_SPLIT[2]),
149 VERSION_MAJOR=VERSION_SPLIT[0],
150 VERSION_MINOR=VERSION_SPLIT[1],
151 VERSION_PATCH=VERSION_SPLIT[2],
152 )
153
Alexander Afanasyev5519cc72015-03-01 14:25:03 -0800154 libndn_cxx = dict(
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700155 target="ndn-cxx",
156 name="ndn-cxx",
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700157 source=bld.path.ant_glob('src/**/*.cpp',
Junxiao Shie30aaea2014-12-03 20:48:34 -0700158 excl=['src/**/*-osx.cpp', 'src/**/*-sqlite3.cpp']),
Alexander Afanasyev8b1674a2014-05-15 00:58:43 -0700159 headers='src/common-pch.hpp',
Alexander Afanasyev5519cc72015-03-01 14:25:03 -0800160 use='version BOOST CRYPTOPP SQLITE3 RT PTHREAD',
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700161 includes=". src",
162 export_includes="src",
163 install_path='${LIBDIR}',
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800164 )
165
Alexander Afanasyev59d67a52014-04-03 16:09:31 -0700166 if bld.env['HAVE_OSX_SECURITY']:
Alexander Afanasyev5519cc72015-03-01 14:25:03 -0800167 libndn_cxx['source'] += bld.path.ant_glob('src/**/*-osx.cpp')
168 libndn_cxx['mac_app'] = True
169 libndn_cxx['use'] += " OSX_COREFOUNDATION OSX_SECURITY"
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800170
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -0800171 # In case we want to make it optional later
Alexander Afanasyev5519cc72015-03-01 14:25:03 -0800172 libndn_cxx['source'] += bld.path.ant_glob('src/**/*-sqlite3.cpp')
173
174 if bld.env.enable_shared:
175 bld(features=['pch', 'cxx', 'cxxshlib'],
176 vnum=VERSION_BASE,
177 cnum=VERSION_BASE,
178 **libndn_cxx)
179
180 if bld.env.enable_static:
181 bld(features=['pch', 'cxx', 'cxxstlib'],
182 **libndn_cxx)
Alexander Afanasyevd409d592014-01-28 18:36:38 -0800183
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700184 # Prepare flags that should go to pkgconfig file
Alexander Afanasyev200dd6f2014-01-28 19:04:25 -0800185 pkgconfig_libs = []
186 pkgconfig_ldflags = []
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -0800187 pkgconfig_linkflags = []
Alexander Afanasyev200dd6f2014-01-28 19:04:25 -0800188 pkgconfig_includes = []
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -0800189 pkgconfig_cxxflags = []
Alexander Afanasyev5519cc72015-03-01 14:25:03 -0800190 for lib in Utils.to_list(libndn_cxx['use']):
Alexander Afanasyev200dd6f2014-01-28 19:04:25 -0800191 if bld.env['LIB_%s' % lib]:
192 pkgconfig_libs += Utils.to_list(bld.env['LIB_%s' % lib])
193 if bld.env['LIBPATH_%s' % lib]:
194 pkgconfig_ldflags += Utils.to_list(bld.env['LIBPATH_%s' % lib])
195 if bld.env['INCLUDES_%s' % lib]:
196 pkgconfig_includes += Utils.to_list(bld.env['INCLUDES_%s' % lib])
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -0800197 if bld.env['LINKFLAGS_%s' % lib]:
198 pkgconfig_linkflags += Utils.to_list(bld.env['LINKFLAGS_%s' % lib])
199 if bld.env['CXXFLAGS_%s' % lib]:
200 pkgconfig_cxxflags += Utils.to_list(bld.env['CXXFLAGS_%s' % lib])
Alexander Afanasyev200dd6f2014-01-28 19:04:25 -0800201
Alexander Afanasyev84cf4292014-01-29 22:16:27 -0800202 EXTRA_FRAMEWORKS = "";
Alexander Afanasyev59d67a52014-04-03 16:09:31 -0700203 if bld.env['HAVE_OSX_SECURITY']:
Alexander Afanasyev84cf4292014-01-29 22:16:27 -0800204 EXTRA_FRAMEWORKS = "-framework CoreFoundation -framework Security"
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -0800205
Alexander Afanasyev200dd6f2014-01-28 19:04:25 -0800206 def uniq(alist):
207 set = {}
208 return [set.setdefault(e,e) for e in alist if e not in set]
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -0800209
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700210 pkconfig = bld(features="subst",
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700211 source="libndn-cxx.pc.in",
212 target="libndn-cxx.pc",
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700213 install_path="${LIBDIR}/pkgconfig",
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -0700214 VERSION=VERSION_BASE,
Alexander Afanasyev200dd6f2014-01-28 19:04:25 -0800215
216 # This probably not the right thing to do, but to simplify life of apps
217 # that use the library
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700218 EXTRA_LIBS=" ".join([('-l%s' % i) for i in uniq(pkgconfig_libs)]),
219 EXTRA_LDFLAGS=" ".join([('-L%s' % i) for i in uniq(pkgconfig_ldflags)]),
220 EXTRA_LINKFLAGS=" ".join(uniq(pkgconfig_linkflags)),
221 EXTRA_INCLUDES=" ".join([('-I%s' % i) for i in uniq(pkgconfig_includes)]),
222 EXTRA_CXXFLAGS=" ".join(uniq(pkgconfig_cxxflags)),
223 EXTRA_FRAMEWORKS=EXTRA_FRAMEWORKS,
Alexander Afanasyevd409d592014-01-28 18:36:38 -0800224 )
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -0800225
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800226 # Unit tests
227 if bld.env['WITH_TESTS']:
Alexander Afanasyevd409d592014-01-28 18:36:38 -0800228 bld.recurse('tests')
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800229
Yingdi Yue6bfab22014-02-06 16:01:19 -0800230 if bld.env['WITH_TOOLS']:
Alexander Afanasyevc8bcd452014-05-12 17:58:47 -0700231 bld.recurse("tools")
232
233 if bld.env['WITH_EXAMPLES']:
Yingdi Yue6bfab22014-02-06 16:01:19 -0800234 bld.recurse("examples")
Alexander Afanasyevf5df8e62014-02-16 19:56:21 -0800235
Junxiao Shie30aaea2014-12-03 20:48:34 -0700236 headers = bld.path.ant_glob(['src/**/*.hpp'],
Alexander Afanasyev7b3080f2015-01-28 21:21:01 -0800237 excl=['src/**/*-osx.hpp', 'src/detail/**/*'])
Junxiao Shie30aaea2014-12-03 20:48:34 -0700238 if bld.env['HAVE_OSX_SECURITY']:
239 headers += bld.path.ant_glob('src/**/*-osx.hpp')
240
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700241 bld.install_files("%s/ndn-cxx" % bld.env['INCLUDEDIR'], headers,
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700242 relative_trick=True, cwd=bld.path.find_node('src'))
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800243
Alexander Afanasyev766cea72014-04-24 19:16:42 -0700244 bld.install_files("%s/ndn-cxx" % bld.env['INCLUDEDIR'],
245 bld.path.find_resource('src/ndn-cxx-config.hpp'))
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800246
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -0700247 bld.install_files("%s/ndn-cxx" % bld.env['INCLUDEDIR'],
248 bld.path.find_resource('src/version.hpp'))
249
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600250 bld.install_files("${SYSCONFDIR}/ndn", "client.conf.sample")
251
Alexander Afanasyev7c6aeb02014-04-10 19:59:19 -0700252 if bld.env['SPHINX_BUILD']:
253 bld(features="sphinx",
254 builder="man",
255 outdir="docs/manpages",
256 config="docs/conf.py",
257 source=bld.path.ant_glob('docs/manpages/**/*.rst'),
Alexander Afanasyeva06fdda2014-04-29 19:15:00 -0700258 install_path="${MANDIR}/",
259 VERSION=VERSION)
Alexander Afanasyev7c6aeb02014-04-10 19:59:19 -0700260
261def docs(bld):
262 from waflib import Options
263 Options.commands = ['doxygen', 'sphinx'] + Options.commands
264
Alexander Afanasyev401a2362014-03-02 00:03:11 +0000265def doxygen(bld):
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -0700266 version(bld)
267
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800268 if not bld.env.DOXYGEN:
Alexander Afanasyev7c6aeb02014-04-10 19:59:19 -0700269 Logs.error("ERROR: cannot build documentation (`doxygen' is not found in $PATH)")
270 else:
Alexander Afanasyeva06fdda2014-04-29 19:15:00 -0700271 bld(features="subst",
272 name="doxygen-conf",
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -0700273 source=["docs/doxygen.conf.in",
274 "docs/named_data_theme/named_data_footer-with-analytics.html.in"],
275 target=["docs/doxygen.conf",
276 "docs/named_data_theme/named_data_footer-with-analytics.html"],
Alexander Afanasyeva06fdda2014-04-29 19:15:00 -0700277 VERSION=VERSION,
Alexander Afanasyev9b0e1142014-05-08 00:17:34 -0700278 HTML_FOOTER="../build/docs/named_data_theme/named_data_footer-with-analytics.html" \
279 if os.getenv('GOOGLE_ANALYTICS', None) \
280 else "../docs/named_data_theme/named_data_footer.html",
281 GOOGLE_ANALYTICS=os.getenv('GOOGLE_ANALYTICS', ""),
Alexander Afanasyeva06fdda2014-04-29 19:15:00 -0700282 )
283
Alexander Afanasyev7c6aeb02014-04-10 19:59:19 -0700284 bld(features="doxygen",
Alexander Afanasyeva06fdda2014-04-29 19:15:00 -0700285 doxyfile='docs/doxygen.conf',
286 use="doxygen-conf")
Alexander Afanasyeva1ae0a12014-01-28 15:21:02 -0800287
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700288def sphinx(bld):
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -0700289 version(bld)
290
Alexander Afanasyev1160baa2014-04-10 18:50:29 -0700291 if not bld.env.SPHINX_BUILD:
292 bld.fatal("ERROR: cannot build documentation (`sphinx-build' is not found in $PATH)")
Alexander Afanasyev7c6aeb02014-04-10 19:59:19 -0700293 else:
294 bld(features="sphinx",
295 outdir="docs",
296 source=bld.path.ant_glob("docs/**/*.rst"),
Alexander Afanasyeva06fdda2014-04-29 19:15:00 -0700297 config="docs/conf.py",
298 VERSION=VERSION)
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -0700299
300
301def version(ctx):
302 if getattr(Context.g_module, 'VERSION_BASE', None):
303 return
304
305 Context.g_module.VERSION_BASE = Context.g_module.VERSION
306 Context.g_module.VERSION_SPLIT = [v for v in VERSION_BASE.split('.')]
307
Alexander Afanasyev6c632302014-10-31 12:34:11 -0700308 didGetVersion = False
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -0700309 try:
Alexander Afanasyev6c632302014-10-31 12:34:11 -0700310 cmd = ['git', 'describe', '--always', '--match', '%s*' % GIT_TAG_PREFIX]
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -0700311 p = Utils.subprocess.Popen(cmd, stdout=Utils.subprocess.PIPE,
312 stderr=None, stdin=None)
Alexander Afanasyev6c632302014-10-31 12:34:11 -0700313 out = str(p.communicate()[0].strip())
314 didGetVersion = (p.returncode == 0 and out != "")
315 if didGetVersion:
316 if out.startswith(GIT_TAG_PREFIX):
317 Context.g_module.VERSION = out[len(GIT_TAG_PREFIX):]
318 else:
319 Context.g_module.VERSION = "%s-commit-%s" % (Context.g_module.VERSION_BASE, out)
320 except OSError:
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -0700321 pass
322
Alexander Afanasyev6c632302014-10-31 12:34:11 -0700323 versionFile = ctx.path.find_node('VERSION')
324
325 if not didGetVersion and versionFile is not None:
326 try:
327 Context.g_module.VERSION = versionFile.read()
328 return
329 except (OSError, IOError):
330 pass
331
332 # version was obtained from git, update VERSION file if necessary
333 if versionFile is not None:
334 try:
335 version = versionFile.read()
336 if version == Context.g_module.VERSION:
337 return # no need to update
338 except (OSError, IOError):
339 Logs.warn("VERSION file exists, but not readable")
340 else:
341 versionFile = ctx.path.make_node('VERSION')
342
343 if versionFile is None:
344 return
345
346 try:
347 versionFile.write(Context.g_module.VERSION)
348 except (OSError, IOError):
349 Logs.warn("VERSION file is not writeable")
350
Alexander Afanasyevcfe0b062014-05-08 18:26:50 -0700351def dist(ctx):
352 version(ctx)
353
354def distcheck(ctx):
355 version(ctx)