Verified Commit e0a315ac authored by Andrei Pavel's avatar Andrei Pavel 🐧

[#1453] add tools/add-config.h

parent 082ca2fb
#!/bin/sh
# Copyright (C) 2020 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This script replaces @prefix@, @localstatedir@ and other automake/autoconf
# variables with their actual content.
#
# Usage:
#
# ./tools/add-config-h [-n|--name-only]
#
# Run from the root of the repository to add `#include <config.h>` lines to
# non-generated .cc files.
red='\e[1m\e[31m'
reset='\e[1m\e[0m'
mandatory_commands() {
while test ${#} -gt 0; do
if ! command -v "${1}" > /dev/null 2>&1; then
printf "${red}%s${reset} is mandatory.\\n" "${1}" >&2
exit 3
fi
shift
done
}
print_usage() {
printf \
'Usage: %s {{options}}
Options:
[-d|--debug] enable debug mode, showing every executed
statement
[-h|--help] print usage (this text)
[-n|--name-only] only show file names of files that will be
modified
' \
"$(basename "${0}")"
}
# Parse parameters.
while test ${#} -gt 0; do
case "${1}" in
# [-d|--debug] enable debug mode, showing every executed
# statement
'-d'|'--debug') set -x ;;
# [-h|--help] print usage (this text).
'-h'|'--help') print_usage ;;
# [-n|--name-only] only show file names of files that will be
# modified
'-n'|'--name-only') name_only=true ;;
# Unrecognized argument
*)
printf "${red}ERROR: Unrecognized argument '%s'${reset}\\n" "${1}" 1>&2; print_usage; exit 1 ;;
esac; shift
done
# Default parameters.
test -z "${name_only+x}" && name_only=false
#------------------------------------------------------------------------------#
filtered_out=(
./src/bin/admin/messages.cc
./src/bin/agent/agent_lexer.cc
./src/bin/agent/agent_parser.cc
./src/bin/agent/ca_messages.cc
./src/lib/cfgrpt/config_report.cc
./src/bin/d2/d2_lexer.cc
./src/bin/d2/d2_messages.cc
./src/bin/d2/d2_parser.cc
./src/bin/dhcp4/dhcp4_lexer.cc
./src/bin/dhcp4/dhcp4_messages.cc
./src/bin/dhcp4/dhcp4_parser.cc
./src/bin/dhcp6/dhcp6_lexer.cc
./src/bin/dhcp6/dhcp6_messages.cc
./src/bin/dhcp6/dhcp6_parser.cc
./src/bin/kea_config_tool/messages.cc
./src/bin/lfc/lfc_messages.cc
./src/bin/netconf/netconf_lexer.cc
./src/bin/netconf/netconf_messages.cc
./src/bin/netconf/netconf_parser.cc
./src/hooks/dhcp/bootp/bootp_messages.cc
./src/hooks/dhcp/flex_option/flex_option_messages.cc
./src/hooks/dhcp/high_availability/ha_messages.cc
./src/hooks/dhcp/lease_cmds/lease_cmds_messages.cc
./src/hooks/dhcp/mysql_cb/mysql_cb_messages.cc
./src/hooks/dhcp/stat_cmds/stat_cmds_messages.cc
./src/hooks/dhcp/user_chk/user_chk_messages.cc
./src/lib/asiodns/asiodns_messages.cc
./src/lib/config/config_messages.cc
./src/lib/database/db_messages.cc
./src/lib/database/server_selector.cc
./src/lib/dhcp_ddns/dhcp_ddns_messages.cc
./src/lib/dhcpsrv/alloc_engine_messages.cc
./src/lib/dhcpsrv/dhcpsrv_messages.cc
./src/lib/dhcpsrv/fuzz_messages.cc
./src/lib/dhcpsrv/hosts_messages.cc
./src/lib/eval/eval_messages.cc
./src/lib/eval/lexer.cc
./src/lib/eval/parser.cc
./src/lib/hooks/hooks_messages.cc
./src/lib/http/http_messages.cc
./src/lib/log/tests/log_test_messages.cc
./src/lib/process/process_messages.cc
)
get_source_files() {
mandatory_commands find grep cut sort uniq
source_files=$(find . -type f | grep -Fv '.git' | grep -E '\.cc$' | xargs grep -EL '#include\s*[\"|\<]config.h[\"|\>]' | cut -d ':' -f 1 | sort -uV | uniq)
for file in "${filtered_out[@]}"; do
source_files=$(echo "${source_files}" | sed "s#${file}##g" | sed '/^$/d')
done
printf '%s' "${source_files}"
}
mandatory_commands sed
if "${name_only}"; then
get_source_files
if test -n "${source_files}"; then
printf '\n'
fi
else
# Run once for files that have at least one include.
for i in $(get_source_files); do
sed -i '0,/#include/s//#include <config.h>\n\n#include/' "${i}"
done
# Run again for files that don't have any includes. The second command would
# work for the first case, but the first command is more elegant.
for i in $(get_source_files); do
sed -i '0,/\(\/\/ file, You can obtain one.*\)/s//\1\n\n#include <config.h>/' "${i}"
done
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment