blob: 9b2a76a892b2a6951fe87d3358171b2e73d49cee [file] [log] [blame]
Alexander Afanasyev2aa39622014-01-22 11:51:11 -08001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07002
3"""
Alexander Afanasyev19e2e6d2017-01-20 20:21:36 -08004Copyright (c) 2014-2017, Regents of the University of California,
taylorchu5ae28772015-03-08 17:45:52 -07005 Arizona Board of Regents,
6 Colorado State University,
7 University Pierre & Marie Curie, Sorbonne University,
8 Washington University in St. Louis,
9 Beijing Institute of Technology,
10 The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011
12This file is part of NFD (Named Data Networking Forwarding Daemon).
13See AUTHORS.md for complete list of NFD authors and contributors.
14
15NFD is free software: you can redistribute it and/or modify it under the terms
16of the GNU General Public License as published by the Free Software Foundation,
17either version 3 of the License, or (at your option) any later version.
18
19NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21PURPOSE. See the GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License along with
24NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
25"""
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080026
susmit3f94ec32018-02-13 12:21:01 -070027VERSION = "0.6.1"
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070028APPNAME = "nfd"
Eric Newberry81a9a862016-12-27 22:59:27 -070029BUGREPORT = "https://redmine.named-data.net/projects/nfd"
30URL = "https://named-data.net/doc/NFD/"
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -070031GIT_TAG_PREFIX = "NFD-"
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070032
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070033from waflib import Logs, Utils, Context
Alexander Afanasyev20757882014-08-25 22:39:08 -070034import os
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080035
36def options(opt):
Alexander Afanasyev5cda2e02014-04-11 13:58:44 -070037 opt.load(['compiler_cxx', 'gnu_dirs'])
Davide Pesavento67f30272016-08-10 01:55:16 +000038 opt.load(['default-compiler-flags', 'compiler-features', 'type_traits',
39 'coverage', 'pch', 'sanitizers', 'boost', 'boost-kqueue',
40 'dependency-checker', 'unix-socket', 'websocket',
41 'doxygen', 'sphinx_build'],
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070042 tooldir=['.waf-tools'])
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080043
44 nfdopt = opt.add_option_group('NFD Options')
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070045 opt.addUnixOptions(nfdopt)
Wentao Shang53df1632014-04-21 12:01:32 -070046 opt.addWebsocketOptions(nfdopt)
Davide Pesavento67f30272016-08-10 01:55:16 +000047
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070048 opt.addDependencyOptions(nfdopt, 'libpcap')
49 nfdopt.add_option('--without-libpcap', action='store_true', default=False,
50 dest='without_libpcap',
51 help='''Disable libpcap (Ethernet face support will be disabled)''')
52
Alexander Afanasyev5cda2e02014-04-11 13:58:44 -070053 opt.addDependencyOptions(nfdopt, 'librt', '(optional)')
54 opt.addDependencyOptions(nfdopt, 'libresolv', '(optional)')
55
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070056 nfdopt.add_option('--with-tests', action='store_true', default=False,
57 dest='with_tests', help='''Build unit tests''')
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040058 nfdopt.add_option('--with-other-tests', action='store_true', default=False,
59 dest='with_other_tests', help='''Build other tests''')
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060060
Alexander Afanasyev8269a052015-02-09 16:25:36 -080061 nfdopt.add_option('--with-custom-logger', type='string', default=None,
62 dest='with_custom_logger',
63 help='''Path to custom-logger.hpp and custom-logger-factory.hpp '''
64 '''implementing Logger and LoggerFactory interfaces''')
65
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080066def configure(conf):
Beichuan Zhang55b8ed42014-04-26 22:25:44 -070067 conf.load(['compiler_cxx', 'gnu_dirs',
Davide Pesavento67f30272016-08-10 01:55:16 +000068 'default-compiler-flags', 'compiler-features', 'type_traits',
Eric Newberry27bdd1a2016-11-01 18:02:54 -070069 'pch', 'boost', 'boost-kqueue', 'dependency-checker', 'websocket',
Davide Pesavento67f30272016-08-10 01:55:16 +000070 'doxygen', 'sphinx_build'])
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -070071
Davide Pesaventof0ae4422014-05-05 16:32:12 +020072 conf.find_program('bash', var='BASH')
73
taylorchu5ae28772015-03-08 17:45:52 -070074 if 'PKG_CONFIG_PATH' not in os.environ:
Alexander Afanasyevcdb8f362015-03-30 10:52:54 -070075 os.environ['PKG_CONFIG_PATH'] = Utils.subst_vars('${LIBDIR}/pkgconfig', conf.env)
Alexander Afanasyev4a771362014-04-24 21:29:33 -070076 conf.check_cfg(package='libndn-cxx', args=['--cflags', '--libs'],
77 uselib_store='NDN_CXX', mandatory=True)
Davide Pesavento44deacc2014-02-19 10:48:07 +010078
Davide Pesaventob499a602014-11-18 22:36:56 +010079 conf.checkDependency(name='librt', lib='rt', mandatory=False)
80 conf.checkDependency(name='libresolv', lib='resolv', mandatory=False)
81
Alexander Afanasyev748e9892015-01-28 15:07:41 -080082 if not conf.check_cxx(msg='Checking if privilege drop/elevation is supported', mandatory=False,
83 define_name='HAVE_PRIVILEGE_DROP_AND_ELEVATE', fragment='''
84#include <unistd.h>
85#include <pwd.h>
86#include <grp.h>
87int
88main(int, char**)
89{
Alexander Afanasyeve0d71b02016-05-25 11:39:13 -070090 char buffer[100];
Alexander Afanasyev748e9892015-01-28 15:07:41 -080091 ::sysconf(_SC_GETGR_R_SIZE_MAX);
92 group grp;
Alexander Afanasyeve0d71b02016-05-25 11:39:13 -070093 group* grpRes;
94 getgrnam_r("nogroup", &grp, buffer, 100, &grpRes);
Alexander Afanasyev748e9892015-01-28 15:07:41 -080095 passwd pwd;
Alexander Afanasyeve0d71b02016-05-25 11:39:13 -070096 passwd* pwdRes;
97 getpwnam_r("nobody", &pwd, buffer, 100, &pwdRes);
Alexander Afanasyev748e9892015-01-28 15:07:41 -080098
99 int ret = setegid(grp.gr_gid);
100 ret = seteuid(pwd.pw_uid);
101 (void)(ret);
Weiwei Liuf5aee942016-03-19 07:00:42 +0000102
103 getegid();
104 geteuid();
Alexander Afanasyev748e9892015-01-28 15:07:41 -0800105 return 0;
106}
107'''):
Junxiao Shia5765d62017-08-09 04:07:46 +0000108 Logs.warn('Dropping privileges is not supported on this platform')
Alexander Afanasyev748e9892015-01-28 15:07:41 -0800109
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000110 conf.check_cxx(header_name='valgrind/valgrind.h', define_name='HAVE_VALGRIND', mandatory=False)
Alexander Afanasyev49343f62015-01-26 21:58:07 -0800111
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800112 if conf.options.with_tests:
113 conf.env['WITH_TESTS'] = 1
Davide Pesavento67f30272016-08-10 01:55:16 +0000114 conf.define('WITH_TESTS', 1)
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800115
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400116 if conf.options.with_other_tests:
117 conf.env['WITH_OTHER_TESTS'] = 1
Eric Newberry52ae3292017-11-16 13:54:52 -0700118 conf.define('WITH_OTHER_TESTS', 1)
119
120 boost_libs = 'system chrono program_options thread log log_setup'
121 if conf.options.with_tests or conf.options.with_other_tests:
122 boost_libs += ' unit_test_framework'
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400123
Yumin Xiaab497452016-05-10 20:23:24 +0800124 conf.check_boost(lib=boost_libs, mt=True)
Davide Pesaventocafae242016-04-22 02:21:35 +0200125 if conf.env.BOOST_VERSION_NUMBER < 105400:
126 Logs.error("Minimum required boost version is 1.54.0")
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700127 Logs.error("Please upgrade your distribution or install custom boost libraries" +
Eric Newberry81a9a862016-12-27 22:59:27 -0700128 " (https://redmine.named-data.net/projects/nfd/wiki/Boost_FAQ)")
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800129 return
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800130
Alexander Afanasyev6602b3b2016-03-15 14:14:11 -0700131 if conf.env['CXX_NAME'] == 'clang' and conf.env.BOOST_VERSION_NUMBER < 105800:
Eric Newberry81a9a862016-12-27 22:59:27 -0700132 conf.define('BOOST_ASIO_HAS_STD_ARRAY', 1) # Workaround for https://redmine.named-data.net/issues/3360#note-14
Eric Newberrycb27ea82016-07-19 12:40:52 -0700133 conf.define('BOOST_ASIO_HAS_STD_CHRONO', 1) # Solution documented at https://redmine.named-data.net/issues/3588#note-10
Alexander Afanasyev6602b3b2016-03-15 14:14:11 -0700134
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800135 conf.load('unix-socket')
Wentao Shang53df1632014-04-21 12:01:32 -0700136 conf.checkWebsocket(mandatory=True)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100137
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700138 if not conf.options.without_libpcap:
Alexander Afanasyeve9186212014-08-23 20:15:48 -0700139 conf.check_asio_pcap_support()
140 if conf.env['HAVE_ASIO_PCAP_SUPPORT']:
141 conf.checkDependency(name='libpcap', lib='pcap', mandatory=True,
142 errmsg='not found, but required for Ethernet face support. '
143 'Specify --without-libpcap to disable Ethernet face support.')
144 else:
Junxiao Shia5765d62017-08-09 04:07:46 +0000145 Logs.warn('Warning: Ethernet face is not supported on this platform with Boost libraries version 1.56. '
Eric Newberry81a9a862016-12-27 22:59:27 -0700146 'See https://redmine.named-data.net/issues/1877 for more details')
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200147 if conf.env['HAVE_LIBPCAP']:
148 conf.check_cxx(function_name='pcap_set_immediate_mode', header_name='pcap/pcap.h',
149 cxxflags='-Wno-error', use='LIBPCAP', mandatory=False)
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700150
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800151 if conf.options.with_custom_logger:
152 conf.define('HAVE_CUSTOM_LOGGER', 1)
153 conf.env['INCLUDES_CUSTOM_LOGGER'] = [conf.options.with_custom_logger]
154 conf.env['HAVE_CUSTOM_LOGGER'] = 1
155
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400156 conf.check_compiler_flags()
157
Alexander Afanasyev689569b2014-02-16 20:20:07 -0800158 conf.load('coverage')
159
Eric Newberry27bdd1a2016-11-01 18:02:54 -0700160 conf.load('sanitizers')
161
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600162 conf.define('DEFAULT_CONFIG_FILE', '%s/ndn/nfd.conf' % conf.env['SYSCONFDIR'])
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700163
Junxiao Shi759f7062014-11-29 10:19:22 -0700164 # disable assertions in release builds
165 if not conf.options.debug:
166 conf.define('NDEBUG', 1)
167
Junxiao Shi9f5b01d2016-08-05 03:54:28 +0000168 conf.write_config_header('core/config.hpp')
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800169
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700170def build(bld):
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700171 version(bld)
172
173 bld(features="subst",
174 name='version',
Junxiao Shi9f5b01d2016-08-05 03:54:28 +0000175 source='core/version.hpp.in',
176 target='core/version.hpp',
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700177 install_path=None,
178 VERSION_STRING=VERSION_BASE,
179 VERSION_BUILD=VERSION,
180 VERSION=int(VERSION_SPLIT[0]) * 1000000 +
181 int(VERSION_SPLIT[1]) * 1000 +
182 int(VERSION_SPLIT[2]),
183 VERSION_MAJOR=VERSION_SPLIT[0],
184 VERSION_MINOR=VERSION_SPLIT[1],
Davide Pesavento67f30272016-08-10 01:55:16 +0000185 VERSION_PATCH=VERSION_SPLIT[2])
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700186
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700187 core = bld(
188 target='core-objects',
189 name='core-objects',
Alexander Afanasyev58ea4582014-05-30 08:38:56 +0300190 features='cxx pch',
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800191 source=bld.path.ant_glob(['core/**/*.cpp'],
192 excl=['core/logger*.cpp']),
Alexander Afanasyev3d1874a2016-06-15 12:25:01 -0700193 use='version NDN_CXX BOOST LIBRT',
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700194 includes='. core',
Junxiao Shi9f5b01d2016-08-05 03:54:28 +0000195 export_includes='.',
Davide Pesavento67f30272016-08-10 01:55:16 +0000196 headers='core/common.hpp')
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700197
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800198 if bld.env['HAVE_CUSTOM_LOGGER']:
199 core.use += " CUSTOM_LOGGER"
200 else:
201 core.source += bld.path.ant_glob(['core/logger*.cpp'])
202
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800203 nfd_objects = bld(
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700204 target='daemon-objects',
205 name='daemon-objects',
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -0700206 features='cxx',
207 source=bld.path.ant_glob(['daemon/**/*.cpp'],
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400208 excl=['daemon/face/*ethernet*.cpp',
209 'daemon/face/*pcap*.cpp',
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -0700210 'daemon/face/unix-*.cpp',
Wentao Shang53df1632014-04-21 12:01:32 -0700211 'daemon/face/websocket-*.cpp',
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -0700212 'daemon/main.cpp']),
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600213 use='core-objects WEBSOCKET',
214 includes='daemon',
Davide Pesavento67f30272016-08-10 01:55:16 +0000215 export_includes='daemon')
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700216
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700217 if bld.env['HAVE_LIBPCAP']:
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400218 nfd_objects.source += bld.path.ant_glob('daemon/face/*ethernet*.cpp')
219 nfd_objects.source += bld.path.ant_glob('daemon/face/*pcap*.cpp')
Alexander Afanasyev5cda2e02014-04-11 13:58:44 -0700220 nfd_objects.use += ' LIBPCAP'
Davide Pesavento44deacc2014-02-19 10:48:07 +0100221
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800222 if bld.env['HAVE_UNIX_SOCKETS']:
Davide Pesavento44deacc2014-02-19 10:48:07 +0100223 nfd_objects.source += bld.path.ant_glob('daemon/face/unix-*.cpp')
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800224
Wentao Shang53df1632014-04-21 12:01:32 -0700225 if bld.env['HAVE_WEBSOCKET']:
226 nfd_objects.source += bld.path.ant_glob('daemon/face/websocket-*.cpp')
227
Eric Newberry69b63dc2017-11-04 15:42:46 -0700228 if bld.env['WITH_OTHER_TESTS']:
229 nfd_objects.source += bld.path.ant_glob('tests/other/fw/*.cpp')
230
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700231 rib_objects = bld(
232 target='rib-objects',
233 name='rib-objects',
234 features='cxx',
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800235 source=bld.path.ant_glob(['rib/**/*.cpp']),
Davide Pesavento67f30272016-08-10 01:55:16 +0000236 use='core-objects')
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700237
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800238 bld(target='bin/nfd',
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700239 features='cxx cxxprogram',
Alexander Afanasyevf08a7372015-02-09 21:28:19 -0800240 source='daemon/main.cpp',
Davide Pesavento67f30272016-08-10 01:55:16 +0000241 use='daemon-objects rib-objects')
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700242
Alexander Afanasyev262203b2015-01-26 16:39:59 -0800243 bld.recurse("tools")
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700244 bld.recurse("tests")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400245
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -0700246 bld(features="subst",
247 source='nfd.conf.sample.in',
248 target='nfd.conf.sample',
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700249 install_path="${SYSCONFDIR}/ndn",
Wentao Shang53df1632014-04-21 12:01:32 -0700250 IF_HAVE_LIBPCAP="" if bld.env['HAVE_LIBPCAP'] else "; ",
251 IF_HAVE_WEBSOCKET="" if bld.env['HAVE_WEBSOCKET'] else "; ")
Alexander Afanasyev35fc2b72014-02-13 16:56:21 -0800252
Alexander Afanasyev49272f72014-04-06 21:49:46 -0700253 if bld.env['SPHINX_BUILD']:
254 bld(features="sphinx",
255 builder="man",
256 outdir="docs/manpages",
257 config="docs/conf.py",
258 source=bld.path.ant_glob('docs/manpages/**/*.rst'),
Beichuan Zhang55b8ed42014-04-26 22:25:44 -0700259 install_path="${MANDIR}/",
260 VERSION=VERSION)
Junxiao Shi6c135622016-11-21 14:30:33 +0000261 bld.symlink_as('${MANDIR}/man1/nfdc-channel.1', 'nfdc-face.1')
Junxiao Shi6c135622016-11-21 14:30:33 +0000262 bld.symlink_as('${MANDIR}/man1/nfdc-fib.1', 'nfdc-route.1')
263 bld.symlink_as('${MANDIR}/man1/nfdc-register.1', 'nfdc-route.1')
264 bld.symlink_as('${MANDIR}/man1/nfdc-unregister.1', 'nfdc-route.1')
265 bld.symlink_as('${MANDIR}/man1/nfdc-set-strategy.1', 'nfdc-strategy.1')
266 bld.symlink_as('${MANDIR}/man1/nfdc-unset-strategy.1', 'nfdc-strategy.1')
Alexander Afanasyev49272f72014-04-06 21:49:46 -0700267
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800268 bld.install_files("${SYSCONFDIR}/ndn", "autoconfig.conf.sample")
269
Alexander Afanasyev284257b2014-04-11 14:16:51 -0700270def docs(bld):
271 from waflib import Options
272 Options.commands = ['doxygen', 'sphinx'] + Options.commands
273
Alexander Afanasyev35fc2b72014-02-13 16:56:21 -0800274def doxygen(bld):
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700275 version(bld)
276
Alexander Afanasyev35fc2b72014-02-13 16:56:21 -0800277 if not bld.env.DOXYGEN:
Beichuan Zhang55b8ed42014-04-26 22:25:44 -0700278 Logs.error("ERROR: cannot build documentation (`doxygen' is not found in $PATH)")
279 else:
280 bld(features="subst",
281 name="doxygen-conf",
Alexander Afanasyev20757882014-08-25 22:39:08 -0700282 source=["docs/doxygen.conf.in",
283 "docs/named_data_theme/named_data_footer-with-analytics.html.in"],
284 target=["docs/doxygen.conf",
285 "docs/named_data_theme/named_data_footer-with-analytics.html"],
Alexander Afanasyevabb307c2015-10-23 17:00:13 -0700286 VERSION=VERSION,
Alexander Afanasyev20757882014-08-25 22:39:08 -0700287 HTML_FOOTER="../build/docs/named_data_theme/named_data_footer-with-analytics.html" \
288 if os.getenv('GOOGLE_ANALYTICS', None) \
289 else "../docs/named_data_theme/named_data_footer.html",
Davide Pesavento67f30272016-08-10 01:55:16 +0000290 GOOGLE_ANALYTICS=os.getenv('GOOGLE_ANALYTICS', ""))
Beichuan Zhang55b8ed42014-04-26 22:25:44 -0700291
292 bld(features="doxygen",
293 doxyfile='docs/doxygen.conf',
294 use="doxygen-conf")
Alexander Afanasyev49272f72014-04-06 21:49:46 -0700295
296def sphinx(bld):
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700297 version(bld)
298
Beichuan Zhang55b8ed42014-04-26 22:25:44 -0700299 if not bld.env.SPHINX_BUILD:
300 bld.fatal("ERROR: cannot build documentation (`sphinx-build' is not found in $PATH)")
301 else:
302 bld(features="sphinx",
303 outdir="docs",
304 source=bld.path.ant_glob('docs/**/*.rst'),
305 config="docs/conf.py",
Alexander Afanasyevabb307c2015-10-23 17:00:13 -0700306 VERSION=VERSION)
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700307
308def version(ctx):
Alexander Afanasyev26181532014-05-07 23:38:51 -0700309 if getattr(Context.g_module, 'VERSION_BASE', None):
310 return
311
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700312 Context.g_module.VERSION_BASE = Context.g_module.VERSION
313 Context.g_module.VERSION_SPLIT = [v for v in VERSION_BASE.split('.')]
314
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700315 didGetVersion = False
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700316 try:
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700317 cmd = ['git', 'describe', '--always', '--match', '%s*' % GIT_TAG_PREFIX]
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700318 p = Utils.subprocess.Popen(cmd, stdout=Utils.subprocess.PIPE,
319 stderr=None, stdin=None)
Alexander Afanasyev3b21fa32014-09-01 13:25:20 -0700320 out = str(p.communicate()[0].strip())
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700321 didGetVersion = (p.returncode == 0 and out != "")
322 if didGetVersion:
323 if out.startswith(GIT_TAG_PREFIX):
324 Context.g_module.VERSION = out[len(GIT_TAG_PREFIX):]
325 else:
326 Context.g_module.VERSION = "%s-commit-%s" % (Context.g_module.VERSION_BASE, out)
327 except OSError:
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700328 pass
329
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700330 versionFile = ctx.path.find_node('VERSION')
331
332 if not didGetVersion and versionFile is not None:
333 try:
334 Context.g_module.VERSION = versionFile.read()
335 return
336 except (OSError, IOError):
337 pass
338
339 # version was obtained from git, update VERSION file if necessary
340 if versionFile is not None:
341 try:
342 version = versionFile.read()
343 if version == Context.g_module.VERSION:
344 return # no need to update
345 except (OSError, IOError):
346 Logs.warn("VERSION file exists, but not readable")
347 else:
348 versionFile = ctx.path.make_node('VERSION')
349
350 if versionFile is None:
351 return
352
353 try:
354 versionFile.write(Context.g_module.VERSION)
355 except (OSError, IOError):
356 Logs.warn("VERSION file is not writeable")
357
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700358def dist(ctx):
359 version(ctx)
360
361def distcheck(ctx):
362 version(ctx)