1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
#!/usr/bin/env python
import argparse
import os, sys
import re
import datetime as dt
# python 3 compatibility
try:
import cStringIO as sstream
except ImportError:
from io import StringIO
description = "Converts sol to a single file for convenience."
# command line parser
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',
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'))
parser.add_argument('--quiet', help='suppress all output', action='store_true')
args = parser.parse_args()
single_file = ''
forward_single_file = ''
single_file = os.path.normpath(args.output[0])
if len(args.output) > 1:
forward_single_file = os.path.normpath(args.output[1])
else:
a, b = os.path.splitext(single_file)
a = os.path.dirname(single_file)
forward_single_file = os.path.normpath(
os.path.join(a + '/', 'forward' + b))
if len(args.output) > 2:
config_single_file = os.path.normpath(args.output[2])
else:
a, b = os.path.splitext(single_file)
a = os.path.dirname(single_file)
config_single_file = os.path.normpath(os.path.join(a + '/', 'config' + b))
single_file_dir = os.path.dirname(single_file)
forward_single_file_dir = os.path.dirname(forward_single_file)
config_single_file_dir = os.path.dirname(forward_single_file)
script_path = os.path.normpath(args.input)
working_dir = os.getcwd()
os.chdir(script_path)
# 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)
forward_single_file_dir = os.path.join(working_dir,
forward_single_file_dir)
if not os.path.isabs(config_single_file):
config_single_file = os.path.join(working_dir, config_single_file)
config_single_file_dir = os.path.join(working_dir, config_single_file_dir)
intro = """// The MIT License (MIT)
// Copyright (c) 2013-2020 Rapptz, ThePhD and contributors
// 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})
// https://github.com/ThePhD/sol2
#ifndef {guard}
#define {guard}
"""
includes = set([])
standard_include = re.compile(r'\s*#include <(.*?)>')
local_include = re.compile(r'\s*#(\s*?)include "(.*?)"')
project_include = re.compile(r'\s*#(\s*?)include <(sol/.*?)>')
project_config_include = re.compile(r'\s*#(\s*?)include <sol/config.hpp>')
pragma_once_cpp = re.compile(r'(\s*)#(\s*)pragma(\s+)once')
ifndef_cpp = re.compile(r'#ifndef SOL_.*?_HPP')
define_cpp = re.compile(r'#define SOL_.*?_HPP')
endif_cpp = re.compile(r'#endif // SOL_.*?_HPP')
forward_cpp = re.compile(r'SOL_FORWARD_HPP')
forward_detail_cpp = re.compile(r'SOL_FORWARD_DETAIL_HPP')
def get_include(line, base_path):
project_config_match = project_config_include.match(line)
if project_config_match:
# do not touch project config include file
return None
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:
# project-local include found
full_path = os.path.normpath(
os.path.join(script_path,
project_match.group(2))).replace('\\', '/')
return full_path
return None
def is_include_guard(line):
is_regular_guard = ifndef_cpp.match(line) or define_cpp.match(
line) or endif_cpp.match(line) or pragma_once_cpp.match(line)
if is_regular_guard:
return not forward_cpp.search(
line) and not forward_detail_cpp.search(line)
return is_regular_guard
def get_revision():
return os.popen('git rev-parse --short HEAD').read().strip()
def get_version():
return os.popen('git describe --tags --abbrev=0').read().strip()
def process_file(filename, out):
global includes
filename = os.path.normpath(filename)
relativefilename = filename.replace(script_path + os.sep,
"").replace("\\", "/")
if filename in includes:
return
includes.add(filename)
if not args.quiet:
print('processing {}'.format(filename))
out.write('// beginning of {}\n\n'.format(relativefilename))
empty_line_state = True
with open(filename, 'r', encoding='utf-8') as f:
for line in f:
# skip comments
if line.startswith('//'):
continue
# skip include guard non-sense
if is_include_guard(line):
continue
# get relative directory
base_path = os.path.dirname(filename)
# check if it's a standard file
# TODO: this is FAR too aggressive and catches
# includes and files not part of the standard (C includes)
# and friends.
# we should add a list of standard includes here??
# or handle behavior differently...
#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)
# see if it's an include file
name = get_include(line, base_path)
if name:
process_file(name, out)
continue
empty_line = len(line.strip()) == 0
if empty_line and empty_line_state:
continue
empty_line_state = empty_line
# line is fine
out.write(line)
out.write('// end of {}\n\n'.format(relativefilename))
version = get_version()
revision = get_revision()
include_guard = 'SOL_SINGLE_INCLUDE_SOL_HPP'
forward_include_guard = 'SOL_SINGLE_INCLUDE_SOL_FORWARD_HPP'
config_include_guard = 'SOL_SINGLE_SOL_CONFIG_HPP'
processed_files = [os.path.join(script_path, x) for x in ['sol/sol.hpp']]
forward_processed_files = [
os.path.join(script_path, x) for x in ['sol/forward.hpp']
]
config_processed_files = [
os.path.join(script_path, x) for x in ['sol/config.hpp']
]
result = ''
forward_result = ''
config_result = ''
if not args.quiet:
print('Current version: {version} (revision {revision})\n'.format(
version=version, revision=revision))
print('Creating single header for sol')
ss = StringIO()
ss.write(
intro.format(time=dt.datetime.utcnow(),
revision=revision,
version=version,
guard=include_guard))
for processed_file in processed_files:
process_file(processed_file, ss)
ss.write('#endif // {}\n'.format(include_guard))
result = ss.getvalue()
ss.close()
if not args.quiet:
print('finished creating single header for sol\n')
if not args.quiet:
print('Creating single forward declaration header for sol')
includes = set([])
forward_ss = StringIO()
forward_ss.write(
intro.format(time=dt.datetime.utcnow(),
revision=revision,
version=version,
guard=forward_include_guard))
for forward_processed_file in forward_processed_files:
process_file(forward_processed_file, forward_ss)
forward_ss.write('#endif // {}\n'.format(forward_include_guard))
forward_result = forward_ss.getvalue()
forward_ss.close()
if not args.quiet:
print('finished creating single forward declaration header for sol\n')
if not args.quiet:
print('Creating single config header for sol')
includes = set([])
config_ss = StringIO()
config_ss.write(
intro.format(time=dt.datetime.utcnow(),
revision=revision,
version=version,
guard=config_include_guard))
for config_processed_file in config_processed_files:
process_file(config_processed_file, config_ss)
config_ss.write('#endif // {}\n'.format(config_include_guard))
config_result = config_ss.getvalue()
config_ss.close()
if not args.quiet:
print('finished creating single config header for sol\n')
# 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)
os.makedirs(config_single_file_dir, exist_ok=True)
with open(single_file, 'w', encoding='utf-8') as f:
if not args.quiet:
print('writing {}...'.format(single_file))
f.write(result)
with open(forward_single_file, 'w', encoding='utf-8') as f:
if not args.quiet:
print('writing {}...'.format(forward_single_file))
f.write(forward_result)
with open(config_single_file, 'w', encoding='utf-8') as f:
if not args.quiet:
print('writing {}...'.format(config_single_file))
f.write(config_result)
|