Skip to content

Commit

Permalink
SCons: Format buildsystem files with psf/black
Browse files Browse the repository at this point in the history
Configured for a max line length of 120 characters.

psf/black is very opinionated and purposely doesn't leave much room for
configuration. The output is mostly OK so that should be fine for us,
but some things worth noting:

- Manually wrapped strings will be reflowed, so by using a line length
  of 120 for the sake of preserving readability for our long command
  calls, it also means that some manually wrapped strings are back on
  the same line and should be manually merged again.

- Code generators using string concatenation extensively look awful,
  since black puts each operand on a single line. We need to refactor
  these generators to use more pythonic string formatting, for which
  many options are available (`%`, `format` or f-strings).

- CI checks and a pre-commit hook will be added to ensure that future
  buildsystem changes are well-formatted.

(cherry picked from commit cd4e46e)
  • Loading branch information
akien-mga committed Jun 10, 2020
1 parent c3d0416 commit 7bf9787
Show file tree
Hide file tree
Showing 189 changed files with 4,095 additions and 3,360 deletions.
439 changes: 240 additions & 199 deletions SConstruct

Large diffs are not rendered by default.

44 changes: 33 additions & 11 deletions compat.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,90 @@
import sys

if sys.version_info < (3,):

def isbasestring(s):
return isinstance(s, basestring)

def open_utf8(filename, mode):
return open(filename, mode)

def byte_to_str(x):
return str(ord(x))

import cStringIO

def StringIO():
return cStringIO.StringIO()

def encode_utf8(x):
return x

def decode_utf8(x):
return x

def iteritems(d):
return d.iteritems()

def itervalues(d):
return d.itervalues()

def escape_string(s):
if isinstance(s, unicode):
s = s.encode('ascii')
result = ''
s = s.encode("ascii")
result = ""
for c in s:
if not (32 <= ord(c) < 127) or c in ('\\', '"'):
result += '\\%03o' % ord(c)
if not (32 <= ord(c) < 127) or c in ("\\", '"'):
result += "\\%03o" % ord(c)
else:
result += c
return result


else:

def isbasestring(s):
return isinstance(s, (str, bytes))

def open_utf8(filename, mode):
return open(filename, mode, encoding="utf-8")

def byte_to_str(x):
return str(x)

import io

def StringIO():
return io.StringIO()

import codecs

def encode_utf8(x):
return codecs.utf_8_encode(x)[0]

def decode_utf8(x):
return codecs.utf_8_decode(x)[0]

def iteritems(d):
return iter(d.items())

def itervalues(d):
return iter(d.values())

