Skip to content

Commit

Permalink
#338 arg_router_translation_generator isn't detecting changes in the …
Browse files Browse the repository at this point in the history
…TOML files

Fix #338
  • Loading branch information
cmannett85 committed Jun 28, 2023
1 parent e63c318 commit 4e925b8
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 93 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/merge_checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ jobs:
Test-Path -Path "${{ env.INSTALL_DIR }}/share/arg_router/arg_router.cmake" -PathType Leaf
Test-Path -Path "${{ env.INSTALL_DIR }}/share/arg_router/arg_router-config.cmake" -PathType Leaf
Test-Path -Path "${{ env.INSTALL_DIR }}/share/arg_router/arg_router-config-version.cmake" -PathType Leaf
Test-Path -Path "${{ env.INSTALL_DIR }}/share/arg_router/translation_generator.cmake" -PathType Leaf
Test-Path -Path "${{ env.INSTALL_DIR }}/share/arg_router/translation_generator/translation_generator.cmake" -PathType Leaf
Test-Path -Path "${{ env.INSTALL_DIR }}/share/arg_router/translation_generator/translation_generator_script.cmake" -PathType Leaf
- name: Test CMake package integrity
run: |
Expand Down Expand Up @@ -240,7 +241,8 @@ jobs:
[[ -f "./share/arg_router/arg_router.cmake" ]]
[[ -f "./share/arg_router/arg_router-config.cmake" ]]
[[ -f "./share/arg_router/arg_router-config-version.cmake" ]]
[[ -f "./share/arg_router/translation_generator.cmake" ]]
[[ -f "./share/arg_router/translation_generator/translation_generator.cmake" ]]
[[ -f "./share/arg_router/translation_generator/translation_generator_script.cmake" ]]
- name: Test CMake package integrity
run: |
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/pr_checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ jobs:
[[ -f "./share/arg_router/arg_router.cmake" ]]
[[ -f "./share/arg_router/arg_router-config.cmake" ]]
[[ -f "./share/arg_router/arg_router-config-version.cmake" ]]
[[ -f "./share/arg_router/translation_generator.cmake" ]]
[[ -f "./share/arg_router/translation_generator/translation_generator.cmake" ]]
[[ -f "./share/arg_router/translation_generator/translation_generator_script.cmake" ]]
- name: Test CMake package integrity
run: |
Expand Down Expand Up @@ -234,7 +235,8 @@ jobs:
[[ -f "/usr/share/arg_router/arg_router.cmake" ]]
[[ -f "/usr/share/arg_router/arg_router-config.cmake" ]]
[[ -f "/usr/share/arg_router/arg_router-config-version.cmake" ]]
[[ -f "/usr/share/arg_router/translation_generator.cmake" ]]
[[ -f "/usr/share/arg_router/translation_generator/translation_generator.cmake" ]]
[[ -f "/usr/share/arg_router/translation_generator/translation_generator_script.cmake" ]]
- name: Test CMake package integrity
timeout-minutes: 30
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -966,9 +966,10 @@ arg_router_translation_generator(
"${CMAKE_SOURCE_DIR}/fr.toml"
"${CMAKE_SOURCE_DIR}/ja.toml"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/translations/"
GENERATED_FILES_VAR translation_files
TARGET my_exe_translations
)
add_executable(my_exe ... ${translation_files})
add_executable(my_exe ...)
add_dependencies(my_exe my_exe_translations)
target_include_directories(my_exe PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
```
Creates a header for each language with the corresponding translation specialisation that can be included in your root source file. You can see this in the [buildable example](https://cmannett85.github.io/arg_router/c_09_0920_2simple_ml_gen_2main_8cpp-example.html)). This is now the recommended approach for writing translations, but is certainly not (and never will be) a requirement.
Expand Down
21 changes: 10 additions & 11 deletions cmake/build_types/unit_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,17 @@ create_clangformat_target(
SOURCES ${TEST_HEADERS} ${TEST_SRCS}
)

# Translation generator unit test input files
include("${CMAKE_SOURCE_DIR}/cmake/translation_generator/translation_generator.cmake")
arg_router_translation_generator(
SOURCES "${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/en_GB.toml"
"${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/ja.toml"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/translations/"
TARGET translation_arg_router_test
)

add_executable(arg_router_test ${TEST_HEADERS} ${TEST_SRCS})
add_dependencies(arg_router_test clangformat_test arg_router)
add_dependencies(arg_router_test translation_arg_router_test clangformat_test arg_router)

set_target_properties(arg_router_test PROPERTIES CXX_EXTENSIONS OFF)
target_compile_definitions(arg_router_test PRIVATE UNIT_TEST_BUILD)
Expand Down Expand Up @@ -152,14 +161,4 @@ target_compile_definitions(arg_router_test PRIVATE
UNIT_TEST_BIN_DIR="${CMAKE_CURRENT_BINARY_DIR}"
)

# Translation generator unit test input files
include("${CMAKE_SOURCE_DIR}/cmake/translation_generator.cmake")
arg_router_translation_generator(
SOURCES "${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/en_GB.toml"
"${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/ja.toml"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/translations/"
GENERATED_FILES_VAR translation_files
)
target_sources(arg_router_test PRIVATE ${translation_files})

add_test(NAME arg_router_test COMMAND arg_router_test -l message)
24 changes: 13 additions & 11 deletions cmake/build_types/unit_test_coverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
### Distributed under the Boost Software License, Version 1.0.
### (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)

# Translation generator unit test input files
include("${CMAKE_SOURCE_DIR}/cmake/translation_generator/translation_generator.cmake")
arg_router_translation_generator(
SOURCES "${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/en_GB.toml"
"${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/ja.toml"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/translations/"
TARGET translation_arg_router_test_coverage
)

add_executable(arg_router_test_coverage EXCLUDE_FROM_ALL ${TEST_HEADERS} ${TEST_SRCS})
add_dependencies(arg_router_test_coverage clangformat_test arg_router)
add_dependencies(
arg_router_test_coverage
translation_arg_router_test_coverage
clangformat_test arg_router)

set_target_properties(arg_router_test_coverage PROPERTIES CXX_EXTENSIONS OFF)
target_compile_definitions(arg_router_test_coverage PRIVATE
Expand Down Expand Up @@ -34,13 +46,3 @@ target_link_libraries(arg_router_test_coverage
target_link_options(arg_router_test_coverage
PRIVATE --coverage
)

# Translation generator unit test input files
include("${CMAKE_SOURCE_DIR}/cmake/translation_generator.cmake")
arg_router_translation_generator(
SOURCES "${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/en_GB.toml"
"${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/ja.toml"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/translations/"
GENERATED_FILES_VAR translation_files
)
target_sources(arg_router_test_coverage PRIVATE ${translation_files})
6 changes: 4 additions & 2 deletions cmake/package/install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ set(INSTALL_BASE_DIR "include")
set(INSTALL_AR_DIR "${INSTALL_BASE_DIR}/arg_router")
set(CONFIG_FILE "${CMAKE_BINARY_DIR}/cmake/package/arg_router-config.cmake")
set(CONFIG_VERSION_FILE "${CMAKE_BINARY_DIR}/cmake/package/arg_router-config-version.cmake")
set(TRANSLATION_GENERATOR "${CMAKE_SOURCE_DIR}/cmake/translation_generator.cmake")
set(TRANSLATION_GENERATOR_FILES
"${CMAKE_SOURCE_DIR}/cmake/translation_generator/translation_generator.cmake"
"${CMAKE_SOURCE_DIR}/cmake/translation_generator/translation_generator_script.cmake")
set(AR_CMAKE_PACKAGE_DIR "share/arg_router"
CACHE STRING "Installation suffix of CMake package config files, defaults to share/arg_router")

Expand All @@ -34,7 +36,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/README.md"
DESTINATION ${INSTALL_AR_DIR})
install(FILES "${CONFIG_FILE}"
"${CONFIG_VERSION_FILE}"
"${TRANSLATION_GENERATOR}"
"${TRANSLATION_GENERATOR_FILES}"
DESTINATION ${AR_CMAKE_PACKAGE_DIR})

# CPack package configuration
Expand Down
37 changes: 37 additions & 0 deletions cmake/translation_generator/translation_generator.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### Copyright (C) 2023 by Camden Mannett.
### Distributed under the Boost Software License, Version 1.0.
### (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)

function(arg_router_translation_generator)
set(single_value_args OUTPUT_DIR TARGET)
set(multi_value_args SOURCES)
cmake_parse_arguments(ARGS "" "${single_value_args}" "${multi_value_args}" ${ARGN})

if (NOT DEFINED ARGS_SOURCES)
message(FATAL_ERROR "Translation generator requires at least one source file")
endif()
if (NOT DEFINED ARGS_OUTPUT_DIR)
message(FATAL_ERROR "Translation generator output directory must be set, typically CMAKE_CURRENT_BINARY_DIR")
endif()
if (NOT DEFINED ARGS_TARGET)
message(FATAL_ERROR "Translation generator requires a target output variable")
endif()

add_custom_target(${ARGS_TARGET}
COMMAND ${CMAKE_COMMAND}
"-DSOURCES=${ARGS_SOURCES}"
-DOUTPUT_DIR=${ARGS_OUTPUT_DIR}
-P ${CMAKE_SOURCE_DIR}/cmake/translation_generator/translation_generator_script.cmake
SOURCES ${ARGS_SOURCES}
"${CMAKE_SOURCE_DIR}/cmake/translation_generator/translation_generator_script.cmake"
VERBATIM)

# set_source_files_properties cannot be used in a script so that's set here
foreach(source ${ARGS_SOURCES})
get_filename_component(language_id "${source}" NAME_WLE)
set(output_file "${ARGS_OUTPUT_DIR}/${language_id}.hpp")

target_sources(${ARGS_TARGET} PRIVATE "${output_file}")
set_source_files_properties("${output_file}" PROPERTIES GENERATED TRUE)
endforeach()
endfunction()
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public:
set(output_data "${${ARGS_OUTPUT_VAR}}${header}")
set(error_code_section FALSE)

cmake_policy(PUSH)
cmake_policy(SET CMP0007 NEW)

# CMake ranges are inclusive for some reason, so we need to decrement the array length before
# using in the for-loop
file(STRINGS "${ARGS_SOURCE}" lines ENCODING UTF-8)
Expand Down Expand Up @@ -79,6 +82,8 @@ public:
endif()
endforeach()

cmake_policy(POP)

# Write the footer and finish
if (error_code_section)
string(APPEND output_data "${error_code_footer}")
Expand All @@ -88,61 +93,42 @@ public:
set(${ARGS_OUTPUT_VAR} "${output_data}" PARENT_SCOPE)
endfunction()

function(arg_router_translation_generator)
set(single_value_args OUTPUT_DIR GENERATED_FILES_VAR)
set(multi_value_args SOURCES)
cmake_parse_arguments(ARGS "" "${single_value_args}" "${multi_value_args}" ${ARGN})

if (NOT DEFINED ARGS_SOURCES)
message(FATAL_ERROR "Translation generator requires at least one source file")
endif()
if (NOT DEFINED ARGS_OUTPUT_DIR)
message(FATAL_ERROR "Translation generator output directory must be set, typically CMAKE_CURRENT_BINARY_DIR")
endif()
if (NOT DEFINED ARGS_GENERATED_FILES_VAR)
message(FATAL_ERROR "Translation generator requires a generated file output variable")
endif()

set(header "// Generated by CMake, do not modify manually
# Script entry point
set(header "// Generated by CMake, do not modify manually
namespace arg_router::multi_lang
{
#ifdef AR_ENABLE_CPP20_STRINGS
")
set(midder "#else\n")
set(footer "#endif\n} // namespace arg_router::multi_lang\n")

foreach(source ${ARGS_SOURCES})
get_filename_component(language_id "${source}" NAME_WLE)
set(output_file "${ARGS_OUTPUT_DIR}/${language_id}.hpp")
list(APPEND output_file_list "${output_file}")

set(output_data "${header}")

__ar_translation_body_generator(
LANGUAGE "${language_id}"
SOURCE "${source}"
OUTPUT_VAR output_data)
string(APPEND output_data "${midder}")

__ar_translation_body_generator(
LANGUAGE "${language_id}"
SOURCE "${source}"
OUTPUT_VAR output_data
CPP17)
string(APPEND output_data "${footer}")

# Only write out the data if it differs from the existing
if (EXISTS "${output_file}")
file(READ "${output_file}" existing_data)
if (NOT "${existing_data}" STREQUAL "${output_data}")
message(STATUS "Generated ${language_id} translation file")
file(WRITE "${output_file}" "${output_data}")
endif()
else()
set(midder "#else\n")
set(footer "#endif\n} // namespace arg_router::multi_lang\n")

foreach(source ${SOURCES})
get_filename_component(language_id "${source}" NAME_WLE)
set(output_file "${OUTPUT_DIR}/${language_id}.hpp")
set(output_data "${header}")

__ar_translation_body_generator(
LANGUAGE "${language_id}"
SOURCE "${source}"
OUTPUT_VAR output_data)
string(APPEND output_data "${midder}")

__ar_translation_body_generator(
LANGUAGE "${language_id}"
SOURCE "${source}"
OUTPUT_VAR output_data
CPP17)
string(APPEND output_data "${footer}")

# Only write out the data if it differs from the existing
if (EXISTS "${output_file}")
file(READ "${output_file}" existing_data)
if (NOT "${existing_data}" STREQUAL "${output_data}")
message(STATUS "Generated ${language_id} translation file")
file(WRITE "${output_file}" "${output_data}")
endif()
endforeach()

set(${ARGS_GENERATED_FILES_VAR} "${output_file_list}" PARENT_SCOPE)
endfunction()
else()
message(STATUS "Generated ${language_id} translation file")
file(WRITE "${output_file}" "${output_data}")
endif()
endforeach()
13 changes: 7 additions & 6 deletions examples/c++17/simple_ml_gen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
### Distributed under the Boost Software License, Version 1.0.
### (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)

include("${CMAKE_SOURCE_DIR}/cmake/translation_generator.cmake")

include("${CMAKE_SOURCE_DIR}/cmake/translation_generator/translation_generator.cmake")
arg_router_translation_generator(
SOURCES "${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/en_GB.toml"
"${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/fr.toml"
"${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/ja.toml"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/translations/"
GENERATED_FILES_VAR translation_files
TARGET translation_example_simple_ml_gen
)

create_clangformat_target(
Expand All @@ -18,9 +17,11 @@ create_clangformat_target(
)

add_executable(example_simple_ml_gen
"${CMAKE_CURRENT_SOURCE_DIR}/main.cpp"
${translation_files})
add_dependencies(example_simple_ml_gen clangformat_example_simple_ml)
"${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
add_dependencies(
example_simple_ml_gen
translation_example_simple_ml_gen
clangformat_example_simple_ml)

target_include_directories(example_simple_ml_gen PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")

Expand Down
13 changes: 7 additions & 6 deletions examples/c++20/simple_ml_gen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
### Distributed under the Boost Software License, Version 1.0.
### (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)

include("${CMAKE_SOURCE_DIR}/cmake/translation_generator.cmake")

include("${CMAKE_SOURCE_DIR}/cmake/translation_generator/translation_generator.cmake")
arg_router_translation_generator(
SOURCES "${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/en_GB.toml"
"${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/fr.toml"
"${CMAKE_SOURCE_DIR}/examples/resources/simple_ml_gen/ja.toml"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/translations/"
GENERATED_FILES_VAR translation_files
TARGET translation_example_simple_ml_gen_cpp20
)

create_clangformat_target(
Expand All @@ -18,9 +17,11 @@ create_clangformat_target(
)

add_executable(example_simple_ml_gen_cpp20
"${CMAKE_CURRENT_SOURCE_DIR}/main.cpp"
${translation_files})
add_dependencies(example_simple_ml_gen_cpp20 clangformat_example_simple_ml_gen_cpp20)
"${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
add_dependencies(
example_simple_ml_gen_cpp20
translation_example_simple_ml_gen_cpp20
clangformat_example_simple_ml_gen_cpp20)

target_include_directories(example_simple_ml_gen_cpp20 PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")

Expand Down

0 comments on commit 4e925b8

Please sign in to comment.