Meta: Make embed_as_string_view.py produce Strings instead

This is only used for CSS style sheets. One case wants it as a String,
and the others don't care, but will in future also want to have the
source as a String.
This commit is contained in:
Sam Atkins 2024-08-22 12:42:12 +01:00 committed by Sam Atkins
parent fd49562f50
commit 8cbc211616
Notes: github-actions[bot] 2024-09-03 09:13:30 +00:00
9 changed files with 31 additions and 32 deletions

View file

@ -2,7 +2,7 @@
# Functions for generating sources using host tools # Functions for generating sources using host tools
# #
function(embed_as_string_view name source_file output source_variable_name) function(embed_as_string name source_file output source_variable_name)
cmake_parse_arguments(PARSE_ARGV 4 EMBED_STRING_VIEW "" "NAMESPACE" "") cmake_parse_arguments(PARSE_ARGV 4 EMBED_STRING_VIEW "" "NAMESPACE" "")
set(namespace_arg "") set(namespace_arg "")
if (EMBED_STRING_VIEW_NAMESPACE) if (EMBED_STRING_VIEW_NAMESPACE)
@ -11,11 +11,11 @@ function(embed_as_string_view name source_file output source_variable_name)
find_package(Python3 REQUIRED COMPONENTS Interpreter) find_package(Python3 REQUIRED COMPONENTS Interpreter)
add_custom_command( add_custom_command(
OUTPUT "${output}" OUTPUT "${output}"
COMMAND "${Python3_EXECUTABLE}" "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string_view.py" "${source_file}" -o "${output}.tmp" -n "${source_variable_name}" ${namespace_arg} COMMAND "${Python3_EXECUTABLE}" "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string.py" "${source_file}" -o "${output}.tmp" -n "${source_variable_name}" ${namespace_arg}
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${output}.tmp" "${output}" COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${output}.tmp" "${output}"
COMMAND "${CMAKE_COMMAND}" -E remove "${output}.tmp" COMMAND "${CMAKE_COMMAND}" -E remove "${output}.tmp"
VERBATIM VERBATIM
DEPENDS "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string_view.py" DEPENDS "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string.py"
MAIN_DEPENDENCY "${source_file}" MAIN_DEPENDENCY "${source_file}"
) )
@ -78,4 +78,3 @@ function(compile_ipc source output)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${output} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${current_source_dir_relative}" OPTIONAL) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${output} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${current_source_dir_relative}" OPTIONAL)
endif() endif()
endfunction() endfunction()

View file

