<commit_before>#!/bin/bash

function discover_depends {
    file=$1
    includes=$(cat $file | perl -ne '/#include\s*<(.*)>/ && print "$1\n"')
    
    for i in $includes ; do
        if [[ -f include/$i ]] ; then
            echo -n "include/$i "
        fi
    done
}

source_files=$(find * -name '*cpp' -and -not -name 'main.cpp' -and -not -name 'test_*')
mains=$(find * -name 'main_*.cpp')

cat <<- EOF
# This file was autogenerated using ./genmake.sh
# You should not have to edit this file by hand!
# If you do have to edit this file by hand please
# contact the sourceror
#
# To modify the different attributes of everything,
# check out the targets directory as it contains all
# the configuration for compilation.

TGT?=x86
MODE?=devel

.INCLUDE_DIRS += targets/\$(TGT)/
include targets/\$(TGT)/\$(MODE).mk 

default:
EOF
echo '	mkdir -p _\$(TGT)_obs/ && make all'
echo ''

objects=""

for file in $source_files ; do
    obj_file=${file/cpp/o}
    obj_file=_'$(TGT)'_obs/${obj_file//\//_}

    echo "$obj_file: $file $(discover_depends $file)"
    echo '	$(CXX) -o '$obj_file' $(CXXFLAGS) -c '$file
    echo ''

    objects=$objects' '$obj_file
done

echo 'clean:'
echo '	rm -rf _$(TGT)_obs/'
echo '	rm mercury'
echo ''
echo '_$(TGT)_obs/libmerc.a: '$objects
echo '	ar -r $@ $^'
echo ''
echo 'all: _$(TGT)_obs/libmerc.a'
echo '	$(CXX) -o _$(TGT)_obs/mercury main.cpp $(CXXFLAGS) -L _$(TGT)_obs/ -lmerc'
echo '	ln -sf _$(TGT)_obs/mercury .'
echo ''
echo 'genmake: genmake.sh'
echo '	./genmake.sh > Makefile'
echo ''
echo 'cleanall:'
echo '	rm -rf _*_obs'
echo '	rm mercury'
<commit_msg>modified genmake script to do full dependency resolution<commit_after>#!/bin/bash

# This script will recursively locate c++ files and try to extract
# their dependencies to create a robust and automatically generated
# Makefile that has extensibility features built in.
#
# To use this script to genrate a makefile, make sure to be in the directory
# of the project (oplabs.git/mercury) and run the command:
#
#   $ ./genmake.sh > Makefile
#
# To learn how to use the makefile, generate it and read the comments
# at the top (or look down like 4 inches)

function discover_depends {
    local depth=$2
    if [ -z $depth ] ; then 
        depth=1
    fi

    if [[ $depth -gt 20 ]] ; then
        echo "Max dependency depth exceeded" >&2
    else
	    file=$1
	    includes=$(cat $file | perl -ne '/#include\s*<(.*)>/ && print "$1\n"')
	    
	    for i in $includes ; do
	        if [[ -e "include/$i" ]] ; then
	            echo -n -e ' \\\n         include/'$i
	            discover_depends "include/$i" $(($depth + 1))
	        fi
	    done 
    fi
}


source_files=$(find * -name '*cpp' -and -not -name 'main.cpp' -and -not -name 'test_*')
mains=$(find * -name 'main_*.cpp')

cat <<- EOF
# This file was autogenerated using ./genmake.sh You should not have to edit
# this file by hand!  If you do have to edit this file by hand please contact
# the sourceror
#
# To modify the different attributes of everything, check out the targets
# directory as it contains all the configuration for compilation.
#
# The genmake script will track dependencies and automatically add them to the
# make script. This includes header dependencies.
#
# In this make file, there is a concept of a target (TGT) and environment (ENV)
# The target tells the make file for which architecture we are building
# Currently only two are configured: x86 and i386.
#
# The environment specifies what environment we are using (e.g. devel, test, release)
#
# What these variables actually specify is a .mk file for this make file to include
# according to the template:
#
#           targets/TGT/ENV.mk
#
# examples of how to use this Makefile:
#   
#   $ make                      # builds x86 development binary (default)
#   $ TGT=x86 ENV=devel make    # explcitly builds x86 development binary (default)
#   $ TGT=i386 ENV=release make # builds release i386
#   $ TGT=i386 make             # builds devel i386
#   $ ENV=devel make            # builds devel x86
#   $ ENV=flyingspaghettimonster make            # builds flyingspaghettimonster x86 (if such a thing exists)
#

TGT?=x86
ENV?=devel

.INCLUDE_DIRS += targets/\$(TGT)/
include targets/\$(TGT)/\$(ENV).mk 

default:
EOF
echo '	mkdir -p _\$(TGT)_obs/ && make all'
echo ''

objects=""

for file in $source_files ; do
    obj_file=${file/cpp/o}
    obj_file=_'$(TGT)'_obs/${obj_file//\//_}

    echo "$obj_file: $file $(discover_depends $file)"
    echo '	$(CXX) -o '$obj_file' $(CXXFLAGS) -c '$file
    echo ''

    objects=$objects' '$obj_file
done

echo 'clean:'
echo '	rm -rf _$(TGT)_obs/'
echo '	rm mercury'
echo ''
echo '_$(TGT)_obs/libmerc.a: '$objects
echo '	ar -r $@ $^'
echo ''
echo 'all: _$(TGT)_obs/libmerc.a ' "$(discover_depends main.cpp | awk ' !x[$0]++')"
echo '	$(CXX) -o _$(TGT)_obs/mercury main.cpp $(CXXFLAGS) -L _$(TGT)_obs/ -lmerc'
echo '	ln -sf _$(TGT)_obs/mercury .'
echo ''
echo 'genmake: genmake.sh'
echo '	./genmake.sh > Makefile'
echo ''
echo 'cleanall:'
echo '	rm -rf _*_obs'
echo '	rm mercury'

{'fraction_non_alphanumeric': 0.14150009895111815, 'fraction_numerical': 0.008509796160696616, 'mean_word_length': 3.069243156199678, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6049994', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1723, 'n_words': 605}