2015-07-22 07:49:06 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os, sys
|
|
|
|
import re
|
|
|
|
import datetime as dt
|
|
|
|
|
|
|
|
# python 3 compatibility
|
|
|
|
try:
|
2018-04-18 00:29:03 +08:00
|
|
|
import cStringIO as sstream
|
2015-07-22 07:49:06 +08:00
|
|
|
except ImportError:
|
2018-04-18 00:29:03 +08:00
|
|
|
from io import StringIO
|
2015-07-22 07:49:06 +08:00
|
|
|
|
|
|
|
description = "Converts sol to a single file for convenience."
|
|
|
|
|
|
|
|
# command line parser
|
2018-04-18 00:29:03 +08:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
usage='%(prog)s [options...]', description=description)
|
|
|
|
parser.add_argument(
|
|
|
|
'--output',
|
|
|
|
'-o',
|
|
|
|
nargs='+',
|
|
|
|
help=
|
|
|
|
'name and location of where to place file (and forward declaration file)',
|
|
|
|
metavar='file',
|
2018-12-20 12:17:15 +08:00
|
|
|
default=['single/include/sol/sol.hpp'])
|
|
|
|
parser.add_argument(
|
|
|
|
'--input',
|
|
|
|
'-i',
|
|
|
|
help='the path to use to get to the sol files',
|
|
|
|
metavar='path',
|
|
|
|
default=os.path.normpath(
|
|
|
|
os.path.dirname(os.path.realpath(__file__)) + '/../include'))
|
2015-07-22 07:49:06 +08:00
|
|
|
parser.add_argument('--quiet', help='suppress all output', action='store_true')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-12-09 15:52:08 +08:00
|
|
|
single_file = ''
|
|
|
|
forward_single_file = ''
|
2018-12-20 12:17:15 +08:00
|
|
|
single_file = os.path.normpath(args.output[0])
|
2018-06-06 14:49:40 +08:00
|
|
|
|
2017-12-09 15:52:08 +08:00
|
|
|
if len(args.output) > 1:
|
2020-06-19 06:07:05 +08:00
|
|
|
forward_single_file = os.path.normpath(args.output[1])
|
2017-12-09 15:52:08 +08:00
|
|
|
else:
|
2018-04-18 00:29:03 +08:00
|
|
|
a, b = os.path.splitext(single_file)
|
2018-12-20 12:17:15 +08:00
|
|
|
a = os.path.dirname(single_file)
|
|
|
|
forward_single_file = os.path.normpath(
|
|
|
|
os.path.join(a + '/', 'forward' + b))
|
2018-06-06 14:49:40 +08:00
|
|
|
|
|
|
|
single_file_dir = os.path.dirname(single_file)
|
|
|
|
forward_single_file_dir = os.path.dirname(forward_single_file)
|
|
|
|
|
2020-06-19 06:07:05 +08:00
|
|
|
script_path = os.path.normpath(args.input)
|
2015-07-22 07:49:06 +08:00
|
|
|
working_dir = os.getcwd()
|
|
|
|
os.chdir(script_path)
|
|
|
|
|
2018-06-06 14:49:40 +08:00
|
|
|
# If the user didn't provide absolute paths then construct them based on the current working dir.
|
|
|
|
if not os.path.isabs(single_file):
|
|
|
|
single_file = os.path.join(working_dir, single_file)
|
|
|
|
single_file_dir = os.path.join(working_dir, single_file_dir)
|
|
|
|
|
|
|
|
if not os.path.isabs(forward_single_file):
|
|
|
|
forward_single_file = os.path.join(working_dir, forward_single_file)
|
2018-12-20 12:17:15 +08:00
|
|
|
forward_single_file_dir = os.path.join(working_dir,
|
|
|
|
forward_single_file_dir)
|
2018-06-06 14:49:40 +08:00
|
|
|
|
2015-07-22 07:49:06 +08:00
|
|
|
intro = """// The MIT License (MIT)
|
|
|
|
|
2020-03-10 09:33:41 +08:00
|
|
|
// Copyright (c) 2013-2020 Rapptz, ThePhD and contributors
|
2015-07-22 07:49:06 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
// This file was generated with a script.
|
|
|
|
// Generated {time} UTC
|
|
|
|
// This header was generated with sol {version} (revision {revision})
|
2016-03-12 07:41:44 +08:00
|
|
|
// https://github.com/ThePhD/sol2
|
2015-07-22 07:49:06 +08:00
|
|
|
|
|
|
|
#ifndef {guard}
|
|
|
|
#define {guard}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
includes = set([])
|
|
|
|
standard_include = re.compile(r'#include <(.*?)>')
|
2017-09-10 09:38:13 +08:00
|
|
|
local_include = re.compile(r'#(\s*?)include "(.*?)"')
|
2018-04-18 00:29:03 +08:00
|
|
|
project_include = re.compile(r'#(\s*?)include <sol/(.*?)>')
|
|
|
|
pragma_once_cpp = re.compile(r'(\s*)#(\s*)pragma(\s+)once')
|
2015-07-22 07:49:06 +08:00
|
|
|
ifndef_cpp = re.compile(r'#ifndef SOL_.*?_HPP')
|
|
|
|
define_cpp = re.compile(r'#define SOL_.*?_HPP')
|
|
|
|
endif_cpp = re.compile(r'#endif // SOL_.*?_HPP')
|
2018-12-20 12:17:15 +08:00
|
|
|
forward_cpp = re.compile(r'SOL_FORWARD_HPP')
|
|
|
|
forward_detail_cpp = re.compile(r'SOL_FORWARD_DETAIL_HPP')
|
2015-07-22 07:49:06 +08:00
|
|
|
|
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
def get_include(line, base_path):
|
|
|
|
local_match = local_include.match(line)
|
|
|
|
if local_match:
|
|
|
|
# local include found
|
|
|
|
full_path = os.path.normpath(
|
|
|
|
os.path.join(base_path, local_match.group(2))).replace(
|
|
|
|
'\\', '/')
|
|
|
|
return full_path
|
|
|
|
project_match = project_include.match(line)
|
|
|
|
if project_match:
|
|
|
|
# local include found
|
|
|
|
full_path = os.path.normpath(
|
|
|
|
os.path.join(base_path, project_match.group(2))).replace(
|
|
|
|
'\\', '/')
|
|
|
|
return full_path
|
|
|
|
return None
|
2015-07-22 07:49:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
def is_include_guard(line):
|
2018-12-20 12:17:15 +08:00
|
|
|
is_regular_guard = ifndef_cpp.match(line) or define_cpp.match(
|
2018-04-18 00:29:03 +08:00
|
|
|
line) or endif_cpp.match(line) or pragma_once_cpp.match(line)
|
2018-12-20 12:17:15 +08:00
|
|
|
if is_regular_guard:
|
|
|
|
return not forward_cpp.search(line) and not forward_detail_cpp.search(line)
|
|
|
|
return is_regular_guard
|
2018-04-18 00:29:03 +08:00
|
|
|
|
2015-07-22 07:49:06 +08:00
|
|
|
|
|
|
|
def get_revision():
|
2018-04-18 00:29:03 +08:00
|
|
|
return os.popen('git rev-parse --short HEAD').read().strip()
|
|
|
|
|
2015-07-22 07:49:06 +08:00
|
|
|
|
|
|
|
def get_version():
|
2018-04-18 00:29:03 +08:00
|
|
|
return os.popen('git describe --tags --abbrev=0').read().strip()
|
|
|
|
|
2015-07-22 07:49:06 +08:00
|
|
|
|
|
|
|
def process_file(filename, out):
|
2018-04-18 00:29:03 +08:00
|
|
|
global includes
|
|
|
|
filename = os.path.normpath(filename)
|
|
|
|
relativefilename = filename.replace(script_path + os.sep, "").replace(
|
|
|
|
"\\", "/")
|
|
|
|
|
|
|
|
if filename in includes:
|
|
|
|
return
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
includes.add(filename)
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
if not args.quiet:
|
|
|
|
print('processing {}'.format(filename))
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
out.write('// beginning of {}\n\n'.format(relativefilename))
|
|
|
|
empty_line_state = True
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
with open(filename, 'r', encoding='utf-8') as f:
|
|
|
|
for line in f:
|
|
|
|
# skip comments
|
|
|
|
if line.startswith('//'):
|
|
|
|
continue
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
# skip include guard non-sense
|
|
|
|
if is_include_guard(line):
|
|
|
|
continue
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
# get relative directory
|
|
|
|
base_path = os.path.dirname(filename)
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
# check if it's a standard file
|
|
|
|
std = standard_include.search(line)
|
|
|
|
if std:
|
|
|
|
std_file = os.path.join('std', std.group(0))
|
|
|
|
if std_file in includes:
|
|
|
|
continue
|
|
|
|
includes.add(std_file)
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
# see if it's an include file
|
|
|
|
name = get_include(line, base_path)
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
if name:
|
|
|
|
process_file(name, out)
|
|
|
|
continue
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
empty_line = len(line.strip()) == 0
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
if empty_line and empty_line_state:
|
|
|
|
continue
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
empty_line_state = empty_line
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
# line is fine
|
|
|
|
out.write(line)
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-04-18 00:29:03 +08:00
|
|
|
out.write('// end of {}\n\n'.format(relativefilename))
|
2015-07-22 07:49:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
version = get_version()
|
|
|
|
revision = get_revision()
|
|
|
|
include_guard = 'SOL_SINGLE_INCLUDE_HPP'
|
2017-12-09 15:52:08 +08:00
|
|
|
forward_include_guard = 'SOL_SINGLE_INCLUDE_FORWARD_HPP'
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
processed_files = [os.path.join(script_path, x) for x in ['sol/sol.hpp']]
|
2018-04-18 00:29:03 +08:00
|
|
|
forward_processed_files = [
|
|
|
|
os.path.join(script_path, x) for x in ['sol/forward.hpp']
|
|
|
|
]
|
2015-07-22 07:49:06 +08:00
|
|
|
result = ''
|
2017-12-09 15:52:08 +08:00
|
|
|
forward_result = ''
|
|
|
|
|
|
|
|
if not args.quiet:
|
2018-04-18 00:29:03 +08:00
|
|
|
print('Current version: {version} (revision {revision})\n'.format(
|
|
|
|
version=version, revision=revision))
|
|
|
|
print('Creating single header for sol')
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2016-03-13 22:03:05 +08:00
|
|
|
ss = StringIO()
|
2018-04-18 00:29:03 +08:00
|
|
|
ss.write(
|
|
|
|
intro.format(
|
|
|
|
time=dt.datetime.utcnow(),
|
|
|
|
revision=revision,
|
|
|
|
version=version,
|
|
|
|
guard=include_guard))
|
2015-07-22 07:49:06 +08:00
|
|
|
for processed_file in processed_files:
|
2018-04-18 00:29:03 +08:00
|
|
|
process_file(processed_file, ss)
|
2015-07-22 07:49:06 +08:00
|
|
|
|
|
|
|
ss.write('#endif // {}\n'.format(include_guard))
|
|
|
|
result = ss.getvalue()
|
|
|
|
ss.close()
|
|
|
|
|
2017-12-09 15:52:08 +08:00
|
|
|
if not args.quiet:
|
2018-04-18 00:29:03 +08:00
|
|
|
print('finished creating single header for sol\n')
|
2017-12-09 15:52:08 +08:00
|
|
|
|
|
|
|
if not args.quiet:
|
2018-04-18 00:29:03 +08:00
|
|
|
print('Creating single forward declaration header for sol')
|
2017-12-09 15:52:08 +08:00
|
|
|
|
|
|
|
includes = set([])
|
|
|
|
forward_ss = StringIO()
|
2018-04-18 00:29:03 +08:00
|
|
|
forward_ss.write(
|
|
|
|
intro.format(
|
|
|
|
time=dt.datetime.utcnow(),
|
|
|
|
revision=revision,
|
|
|
|
version=version,
|
|
|
|
guard=forward_include_guard))
|
2017-12-09 15:52:08 +08:00
|
|
|
for forward_processed_file in forward_processed_files:
|
2018-04-18 00:29:03 +08:00
|
|
|
process_file(forward_processed_file, forward_ss)
|
2017-12-09 15:52:08 +08:00
|
|
|
|
|
|
|
forward_ss.write('#endif // {}\n'.format(forward_include_guard))
|
|
|
|
forward_result = forward_ss.getvalue()
|
|
|
|
forward_ss.close()
|
|
|
|
|
|
|
|
if not args.quiet:
|
2018-04-18 00:29:03 +08:00
|
|
|
print('finished creating single forward declaration header for sol\n')
|
2017-12-09 15:52:08 +08:00
|
|
|
|
2018-06-06 14:49:40 +08:00
|
|
|
# Create the output directories if they don't already exist.
|
|
|
|
os.makedirs(single_file_dir, exist_ok=True)
|
|
|
|
os.makedirs(forward_single_file_dir, exist_ok=True)
|
|
|
|
|
2017-12-09 15:52:08 +08:00
|
|
|
with open(single_file, 'w', encoding='utf-8') as f:
|
2018-04-18 00:29:03 +08:00
|
|
|
if not args.quiet:
|
|
|
|
print('writing {}...'.format(single_file))
|
|
|
|
f.write(result)
|
2015-07-22 07:49:06 +08:00
|
|
|
|
2017-12-09 15:52:08 +08:00
|
|
|
with open(forward_single_file, 'w', encoding='utf-8') as f:
|
2018-04-18 00:29:03 +08:00
|
|
|
if not args.quiet:
|
|
|
|
print('writing {}...'.format(forward_single_file))
|
|
|
|
f.write(forward_result)
|