diff --git a/src/dev-utils/CMakeLists.txt b/src/dev-utils/CMakeLists.txt index e1d8243b03..28c261f8a6 100644 --- a/src/dev-utils/CMakeLists.txt +++ b/src/dev-utils/CMakeLists.txt @@ -31,16 +31,11 @@ function(encoding_check TARGET) # Define top-level encoding check target for this ${TARGET} add_custom_target(encoding-check-${TARGET} DEPENDS encoding-check ${T_SOURCES} + COMMAND $ ${TARGET} ${T_SOURCES} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMENT "Checking source files encodings for target ${TARGET}" ) - # Add checking of each source file as a subcommand of encoding-check-${TARGET} - foreach(file ${T_SOURCES}) - add_custom_command(TARGET encoding-check-${TARGET} PRE_BUILD - COMMAND $ ${TARGET} ${file} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - ) - endforeach() # This adds dependency on encoding-check-${TARGET} to ${TARET} # via the global-encoding-check diff --git a/src/dev-utils/encoding-check.cpp b/src/dev-utils/encoding-check.cpp index 89f225572b..f4615a7b15 100644 --- a/src/dev-utils/encoding-check.cpp +++ b/src/dev-utils/encoding-check.cpp @@ -69,28 +69,24 @@ unsigned char *utf8_check(unsigned char *s) } -int main(int argc, char const *argv[]) +static const char* target; + +void error_exit(const char* error, const char* filename) { - if (argc != 3) { - std::cerr << "Usage: " << argv[0] << " " << std::endl; - return -1; - } + std::cerr << "\n\tError: " << error << ": " << filename << "\n" + << "\tTarget: " << target << "\n" + << std::endl; + std::exit(-2); +} - const char* target = argv[1]; - const char* filename = argv[2]; - - const auto error_exit = [=](const char* error) { - std::cerr << "\n\tError: " << error << ": " << filename << "\n" - << "\tTarget: " << target << "\n" - << std::endl; - std::exit(-2); - }; +void utf8_check_file(const char* filename) +{ std::ifstream file(filename, std::ios::binary | std::ios::ate); const auto size = file.tellg(); if (size == 0) { - return 0; + return; } file.seekg(0, std::ios::beg); @@ -101,7 +97,7 @@ int main(int argc, char const *argv[]) // Check UTF-8 validity if (utf8_check(reinterpret_cast(buffer.data())) != nullptr) { - error_exit("Source file does not contain (valid) UTF-8"); + error_exit("Source file does not contain (valid) UTF-8", filename); } // Check against a BOM mark @@ -109,10 +105,25 @@ int main(int argc, char const *argv[]) && buffer[0] == '\xef' && buffer[1] == '\xbb' && buffer[2] == '\xbf') { - error_exit("Source file is valid UTF-8 but contains a BOM mark"); + error_exit("Source file is valid UTF-8 but contains a BOM mark", filename); } } else { - error_exit("Could not read source file"); + error_exit("Could not read source file", filename); + } +} + + +int main(int argc, char const *argv[]) +{ + if (argc < 3) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return -1; + } + + target = argv[1]; + + for (int i = 2; i < argc; i++) { + utf8_check_file(argv[i]); } return 0;