blob: 8a2efd4e36bdb50795d73f29de20a7bfba4bf839 [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"""
Davide Pesavento0064c1d2018-03-03 18:43:53 -05003Copyright (c) 2014-2018, Regents of the University of California,
taylorchu5ae28772015-03-08 17:45:52 -07004 Arizona Board of Regents,
5 Colorado State University,
6 University Pierre & Marie Curie, Sorbonne University,
7 Washington University in St. Louis,
8 Beijing Institute of Technology,
9 The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010
11This file is part of NFD (Named Data Networking Forwarding Daemon).
12See AUTHORS.md for complete list of NFD authors and contributors.
13
14NFD is free software: you can redistribute it and/or modify it under the terms
15of the GNU General Public License as published by the Free Software Foundation,
16either version 3 of the License, or (at your option) any later version.
17
18NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20PURPOSE. See the GNU General Public License for more details.
21
22You should have received a copy of the GNU General Public License along with
23NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24"""
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080025
Davide Pesavento0064c1d2018-03-03 18:43:53 -050026from waflib import Context, Logs, Utils
27import os, subprocess
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070028
Alexander Afanasyev782a1d12018-05-02 16:23:26 -040029VERSION = '0.6.2'
Davide Pesavento0064c1d2018-03-03 18:43:53 -050030APPNAME = 'nfd'
31BUGREPORT = 'https://redmine.named-data.net/projects/nfd'
32URL = 'https://named-data.net/doc/NFD/'
33GIT_TAG_PREFIX = 'NFD-'
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080034
35def options(opt):
Alexander Afanasyev5cda2e02014-04-11 13:58:44 -070036 opt.load(['compiler_cxx', 'gnu_dirs'])
Davide Pesavento67f30272016-08-10 01:55:16 +000037 opt.load(['default-compiler-flags', 'compiler-features', 'type_traits',
38 'coverage', 'pch', 'sanitizers', 'boost', 'boost-kqueue',
39 'dependency-checker', 'unix-socket', 'websocket',
40 'doxygen', 'sphinx_build'],
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070041 tooldir=['.waf-tools'])
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080042
43 nfdopt = opt.add_option_group('NFD Options')
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070044 opt.addUnixOptions(nfdopt)
Wentao Shang53df1632014-04-21 12:01:32 -070045 opt.addWebsocketOptions(nfdopt)
Davide Pesavento67f30272016-08-10 01:55:16 +000046
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070047 opt.addDependencyOptions(nfdopt, 'libpcap')
48 nfdopt.add_option('--without-libpcap', action='store_true', default=False,
Davide Pesavento0064c1d2018-03-03 18:43:53 -050049 help='Disable libpcap (Ethernet face support will be disabled)')
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070050
Davide Pesavento0064c1d2018-03-03 18:43:53 -050051 opt.addDependencyOptions(nfdopt, 'librt')
52 opt.addDependencyOptions(nfdopt, 'libresolv')
Alexander Afanasyev5cda2e02014-04-11 13:58:44 -070053
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070054 nfdopt.add_option('--with-tests', action='store_true', default=False,
Davide Pesavento0064c1d2018-03-03 18:43:53 -050055 help='Build unit tests')
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040056 nfdopt.add_option('--with-other-tests', action='store_true', default=False,
Davide Pesavento0064c1d2018-03-03 18:43:53 -050057 help='Build other tests')
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060058
Davide Pesavento0064c1d2018-03-03 18:43:53 -050059 nfdopt.add_option('--with-custom-logger', metavar='PATH',
60 help='Path to custom-logger.hpp and custom-logger-factory.hpp '
61 'implementing Logger and LoggerFactory interfaces')
62
63PRIVILEGE_CHECK_CODE = '''
64#include <unistd.h>
65#include <grp.h>
66#include <pwd.h>
67int main()
68{
69 sysconf(_SC_GETGR_R_SIZE_MAX);
70
71 char buffer[100];
72 group grp;
73 group* grpRes;
74 getgrnam_r("nogroup", &grp, buffer, 100, &grpRes);
75 passwd pwd;
76 passwd* pwdRes;
77 getpwnam_r("nobody", &pwd, buffer, 100, &pwdRes);
78
79 int ret = setegid(grp.gr_gid);
80 ret = seteuid(pwd.pw_uid);
81 (void)(ret);
82
83 getegid();
84 geteuid();
85}
86'''
Alexander Afanasyev8269a052015-02-09 16:25:36 -080087
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080088def configure(conf):
Beichuan Zhang55b8ed42014-04-26 22:25:44 -070089 conf.load(['compiler_cxx', 'gnu_dirs',
Davide Pesavento67f30272016-08-10 01:55:16 +000090 'default-compiler-flags', 'compiler-features', 'type_traits',
Eric Newberry27bdd1a2016-11-01 18:02:54 -070091 'pch', 'boost', 'boost-kqueue', 'dependency-checker', 'websocket',
Davide Pesavento67f30272016-08-10 01:55:16 +000092 'doxygen', 'sphinx_build'])
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -070093
Davide Pesaventof0ae4422014-05-05 16:32:12 +020094 conf.find_program('bash', var='BASH')
95
taylorchu5ae28772015-03-08 17:45:52 -070096 if 'PKG_CONFIG_PATH' not in os.environ:
Alexander Afanasyevcdb8f362015-03-30 10:52:54 -070097 os.environ['PKG_CONFIG_PATH'] = Utils.subst_vars('${LIBDIR}/pkgconfig', conf.env)
Alexander Afanasyev4a771362014-04-24 21:29:33 -070098 conf.check_cfg(package='libndn-cxx', args=['--cflags', '--libs'],
99 uselib_store='NDN_CXX', mandatory=True)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100100
Davide Pesaventob499a602014-11-18 22:36:56 +0100101 conf.checkDependency(name='librt', lib='rt', mandatory=False)
102 conf.checkDependency(name='libresolv', lib='resolv', mandatory=False)
103
Alexander Afanasyev748e9892015-01-28 15:07:41 -0800104 if not conf.check_cxx(msg='Checking if privilege drop/elevation is supported', mandatory=False,
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500105 define_name='HAVE_PRIVILEGE_DROP_AND_ELEVATE', fragment=PRIVILEGE_CHECK_CODE):
Junxiao Shia5765d62017-08-09 04:07:46 +0000106 Logs.warn('Dropping privileges is not supported on this platform')
Alexander Afanasyev748e9892015-01-28 15:07:41 -0800107
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000108 conf.check_cxx(header_name='valgrind/valgrind.h', define_name='HAVE_VALGRIND', mandatory=False)
Alexander Afanasyev49343f62015-01-26 21:58:07 -0800109
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800110 if conf.options.with_tests:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500111 conf.env.WITH_TESTS = True
Davide Pesavento67f30272016-08-10 01:55:16 +0000112 conf.define('WITH_TESTS', 1)
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800113
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400114 if conf.options.with_other_tests:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500115 conf.env.WITH_OTHER_TESTS = True
Eric Newberry52ae3292017-11-16 13:54:52 -0700116 conf.define('WITH_OTHER_TESTS', 1)
117
118 boost_libs = 'system chrono program_options thread log log_setup'
119 if conf.options.with_tests or conf.options.with_other_tests:
120 boost_libs += ' unit_test_framework'
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400121
Yumin Xiaab497452016-05-10 20:23:24 +0800122 conf.check_boost(lib=boost_libs, mt=True)
Davide Pesaventocafae242016-04-22 02:21:35 +0200123 if conf.env.BOOST_VERSION_NUMBER < 105400:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500124 conf.fatal('Minimum required Boost version is 1.54.0\n'
125 'Please upgrade your distribution or manually install a newer version of Boost'
126 ' (https://redmine.named-data.net/projects/nfd/wiki/Boost_FAQ)')
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800127
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500128 if conf.env.CXX_NAME == 'clang' and conf.env.BOOST_VERSION_NUMBER < 105800:
Eric Newberry81a9a862016-12-27 22:59:27 -0700129 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 -0700130 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 -0700131
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800132 conf.load('unix-socket')
Wentao Shang53df1632014-04-21 12:01:32 -0700133 conf.checkWebsocket(mandatory=True)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100134
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700135 if not conf.options.without_libpcap:
Alexander Afanasyeve9186212014-08-23 20:15:48 -0700136 conf.check_asio_pcap_support()
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500137 if conf.env.HAVE_ASIO_PCAP_SUPPORT:
Alexander Afanasyeve9186212014-08-23 20:15:48 -0700138 conf.checkDependency(name='libpcap', lib='pcap', mandatory=True,
139 errmsg='not found, but required for Ethernet face support. '
140 'Specify --without-libpcap to disable Ethernet face support.')
141 else:
Junxiao Shia5765d62017-08-09 04:07:46 +0000142 Logs.warn('Warning: Ethernet face is not supported on this platform with Boost libraries version 1.56. '
Eric Newberry81a9a862016-12-27 22:59:27 -0700143 'See https://redmine.named-data.net/issues/1877 for more details')
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500144 if conf.env.HAVE_LIBPCAP:
145 conf.check_cxx(msg='Checking if libpcap supports pcap_set_immediate_mode', mandatory=False,
146 define_name='HAVE_PCAP_SET_IMMEDIATE_MODE', use='LIBPCAP',
147 fragment='''#include <pcap/pcap.h>
148 int main() { pcap_t* p; pcap_set_immediate_mode(p, 1); }''')
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700149
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800150 if conf.options.with_custom_logger:
151 conf.define('HAVE_CUSTOM_LOGGER', 1)
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500152 conf.env.HAVE_CUSTOM_LOGGER = True
153 conf.env.INCLUDES_CUSTOM_LOGGER = [conf.options.with_custom_logger]
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800154
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400155 conf.check_compiler_flags()
156
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500157 # Loading "late" to prevent tests from being compiled with profiling flags
Alexander Afanasyev689569b2014-02-16 20:20:07 -0800158 conf.load('coverage')
Eric Newberry27bdd1a2016-11-01 18:02:54 -0700159 conf.load('sanitizers')
160
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500161 conf.define('DEFAULT_CONFIG_FILE', '%s/ndn/nfd.conf' % conf.env.SYSCONFDIR)
Junxiao Shi759f7062014-11-29 10:19:22 -0700162 # disable assertions in release builds
163 if not conf.options.debug:
164 conf.define('NDEBUG', 1)
Junxiao Shi9f5b01d2016-08-05 03:54:28 +0000165 conf.write_config_header('core/config.hpp')
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800166
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700167def build(bld):
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700168 version(bld)
169
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500170 bld(features='subst',
171 name='version.hpp',
Junxiao Shi9f5b01d2016-08-05 03:54:28 +0000172 source='core/version.hpp.in',
173 target='core/version.hpp',
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700174 install_path=None,
175 VERSION_STRING=VERSION_BASE,
176 VERSION_BUILD=VERSION,
177 VERSION=int(VERSION_SPLIT[0]) * 1000000 +
178 int(VERSION_SPLIT[1]) * 1000 +
179 int(VERSION_SPLIT[2]),
180 VERSION_MAJOR=VERSION_SPLIT[0],
181 VERSION_MINOR=VERSION_SPLIT[1],
Davide Pesavento67f30272016-08-10 01:55:16 +0000182 VERSION_PATCH=VERSION_SPLIT[2])
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700183
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500184 core = bld.objects(
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700185 target='core-objects',
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500186 features='pch',
187 source=bld.path.ant_glob('core/**/*.cpp', excl=['core/logger*.cpp']),
188 use='version.hpp NDN_CXX BOOST LIBRT',
189 includes='.',
Junxiao Shi9f5b01d2016-08-05 03:54:28 +0000190 export_includes='.',
Davide Pesavento67f30272016-08-10 01:55:16 +0000191 headers='core/common.hpp')
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700192
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500193 if bld.env.HAVE_CUSTOM_LOGGER:
194 core.use += ' CUSTOM_LOGGER'
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800195 else:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500196 core.source += bld.path.ant_glob('core/logger*.cpp')
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800197
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500198 nfd_objects = bld.objects(
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700199 target='daemon-objects',
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500200 source=bld.path.ant_glob('daemon/**/*.cpp',
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400201 excl=['daemon/face/*ethernet*.cpp',
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500202 'daemon/face/pcap*.cpp',
203 'daemon/face/unix*.cpp',
204 'daemon/face/websocket*.cpp',
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -0700205 'daemon/main.cpp']),
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500206 use='core-objects',
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600207 includes='daemon',
Davide Pesavento67f30272016-08-10 01:55:16 +0000208 export_includes='daemon')
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700209
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500210 if bld.env.HAVE_LIBPCAP:
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400211 nfd_objects.source += bld.path.ant_glob('daemon/face/*ethernet*.cpp')
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500212 nfd_objects.source += bld.path.ant_glob('daemon/face/pcap*.cpp')
Alexander Afanasyev5cda2e02014-04-11 13:58:44 -0700213 nfd_objects.use += ' LIBPCAP'
Davide Pesavento44deacc2014-02-19 10:48:07 +0100214
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500215 if bld.env.HAVE_UNIX_SOCKETS:
216 nfd_objects.source += bld.path.ant_glob('daemon/face/unix*.cpp')
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800217
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500218 if bld.env.HAVE_WEBSOCKET:
219 nfd_objects.source += bld.path.ant_glob('daemon/face/websocket*.cpp')
220 nfd_objects.use += ' WEBSOCKET'
Wentao Shang53df1632014-04-21 12:01:32 -0700221
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500222 if bld.env.WITH_OTHER_TESTS:
Eric Newberry69b63dc2017-11-04 15:42:46 -0700223 nfd_objects.source += bld.path.ant_glob('tests/other/fw/*.cpp')
224
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500225 bld.objects(target='rib-objects',
226 source=bld.path.ant_glob('rib/**/*.cpp'),
227 use='core-objects')
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700228
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500229 bld.program(name='nfd',
230 target='bin/nfd',
231 source='daemon/main.cpp',
232 use='daemon-objects rib-objects')
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700233
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500234 bld.recurse('tools')
235 bld.recurse('tests')
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400236
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500237 bld(features='subst',
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -0700238 source='nfd.conf.sample.in',
239 target='nfd.conf.sample',
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500240 install_path='${SYSCONFDIR}/ndn',
241 IF_HAVE_LIBPCAP='' if bld.env.HAVE_LIBPCAP else '; ',
242 IF_HAVE_WEBSOCKET='' if bld.env.HAVE_WEBSOCKET else '; ')
Alexander Afanasyev35fc2b72014-02-13 16:56:21 -0800243
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500244 if bld.env.SPHINX_BUILD:
245 bld(features='sphinx',
246 name='manpages',
247 builder='man',
248 config='docs/conf.py',
249 outdir='docs/manpages',
Alexander Afanasyev49272f72014-04-06 21:49:46 -0700250 source=bld.path.ant_glob('docs/manpages/**/*.rst'),
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500251 install_path='${MANDIR}',
Beichuan Zhang55b8ed42014-04-26 22:25:44 -0700252 VERSION=VERSION)
Junxiao Shi6c135622016-11-21 14:30:33 +0000253 bld.symlink_as('${MANDIR}/man1/nfdc-channel.1', 'nfdc-face.1')
Junxiao Shi6c135622016-11-21 14:30:33 +0000254 bld.symlink_as('${MANDIR}/man1/nfdc-fib.1', 'nfdc-route.1')
255 bld.symlink_as('${MANDIR}/man1/nfdc-register.1', 'nfdc-route.1')
256 bld.symlink_as('${MANDIR}/man1/nfdc-unregister.1', 'nfdc-route.1')
257 bld.symlink_as('${MANDIR}/man1/nfdc-set-strategy.1', 'nfdc-strategy.1')
258 bld.symlink_as('${MANDIR}/man1/nfdc-unset-strategy.1', 'nfdc-strategy.1')
Alexander Afanasyev49272f72014-04-06 21:49:46 -0700259
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500260 bld.install_files('${SYSCONFDIR}/ndn', 'autoconfig.conf.sample')
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800261
Alexander Afanasyev284257b2014-04-11 14:16:51 -0700262def docs(bld):
263 from waflib import Options
264 Options.commands = ['doxygen', 'sphinx'] + Options.commands
265
Alexander Afanasyev35fc2b72014-02-13 16:56:21 -0800266def doxygen(bld):
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700267 version(bld)
268
Alexander Afanasyev35fc2b72014-02-13 16:56:21 -0800269 if not bld.env.DOXYGEN:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500270 bld.fatal('Cannot build documentation ("doxygen" not found in PATH)')
Beichuan Zhang55b8ed42014-04-26 22:25:44 -0700271
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500272 bld(features='subst',
273 name='doxygen.conf',
274 source=['docs/doxygen.conf.in',
275 'docs/named_data_theme/named_data_footer-with-analytics.html.in'],
276 target=['docs/doxygen.conf',
277 'docs/named_data_theme/named_data_footer-with-analytics.html'],
278 VERSION=VERSION,
279 HTML_FOOTER='../build/docs/named_data_theme/named_data_footer-with-analytics.html' \
280 if os.getenv('GOOGLE_ANALYTICS', None) \
281 else '../docs/named_data_theme/named_data_footer.html',
282 GOOGLE_ANALYTICS=os.getenv('GOOGLE_ANALYTICS', ''))
283
284 bld(features='doxygen',
285 doxyfile='docs/doxygen.conf',
286 use='doxygen.conf')
Alexander Afanasyev49272f72014-04-06 21:49:46 -0700287
288def sphinx(bld):
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700289 version(bld)
290
Beichuan Zhang55b8ed42014-04-26 22:25:44 -0700291 if not bld.env.SPHINX_BUILD:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500292 bld.fatal('Cannot build documentation ("sphinx-build" not found in PATH)')
293
294 bld(features='sphinx',
295 config='docs/conf.py',
296 outdir='docs',
297 source=bld.path.ant_glob('docs/**/*.rst'),
298 VERSION=VERSION)
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700299
300def version(ctx):
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500301 # don't execute more than once
Alexander Afanasyev26181532014-05-07 23:38:51 -0700302 if getattr(Context.g_module, 'VERSION_BASE', None):
303 return
304
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700305 Context.g_module.VERSION_BASE = Context.g_module.VERSION
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500306 Context.g_module.VERSION_SPLIT = VERSION_BASE.split('.')
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700307
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500308 # first, try to get a version string from git
309 gotVersionFromGit = False
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700310 try:
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700311 cmd = ['git', 'describe', '--always', '--match', '%s*' % GIT_TAG_PREFIX]
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500312 out = subprocess.check_output(cmd, universal_newlines=True).strip()
313 if out:
314 gotVersionFromGit = True
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700315 if out.startswith(GIT_TAG_PREFIX):
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500316 Context.g_module.VERSION = out.lstrip(GIT_TAG_PREFIX)
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700317 else:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500318 # no tags matched
319 Context.g_module.VERSION = '%s-commit-%s' % (VERSION_BASE, out)
320 except subprocess.CalledProcessError:
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700321 pass
322
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700323 versionFile = ctx.path.find_node('VERSION')
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500324 if not gotVersionFromGit and versionFile is not None:
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700325 try:
326 Context.g_module.VERSION = versionFile.read()
327 return
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500328 except EnvironmentError:
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700329 pass
330
331 # version was obtained from git, update VERSION file if necessary
332 if versionFile is not None:
333 try:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500334 if versionFile.read() == Context.g_module.VERSION:
335 # already up-to-date
336 return
337 except EnvironmentError as e:
338 Logs.warn('%s exists but is not readable (%s)' % (versionFile, e.strerror))
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700339 else:
340 versionFile = ctx.path.make_node('VERSION')
341
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700342 try:
343 versionFile.write(Context.g_module.VERSION)
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500344 except EnvironmentError as e:
345 Logs.warn('%s is not writable (%s)' % (versionFile, e.strerror))
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700346
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700347def dist(ctx):
348 version(ctx)
349
350def distcheck(ctx):
351 version(ctx)