Skip to content

Incorrect Parsing of `-with-bear` and `-bear-output-dir` in `AllwmakeParseArguments`

Summary

-with-bear accepts the optional argument -bear-output-dir to set the compile_commands.json output directory. -with-bear -bear-output-dir=/dev/null, for example, produces

$ ./Allwmake -with-bear -bear-output-dir=/dev/null
Use bear 3.1.5
Output = /Users/ali/OpenFOAM/OpenFOAM-v2412/build/darwin64ClangDPInt64Debug/compile_commands.json

Error encountered:
    unknown option: '-bear-output-dir=/dev/null'

See 'wmake -help' for usage
 or 'wmake -help-full' for extended usage
...

It seems that AllwmakeParseArguments does not parse -bear-output-dir immediately following -with-bear. For example, sh -x ./Allwmake -j -s -with-bear -bear-output-dir=/dev/null results in

...
++ exec wmake -with-bear -all -j -s -bear-output-dir=/dev/null
...

Here's a list of files that might be related to this issue:

Steps to reproduce

  1. Run ./Allwmake -with-bear -bear-output-dir=$HOME

Example case

What is the current bug behaviour?

Using -bear-output-dir with -with-bear causes the path to be ignored and an error to be emitted.

What is the expected correct behavior?

compile_commands.json is stored in the requested directory with no error.

Relevant logs and/or images

Environment information

  • OpenFOAM version : v2412 (also v2312)
  • Operating system : macOS (Sonoma 14.7)
  • Hardware info :
  • Compiler : clang

Possible fixes

Here's the diff of $WM_DIR/scripts/AllwmakeParseArguments

diff --git a/wmake/scripts/AllwmakeParseArguments b/wmake/scripts/AllwmakeParseArguments
index 63a7501a30..4e8148105d 100644
--- a/wmake/scripts/AllwmakeParseArguments
+++ b/wmake/scripts/AllwmakeParseArguments
@@ -88,7 +88,7 @@ USAGE
 #
 #------------------------------------------------------------------------------
 
-unset wmakeOpt_frontend wmakeOpt_nonRecursive
+unset wmakeOpt_frontend wmakeOpt_withBearOutputDir wmakeOpt_nonRecursive
 unset wmakeOpt_debug wmakeOpt_log wmakeOpt_openmp wmakeOpt_strict wmakeOpt_queue
 
 for arg in "$@"
@@ -104,9 +104,23 @@ do
     -with-bear)
         # Use 'bear' as frontend wrapper to wmake
         wmakeOpt_frontend="-with-bear"
+
+        # Check if the next argument is `-bear-output-dir=*`
+        if [ "${1#-bear-output-dir=}" != "$1" ]; then
+            wmakeOpt_withBearOutputDir="$1"
+
+            # Remove `-bear-output-dir=*` from the list of arguments
+            shift
+        fi
+
         continue    # Argument handled, remove it
         ;;
 
+    -bear-output-dir=*)
+        # It is already handled by `-with-bear`
+        continue
+        ;;
+
     -no-recurs* | -fromWmake)
         # Avoid recursion (eg, if called from wmake)
         wmakeOpt_nonRecursive=true
@@ -214,13 +228,13 @@ if [ -z "$wmakeOpt_nonRecursive" ]
 then
     if [ -z "$wmakeOpt_log" ]
     then
-        exec wmake $wmakeOpt_frontend -all \
+        exec wmake $wmakeOpt_frontend "$wmakeOpt_withBearOutputDir" -all \
             $wmakeOpt_debug $wmakeOpt_strict $wmakeOpt_queue $wmakeOpt_openmp $*
         exit $? # Unneeded, but just in case something went wrong
     else
         echo "Logging wmake -all output to '$wmakeOpt_log'" 1>&2
         echo 1>&2
-        exec wmake $wmakeOpt_frontend -all \
+        exec wmake $wmakeOpt_frontend "$wmakeOpt_withBearOutputDir" -all \
             $wmakeOpt_debug $wmakeOpt_strict $wmakeOpt_queue $wmakeOpt_openmp $* 2>&1 | \
             /usr/bin/tee $wmakeOpt_log
         # Need to cleanup after the tee
@@ -245,9 +259,11 @@ fi
 # Cleanup local variables and functions
 #------------------------------------------------------------------------------
 
-unset wmakeOpt_frontend wmakeOpt_nonRecursive
+unset wmakeOpt_frontend wmakeOpt_withBearOutputDir wmakeOpt_nonRecursive
 unset wmakeOpt_debug wmakeOpt_log wmakeOpt_openmp wmakeOpt_strict wmakeOpt_queue
 unset -f usage
 
 
 #------------------------------------------------------------------------------
+
+# vim: ft=sh