def charcode_to_c_escapes(c):
rev_result = []
while c >= 256:
c, low = (c // 256, c % 256)
rev_result.append('\\%03o' % low)
rev_result.append('\\%03o' % c)
return ''.join(reversed(rev_result))
rev_result.append("\\%03o" % low)
rev_result.append("\\%03o" % c)
return "".join(reversed(rev_result))

def escape_string(s):
result = ''
result = ""
if isinstance(s, str):
s = s.encode('utf-8')
s = s.encode("utf-8")
for c in s:
if not(32 <= c < 127) or c in (ord('\\'), ord('"')):
if not (32 <= c < 127) or c in (ord("\\"), ord('"')):
result += charcode_to_c_escapes(c)
else:
result += chr(c)
return result

107 changes: 60 additions & 47 deletions core/SCsub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

Import('env')
Import("env")

import core_builders
import make_binders
Expand All @@ -11,31 +11,32 @@ env.core_sources = []

# Generate AES256 script encryption key
import os

txt = "0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0"
if ("SCRIPT_AES256_ENCRYPTION_KEY" in os.environ):
if "SCRIPT_AES256_ENCRYPTION_KEY" in os.environ:
e = os.environ["SCRIPT_AES256_ENCRYPTION_KEY"]
txt = ""
ec_valid = True
if (len(e) != 64):
if len(e) != 64:
ec_valid = False
else:

for i in range(len(e) >> 1):
if (i > 0):
if i > 0:
txt += ","
txts = "0x" + e[i * 2:i * 2 + 2]
txts = "0x" + e[i * 2 : i * 2 + 2]
try:
int(txts, 16)
except:
ec_valid = False
txt += txts
if (not ec_valid):
if not ec_valid:
txt = "0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0"
print("Invalid AES256 encryption key, not 64 bits hex: " + e)

# NOTE: It is safe to generate this file here, since this is still executed serially
with open("script_encryption_key.gen.cpp", "w") as f:
f.write("#include \"core/project_settings.h\"\nuint8_t script_encryption_key[32]={" + txt + "};\n")
f.write('#include "core/project_settings.h"\nuint8_t script_encryption_key[32]={' + txt + "};\n")


# Add required thirdparty code.
Expand All @@ -49,7 +50,6 @@ thirdparty_misc_sources = [
# C sources
"fastlz.c",
"smaz.c",

# C++ sources
"hq2x.cpp",
"pcg.cpp",
Expand All @@ -60,30 +60,30 @@ thirdparty_misc_sources = [thirdparty_misc_dir + file for file in thirdparty_mis
env_thirdparty.add_source_files(env.core_sources, thirdparty_misc_sources)

# Zlib library, can be unbundled
if env['builtin_zlib']:
thirdparty_zlib_dir = "#thirdparty/zlib/"
thirdparty_zlib_sources = [
"adler32.c",
"compress.c",
"crc32.c",
"deflate.c",
"infback.c",
"inffast.c",
"inflate.c",
"inftrees.c",
"trees.c",
"uncompr.c",
"zutil.c",
]
thirdparty_zlib_sources = [thirdparty_zlib_dir + file for file in thirdparty_zlib_sources]

env_thirdparty.Prepend(CPPPATH=[thirdparty_zlib_dir])
# Needs to be available in main env too
env.Prepend(CPPPATH=[thirdparty_zlib_dir])
if (env['target'] == 'debug'):
env_thirdparty.Append(CPPDEFINES=['ZLIB_DEBUG'])

env_thirdparty.add_source_files(env.core_sources, thirdparty_zlib_sources)
if env["builtin_zlib"]:
thirdparty_zlib_dir = "#thirdparty/zlib/"
thirdparty_zlib_sources = [
"adler32.c",
"compress.c",
"crc32.c",
"deflate.c",
"infback.c",
"inffast.c",
"inflate.c",
"inftrees.c",
"trees.c",
"uncompr.c",
"zutil.c",
]
thirdparty_zlib_sources = [thirdparty_zlib_dir + file for file in thirdparty_zlib_sources]

env_thirdparty.Prepend(CPPPATH=[thirdparty_zlib_dir])
# Needs to be available in main env too
env.Prepend(CPPPATH=[thirdparty_zlib_dir])
if env["target"] == "debug":
env_thirdparty.Append(CPPDEFINES=["ZLIB_DEBUG"])

env_thirdparty.add_source_files(env.core_sources, thirdparty_zlib_sources)

# Minizip library, could be unbundled in theory
# However, our version has some custom modifications, so it won't compile with the system one
Expand All @@ -99,7 +99,7 @@ env_thirdparty.add_source_files(env.core_sources, thirdparty_minizip_sources)
# Zstd library, can be unbundled in theory
# though we currently use some private symbols
# https://github.com/godotengine/godot/issues/17374
if env['builtin_zstd']:
if env["builtin_zstd"]:
thirdparty_zstd_dir = "#thirdparty/zstd/"
thirdparty_zstd_sources = [
"common/debug.c",
Expand Down Expand Up @@ -142,30 +142,43 @@ if env['builtin_zstd']:
env.add_source_files(env.core_sources, "*.cpp")

# Certificates
env.Depends("#core/io/certs_compressed.gen.h", ["#thirdparty/certs/ca-certificates.crt", env.Value(env['builtin_certs']), env.Value(env['system_certs_path'])])
env.CommandNoCache("#core/io/certs_compressed.gen.h", "#thirdparty/certs/ca-certificates.crt", run_in_subprocess(core_builders.make_certs_header))
env.Depends(
"#core/io/certs_compressed.gen.h",
["#thirdparty/certs/ca-certificates.crt", env.Value(env["builtin_certs"]), env.Value(env["system_certs_path"])],
)
env.CommandNoCache(
"#core/io/certs_compressed.gen.h",
"#thirdparty/certs/ca-certificates.crt",
run_in_subprocess(core_builders.make_certs_header),
)

# Make binders
env.CommandNoCache(['method_bind.gen.inc', 'method_bind_ext.gen.inc', 'method_bind_free_func.gen.inc'], 'make_binders.py', run_in_subprocess(make_binders.run))
env.CommandNoCache(
["method_bind.gen.inc", "method_bind_ext.gen.inc", "method_bind_free_func.gen.inc"],
"make_binders.py",
run_in_subprocess(make_binders.run),
)

# Authors
env.Depends('#core/authors.gen.h', "../AUTHORS.md")
env.CommandNoCache('#core/authors.gen.h', "../AUTHORS.md", run_in_subprocess(core_builders.make_authors_header))
env.Depends("#core/authors.gen.h", "../AUTHORS.md")
env.CommandNoCache("#core/authors.gen.h", "../AUTHORS.md", run_in_subprocess(core_builders.make_authors_header))

# Donors
env.Depends('#core/donors.gen.h', "../DONORS.md")
env.CommandNoCache('#core/donors.gen.h', "../DONORS.md", run_in_subprocess(core_builders.make_donors_header))
env.Depends("#core/donors.gen.h", "../DONORS.md")
env.CommandNoCache("#core/donors.gen.h", "../DONORS.md", run_in_subprocess(core_builders.make_donors_header))

# License
env.Depends('#core/license.gen.h', ["../COPYRIGHT.txt", "../LICENSE.txt"])
env.CommandNoCache('#core/license.gen.h', ["../COPYRIGHT.txt", "../LICENSE.txt"], run_in_subprocess(core_builders.make_license_header))
env.Depends("#core/license.gen.h", ["../COPYRIGHT.txt", "../LICENSE.txt"])
env.CommandNoCache(
"#core/license.gen.h", ["../COPYRIGHT.txt", "../LICENSE.txt"], run_in_subprocess(core_builders.make_license_header)
)

# Chain load SCsubs
SConscript('os/SCsub')
SConscript('math/SCsub')
SConscript('crypto/SCsub')
SConscript('io/SCsub')
SConscript('bind/SCsub')
SConscript("os/SCsub")
SConscript("math/SCsub")
SConscript("crypto/SCsub")
SConscript("io/SCsub")
SConscript("bind/SCsub")


# Build it all as a library
Expand Down
2 changes: 1 addition & 1 deletion core/bind/SCsub
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python

Import('env')
Import("env")

env.add_source_files(env.core_sources, "*.cpp")
Loading

0 comments on commit 7bf9787

Please sign in to comment.