ecmwf / ecbuild

A CMake-based build system, consisting of a collection of CMake macros and functions that ease the managing of software build systems
https://ecbuild.readthedocs.io
Apache License 2.0
26 stars 25 forks source link

ecbuild_remove_fortran_flags() uses incorrect REGEX #27

Closed markjolah closed 4 years ago

markjolah commented 4 years ago

The function ecbuild_remove_fortran_flags() uses an incorrect REGEX to remove flags from the Fortran_FLAGS variables. Specifying flag -g will remove the leading -g from any flag that starts that way. e.g., the intel flag -gcc-name=gcc-9.3.0 -> cc-name=gcc-9.3.0 -> BOOM.

This PR fixes the regex to preserve all other flags except the exact matched flag.

Test script for improved REGEX.

function(stripflags flag str)
    string(REGEX REPLACE "(^|[ ]+)${flag}($|[ ]+)" "\\1" out "${str}")
    message("REPLACE FLAG:${flag} Transform: \"${str}\" -> \"${out}\"")
endfunction()

set(flag "-g")
set(tests "-g" " -g" "-g " "-g -foo" "-bar -g -baz" "-ggg -g -gcc -g" "-gcc" "--g -g-" "---g" "   -g" "-g   " "   -g   " "-g-g")

foreach( t IN LISTS tests)
    stripflags(${flag} "${t}")
endforeach()

Run as:

cmake -P test-regex.cmake

Output:

mjo@wwwyzzerdd ~/work/temp/cmake-regex-test $ cmake -P test-regex.cmake 
REPLACE FLAG:-g Transform: "-g" -> ""
REPLACE FLAG:-g Transform: " -g" -> " "
REPLACE FLAG:-g Transform: "-g " -> ""
REPLACE FLAG:-g Transform: "-g -foo" -> "-foo"
REPLACE FLAG:-g Transform: "-bar -g -baz" -> "-bar -baz"
REPLACE FLAG:-g Transform: "-ggg -g -gcc -g" -> "-ggg -gcc "
REPLACE FLAG:-g Transform: "-gcc" -> "-gcc"
REPLACE FLAG:-g Transform: "--g -g-" -> "--g -g-"
REPLACE FLAG:-g Transform: "---g" -> "---g"
REPLACE FLAG:-g Transform: "   -g" -> "   "
REPLACE FLAG:-g Transform: "-g   " -> ""
REPLACE FLAG:-g Transform: "   -g   " -> "   "
REPLACE FLAG:-g Transform: "-g-g" -> "-g-g"
markjolah commented 4 years ago

Yikes! Sorry for that obvious copy paste error. That looks better.