@ -64,7 +64,7 @@ function (generate_css_implementation)
arguments -j "${LIBWEB_INPUT_FOLDER}/CSS/Keywords.json" arguments -j "${LIBWEB_INPUT_FOLDER}/CSS/Keywords.json"
) )
embed_as_string_view( embed_as_string(
"DefaultStyleSheetSource.cpp" "DefaultStyleSheetSource.cpp"
"${LIBWEB_INPUT_FOLDER}/CSS/Default.css" "${LIBWEB_INPUT_FOLDER}/CSS/Default.css"
"CSS/DefaultStyleSheetSource.cpp" "CSS/DefaultStyleSheetSource.cpp"
@ -72,7 +72,7 @@ function (generate_css_implementation)
NAMESPACE "Web::CSS" NAMESPACE "Web::CSS"
) )
embed_as_string_view( embed_as_string(
"QuirksModeStyleSheetSource.cpp" "QuirksModeStyleSheetSource.cpp"
"${LIBWEB_INPUT_FOLDER}/CSS/QuirksMode.css" "${LIBWEB_INPUT_FOLDER}/CSS/QuirksMode.css"
"CSS/QuirksModeStyleSheetSource.cpp" "CSS/QuirksModeStyleSheetSource.cpp"
@ -80,7 +80,7 @@ function (generate_css_implementation)
NAMESPACE "Web::CSS" NAMESPACE "Web::CSS"
) )
embed_as_string_view( embed_as_string(
"MathMLStyleSheetSource.cpp" "MathMLStyleSheetSource.cpp"
"${LIBWEB_INPUT_FOLDER}/MathML/Default.css" "${LIBWEB_INPUT_FOLDER}/MathML/Default.css"
"MathML/MathMLStyleSheetSource.cpp" "MathML/MathMLStyleSheetSource.cpp"
@ -88,7 +88,7 @@ function (generate_css_implementation)
NAMESPACE "Web::CSS" NAMESPACE "Web::CSS"
) )
embed_as_string_view( embed_as_string(
"SVGStyleSheetSource.cpp" "SVGStyleSheetSource.cpp"
"${LIBWEB_INPUT_FOLDER}/SVG/Default.css" "${LIBWEB_INPUT_FOLDER}/SVG/Default.css"
"SVG/SVGStyleSheetSource.cpp" "SVG/SVGStyleSheetSource.cpp"

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
r""" r"""
Embeds a file into a StringView, a la #embed from C++23 Embeds a file into a String, a la #embed from C++23
""" """
import argparse import argparse
@ -21,15 +21,15 @@ def main():
args = parser.parse_args() args = parser.parse_args()
with open(args.output, 'w') as f: with open(args.output, 'w') as f:
f.write("#include <AK/StringView.h>\n") f.write("#include <AK/String.h>\n")
if args.namespace: if args.namespace:
f.write(f"namespace {args.namespace} {{\n") f.write(f"namespace {args.namespace} {{\n")
f.write(f"extern StringView {args.variable_name};\n") f.write(f"extern String {args.variable_name};\n")
f.write(f"StringView {args.variable_name} = R\"~~~(") f.write(f"String {args.variable_name} = R\"~~~(")
with open(args.input, 'r') as input: with open(args.input, 'r') as input:
for line in input.readlines(): for line in input.readlines():
f.write(f"{line}") f.write(f"{line}")
f.write(")~~~\"sv;\n") f.write(")~~~\"_string;\n")
if args.namespace: if args.namespace:
f.write("}\n") f.write("}\n")

View file

@ -1,6 +1,6 @@
# This file introduces a template for calling embed_as_string_view.py. # This file introduces a template for calling embed_as_string.py.
# #
# embed_as_string_view behaves like C++23 #embed, converting an input file into # embed_as_string behaves like C++23 #embed, converting an input file into
# an AK::StringView literal rather than a C-string literal. The literal will # an AK::StringView literal rather than a C-string literal. The literal will
# be placed into a global variable, optionally with a namespace wrapping it. # be placed into a global variable, optionally with a namespace wrapping it.
# #
@ -19,21 +19,21 @@
# #
# Example use: # Example use:
# #
# embed_as_string_view("embed_my_file") { # embed_as_string("embed_my_file") {
# input = "MyFile.txt" # input = "MyFile.txt"
# output = "$root_gen_dir/MyDirectory/MyFile.cpp" # output = "$root_gen_dir/MyDirectory/MyFile.cpp"
# variable_name = "my_file_contents" # variable_name = "my_file_contents"
# namespace = "My::NS" # namespace = "My::NS"
# } # }
template("embed_as_string_view") { template("embed_as_string") {
assert(defined(invoker.input), "must set 'input' in $target_name") assert(defined(invoker.input), "must set 'input' in $target_name")
assert(defined(invoker.output), "must set 'output' in $target_name") assert(defined(invoker.output), "must set 'output' in $target_name")
assert(defined(invoker.variable_name), assert(defined(invoker.variable_name),
"must set 'variable_name' in $target_name") "must set 'variable_name' in $target_name")
action(target_name) { action(target_name) {
script = "//Meta/embed_as_string_view.py" script = "//Meta/embed_as_string.py"
sources = [ invoker.input ] sources = [ invoker.input ]
outputs = [ invoker.output ] outputs = [ invoker.output ]

View file

@ -1,5 +1,5 @@
import("//Meta/gn/build/compiled_action.gni") import("//Meta/gn/build/compiled_action.gni")
import("//Meta/gn/build/embed_as_string_view.gni") import("//Meta/gn/build/embed_as_string.gni")
import("generate_idl_bindings.gni") import("generate_idl_bindings.gni")
import("idl_files.gni") import("idl_files.gni")
@ -220,28 +220,28 @@ compiled_action("generate_css_keyword") {
] ]
} }
embed_as_string_view("generate_default_stylesheet_source") { embed_as_string("generate_default_stylesheet_source") {
input = "CSS/Default.css" input = "CSS/Default.css"
output = "$target_gen_dir/CSS/DefaultStyleSheetSource.cpp" output = "$target_gen_dir/CSS/DefaultStyleSheetSource.cpp"
variable_name = "default_stylesheet_source" variable_name = "default_stylesheet_source"
namespace = "Web::CSS" namespace = "Web::CSS"
} }
embed_as_string_view("generate_mathml_stylesheet_source") { embed_as_string("generate_mathml_stylesheet_source") {
input = "MathML/Default.css" input = "MathML/Default.css"
output = "$target_gen_dir/MathML/MathMLStyleSheetSource.cpp" output = "$target_gen_dir/MathML/MathMLStyleSheetSource.cpp"
variable_name = "mathml_stylesheet_source" variable_name = "mathml_stylesheet_source"
namespace = "Web::CSS" namespace = "Web::CSS"
} }
embed_as_string_view("generate_svg_stylesheet_source") { embed_as_string("generate_svg_stylesheet_source") {
input = "SVG/Default.css" input = "SVG/Default.css"
output = "$target_gen_dir/SVG/SVGStyleSheetSource.cpp" output = "$target_gen_dir/SVG/SVGStyleSheetSource.cpp"
variable_name = "svg_stylesheet_source" variable_name = "svg_stylesheet_source"
namespace = "Web::CSS" namespace = "Web::CSS"
} }
embed_as_string_view("generate_quirks_mode_stylesheet_source") { embed_as_string("generate_quirks_mode_stylesheet_source") {
input = "CSS/QuirksMode.css" input = "CSS/QuirksMode.css"
output = "$target_gen_dir/CSS/QuirksModeStyleSheetSource.cpp" output = "$target_gen_dir/CSS/QuirksModeStyleSheetSource.cpp"
variable_name = "quirks_mode_stylesheet_source" variable_name = "quirks_mode_stylesheet_source"

View file

@ -1,7 +1,7 @@
import("//Meta/gn/build/compiled_action.gni") import("//Meta/gn/build/compiled_action.gni")
import("//Meta/gn/build/download_cache.gni") import("//Meta/gn/build/download_cache.gni")
import("//Meta/gn/build/download_file.gni") import("//Meta/gn/build/download_file.gni")
import("//Meta/gn/build/embed_as_string_view.gni") import("//Meta/gn/build/embed_as_string.gni")
declare_args() { declare_args() {
# If true, Download public suffix list from GitHub. # If true, Download public suffix list from GitHub.
@ -106,7 +106,7 @@ compiled_action("UIProcessServerEndpoint") {
] ]
} }
embed_as_string_view("generate_native_stylesheet_source") { embed_as_string("generate_native_stylesheet_source") {
input = "Native.css" input = "Native.css"
output = "$target_gen_dir/NativeStyleSheetSource.cpp" output = "$target_gen_dir/NativeStyleSheetSource.cpp"
variable_name = "native_stylesheet_source" variable_name = "native_stylesheet_source"

View file

@ -218,7 +218,7 @@ static CSSStyleSheet& default_stylesheet(DOM::Document const& document)
{ {
static JS::Handle<CSSStyleSheet> sheet; static JS::Handle<CSSStyleSheet> sheet;
if (!sheet.cell()) { if (!sheet.cell()) {
extern StringView default_stylesheet_source; extern String default_stylesheet_source;
sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), default_stylesheet_source)); sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), default_stylesheet_source));
} }
return *sheet; return *sheet;
@ -228,7 +228,7 @@ static CSSStyleSheet& quirks_mode_stylesheet(DOM::Document const& document)
{ {
static JS::Handle<CSSStyleSheet> sheet; static JS::Handle<CSSStyleSheet> sheet;
if (!sheet.cell()) { if (!sheet.cell()) {
extern StringView quirks_mode_stylesheet_source; extern String quirks_mode_stylesheet_source;
sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), quirks_mode_stylesheet_source)); sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), quirks_mode_stylesheet_source));
} }
return *sheet; return *sheet;
@ -238,7 +238,7 @@ static CSSStyleSheet& mathml_stylesheet(DOM::Document const& document)
{ {
static JS::Handle<CSSStyleSheet> sheet; static JS::Handle<CSSStyleSheet> sheet;
if (!sheet.cell()) { if (!sheet.cell()) {
extern StringView mathml_stylesheet_source; extern String mathml_stylesheet_source;
sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), mathml_stylesheet_source)); sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), mathml_stylesheet_source));
} }
return *sheet; return *sheet;
@ -248,7 +248,7 @@ static CSSStyleSheet& svg_stylesheet(DOM::Document const& document)
{ {
static JS::Handle<CSSStyleSheet> sheet; static JS::Handle<CSSStyleSheet> sheet;
if (!sheet.cell()) { if (!sheet.cell()) {
extern StringView svg_stylesheet_source; extern String svg_stylesheet_source;
sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), svg_stylesheet_source)); sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), svg_stylesheet_source));
} }
return *sheet; return *sheet;

View file

@ -23,7 +23,7 @@ set(SOURCES
set(GENERATED_SOURCES ${CURRENT_LIB_GENERATED}) set(GENERATED_SOURCES ${CURRENT_LIB_GENERATED})
embed_as_string_view( embed_as_string(
"NativeStyleSheetSource.cpp" "NativeStyleSheetSource.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Native.css" "${CMAKE_CURRENT_SOURCE_DIR}/Native.css"
"NativeStyleSheetSource.cpp" "NativeStyleSheetSource.cpp"

View file

@ -592,8 +592,8 @@ void ViewImplementation::set_user_style_sheet(String source)
void ViewImplementation::use_native_user_style_sheet() void ViewImplementation::use_native_user_style_sheet()
{ {
extern StringView native_stylesheet_source; extern String native_stylesheet_source;
set_user_style_sheet(MUST(String::from_utf8(native_stylesheet_source))); set_user_style_sheet(native_stylesheet_source);
} }
void ViewImplementation::enable_inspector_prototype() void ViewImplementation::enable_inspector_prototype()