blob: fda954ef399aa2e4c52cfd47ce57b906ec45db7a [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 Pesavento4c957712024-01-01 15:40:06 -05003Copyright (c) 2014-2024, 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 Pesaventoa9e1ab22023-10-02 22:10:45 -040026import os
27import subprocess
Davide Pesavento0064c1d2018-03-03 18:43:53 -050028from waflib import Context, Logs, Utils
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070029
Davide Pesaventoa1480a72024-07-28 00:24:25 -040030VERSION = '24.07'
Davide Pesavento0064c1d2018-03-03 18:43:53 -050031APPNAME = 'nfd'
Davide Pesavento0064c1d2018-03-03 18:43:53 -050032GIT_TAG_PREFIX = 'NFD-'
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080033
34def options(opt):
Alexander Afanasyev5cda2e02014-04-11 13:58:44 -070035 opt.load(['compiler_cxx', 'gnu_dirs'])
Davide Pesavento40641272023-03-16 13:31:12 -040036 opt.load(['default-compiler-flags', 'pch',
37 'coverage', 'sanitizers', 'boost',
Davide Pesavento67f30272016-08-10 01:55:16 +000038 'dependency-checker', 'unix-socket', 'websocket',
Davide Pesavento0d89d412024-03-13 14:20:02 -040039 'doxygen', 'sphinx'],
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070040 tooldir=['.waf-tools'])
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080041
Davide Pesavento17521592020-05-14 19:01:32 -040042 optgrp = opt.add_option_group('NFD Options')
Davide Pesavento67f30272016-08-10 01:55:16 +000043
Davide Pesavento17521592020-05-14 19:01:32 -040044 opt.addUnixOptions(optgrp)
45 opt.addDependencyOptions(optgrp, 'libresolv')
46 opt.addDependencyOptions(optgrp, 'librt')
47 opt.addDependencyOptions(optgrp, 'libpcap')
48 optgrp.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)')
Davide Pesavento17521592020-05-14 19:01:32 -040050 optgrp.add_option('--without-systemd', action='store_true', default=False,
Davide Pesavento774071c2018-11-15 21:33:23 -050051 help='Disable systemd integration')
Davide Pesavento17521592020-05-14 19:01:32 -040052 opt.addWebsocketOptions(optgrp)
Alexander Afanasyev5cda2e02014-04-11 13:58:44 -070053
Davide Pesavento17521592020-05-14 19:01:32 -040054 optgrp.add_option('--with-tests', action='store_true', default=False,
Davide Pesavento0064c1d2018-03-03 18:43:53 -050055 help='Build unit tests')
Davide Pesavento17521592020-05-14 19:01:32 -040056 optgrp.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 -050059PRIVILEGE_CHECK_CODE = '''
60#include <unistd.h>
61#include <grp.h>
62#include <pwd.h>
63int main()
64{
65 sysconf(_SC_GETGR_R_SIZE_MAX);
66
67 char buffer[100];
68 group grp;
69 group* grpRes;
70 getgrnam_r("nogroup", &grp, buffer, 100, &grpRes);
71 passwd pwd;
72 passwd* pwdRes;
73 getpwnam_r("nobody", &pwd, buffer, 100, &pwdRes);
74
75 int ret = setegid(grp.gr_gid);
76 ret = seteuid(pwd.pw_uid);
77 (void)(ret);
78
79 getegid();
80 geteuid();
81}
82'''
Alexander Afanasyev8269a052015-02-09 16:25:36 -080083
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080084def configure(conf):
Beichuan Zhang55b8ed42014-04-26 22:25:44 -070085 conf.load(['compiler_cxx', 'gnu_dirs',
Davide Pesavento40641272023-03-16 13:31:12 -040086 'default-compiler-flags', 'pch',
87 'boost', 'dependency-checker', 'websocket',
Davide Pesavento0d89d412024-03-13 14:20:02 -040088 'doxygen', 'sphinx'])
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -070089
Davide Pesavento6ecc3f82019-02-17 22:23:08 -050090 conf.env.WITH_TESTS = conf.options.with_tests
91 conf.env.WITH_OTHER_TESTS = conf.options.with_other_tests
Davide Pesavento774071c2018-11-15 21:33:23 -050092
Davide Pesavento2150da82022-07-08 18:18:51 -040093 conf.find_program('bash')
94 conf.find_program('dot', mandatory=False)
95
96 # Prefer pkgconf if it's installed, because it gives more correct results
97 # on Fedora/CentOS/RHEL/etc. See https://bugzilla.redhat.com/show_bug.cgi?id=1953348
98 # Store the result in env.PKGCONFIG, which is the variable used inside check_cfg()
99 conf.find_program(['pkgconf', 'pkg-config'], var='PKGCONFIG')
Davide Pesaventof0ae4422014-05-05 16:32:12 +0200100
Davide Pesaventodeb54272022-03-11 18:51:05 -0500101 pkg_config_path = os.environ.get('PKG_CONFIG_PATH', f'{conf.env.LIBDIR}/pkgconfig')
Davide Pesaventoa1480a72024-07-28 00:24:25 -0400102 conf.check_cfg(package='libndn-cxx', args=['libndn-cxx >= 0.9.0', '--cflags', '--libs'],
Davide Pesaventodeb54272022-03-11 18:51:05 -0500103 uselib_store='NDN_CXX', pkg_config_path=pkg_config_path)
Davide Pesavento44deacc2014-02-19 10:48:07 +0100104
Davide Pesavento6ecc3f82019-02-17 22:23:08 -0500105 if not conf.options.without_systemd:
106 conf.check_cfg(package='libsystemd', args=['--cflags', '--libs'],
107 uselib_store='SYSTEMD', mandatory=False)
Davide Pesavento774071c2018-11-15 21:33:23 -0500108
Davide Pesaventob499a602014-11-18 22:36:56 +0100109 conf.checkDependency(name='librt', lib='rt', mandatory=False)
110 conf.checkDependency(name='libresolv', lib='resolv', mandatory=False)
111
Davide Pesavento6ecc3f82019-02-17 22:23:08 -0500112 conf.check_cxx(msg='Checking if privilege drop/elevation is supported', mandatory=False,
113 define_name='HAVE_PRIVILEGE_DROP_AND_ELEVATE', fragment=PRIVILEGE_CHECK_CODE)
Alexander Afanasyev748e9892015-01-28 15:07:41 -0800114
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000115 conf.check_cxx(header_name='valgrind/valgrind.h', define_name='HAVE_VALGRIND', mandatory=False)
Alexander Afanasyev49343f62015-01-26 21:58:07 -0800116
Davide Pesavento401d1a42024-12-19 21:10:22 -0500117 conf.check_boost(lib='program_options', mt=True)
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400118 if conf.env.BOOST_VERSION_NUMBER < 107100:
119 conf.fatal('The minimum supported version of Boost is 1.71.0.\n'
Davide Pesavento17521592020-05-14 19:01:32 -0400120 'Please upgrade your distribution or manually install a newer version of Boost.\n'
121 'For more information, see https://redmine.named-data.net/projects/nfd/wiki/Boost')
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800122
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400123 if conf.env.WITH_TESTS or conf.env.WITH_OTHER_TESTS:
124 conf.check_boost(lib='unit_test_framework', mt=True, uselib_store='BOOST_TESTS')
125
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800126 conf.load('unix-socket')
Davide Pesavento44deacc2014-02-19 10:48:07 +0100127
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700128 if not conf.options.without_libpcap:
Davide Pesavento6ecc3f82019-02-17 22:23:08 -0500129 conf.checkDependency(name='libpcap', lib='pcap',
Davide Pesavento5f35f642018-05-10 19:36:03 -0400130 errmsg='not found, but required for Ethernet face support. '
131 'Specify --without-libpcap to disable Ethernet face support.')
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -0700132
Davide Pesavento7a608462024-12-20 14:22:10 -0500133 # WebSocket++ is incompatible with Boost 1.87.0
134 # https://github.com/zaphoyd/websocketpp/issues/1157
135 if conf.env.BOOST_VERSION_NUMBER < 108700:
136 conf.checkWebsocket()
Davide Pesavento774071c2018-11-15 21:33:23 -0500137
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400138 conf.check_compiler_flags()
139
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500140 # Loading "late" to prevent tests from being compiled with profiling flags
Alexander Afanasyev689569b2014-02-16 20:20:07 -0800141 conf.load('coverage')
Eric Newberry27bdd1a2016-11-01 18:02:54 -0700142 conf.load('sanitizers')
143
Davide Pesavento6ecc3f82019-02-17 22:23:08 -0500144 conf.define_cond('WITH_TESTS', conf.env.WITH_TESTS)
145 conf.define_cond('WITH_OTHER_TESTS', conf.env.WITH_OTHER_TESTS)
Davide Pesavento910232f2023-09-08 14:20:02 -0400146 conf.define('DEFAULT_CONFIG_FILE', f'{conf.env.SYSCONFDIR}/ndn/nfd.conf')
Davide Pesavento6ecc3f82019-02-17 22:23:08 -0500147 # The config header will contain all defines that were added using conf.define()
148 # or conf.define_cond(). Everything that was added directly to conf.env.DEFINES
149 # will not appear in the config header, but will instead be passed directly to the
150 # compiler on the command line.
Davide Pesavento264af772021-02-09 21:48:24 -0500151 conf.write_config_header('core/config.hpp', define_prefix='NFD_')
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800152
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700153def build(bld):
Davide Pesavento03f45d22019-04-04 12:34:26 -0400154 versionhpp(bld)
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700155
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500156 bld(features='subst',
Davide Pesavento03f45d22019-04-04 12:34:26 -0400157 name='version.cpp',
158 source='core/version.cpp.in',
159 target='core/version.cpp',
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700160 install_path=None,
161 VERSION_STRING=VERSION_BASE,
Davide Pesavento03f45d22019-04-04 12:34:26 -0400162 VERSION_BUILD=VERSION)
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700163
Davide Pesaventoa3148082018-04-12 18:21:54 -0400164 bld.objects(
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700165 target='core-objects',
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400166 source=bld.path.find_dir('core').ant_glob('*.cpp') + ['core/version.cpp'],
167 use='version.cpp version.hpp BOOST NDN_CXX LIBRT',
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500168 includes='.',
Davide Pesaventoa9b09b62022-06-04 14:07:25 -0400169 export_includes='.')
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700170
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500171 nfd_objects = bld.objects(
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700172 target='daemon-objects',
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500173 source=bld.path.ant_glob('daemon/**/*.cpp',
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400174 excl=['daemon/face/*ethernet*.cpp',
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500175 'daemon/face/pcap*.cpp',
176 'daemon/face/unix*.cpp',
177 'daemon/face/websocket*.cpp',
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -0700178 'daemon/main.cpp']),
Davide Pesaventoa9b09b62022-06-04 14:07:25 -0400179 features='pch',
180 headers='daemon/nfd-pch.hpp',
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500181 use='core-objects',
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600182 includes='daemon',
Davide Pesavento67f30272016-08-10 01:55:16 +0000183 export_includes='daemon')
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700184
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500185 if bld.env.HAVE_LIBPCAP:
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400186 nfd_objects.source += bld.path.ant_glob('daemon/face/*ethernet*.cpp')
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500187 nfd_objects.source += bld.path.ant_glob('daemon/face/pcap*.cpp')
Alexander Afanasyev5cda2e02014-04-11 13:58:44 -0700188 nfd_objects.use += ' LIBPCAP'
Davide Pesavento44deacc2014-02-19 10:48:07 +0100189
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500190 if bld.env.HAVE_UNIX_SOCKETS:
191 nfd_objects.source += bld.path.ant_glob('daemon/face/unix*.cpp')
Alexander Afanasyevc78b1412014-02-19 14:08:26 -0800192
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500193 if bld.env.HAVE_WEBSOCKET:
194 nfd_objects.source += bld.path.ant_glob('daemon/face/websocket*.cpp')
195 nfd_objects.use += ' WEBSOCKET'
Wentao Shang53df1632014-04-21 12:01:32 -0700196
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500197 if bld.env.WITH_OTHER_TESTS:
Eric Newberry69b63dc2017-11-04 15:42:46 -0700198 nfd_objects.source += bld.path.ant_glob('tests/other/fw/*.cpp')
199
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500200 bld.program(name='nfd',
201 target='bin/nfd',
202 source='daemon/main.cpp',
Davide Pesavento1b077f62019-02-19 19:19:44 -0500203 use='daemon-objects SYSTEMD')
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700204
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500205 bld.recurse('tests')
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400206 bld.recurse('tools')
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400207
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400208 # Install sample configs
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500209 bld(features='subst',
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -0700210 source='nfd.conf.sample.in',
211 target='nfd.conf.sample',
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500212 install_path='${SYSCONFDIR}/ndn',
213 IF_HAVE_LIBPCAP='' if bld.env.HAVE_LIBPCAP else '; ',
Eric Newberry22974902020-04-06 23:41:00 -0700214 IF_HAVE_WEBSOCKET='' if bld.env.HAVE_WEBSOCKET else '; ',
Davide Pesavento4c957712024-01-01 15:40:06 -0500215 UNIX_SOCKET_PATH='/run/nfd/nfd.sock' if Utils.unversioned_sys_platform() == 'linux' else '/var/run/nfd/nfd.sock')
Davide Pesaventof392b8f2019-01-08 20:51:55 -0500216 bld.install_files('${SYSCONFDIR}/ndn', 'autoconfig.conf.sample')
217
Davide Pesavento6ecc3f82019-02-17 22:23:08 -0500218 if bld.env.HAVE_SYSTEMD:
Davide Pesaventof392b8f2019-01-08 20:51:55 -0500219 systemd_units = bld.path.ant_glob('systemd/*.in')
220 bld(features='subst',
221 name='systemd-units',
222 source=systemd_units,
223 target=[u.change_ext('') for u in systemd_units])
224
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500225 if bld.env.SPHINX_BUILD:
226 bld(features='sphinx',
227 name='manpages',
228 builder='man',
229 config='docs/conf.py',
230 outdir='docs/manpages',
Davide Pesavento08b91c82019-04-13 19:42:10 -0400231 source=bld.path.ant_glob('docs/manpages/*.rst'),
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500232 install_path='${MANDIR}',
Davide Pesavento08b91c82019-04-13 19:42:10 -0400233 version=VERSION_BASE,
234 release=VERSION)
Junxiao Shi6c135622016-11-21 14:30:33 +0000235 bld.symlink_as('${MANDIR}/man1/nfdc-channel.1', 'nfdc-face.1')
Junxiao Shi6c135622016-11-21 14:30:33 +0000236 bld.symlink_as('${MANDIR}/man1/nfdc-fib.1', 'nfdc-route.1')
237 bld.symlink_as('${MANDIR}/man1/nfdc-register.1', 'nfdc-route.1')
238 bld.symlink_as('${MANDIR}/man1/nfdc-unregister.1', 'nfdc-route.1')
239 bld.symlink_as('${MANDIR}/man1/nfdc-set-strategy.1', 'nfdc-strategy.1')
240 bld.symlink_as('${MANDIR}/man1/nfdc-unset-strategy.1', 'nfdc-strategy.1')
Alexander Afanasyev49272f72014-04-06 21:49:46 -0700241
Davide Pesavento03f45d22019-04-04 12:34:26 -0400242def versionhpp(bld):
243 version(bld)
244
Alexander Afanasyev5006c1d2022-01-27 14:31:20 -0500245 vmajor = int(VERSION_SPLIT[0])
246 vminor = int(VERSION_SPLIT[1]) if len(VERSION_SPLIT) >= 2 else 0
247 vpatch = int(VERSION_SPLIT[2]) if len(VERSION_SPLIT) >= 3 else 0
248
Davide Pesavento03f45d22019-04-04 12:34:26 -0400249 bld(features='subst',
250 name='version.hpp',
251 source='core/version.hpp.in',
252 target='core/version.hpp',
253 install_path=None,
Alexander Afanasyev5006c1d2022-01-27 14:31:20 -0500254 VERSION=vmajor * 1000000 + vminor * 1000 + vpatch,
255 VERSION_MAJOR=str(vmajor),
256 VERSION_MINOR=str(vminor),
257 VERSION_PATCH=str(vpatch))
Davide Pesavento03f45d22019-04-04 12:34:26 -0400258
Alexander Afanasyev284257b2014-04-11 14:16:51 -0700259def docs(bld):
260 from waflib import Options
261 Options.commands = ['doxygen', 'sphinx'] + Options.commands
262
Alexander Afanasyev35fc2b72014-02-13 16:56:21 -0800263def doxygen(bld):
Davide Pesavento03f45d22019-04-04 12:34:26 -0400264 versionhpp(bld)
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700265
Alexander Afanasyev35fc2b72014-02-13 16:56:21 -0800266 if not bld.env.DOXYGEN:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500267 bld.fatal('Cannot build documentation ("doxygen" not found in PATH)')
Beichuan Zhang55b8ed42014-04-26 22:25:44 -0700268
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500269 bld(features='subst',
270 name='doxygen.conf',
271 source=['docs/doxygen.conf.in',
272 'docs/named_data_theme/named_data_footer-with-analytics.html.in'],
273 target=['docs/doxygen.conf',
274 'docs/named_data_theme/named_data_footer-with-analytics.html'],
275 VERSION=VERSION,
Davide Pesaventoa47cca22021-04-16 02:30:11 -0400276 HAVE_DOT='YES' if bld.env.DOT else 'NO',
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500277 HTML_FOOTER='../build/docs/named_data_theme/named_data_footer-with-analytics.html' \
278 if os.getenv('GOOGLE_ANALYTICS', None) \
279 else '../docs/named_data_theme/named_data_footer.html',
280 GOOGLE_ANALYTICS=os.getenv('GOOGLE_ANALYTICS', ''))
281
282 bld(features='doxygen',
283 doxyfile='docs/doxygen.conf',
Davide Pesavento03f45d22019-04-04 12:34:26 -0400284 use='doxygen.conf version.hpp')
Alexander Afanasyev49272f72014-04-06 21:49:46 -0700285
286def sphinx(bld):
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700287 version(bld)
288
Beichuan Zhang55b8ed42014-04-26 22:25:44 -0700289 if not bld.env.SPHINX_BUILD:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500290 bld.fatal('Cannot build documentation ("sphinx-build" not found in PATH)')
291
292 bld(features='sphinx',
293 config='docs/conf.py',
294 outdir='docs',
295 source=bld.path.ant_glob('docs/**/*.rst'),
Davide Pesavento08b91c82019-04-13 19:42:10 -0400296 version=VERSION_BASE,
297 release=VERSION)
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700298
299def version(ctx):
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500300 # don't execute more than once
Alexander Afanasyev26181532014-05-07 23:38:51 -0700301 if getattr(Context.g_module, 'VERSION_BASE', None):
302 return
303
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700304 Context.g_module.VERSION_BASE = Context.g_module.VERSION
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500305 Context.g_module.VERSION_SPLIT = VERSION_BASE.split('.')
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700306
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500307 # first, try to get a version string from git
Davide Pesaventoe1dc79b2024-04-21 18:48:23 -0400308 version_from_git = ''
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700309 try:
Davide Pesaventoe1dc79b2024-04-21 18:48:23 -0400310 cmd = ['git', 'describe', '--abbrev=8', '--always', '--match', f'{GIT_TAG_PREFIX}*']
311 version_from_git = subprocess.run(cmd, capture_output=True, check=True, text=True).stdout.strip()
312 if version_from_git:
Davide Pesaventoc3bf8ad2024-05-03 20:20:54 -0400313 if GIT_TAG_PREFIX and version_from_git.startswith(GIT_TAG_PREFIX):
314 Context.g_module.VERSION = version_from_git[len(GIT_TAG_PREFIX):]
315 elif not GIT_TAG_PREFIX and ('.' in version_from_git or '-' in version_from_git):
316 Context.g_module.VERSION = version_from_git
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700317 else:
Davide Pesaventoc3bf8ad2024-05-03 20:20:54 -0400318 # no tags matched (or we are in a shallow clone)
Davide Pesaventoe1dc79b2024-04-21 18:48:23 -0400319 Context.g_module.VERSION = f'{VERSION_BASE}+git.{version_from_git}'
Davide Pesavento910232f2023-09-08 14:20:02 -0400320 except (OSError, subprocess.SubprocessError):
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700321 pass
322
Davide Pesaventoe1dc79b2024-04-21 18:48:23 -0400323 # fallback to the VERSION.info file, if it exists and is not empty
324 version_from_file = ''
325 version_file = ctx.path.find_node('VERSION.info')
326 if version_file is not None:
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700327 try:
Davide Pesaventoe1dc79b2024-04-21 18:48:23 -0400328 version_from_file = version_file.read().strip()
329 except OSError as e:
330 Logs.warn(f'{e.filename} exists but is not readable ({e.strerror})')
331 if version_from_file and not version_from_git:
332 Context.g_module.VERSION = version_from_file
333 return
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700334
Davide Pesaventoe1dc79b2024-04-21 18:48:23 -0400335 # update VERSION.info if necessary
336 if version_from_file == Context.g_module.VERSION:
337 # already up-to-date
338 return
339 if version_file is None:
340 version_file = ctx.path.make_node('VERSION.info')
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700341 try:
Davide Pesaventoe1dc79b2024-04-21 18:48:23 -0400342 version_file.write(Context.g_module.VERSION)
343 except OSError as e:
344 Logs.warn(f'{e.filename} is not writable ({e.strerror})')
Alexander Afanasyev48f5a3c2014-08-22 22:33:01 -0700345
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700346def dist(ctx):
Davide Pesavento39c67f62023-02-15 00:26:26 -0500347 ctx.algo = 'tar.xz'
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700348 version(ctx)
349
350def distcheck(ctx):
Davide Pesavento39c67f62023-02-15 00:26:26 -0500351 ctx.algo = 'tar.xz'
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700352 version(ctx)