Makefiles: Fix CMAKE_EXPORT_COMPILE_COMMANDS crash with custom compile rule
Extend the fix from !5521 (merged) to work for the <FLAGS>
placeholder too.
Also fix the test case to actually enable EXPORT_COMPILE_COMMANDS
on the test target.
Fixes: #22261 (closed)
Backport: release