Unify TextServer built-in module and GDExtension code.

This commit is contained in:
bruvzg
2022-02-13 14:41:29 +02:00
parent 178961a6dc
commit f19cd44346
49 changed files with 4658 additions and 3126 deletions

View File

@ -0,0 +1,205 @@
#!/usr/bin/env python
import atexit
import os
import sys
import methods
import time
# For the reference:
# - CCFLAGS are compilation flags shared between C and C++
# - CFLAGS are for C-specific compilation flags
# - CXXFLAGS are for C++-specific compilation flags
# - CPPFLAGS are for pre-processor flags
# - CPPDEFINES are for pre-processor defines
# - LINKFLAGS are for linking flags
time_at_start = time.time()
env = SConscript("./godot-cpp/SConstruct")
env.__class__.disable_warnings = methods.disable_warnings
opts = Variables([], ARGUMENTS)
opts.Add(BoolVariable("freetype_enabled", "Use FreeType library", True))
opts.Add(BoolVariable("msdfgen_enabled", "Use MSDFgen library (require FreeType)", True))
opts.Add(BoolVariable("verbose", "Enable verbose output for the compilation", False))
opts.Update(env)
if not env["verbose"]:
methods.no_verbose(sys, env)
# MSDFGEN
if env["msdfgen_enabled"] and env["freetype_enabled"]:
env_msdfgen = env.Clone()
env_msdfgen.disable_warnings()
thirdparty_msdfgen_dir = "../../../thirdparty/msdfgen/"
thirdparty_msdfgen_sources = [
"core/Contour.cpp",
"core/EdgeHolder.cpp",
"core/MSDFErrorCorrection.cpp",
"core/Projection.cpp",
"core/Scanline.cpp",
"core/Shape.cpp",
"core/SignedDistance.cpp",
"core/Vector2.cpp",
"core/contour-combiners.cpp",
"core/edge-coloring.cpp",
"core/edge-segments.cpp",
"core/edge-selectors.cpp",
"core/equation-solver.cpp",
"core/msdf-error-correction.cpp",
"core/msdfgen.cpp",
"core/rasterization.cpp",
"core/render-sdf.cpp",
"core/sdf-error-estimation.cpp",
"core/shape-description.cpp",
]
thirdparty_msdfgen_sources = [thirdparty_msdfgen_dir + file for file in thirdparty_msdfgen_sources]
env_msdfgen.Append(CPPPATH=["../../../thirdparty/freetype/include", "../../../thirdparty/msdfgen"])
env.Append(CPPPATH=["../../../thirdparty/msdfgen"])
env.Append(CPPDEFINES=["MODULE_MSDFGEN_ENABLED"])
lib = env_msdfgen.Library(
f'msdfgen_builtin.{env["platform"]}.{env["target"]}.{env["arch_suffix"]}{env["LIBSUFFIX"]}',
thirdparty_msdfgen_sources,
)
env.Append(LIBS=[lib])
# FreeType
if env["freetype_enabled"]:
env_freetype = env.Clone()
env_freetype.disable_warnings()
thirdparty_freetype_dir = "../../../thirdparty/freetype/"
thirdparty_freetype_sources = [
"src/autofit/autofit.c",
"src/base/ftbase.c",
"src/base/ftbbox.c",
"src/base/ftbdf.c",
"src/base/ftbitmap.c",
"src/base/ftcid.c",
"src/base/ftdebug.c",
"src/base/ftfstype.c",
"src/base/ftgasp.c",
"src/base/ftglyph.c",
"src/base/ftgxval.c",
"src/base/ftinit.c",
"src/base/ftmm.c",
"src/base/ftotval.c",
"src/base/ftpatent.c",
"src/base/ftpfr.c",
"src/base/ftstroke.c",
"src/base/ftsynth.c",
"src/base/ftsystem.c",
"src/base/fttype1.c",
"src/base/ftwinfnt.c",
"src/bdf/bdf.c",
"src/bzip2/ftbzip2.c",
"src/cache/ftcache.c",
"src/cff/cff.c",
"src/cid/type1cid.c",
"src/gxvalid/gxvalid.c",
"src/gzip/ftgzip.c",
"src/lzw/ftlzw.c",
"src/otvalid/otvalid.c",
"src/pcf/pcf.c",
"src/pfr/pfr.c",
"src/psaux/psaux.c",
"src/pshinter/pshinter.c",
"src/psnames/psnames.c",
"src/raster/raster.c",
"src/sdf/sdf.c",
"src/smooth/smooth.c",
"src/truetype/truetype.c",
"src/type1/type1.c",
"src/type42/type42.c",
"src/winfonts/winfnt.c",
"src/sfnt/sfnt.c",
]
thirdparty_freetype_sources = [thirdparty_freetype_dir + file for file in thirdparty_freetype_sources]
thirdparty_png_dir = "../../../thirdparty/libpng/"
thirdparty_png_sources = [
"png.c",
"pngerror.c",
"pngget.c",
"pngmem.c",
"pngpread.c",
"pngread.c",
"pngrio.c",
"pngrtran.c",
"pngrutil.c",
"pngset.c",
"pngtrans.c",
"pngwio.c",
"pngwrite.c",
"pngwtran.c",
"pngwutil.c",
]
thirdparty_freetype_sources += [thirdparty_png_dir + file for file in thirdparty_png_sources]
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_freetype_sources += [thirdparty_zlib_dir + file for file in thirdparty_zlib_sources]
env_freetype.Append(CPPPATH=[thirdparty_freetype_dir + "/include", thirdparty_zlib_dir, thirdparty_png_dir])
env.Append(CPPPATH=[thirdparty_freetype_dir + "/include"])
env_freetype.Append(CPPDEFINES=["FT2_BUILD_LIBRARY", "FT_CONFIG_OPTION_USE_PNG", ("PNG_ARM_NEON_OPT", 0)])
if env["target"] == "debug":
env_freetype.Append(CPPDEFINES=["ZLIB_DEBUG"])
env.Append(CPPDEFINES=["MODULE_FREETYPE_ENABLED"])
lib = env_freetype.Library(
f'freetype_builtin.{env["platform"]}.{env["target"]}.{env["arch_suffix"]}{env["LIBSUFFIX"]}',
thirdparty_freetype_sources,
)
env.Append(LIBS=[lib])
env.Append(CPPDEFINES=["GDEXTENSION"])
env.Append(CPPPATH=["../"])
sources = Glob("../*.cpp")
if env["platform"] == "osx":
methods.write_osx_plist(
f'./bin/libtextserver_fallback.osx.{env["target"]}.framework',
f'libtextserver_fallback.osx.{env["target"]}',
"org.godotengine.textserver_fallback",
"Fallback Text Server",
)
library = env.SharedLibrary(
f'./bin/libtextserver_fallback.osx.{env["target"]}.framework/libtextserver_fallback.osx.{env["target"]}',
source=sources,
)
else:
library = env.SharedLibrary(
f'./bin/libtextserver_fallback.{env["platform"]}.{env["target"]}.{env["arch_suffix"]}{env["SHLIBSUFFIX"]}',
source=sources,
)
Default(library)
def print_elapsed_time():
elapsed_time_sec = round(time.time() - time_at_start, 3)
time_ms = round((elapsed_time_sec % 1) * 1000)
print("[Time elapsed: {}.{:03}]".format(time.strftime("%H:%M:%S", time.gmtime(elapsed_time_sec)), time_ms))
atexit.register(print_elapsed_time)

