Files
ladybird/Meta/CMake/code_generators.cmake
Timothy Flynn 922b811279 Meta: Replace IPCCompiler with a python generator
This replaces the C++ IPC compiler and generator with a python3 port.

For validation, the parser is nearly a line-for-line port of the C++
parser. This patch includes lexer.py, which is a simple port of our
GenericLexer, as the mechanics actually felt pretty nice in python as
well.

The code generator does not include a port of our SourceGenerator, as
that felt less nice. Instead, we write directly to a TextIO instance.
The generated output is almost byte-for-byte identical with our C++
generator, with trivial whitespace differences:

* The C++ generator included extraneous newlines before and after all
  switch statements, which is not kept here.
* The C++ generator did not include newlines between message handling
  functions, which we now do.
* The C++ generator had some extraneous spaces at the end of some lines,
  and incorrect tabbing (3 spaces) at the beginning of some lines.
2026-04-23 07:31:19 -04:00

49 lines
2.2 KiB
CMake

#
# 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}" "${LADYBIRD_SOURCE_DIR}/Meta/Generators/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 "${LADYBIRD_SOURCE_DIR}/Meta/Generators/embed_as_string.py"
MAIN_DEPENDENCY "${source_file}"
)
add_custom_target("generate_${name}" DEPENDS "${output}")
add_dependencies(ladybird_codegen_accumulator "generate_${name}")
endfunction()
function(compile_ipc source output)
if (NOT IS_ABSOLUTE ${source})
set(source ${CMAKE_CURRENT_SOURCE_DIR}/${source})
endif()
find_package(Python3 REQUIRED COMPONENTS Interpreter)
add_custom_command(
OUTPUT ${output}
COMMAND "${Python3_EXECUTABLE}" "${LADYBIRD_SOURCE_DIR}/Meta/Generators/generate_ipc_definitions.py" --input ${source} --output ${output}.tmp
COMMAND "${CMAKE_COMMAND}" -E copy_if_different ${output}.tmp ${output}
COMMAND "${CMAKE_COMMAND}" -E remove ${output}.tmp
VERBATIM
DEPENDS "${LADYBIRD_SOURCE_DIR}/Meta/Generators/generate_ipc_definitions.py"
MAIN_DEPENDENCY ${source}
)
get_filename_component(output_name ${output} NAME)
add_custom_target(generate_${output_name} DEPENDS ${output})
add_dependencies(ladybird_codegen_accumulator generate_${output_name})
cmake_path(RELATIVE_PATH CMAKE_CURRENT_SOURCE_DIR BASE_DIRECTORY ${LADYBIRD_SOURCE_DIR} OUTPUT_VARIABLE current_source_dir_relative)
if (ENABLE_INSTALL_HEADERS)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${output} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${current_source_dir_relative}" OPTIONAL)
endif()
endfunction()