diff --git a/cmake/core.cmake b/cmake/core.cmake index 266a5c92a27..ef98c4eccb3 100644 --- a/cmake/core.cmake +++ b/cmake/core.cmake @@ -6,7 +6,7 @@ endif() add_custom_command( OUTPUT ${MBGL_GENERATED}/include/mbgl/util/version.hpp DEPENDS ${MBGL_VERSION_DEPS} - COMMAND ${CMAKE_SOURCE_DIR}/scripts/build-version.py ${MBGL_GENERATED} + COMMAND ${NodeJS_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/build-version.js ${MBGL_GENERATED} VERBATIM ) diff --git a/scripts/build-version.js b/scripts/build-version.js new file mode 100755 index 00000000000..eb8a00f72c4 --- /dev/null +++ b/scripts/build-version.js @@ -0,0 +1,81 @@ +#!/usr/bin/env node + +var path = require('path'); +var fs = require('fs'); +var util = require('util'); +var mkdirp = require('mkdirp'); +var execSync = require('child_process').execSync; + +const DEFAULT_TAG = [0, 0, 0]; +const DEFAULT_REV = 'unknown'; + +function is_git_repo() { + try { + execSync('git rev-parse', { stdio: ['ignore', 'ignore', 'ignore'] }); + return true; + } catch(err) { + return false; + } +} + + +function parse_tag(raw_tag) { + return raw_tag.replace(/[^0-9.]/g, '').split('.').map(parseFloat).slice(0, 3); +} + +function parse_rev(raw_rev) { + return raw_rev.substr(0, 8); +} + +var output_dir = process.argv[2]; + +if (!output_dir) { + console.warn('No output directory given.'); + console.warn('Usage: %s [output dir]', path.basename(process.argv[1])); + process.exit(1); +} + +if (is_git_repo()) { + var raw_tag = execSync('git describe --tags --always --abbrev=0').toString().trim(); + var raw_rev = execSync('git rev-parse HEAD').toString().trim(); + + // When they're identical, the "git describe" can't find a tag and reports the rev instead. + if (raw_tag == raw_rev) { + var tag = DEFAULT_TAG + var rev = parse_rev(raw_rev) + } else { + var tag = parse_tag(raw_tag) + var rev = parse_rev(raw_rev) + } +} else { + var tag = DEFAULT_TAG; + var rev = DEFAULT_REV; +} + +console.log('Tag: %d.%d.%d', tag[0], tag[1], tag[2]); +console.log('Rev: %s', rev); + +var header = '// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n' + + '#pragma once\n' + + '\n' + + '#define MBGL_VERSION 0x' + (tag[0] << 16 | tag[1] << 8 | tag[2]).toString(16) + '\n' + + '#define MBGL_VERSION_STRING "' + tag[0] + '.' + tag[1] + '.' + tag[2] + '"\n' + + '#define MBGL_VERSION_MAJOR ' + tag[0] + '\n' + + '#define MBGL_VERSION_MINOR ' + tag[1] + '\n' + + '#define MBGL_VERSION_PATCH ' + tag[2] + '\n' + + '#define MBGL_VERSION_REV "' + rev + '"\n' + + '\n' + + 'namespace mbgl {\n' + + 'namespace version {\n' + + '\n' + + 'extern const int major, minor, patch;\n' + + 'extern const char *revision;\n' + + 'extern const char *string;\n' + + 'extern const unsigned int number;\n' + + '\n' + + '} // namespace version\n' + + '} // namespace mbgl\n'; + +var header_path = path.join(output_dir, 'include/mbgl/util/version.hpp') +mkdirp.sync(path.dirname(header_path)); +fs.writeFileSync(header_path, header); diff --git a/scripts/build-version.py b/scripts/build-version.py deleted file mode 100755 index f4a8abb88e8..00000000000 --- a/scripts/build-version.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env python - -import sys, os, errno, re, subprocess - -DEFAULT_TAG = [0, 0, 0] -DEFAULT_REV = 'unknown' - -def is_git_repo(): - try: - subprocess.check_output("git rev-parse", shell=True) - return True - except subprocess.CalledProcessError as exc: - return False - -def parse_tag(raw_tag): - return map(int, re.sub("[^0-9.]", "", raw_tag).split('.')) - -def parse_rev(raw_rev): - return raw_rev[0:8] - -def mkdir_p(path): - try: - os.makedirs(path) - except OSError as exc: # Python >2.5 - if exc.errno == errno.EEXIST and os.path.isdir(path): - pass - else: raise - -output_dir = sys.argv[1] - -if is_git_repo(): - raw_tag = subprocess.check_output("git describe --tags --always --abbrev=0", shell=True) - raw_rev = subprocess.check_output("git rev-parse HEAD", shell=True) - - # When they're identical, the "git describe" can't find a tag and reports the rev instead. - if raw_tag == raw_rev: - tag = DEFAULT_TAG - rev = parse_rev(raw_rev) - else: - tag = parse_tag(raw_tag) - rev = parse_rev(raw_rev) -else: - tag = DEFAULT_TAG - rev = DEFAULT_REV - -tag = tag + [0, 0, 0] -tag = tag[0:3] - -print "Tag: {0}".format(tag) -print "Rev: {0}".format(rev) - -header = """// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. -#pragma once - -#define MBGL_VERSION 0x{major:02x}{minor:02x}{patch:02x} -#define MBGL_VERSION_STRING "{major}.{minor}.{patch}" -#define MBGL_VERSION_MAJOR {major} -#define MBGL_VERSION_MINOR {minor} -#define MBGL_VERSION_PATCH {patch} -#define MBGL_VERSION_REV "{rev}" - -namespace mbgl {{ -namespace version {{ - -extern const int major, minor, patch; -extern const char *revision; -extern const char *string; -extern const unsigned int number; - -}} // namespace version -}} // namespace mbgl -""".format( - major = tag[0], - minor = tag[1], - patch = tag[2], - rev = rev -) - -header_path = os.path.join(output_dir, 'include/mbgl/util/version.hpp') -mkdir_p(os.path.dirname(header_path)) -with open(header_path, 'w') as f: f.write(header)