View File

@ -0,0 +1,130 @@
import os
import sys
def no_verbose(sys, env):
colors = {}
# Colors are disabled in non-TTY environments such as pipes. This means
# that if output is redirected to a file, it will not contain color codes
if sys.stdout.isatty():
colors["blue"] = "\033[0;94m"
colors["bold_blue"] = "\033[1;94m"
colors["reset"] = "\033[0m"
else:
colors["blue"] = ""
colors["bold_blue"] = ""
colors["reset"] = ""
# There is a space before "..." to ensure that source file names can be
# Ctrl + clicked in the VS Code terminal.
compile_source_message = "{}Compiling {}$SOURCE{} ...{}".format(
colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"]
)
java_compile_source_message = "{}Compiling {}$SOURCE{} ...{}".format(
colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"]
)
compile_shared_source_message = "{}Compiling shared {}$SOURCE{} ...{}".format(
colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"]
)
link_program_message = "{}Linking Program {}$TARGET{} ...{}".format(
colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"]
)
link_library_message = "{}Linking Static Library {}$TARGET{} ...{}".format(
colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"]
)
ranlib_library_message = "{}Ranlib Library {}$TARGET{} ...{}".format(
colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"]
)
link_shared_library_message = "{}Linking Shared Library {}$TARGET{} ...{}".format(
colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"]
)
java_library_message = "{}Creating Java Archive {}$TARGET{} ...{}".format(
colors["blue"], colors["bold_blue"], colors["blue"], colors["reset"]
)
env.Append(CXXCOMSTR=[compile_source_message])
env.Append(CCCOMSTR=[compile_source_message])
env.Append(SHCCCOMSTR=[compile_shared_source_message])
env.Append(SHCXXCOMSTR=[compile_shared_source_message])
env.Append(ARCOMSTR=[link_library_message])
env.Append(RANLIBCOMSTR=[ranlib_library_message])
env.Append(SHLINKCOMSTR=[link_shared_library_message])
env.Append(LINKCOMSTR=[link_program_message])
env.Append(JARCOMSTR=[java_library_message])
env.Append(JAVACCOMSTR=[java_compile_source_message])
def disable_warnings(self):
# 'self' is the environment
if self["platform"] == "windows" and not self["use_mingw"]:
# We have to remove existing warning level defines before appending /w,
# otherwise we get: "warning D9025 : overriding '/W3' with '/w'"
warn_flags = ["/Wall", "/W4", "/W3", "/W2", "/W1", "/WX"]
self.Append(CCFLAGS=["/w"])
self.Append(CFLAGS=["/w"])
self.Append(CXXFLAGS=["/w"])
self["CCFLAGS"] = [x for x in self["CCFLAGS"] if not x in warn_flags]
self["CFLAGS"] = [x for x in self["CFLAGS"] if not x in warn_flags]
self["CXXFLAGS"] = [x for x in self["CXXFLAGS"] if not x in warn_flags]
else:
self.Append(CCFLAGS=["-w"])
self.Append(CFLAGS=["-w"])
self.Append(CXXFLAGS=["-w"])
def make_icu_data(target, source, env):
dst = target[0].srcnode().abspath
g = open(dst, "w", encoding="utf-8")
g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
g.write("/* (C) 2016 and later: Unicode, Inc. and others. */\n")
g.write("/* License & terms of use: https://www.unicode.org/copyright.html */\n")
g.write("#ifndef _ICU_DATA_H\n")
g.write("#define _ICU_DATA_H\n")
g.write('#include "unicode/utypes.h"\n')
g.write('#include "unicode/udata.h"\n')
g.write('#include "unicode/uversion.h"\n')
f = open(source[0].srcnode().abspath, "rb")
buf = f.read()
g.write('extern "C" U_EXPORT const size_t U_ICUDATA_SIZE = ' + str(len(buf)) + ";\n")
g.write('extern "C" U_EXPORT const unsigned char U_ICUDATA_ENTRY_POINT[] = {\n')
for i in range(len(buf)):
g.write("\t" + str(buf[i]) + ",\n")
g.write("};\n")
g.write("#endif")
def write_osx_plist(target, binary_name, identifier, name):
os.makedirs(f"{target}/Resourece/", exist_ok=True)
f = open(f"{target}/Resourece/Info.plist", "w")
f.write(f'<?xml version="1.0" encoding="UTF-8"?>\n')
f.write(f'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n')
f.write(f'<plist version="1.0">\n')
f.write(f"<dict>\n")
f.write(f"\t<key>CFBundleExecutable</key>\n")
f.write(f"\t<string>{binary_name}</string>\n")
f.write(f"\t<key>CFBundleIdentifier</key>\n")
f.write(f"\t<string>{identifier}</string>\n")
f.write(f"\t<key>CFBundleInfoDictionaryVersion</key>\n")
f.write(f"\t<string>6.0</string>\n")
f.write(f"\t<key>CFBundleName</key>\n")
f.write(f"\t<string>{name}</string>\n")
f.write(f"\t<key>CFBundlePackageType</key>\n")
f.write(f"\t<string>FMWK</string>\n")
f.write(f"\t<key>CFBundleShortVersionString</key>\n")
f.write(f"\t<string>1.0.0</string>\n")
f.write(f"\t<key>CFBundleSupportedPlatforms</key>\n")
f.write(f"\t<array>\n")
f.write(f"\t\t<string>MacOSX</string>\n")
f.write(f"\t</array>\n")
f.write(f"\t<key>CFBundleVersion</key>\n")
f.write(f"\t<string>1.0.0</string>\n")
f.write(f"\t<key>LSMinimumSystemVersion</key>\n")
f.write(f"\t<string>10.14</string>\n")
f.write(f"</dict>\n")
f.write(f"</plist>\n")

View File

@ -0,0 +1,12 @@
[configuration]
entry_symbol = "textserver_fallback_init"
[libraries]
linux.64.debug = "bin/libtextserver_fallback.linux.debug.64.so"
linux.64.release = "bin/libtextserver_fallback.linux.release.64.so"
windows.64.debug = "bin/libtextserver_fallback.windows.debug.64.dll"
windows.64.release = "bin/libtextserver_fallback.windows.release.64.dll"
macos.debug = "bin/libtextserver_fallback.osx.debug.framework"
macos.release = "bin/libtextserver_fallback.osx.release.framework"