FILE GLOB 在 INCLUDE()d 文件中失败

huangapple go评论62阅读模式
英文:

FILE GLOB fails in INCLUDE()d file

问题

我正在使用 Windows 上的 CMake 3.22.2,并且具有以下结构:

go.bat:

:: 启动无源代码生成,以避免杂乱;生成器可以是任何东西
@cmake -G "Visual Studio 16" -H%~dp0 -B%~dp0build

CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
PROJECT( mve )
INCLUDE( extensions/LookForExtensions.cmake )  # 通过 INCLUDE() 运行一个文件
ADD_SUBDIRECTORY( subprojects )                # 通过其 CMakeLists.txt 文件运行另一个

它运行的两个文件,
subprojects/CMakeLists.txt
extensions/LookForExtensions.cmake
都具有相同的内容,如下所示:

SET( here ${CMAKE_CURRENT_LIST_DIR} )
MESSAGE( "Looking for files/directories in " ${here} ) # 在两种情况下都会返回正确结果
FILE( GLOB contents RELATIVE ${here} * )   # 在一种情况下提供正确结果,但在另一种情况下不提供正确结果
FOREACH( match ${contents} )
    MESSAGE( "* found " ${match} )
ENDFOREACH()

输出由我用 # 注释进行了标注,如下所示:

C:\cmake-trouble>go
-- 选择 Windows SDK 版本 10.0.19041.0 以定位 Windows 10.0.22621。
Looking for files/directories in C:/cmake-trouble/extensions  # 这是正确的
* found ../CMakeLists.txt           # 不正确
* found ../build                    # 不正确
* found ../go.bat                   # 不正确
* found ../subprojects              # 不正确
Looking for files/directories in C:/cmake-trouble/subprojects  # 这是正确的
* found CMakeLists.txt    # 这是正确的
-- 配置完成
-- 生成完成
-- 已将生成文件写入: C:/cmake-trouble/build

为什么 FILE( GLOB ... ) 在这两种不同情况下表现如此不同,尽管两种情况下都提供了正确的输入?更重要的是,如何让它在 LookForExtensions.cmake 中正确运行?似乎 ${here} 是正确的,但是 RELATIVE ${here} 子句明显被忽略了。

英文:

I am using CMake 3.22.2 on Windows and have the following structure:

go.bat:

:: launch an out-of-source build, to avoid clutter; generator could be anything
@cmake -G "Visual Studio 16" -H%~dp0 -B%~dp0build

CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
PROJECT( mve )
INCLUDE( extensions/LookForExtensions.cmake )  # run one file via INCLUDE()
ADD_SUBDIRECTORY( subprojects )                # and another via its CMakeLists.txt file

The two files it runs,
subprojects/CMakeLists.txt and
extensions/LookForExtensions.cmake,
both have identical content, as follows:

SET( here ${CMAKE_CURRENT_LIST_DIR} )
MESSAGE( "Looking for files/directories in " ${here} ) # correct result in both cases
FILE( GLOB contents RELATIVE ${here} * )   # delivers correct results in one case but not the other
FOREACH( match ${contents} )
    MESSAGE( "* found " ${match} )
ENDFOREACH()

And the output, annotated by me with # comments, is as follows:

C:\cmake-trouble>go
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.22621.
Looking for files/directories in C:/cmake-trouble/extensions  # this is correct
* found ../CMakeLists.txt           # not correct
* found ../build                    # not correct
* found ../go.bat                   # not correct
* found ../subprojects              # not correct
Looking for files/directories in C:/cmake-trouble/subprojects  # this is correct
* found CMakeLists.txt    # this is correct
-- Configuring done
-- Generating done
-- Build files have been written to: C:/cmake-trouble/build

Why is FILE( GLOB ... ) behaving so differently in the two different situations, given correct input in both cases? More importantly, how can I get it to behave correctly in LookForExtensions.cmake ? It seems like ${here} is correct but the RELATIVE ${here} clause is simply and unaccountably being ignored.

答案1

得分: 1

"OK,我明白了。FILE( GLOB contents RELATIVE ${here} * )并不意味着,正如我所假设的那样,“匹配${here}的内容与模式相匹配”。它的意思是“将当前工作目录的内容与模式匹配,但将每个输出路径重新表示为${here}的相对路径”。关键证据是extensions目录本身没有被报告为..的内容的一部分。

所以,这个:

FILE( GLOB contents RELATIVE "${here}" "${here}/*" )

就是我想要的效果。"

英文:

OK I got it. FILE( GLOB contents RELATIVE ${here} * ) does not mean, as I had assumed, "match the contents of ${here} to the pattern". It means "match the contents of the current working directory to the pattern, but re-express each output path relative to ${here}". The smoking gun was the fact that the extensions directory itself was not reported as part of the contents of ..

So, this:

FILE( GLOB contents RELATIVE "${here}" "${here}/*" )

does what I intended.

huangapple
  • 本文由 发表于 2023年7月28日 01:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定