Skip to content

Module Headers AutoGenerated Code (Bash – C++)

Versión en español de esta publicación.
Some time ago when I started designing Phoxonics I had some problems related to generating module includes. Phoxonics has multiple modules that have dependencies between them, and when adding a new header class it was necessary to add an entry for this header class in the global module header file in order to know what classes were part of this module.

So I decided to make a small script to automate this simple task that was becoming tedious to do just because of the fact that one needed to remember to add an entry to the global header file each time a new header class was added. With this script it is not needed anymore to remember to add any entry to the global module file cause in the build target we added a reference to the new bash script that generates automatically all the header reference that are contained in the actual module.

Here is the script code for the automatic generation of C++ header code. I hope you find it useful.

module_name="${1}"
module_name_upper=${module_name^^}  # Module name in uppercase
 
# Run as user, -e allows to interpret new line \n
echo -e "\n \
********************************************************** \n \
** Automatic header generation for module ${module_name} \n \
**********************************************************"
 
inc_dir="${2}"
out_file="${3}"
 
# "ls -l $inc_dir"        = get a directory listing
# --time-style="long-iso" = makes sure that the same format for the date-time string is the same in all environments.
# "| egrep '^d'"          = pipe to egrep and select only the directories
# "awk '{print $8}'"      = pipe the result from egrep to awk and print only the 8th field
inc_dirs=`ls -l --time-style="long-iso" $inc_dir | egrep '^d' | awk '{print $8}'`
 
cat <<EOF >"${out_file}"
/*
 * ${module_name}.hpp
 *
 * Auto-Generated module header file, do not modify directly
 * to prevent changes loss, to modify please change GenHeaders.sh
 * 
 *  Created on: 2014
 *      Author: nano
 */
 
#ifndef ${module_name_upper}_HPP_
#define ${module_name_upper}_HPP_
 
// Include all headers that are part of the ${module_name} module
EOF
 
# Loop through the directories
for dir in ${inc_dirs}; do
    echo -e "\n${dir}";  
    curr_dir=${inc_dir}/${dir}
    file_cnt=$( find ${curr_dir} -name "*.h*" | wc -l )
    curr_files=$( find ${curr_dir} -name "*.h*" )
    if [ ${file_cnt} -gt 0 ]; then  # check  if we have headers in dir
        for file in ${curr_files}; do # Loop through files and add includes
            echo "#include \"${file}\"" >> ${out_file};
            echo "${file}";
        done       
    fi
done
echo -e "\n";
 
# Add closing ifdef    
echo "" >> ${out_file}
echo "#endif /* ${module_name_upper}_HPP_ */" >> ${out_file}

And here are some autogenerated entries in the global header module file.

/*
 * common.hpp
 *
 * Auto-Generated module header file, do not modify directly
 * to prevent changes loss, to modify please change GenHeaders.sh
 * 
 *  Created on: 2014
 *      Author: nano
 */

#ifndef COMMON_HPP_
#define COMMON_HPP_

// Include all headers that are part of the common module
#include "./include/common/CommonBase.hpp"
#include "./include/data/Config.hpp"
#include "./include/data/Programs.hpp"
#include "./include/data/VersionInfo.hpp"
#include "./include/exception/CustomException.hpp"
#include "./include/hdf5/Hdf5Grid2D.hpp"
#include "./include/hdf5/Hdf5Grid2DSlice.hpp"
#include "./include/hdf5/Hdf5Grid1DSlice.hpp"
#include "./include/hdf5/Hdf5Grid1D.hpp"
#include "./include/hdf5/Hdf5Base.hpp"
#include "./include/io/Folder.hpp"
#include "./include/io/File.hpp"
#include "./include/utils/DateTime.hpp"
#include "./include/utils/Logger.hpp"
#include "./include/utils/Formatter.hpp"
#include "./include/utils/ArrSizeHelper.hpp"
#include "./include/utils/Matrix.hpp"
#include "./include/utils/Utilities.hpp"
#include "./include/utils/Strings.hpp"
#include "./include/utils/Command.hpp"
#include "./include/utils/SingleLoggerConfig.hpp"

#endif /* COMMON_HPP_ */

Enjoy!
-Yohan

Leave a Reply

Your email address will not be published. Required fields are marked *