Commit ae89967f authored by Brad King's avatar Brad King

FindHDF5: Fix command-line parsing argument extraction order

Re-implement our internal `_HDF5_parse_compile_line` helper to process
command line arguments all at once and in order.  Otherwise the
libraries named by absolute path and those named by `-l` arguments are
not kept in order.

The new implementation will not handle separate arguments like
`-I /path/to/include/dir` but I have not seen the HDF5 compiler
wrappers produce this form.  If necessary the parsing loop can
be extended with a state variable to keep track of such pairs.
parent 2b0256c5
...@@ -340,62 +340,44 @@ macro( _HDF5_parse_compile_line ...@@ -340,62 +340,44 @@ macro( _HDF5_parse_compile_line
libraries libraries
libraries_hl) libraries_hl)
# Match the include paths if(UNIX)
set( RE " -I *([^\" ]+|\"[^\"]+\")") separate_arguments(_HDF5_COMPILE_ARGS UNIX_COMMAND "${${compile_line_var}}")
string( REGEX MATCHALL "${RE}" include_path_flags "${${compile_line_var}}") else()
foreach( IPATH IN LISTS include_path_flags ) separate_arguments(_HDF5_COMPILE_ARGS WINDOWS_COMMAND "${${compile_line_var}}")
string( REGEX REPLACE "${RE}" "\\1" IPATH "${IPATH}" ) endif()
list( APPEND ${include_paths} ${IPATH} )
# Match the definitions
set( RE " -D([^ ]*)")
string( REGEX MATCHALL "${RE}" definition_flags "${${compile_line_var}}" )
foreach( DEF IN LISTS definition_flags )
string( STRIP "${DEF}" DEF )
list( APPEND ${definitions} ${DEF} )
# Match the library paths
set( RE " -L *([^\" ]+|\"[^\"]+\")")
string( REGEX MATCHALL "${RE}" library_path_flags "${${compile_line_var}}")
foreach( LPATH IN LISTS library_path_flags )
string( REGEX REPLACE "${RE}" "\\1" LPATH "${LPATH}" )
list( APPEND ${library_paths} ${LPATH} )
# now search for the lib names specified in the compile line (match -l...)
# match only -l's preceded by a space or comma
set( RE " -l *([^\" ]+|\"[^\"]+\")")
string( REGEX MATCHALL "${RE}" library_name_flags "${${compile_line_var}}")
foreach( LNAME IN LISTS library_name_flags )
string( REGEX REPLACE "${RE}" "\\1" LNAME "${LNAME}" )
if(LNAME MATCHES ".*hl")
list(APPEND ${libraries_hl} ${LNAME})
list(APPEND ${libraries} ${LNAME})
# now search for full library paths with no flags foreach(arg IN LISTS _HDF5_COMPILE_ARGS)
set( RE " ([^\" ]+|\"[^\"]+\")") if("${arg}" MATCHES "^-I(.*)$")
string( REGEX MATCHALL "${RE}" library_name_noflags "${${compile_line_var}}") # include directory
foreach( LIB IN LISTS library_name_noflags ) list(APPEND ${include_paths} "${CMAKE_MATCH_1}")
string( REGEX REPLACE "${RE}" "\\1" LIB "${LIB}" ) elseif("${arg}" MATCHES "^-D(.*)$")
get_filename_component(LIB "${LIB}" ABSOLUTE) # compile definition
if(NOT EXISTS ${LIB} OR IS_DIRECTORY ${LIB}) list(APPEND ${definitions} "${CMAKE_MATCH_1}")
continue() elseif("${arg}" MATCHES "^-L(.*)$")
endif() # library search path
get_filename_component(LPATH ${LIB} DIRECTORY) list(APPEND ${library_paths} "${CMAKE_MATCH_1}")
get_filename_component(LNAME ${LIB} NAME_WE) elseif("${arg}" MATCHES "^-l(hdf5.*hl.*)$")
string( REGEX REPLACE "^lib" "" LNAME ${LNAME} ) # library name (hl)
list( APPEND ${library_paths} ${LPATH} ) list(APPEND ${libraries_hl} "${CMAKE_MATCH_1}")
if(LNAME MATCHES ".*hl") elseif("${arg}" MATCHES "^-l(.*)$")
list(APPEND ${libraries_hl} ${LNAME}) # library name
else() list(APPEND ${libraries} "${CMAKE_MATCH_1}")
list(APPEND ${libraries} ${LNAME}) elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.(a|so|dylib|sl|lib)$")
endif() # library file
endforeach() if(NOT EXISTS "${arg}")
get_filename_component(_HDF5_LPATH "${arg}" DIRECTORY)
get_filename_component(_HDF5_LNAME "${arg}" NAME_WE)
string(REGEX REPLACE "^lib" "" _HDF5_LNAME "${_HDF5_LNAME}")
list(APPEND ${library_paths} "${_HDF5_LPATH}")
if(_HDF5_LNAME MATCHES "hdf5.*hl")
list(APPEND ${libraries_hl} "${_HDF5_LNAME}")
list(APPEND ${libraries} "${_HDF5_LNAME}")
endmacro() endmacro()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment