user209974
user209974

Reputation: 1927

CMake: what are possible target source types and can they be extended?

In CMake, one can define a target, for example a C or C++ library, with add_library, with the following syntax:

add_library(<name> [STATIC | SHARED | MODULE]
            [EXCLUDE_FROM_ALL]
            [source1] [source2 ...])

But what are possible kind of sources? They can be .h or .cxx files containing code obviously. But they can also be .rc resources files, or even .obj object files used by the linker.

So what types of "non-code" are allowed as sources of a target in CMake, depending on the language, target type, platform? The page on SOURCES is rather uninformative. Is it located somewhere else in the documentation?

Also, can this list of allowed source types be customized and extended, and how?

EDIT

As an example, objects provided as sources of a target are used everywhere in tensorflow's CMake files, for example here.

Upvotes: 2

Views: 854

Answers (1)

Florian
Florian

Reputation: 42872

The set of supported extensions mainly depends on the ENABLED_LANGUAGES.

So if I grep for CMAKE_<LANG>_SOURCE_FILE_EXTENSIONS I'll get the following list:

set(CMAKE_ASM${ASM_DIALECT}_SOURCE_FILE_EXTENSIONS "s;S;asm")
set(CMAKE_C_SOURCE_FILE_EXTENSIONS             "c;m")
set(CMAKE_CSharp_SOURCE_FILE_EXTENSIONS        "cs")
set(CMAKE_CUDA_SOURCE_FILE_EXTENSIONS          "cu")
set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS           "C;M;c++;cc;cpp;cxx;mm;CPP")
set(CMAKE_Fortran_SOURCE_FILE_EXTENSIONS       "f;F;fpp;FPP;f77;F77;f90;F90;for;For;FOR;f95;F95")
set(CMAKE_Java_SOURCE_FILE_EXTENSIONS          "java")
set(CMAKE_RC_SOURCE_FILE_EXTENSIONS            "rc;RC")
set(CMAKE_Swift_SOURCE_FILE_EXTENSIONS         "swift")

The special cases

There are some special cases with generator expressions like add_library(... $<TARGET_OBJECTS:objlib> ...) and outputs of add_custom_command() calls.

Edit: The use of object files as source files is actually a sub-case of the add_custom_command() special case implemented in cmSourceFile::CheckExtension():

 // Look for object files.
 if (this->Extension == "obj" || this->Extension == "o" ||
     this->Extension == "lo") {
   this->SetProperty("EXTERNAL_OBJECT", "1");
 }

How to extend the supported source file extensions/types?

Examples can be found here

Upvotes: 1

Related Questions