Extend list of file extensions for a LANGUAGE globally (for the whole project)

In a fairly large project I’m converting over to CMake there are lots of files with .rcv extensions that are actually RC files (windows resource). How I can tell CMake to treat all such files as RC files without setting the LANGUAGE property on each of them manually?

Use CMAKE_<LANG>_SOURCE_FILE_EXTENSIONS.

The default is currently:

set(CMAKE_RC_SOURCE_FILE_EXTENSIONS rc;RC)

So take care of uppercase if that’s relevant for you.

That doesn’t seem to work. I tried this:

cmake_minimum_required(VERSION 3.25)
project(Test C)
set(CMAKE_RC_SOURCE_FILE_EXTENSIONS rc;RC;rcv)
#set(CMAKE_RC_SOURCE_FILE_EXTENSIONS rc;RC;rcv CACHE STRING "") # this also doesn't work
add_executable(test WIN32 test.rcv test.c)

test.rcv doesn’t get compiled at all. If I rename to test.rc then it compiles as resource (as expected).

I do have no specific experience with RC files, but normally you need to enable each language you use first:

project(Test LANGUAGES C RC)

I tried that too, no difference.

OK, after digging a bit of CMake source code itself, I found that lang rules have only one chance to be overridden, because the lang-extension map is set only ONCE. That one chance can be set by CMAKE_USER_MAKE_RULES_OVERRIDE_<LANG>.

So I created a new file rc_overrides.cmake with this one line:

set(CMAKE_RC_SOURCE_FILE_EXTENSIONS rc;RC;rcv)

then modified the above sample project like so:

cmake_minimum_required(VERSION 3.25)
set(CMAKE_USER_MAKE_RULES_OVERRIDE "rc_overrides.cmake") # must set before project()
project(Test LANGUAGES C RC)
message("RC extensions: ${CMAKE_RC_SOURCE_FILE_EXTENSIONS}")
add_executable(test WIN32 test.rcv test.c)

And now it prints the override and it works.

EDIT:
In the above sample I used CMAKE_USER_MAKE_RULES_OVERRIDE (not the lang-specific CMAKE_USER_MAKE_RULES_OVERRIDE_RC). Apparently CMAKE_USER_MAKE_RULES_OVERRIDE_RC doesn’t work, but CMAKE_USER_MAKE_RULES_OVERRIDE_C does! As does the global CMAKE_USER_MAKE_RULES_OVERRIDE Go figure.

1 Like

Didn’t know that. I just used the variable in the context of a compiler definition.

So this variable (and maybe others) need an improved documentation.
I think many compiler-related variables cannot be easily modified, but for the source extension I guessed it can be set anytime or at least once.