From 160b0a033b42af7ebd6dafdab277318ef6f36e84 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 15 Dec 2015 14:36:12 +0000 Subject: [PATCH] Use perl instead of bloody cmake for string manipulation --- CMakeLists.txt | 4 +--- src/rspamadm/CMakeLists.txt | 8 ++++--- src/rspamadm/lua_preprocess.cmake | 22 ------------------- src/rspamadm/lua_preprocess.pl | 35 +++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 28 deletions(-) delete mode 100644 src/rspamadm/lua_preprocess.cmake create mode 100644 src/rspamadm/lua_preprocess.pl diff --git a/CMakeLists.txt b/CMakeLists.txt index 67ff501cd..add82c152 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -157,9 +157,7 @@ INCLUDE(CheckCCompilerFlag) INCLUDE(CMakeParseArguments) FIND_PACKAGE(PkgConfig REQUIRED) -IF (ENABLE_SNOWBALL MATCHES "ON") - FIND_PACKAGE(Perl REQUIRED) -ENDIF() +FIND_PACKAGE(Perl REQUIRED) ############################# MACRO SECTION ############################################# diff --git a/src/rspamadm/CMakeLists.txt b/src/rspamadm/CMakeLists.txt index 80a253c20..c69bf0cbe 100644 --- a/src/rspamadm/CMakeLists.txt +++ b/src/rspamadm/CMakeLists.txt @@ -17,9 +17,11 @@ SET(RSPAMADMSRC rspamadm.c SET(RSPAMADMLUASRC ${CMAKE_CURRENT_SOURCE_DIR}/fuzzy_stat.lua) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) ADD_CUSTOM_TARGET(rspamadm_lua_preprocess - ${CMAKE_COMMAND} -DRSPAMADMLUASRC=${RSPAMADMLUASRC} - -P ${CMAKE_CURRENT_SOURCE_DIR}/lua_preprocess.cmake - SOURCES ${RSPAMADMLUASRC}) + ${PERL_EXECUTABLE} + "${CMAKE_CURRENT_SOURCE_DIR}/lua_preprocess.pl" + "${CMAKE_CURRENT_SOURCE_DIR}" + "${CMAKE_CURRENT_BINARY_DIR}" + SOURCES ${RSPAMADMLUASRC} ${CMAKE_CURRENT_SOURCE_DIR}/lua_preprocess.pl) IF (ENABLE_HYPERSCAN MATCHES "ON") LIST(APPEND RSPAMADMSRC "${CMAKE_SOURCE_DIR}/src/hs_helper.c") ENDIF() diff --git a/src/rspamadm/lua_preprocess.cmake b/src/rspamadm/lua_preprocess.cmake deleted file mode 100644 index 13a23487a..000000000 --- a/src/rspamadm/lua_preprocess.cmake +++ /dev/null @@ -1,22 +0,0 @@ -FOREACH(_LUA_SRC ${RSPAMADMLUASRC}) - GET_FILENAME_COMPONENT(_LUA_BASE ${_LUA_SRC} NAME_WE) - FILE(READ ${_LUA_SRC} _FILE_DATA) - STRING(REPLACE \" \\\" _OUT1 ${_FILE_DATA}) - # Convert into cmake list - STRING(REGEX REPLACE ";" "\\\\;" _OUT2 "${_OUT1}") - STRING(REPLACE "\n" ";" _OUT3 "${_OUT2}") - FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_LUA_BASE}.lua.h" " -#ifndef ${_LUA_BASE}_H -#define ${_LUA_BASE}_H - -static const char rspamadm_script_${_LUA_BASE}[] = \"\" -") - FOREACH(_LINE ${_OUT3}) - STRING(REGEX REPLACE "^(.+)$" "\"\\1\\\\n\"\n" _OUT4 "${_LINE}") - FILE(APPEND "${CMAKE_CURRENT_BINARY_DIR}/${_LUA_BASE}.lua.h" ${_OUT4}) - ENDFOREACH() - - FILE(APPEND "${CMAKE_CURRENT_BINARY_DIR}/${_LUA_BASE}.lua.h" "\"\"; -#endif -") -ENDFOREACH() diff --git a/src/rspamadm/lua_preprocess.pl b/src/rspamadm/lua_preprocess.pl new file mode 100644 index 000000000..6384df28c --- /dev/null +++ b/src/rspamadm/lua_preprocess.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use warnings FATAL => 'all'; +use strict; + +my ($in_dir, $out_dir) = @ARGV; +my @files = <$in_dir/*.lua>; + +foreach my $file (@files) { + $file =~ /([^\/.]+)(.lua)$/; + my $fname = "$1$2"; + my $varname = "rspamadm_script_$1"; + my $definename = uc $varname; + + open(IN, "< $file") or die "input missing"; + open(OUT, "> $out_dir/$fname.h") or die "output missing"; + + print OUT <) { + $_ =~ s/^(.*)$/"$1\\n"/; + print OUT $_; + } + print OUT <