From 5d8630559623303f5e5c4b013e64939ebfda45fb Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 22 Aug 2024 12:42:12 +0100 Subject: [PATCH] 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. (cherry picked from commit 8cbc2116162764479edeec78e4b2b7c41447b643; amended to fix conflicts in two cmake files, due to serenity still having more code_generators, and still buildling OutOfProcessWebView.cpp. Also kept embed_as_string_view because it's used by stringify_gml. Did this by giving embed_as_string.py a --type= argument that defaults to string but can also be set to string-view.) --- Meta/CMake/code_generators.cmake | 25 +++++++++++++++++-- Meta/CMake/libweb_generators.cmake | 8 +++--- ...d_as_string_view.py => embed_as_string.py} | 22 ++++++++++++---- ...as_string_view.gni => embed_as_string.gni} | 10 ++++---- .../Userland/Libraries/LibWeb/BUILD.gn | 10 ++++---- .../Userland/Libraries/LibWebView/BUILD.gn | 4 +-- .../Libraries/LibWeb/CSS/StyleComputer.cpp | 8 +++--- Userland/Libraries/LibWebView/CMakeLists.txt | 2 +- .../LibWebView/ViewImplementation.cpp | 4 +-- 9 files changed, 63 insertions(+), 30 deletions(-) rename Meta/{embed_as_string_view.py => embed_as_string.py} (54%) rename Meta/gn/build/{embed_as_string_view.gni => embed_as_string.gni} (85%) diff --git a/Meta/CMake/code_generators.cmake b/Meta/CMake/code_generators.cmake index 0c96f2d8f4f..9d2015a6216 100644 --- a/Meta/CMake/code_generators.cmake +++ b/Meta/CMake/code_generators.cmake @@ -2,6 +2,27 @@ # Functions for generating sources using host tools # +function(embed_as_string name source_file output source_variable_name) + cmake_parse_arguments(PARSE_ARGV 4 EMBED_STRING_VIEW "" "NAMESPACE" "") + set(namespace_arg "") + if (EMBED_STRING_VIEW_NAMESPACE) + set(namespace_arg "-s ${EMBED_STRING_VIEW_NAMESPACE}") + endif() + find_package(Python3 REQUIRED COMPONENTS Interpreter) + add_custom_command( + OUTPUT "${output}" + 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 remove "${output}.tmp" + VERBATIM + DEPENDS "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string.py" + MAIN_DEPENDENCY "${source_file}" + ) + + add_custom_target("generate_${name}" DEPENDS "${output}") + add_dependencies(all_generated "generate_${name}") +endfunction() + function(embed_as_string_view name source_file output source_variable_name) cmake_parse_arguments(PARSE_ARGV 4 EMBED_STRING_VIEW "" "NAMESPACE" "") set(namespace_arg "") @@ -11,11 +32,11 @@ function(embed_as_string_view name source_file output source_variable_name) find_package(Python3 REQUIRED COMPONENTS Interpreter) add_custom_command( 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" --type=string-view "${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 remove "${output}.tmp" VERBATIM - DEPENDS "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string_view.py" + DEPENDS "${SerenityOS_SOURCE_DIR}/Meta/embed_as_string.py" MAIN_DEPENDENCY "${source_file}" ) diff --git a/Meta/CMake/libweb_generators.cmake b/Meta/CMake/libweb_generators.cmake index a81498dd3b4..db04b770db5 100644 --- a/Meta/CMake/libweb_generators.cmake +++ b/Meta/CMake/libweb_generators.cmake @@ -64,7 +64,7 @@ function (generate_css_implementation) arguments -j "${LIBWEB_INPUT_FOLDER}/CSS/Keywords.json" ) - embed_as_string_view( + embed_as_string( "DefaultStyleSheetSource.cpp" "${LIBWEB_INPUT_FOLDER}/CSS/Default.css" "CSS/DefaultStyleSheetSource.cpp" @@ -72,7 +72,7 @@ function (generate_css_implementation) NAMESPACE "Web::CSS" ) - embed_as_string_view( + embed_as_string( "QuirksModeStyleSheetSource.cpp" "${LIBWEB_INPUT_FOLDER}/CSS/QuirksMode.css" "CSS/QuirksModeStyleSheetSource.cpp" @@ -80,7 +80,7 @@ function (generate_css_implementation) NAMESPACE "Web::CSS" ) - embed_as_string_view( + embed_as_string( "MathMLStyleSheetSource.cpp" "${LIBWEB_INPUT_FOLDER}/MathML/Default.css" "MathML/MathMLStyleSheetSource.cpp" @@ -88,7 +88,7 @@ function (generate_css_implementation) NAMESPACE "Web::CSS" ) - embed_as_string_view( + embed_as_string( "SVGStyleSheetSource.cpp" "${LIBWEB_INPUT_FOLDER}/SVG/Default.css" "SVG/SVGStyleSheetSource.cpp" diff --git a/Meta/embed_as_string_view.py b/Meta/embed_as_string.py similarity index 54% rename from Meta/embed_as_string_view.py rename to Meta/embed_as_string.py index a9bd7a16e14..078a4415959 100644 --- a/Meta/embed_as_string_view.py +++ b/Meta/embed_as_string.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 r""" - Embeds a file into a StringView, a la #embed from C++23 + Embeds a file as a String or StringView, a la #embed from C++23 """ import argparse @@ -12,6 +12,8 @@ def main(): epilog=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('input', help='input file to stringify') + parser.add_argument('--type', choices=['string', 'string-view'], + default='string') parser.add_argument('-o', '--output', required=True, help='output file') parser.add_argument('-n', '--variable-name', required=True, @@ -21,15 +23,25 @@ def main(): args = parser.parse_args() with open(args.output, 'w') as f: - f.write("#include \n") + if args.type == 'string': + f.write("#include \n") + elif args.type == 'string-view': + f.write("#include \n") if args.namespace: f.write(f"namespace {args.namespace} {{\n") - f.write(f"extern StringView {args.variable_name};\n") - f.write(f"StringView {args.variable_name} = R\"~~~(") + if args.type == 'string': + f.write(f"extern String {args.variable_name};\n") + f.write(f"String {args.variable_name} = R\"~~~(") + elif args.type == 'string-view': + f.write(f"extern StringView {args.variable_name};\n") + f.write(f"StringView {args.variable_name} = R\"~~~(") with open(args.input, 'r') as input: for line in input.readlines(): f.write(f"{line}") - f.write(")~~~\"sv;\n") + if args.type == 'string': + f.write(")~~~\"_string;\n") + elif args.type == 'string-view': + f.write(")~~~\"sv;\n") if args.namespace: f.write("}\n") diff --git a/Meta/gn/build/embed_as_string_view.gni b/Meta/gn/build/embed_as_string.gni similarity index 85% rename from Meta/gn/build/embed_as_string_view.gni rename to Meta/gn/build/embed_as_string.gni index eab79dbadc9..70301e00af1 100644 --- a/Meta/gn/build/embed_as_string_view.gni +++ b/Meta/gn/build/embed_as_string.gni @@ -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 # be placed into a global variable, optionally with a namespace wrapping it. # @@ -19,21 +19,21 @@ # # Example use: # -# embed_as_string_view("embed_my_file") { +# embed_as_string("embed_my_file") { # input = "MyFile.txt" # output = "$root_gen_dir/MyDirectory/MyFile.cpp" # variable_name = "my_file_contents" # 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.output), "must set 'output' in $target_name") assert(defined(invoker.variable_name), "must set 'variable_name' in $target_name") action(target_name) { - script = "//Meta/embed_as_string_view.py" + script = "//Meta/embed_as_string.py" sources = [ invoker.input ] outputs = [ invoker.output ] diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn index b308b75ac6a..e84871de80c 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn @@ -1,5 +1,5 @@ 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("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" output = "$target_gen_dir/CSS/DefaultStyleSheetSource.cpp" variable_name = "default_stylesheet_source" namespace = "Web::CSS" } -embed_as_string_view("generate_mathml_stylesheet_source") { +embed_as_string("generate_mathml_stylesheet_source") { input = "MathML/Default.css" output = "$target_gen_dir/MathML/MathMLStyleSheetSource.cpp" variable_name = "mathml_stylesheet_source" namespace = "Web::CSS" } -embed_as_string_view("generate_svg_stylesheet_source") { +embed_as_string("generate_svg_stylesheet_source") { input = "SVG/Default.css" output = "$target_gen_dir/SVG/SVGStyleSheetSource.cpp" variable_name = "svg_stylesheet_source" namespace = "Web::CSS" } -embed_as_string_view("generate_quirks_mode_stylesheet_source") { +embed_as_string("generate_quirks_mode_stylesheet_source") { input = "CSS/QuirksMode.css" output = "$target_gen_dir/CSS/QuirksModeStyleSheetSource.cpp" variable_name = "quirks_mode_stylesheet_source" diff --git a/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn index b734d2cc23e..5bc5eb96590 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn @@ -1,7 +1,7 @@ import("//Meta/gn/build/compiled_action.gni") import("//Meta/gn/build/download_cache.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() { # 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" output = "$target_gen_dir/NativeStyleSheetSource.cpp" variable_name = "native_stylesheet_source" diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index b913e9f382c..61e51e45a98 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -224,7 +224,7 @@ static CSSStyleSheet& default_stylesheet(DOM::Document const& document) { static JS::Handle sheet; 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)); } return *sheet; @@ -234,7 +234,7 @@ static CSSStyleSheet& quirks_mode_stylesheet(DOM::Document const& document) { static JS::Handle sheet; 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)); } return *sheet; @@ -244,7 +244,7 @@ static CSSStyleSheet& mathml_stylesheet(DOM::Document const& document) { static JS::Handle sheet; 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)); } return *sheet; @@ -254,7 +254,7 @@ static CSSStyleSheet& svg_stylesheet(DOM::Document const& document) { static JS::Handle sheet; 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)); } return *sheet; diff --git a/Userland/Libraries/LibWebView/CMakeLists.txt b/Userland/Libraries/LibWebView/CMakeLists.txt index 42503f7d701..b4fdf866811 100644 --- a/Userland/Libraries/LibWebView/CMakeLists.txt +++ b/Userland/Libraries/LibWebView/CMakeLists.txt @@ -25,7 +25,7 @@ if (SERENITYOS) list(APPEND SOURCES OutOfProcessWebView.cpp) endif() -embed_as_string_view( +embed_as_string( "NativeStyleSheetSource.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Native.css" "NativeStyleSheetSource.cpp" diff --git a/Userland/Libraries/LibWebView/ViewImplementation.cpp b/Userland/Libraries/LibWebView/ViewImplementation.cpp index 2bd20b188db..e6a730b414e 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.cpp +++ b/Userland/Libraries/LibWebView/ViewImplementation.cpp @@ -624,8 +624,8 @@ void ViewImplementation::set_user_style_sheet(String source) void ViewImplementation::use_native_user_style_sheet() { - extern StringView native_stylesheet_source; - set_user_style_sheet(MUST(String::from_utf8(native_stylesheet_source))); + extern String native_stylesheet_source; + set_user_style_sheet(native_stylesheet_source); } void ViewImplementation::enable_inspector_prototype()