Skip to content

Commit

Permalink
feat chaotic: allow searching clang-format in non-PATH
Browse files Browse the repository at this point in the history
commit_hash:dd686cda3d75e25e4608d5fca40672731553bc32
  • Loading branch information
segoon committed Dec 19, 2024
1 parent f4b9296 commit fc15442
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/alpine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
pwd
mkdir build_debug
cd build_debug
CMAKE_PROGRAM_PATH=/usr/lib/llvm17/bin/ \
cmake \
-DCMAKE_BUILD_TYPE=Debug \
-DUSERVER_NO_WERROR=OFF \
Expand Down
14 changes: 4 additions & 10 deletions chaotic/chaotic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ def parse_args() -> argparse.Namespace:
help='in-file path (e.g. /schemas/Type) to C++ type mapping',
)

parser.add_argument(
'-f',
'--file-map',
type=NameMapItem,
required=True,
action='append',
help='full filepath to virtual filepath mapping',
)

parser.add_argument(
'-u',
'--userver',
Expand Down Expand Up @@ -246,7 +237,10 @@ def main() -> None:
args = parse_args()

schemas = read_schemas(
args.erase_path_prefix, args.file, args.name_map, args.file_map,
args.erase_path_prefix,
args.file,
args.name_map,
[NameMapItem('(.*)={0}')],
)
cpp_name_func = generate_cpp_name_func(
args.name_map, args.erase_path_prefix,
Expand Down
2 changes: 1 addition & 1 deletion chaotic/golden_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ file(GLOB_RECURSE SCHEMAS "${CMAKE_CURRENT_SOURCE_DIR}/schemas/*.yaml")
userver_target_generate_chaotic(${PROJECT_NAME}-chgen
ARGS
-n "/components/schemas/([^/]*)/=ns::{0}"
-f "(.*)={0}"
-I "${CMAKE_CURRENT_SOURCE_DIR}/include"
-I "${CMAKE_CURRENT_SOURCE_DIR}/../include"
--parse-extra-formats
--generate-serializers
FORMAT ON
OUTPUT_DIR
"${CMAKE_CURRENT_BINARY_DIR}/src"
SCHEMAS
Expand Down
2 changes: 0 additions & 2 deletions chaotic/integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ file(GLOB_RECURSE SCHEMAS ${CMAKE_CURRENT_SOURCE_DIR}/schemas/*.yaml)
userver_target_generate_chaotic(${PROJECT_NAME}-chgen
ARGS
-n "/definitions/([^/]*)/=ns::{0}"
-f "(.*)={0}"
-I ${CMAKE_CURRENT_SOURCE_DIR}/include
-I ${CMAKE_CURRENT_SOURCE_DIR}/../include
--clang-format=
--parse-extra-formats
--generate-serializers
OUTPUT_DIR
Expand Down
30 changes: 27 additions & 3 deletions cmake/ChaoticGen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ function(_userver_prepare_chaotic)
message(STATUS "Found chaotic-openapi-gen: ${CHAOTIC_OPENAPI_BIN}")
set_property(GLOBAL PROPERTY userver_chaotic_openapi_bin "${CHAOTIC_OPENAPI_BIN}")

find_program(CLANG_FORMAT_BIN clang-format)
message(STATUS "Found clang-format: ${CLANG_FORMAT_BIN}")
set_property(GLOBAL PROPERTY userver_clang_format_bin "${CLANG_FORMAT_BIN}")
option(USERVER_CHAOTIC_FORMAT ON "Whether to format generated code")

if(NOT USERVER_CHAOTIC_SCRIPTS_PATH)
get_filename_component(USERVER_DIR "${CMAKE_CURRENT_LIST_DIR}" DIRECTORY)
set(USERVER_CHAOTIC_SCRIPTS_PATH "${USERVER_DIR}/scripts/chaotic")
Expand All @@ -42,18 +47,27 @@ endfunction()

_userver_prepare_chaotic()

# Generates ${TARGET} cmake target for C++ types, parsers, serializers
# from JSONSchema file(s).
#
# Options:
# - OUTPUT_DIR - where to put generated .cpp/.hpp/.ipp files, usually ${CMAKE_CURRENT_BINARY_DIR}/smth
# - RELATIVE_TO - --relative-to option to chaotic-gen
# - FORMAT - can be ON/OFF, enable to format generated files, defaults to USERVER_CHAOTIC_FORMAT
# - SCHEMAS - JSONSchema source files
# - ARGS - extra args to chaotic-gen
function(userver_target_generate_chaotic TARGET)
set(OPTIONS)
set(ONE_VALUE_ARGS OUTPUT_DIR RELATIVE_TO)
set(ONE_VALUE_ARGS OUTPUT_DIR RELATIVE_TO FORMAT)
set(MULTI_VALUE_ARGS SCHEMAS ARGS)
cmake_parse_arguments(
PARSE "${OPTIONS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN}
)

get_property(CHAOTIC_BIN GLOBAL PROPERTY userver_chaotic_bin)
get_property(CHAOTIC_EXTRA_ARGS GLOBAL PROPERTY userver_chaotic_extra_args)
get_property(USERVER_CHAOTIC_PYTHON_BINARY
GLOBAL PROPERTY userver_chaotic_python_binary)
get_property(USERVER_CHAOTIC_PYTHON_BINARY GLOBAL PROPERTY userver_chaotic_python_binary)
get_property(CLANG_FORMAT_BIN GLOBAL PROPERTY userver_clang_format_bin)

if (NOT DEFINED PARSE_OUTPUT_DIR)
set(PARSE_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
Expand All @@ -68,6 +82,15 @@ function(userver_target_generate_chaotic TARGET)
message(FATAL_ERROR "RELATIVE_TO is required")
endif()

if (PARSE_FORMAT OR (NOT DEFINED PARSE_FORMAT AND USERVER_CHAOTIC_FORMAT))
if (NOT CLANG_FORMAT_BIN)
message(FATAL_ERROR "clang-format is not found and FORMAT option is set. Please install clang-format.")
endif()
set(CLANG_FORMAT "${CLANG_FORMAT_BIN}")
else()
set(CLANG_FORMAT "")
endif()

set(SCHEMAS)
foreach(PARSE_SCHEMA ${PARSE_SCHEMAS})
file(RELATIVE_PATH SCHEMA "${PARSE_RELATIVE_TO}" "${PARSE_SCHEMA}")
Expand All @@ -89,6 +112,7 @@ function(userver_target_generate_chaotic TARGET)
${PARSE_ARGS}
-o "${PARSE_OUTPUT_DIR}"
--relative-to "${PARSE_RELATIVE_TO}"
--clang-format "${CLANG_FORMAT}"
${PARSE_SCHEMAS}
DEPENDS
${PARSE_SCHEMAS}
Expand Down
3 changes: 0 additions & 3 deletions libraries/easy/samples/3_json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ userver_target_generate_chaotic(${PROJECT_NAME}-chgen
ARGS
# Map '/components/schemas/*' JSONSchema types to C++ types in 'schemas' namespace
-n "/components/schemas/([^/]*)/=schemas::{0}"
-f "(.*)={0}"
# Don't call clang-format
--clang-format=
# Generate serializers for responses
--generate-serializers
OUTPUT_DIR
Expand Down
3 changes: 0 additions & 3 deletions samples/chaotic_service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ userver_target_generate_chaotic(${PROJECT_NAME}-chgen
ARGS
# Map '/components/schemas/*' JSONSchema types to C++ types in 'samples::hello' namespace
-n "/components/schemas/([^/]*)/=samples::hello::{0}"
-f "(.*)={0}"
# Don't call clang-format
--clang-format=
# Generate serializers for responses
--generate-serializers
OUTPUT_DIR
Expand Down
1 change: 1 addition & 0 deletions scripts/docs/en/userver/build/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ cmake ... -DCMAKE_C_COMPILER=clang-12 -DCMAKE_CXX_COMPILER=clang++-12
| `USERVER_INSTALL` | Build userver for further installation | `OFF` |
| `USERVER_CONAN` | Build userver using Conan packages | `ON` if build is launched from Conan, `OFF` otherwise |
| `USERVER_GENERATE_PROTOS_AT_CONFIGURE` | Run protoc at CMake Configure time for better IDE integration | `OFF` for downloaded Protobuf, `ON` otherwise |
| `USERVER_CHAOTIC_FORMAT` | Whether to format generated code if FORMAT option is missing | `ON` |

@warning Using LTO can lead to [some problems](https://github.com/userver-framework/userver/issues/242). We don't recommend using `USERVER_LTO`.

Expand Down
2 changes: 0 additions & 2 deletions scripts/docs/en/userver/chaotic.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ Some frequently used parameters are described below.
* `-n` defines types mapping from in-yaml object path to C++ type name (with namespace).
The path regex is written first, then equal sign `=`, then C++ type name.
`-n` can be passed multiple times.
* `-f` defines file mapping from yaml filenames to C++ filenames (excluding the extensions).
Usually as-is mapping is used.
* `--parse-extra-formats` generates YAML and YAML config parsers besides JSON parser.
* `--generate-serializers` generates serializers into JSON besides JSON parser from `formats::json::Value`.

Expand Down

0 comments on commit fc15442

Please sign in to comment.