From 508277d0f8c095a1a1fb03a4f93b07793de4d89f Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 23 Aug 2019 17:34:25 +0200 Subject: [PATCH] COMFIG: update flex make rules to use '-f' for C-code as well - add additional rule (ending '.ll') for the combination of flex for C-code but compiling the result as C++. This can be needed for re-entrant parsers. - update bison rule to handle renaming of skeleton files. Use a wrap-bison script to manage this. --- wmake/rules/General/bison | 7 +- wmake/rules/General/flex | 4 +- wmake/rules/General/flex++ | 10 +- wmake/scripts/wrap-bison | 191 +++++++++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+), 5 deletions(-) create mode 100755 wmake/scripts/wrap-bison diff --git a/wmake/rules/General/bison b/wmake/rules/General/bison index 590996c953..8a17ab10ed 100644 --- a/wmake/rules/General/bison +++ b/wmake/rules/General/bison @@ -1,4 +1,4 @@ -SUFFIXES += .y .Y +SUFFIXES += .Y .y .yy ytoo = $E $(call QUIET_MESSAGE,bison,$(. +# +# Script +# wrap-bison +# +# Usage +# wrap-bison -input=*.yy -output=*.cc [bison-options] +# +# Description +# A wrapper to handle renaming/relocation of bison-generated files. +# +# When bison is used, it generates several output files. +# The names of the regular output files may not match our expectations. +# The skeleton files are always named the same, which can cause +# file-name collisions in some cases. +# +# Input: +# - myFile.yy +# +# Output: +# - myFile.tab.hh +# - myFile.tab.cc +# - location.hh +# - position.hh +# - stack.hh +# +# Approach +# - call bison from within a local Make/some-name/ directory. +# - use sed to modify the #include contents and rename files. +# From location.hh -> myFile.location.hh +# - place generated *.hh files directly into lnInclude/ +# - place generated *.cc file into the build/ directory +# +# Note +# General idea lifted from swak +#------------------------------------------------------------------------------ +usage() { + exec 1>&2 + while [ "$#" -ge 1 ]; do echo "$1"; shift; done + cat< [bison-options] + +options: + -input=NAME Perform the renaming actions + -output=NAME Perform the renaming actions + -h, -help Print the usage + +A bison wrapper to handle renaming of the skeleton files + +USAGE + exit 1 +} + +# The file extensions used +extCode="cc" +extHead="hh" + +#------------------------------------------------------------------------------ +# Parse arguments and options +#------------------------------------------------------------------------------ + +# bison -input=... -output=... +unset inputFile outputFile +while [ "$#" -gt 0 ] +do + case "$1" in + (-h | -help*) usage ;; + + (-input=*) inputFile="${1#*=}" ;; + (-output=*) outputFile="${1#*=}" ;; + + (*) break ;; + esac + shift +done + +# No rename action supplied? Juet execute bison directly. +if [ -z "$inputFile" ] +then + bison $* + exit $? +fi + +#------------------------------------------------------------------------------ +# Renaming requested + +# Need lnInclude/ directory +[ -d lnInclude ] || mkdir lnInclude 2>/dev/null || { + echo "Cannot continue without an lnInclude directory" 1>&2 + pwd -L + exit 1 +} + +# Get a baseName (stem) for the output +baseName="${inputFile##*/}" +baseName="${baseName%.*}" + +# Fallback for output +if [ -z "$outputFile" ] +then + outputFile="$(dirname ${inputFile})/${baseName}.$extCode" +fi + +# Run bison in temporary directory (keeps files together) +cwd="$(pwd -L)" +tmpDir="Make/bisonWrapper-$baseName" +rm -rf "$tmpDir" 2>/dev/null +mkdir "$tmpDir" 2>/dev/null + +cd "$tmpDir" || exit 1 +rc=1 + +# DO WE WANT THIS? +# trap 'cd $cwd; rm -f $tmpDir 2>/dev/null; exit $rc' EXIT TERM INT + +bison "$@" "../../$inputFile" +rc=$? + +cd "../.." || exit 1 + +if [ "$rc" -ne 0 ] +then + rm -rf $tmpDir 2>/dev/null + exit "$rc" # Exit with bison return code +fi + + +# Check for/remove .tab. tag? +unset untabFilter + +# withTab=$/include *"stack/s/"/"'"${baseName}."'/;' \ + +hasTab="${outputFile##*/}" +hasTab="${hasTab%.*}" + +if [ "$hasTab" = "${hasTab%.tab}" ] +then + untab='/^#.*".*\.tab\./s/\.tab\././' +fi + +# Filter include names to generate new files +# "$1" = input +# "$2" = output +filterRename() +{ + if [ -f "$1" ] && [ -n "$2" ] + then + sed \ + -e '/include *"location/s/"/"'"${baseName}."'/;' \ + -e '/include *"position/s/"/"'"${baseName}."'/;' \ + -e '/include *"stack/s/"/"'"${baseName}."'/;' \ + -e "$untab;" \ + "$1" >| "$2" + fi +} + + +# Boilerplate -> lnInclude/ directory with new name +for file in position location stack +do + filterRename \ + "${tmpDir}/${file}.$extHead" \ + "lnInclude/${baseName}.${file}.$extHead" +done + +# Header -> lnInclude/ directory, possibly with .tab.hh to .hh +filterRename \ + "${tmpDir}/${baseName}.tab.$extHead" \ + "lnInclude/${baseName}${untab:-.tab}.$extHead" + +# Code -> build directory, possibly with .tab.hh to .hh +filterRename \ + "${tmpDir}/${baseName}.tab.$extCode" \ + "${outputFile}" + + +rm -rf $tmpDir 2>/dev/null +exit "$rc" # Exit with bison return code + +#------------------------------------------------------------------------------