2

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.

user209974
  • 1,737
  • 2
  • 15
  • 31
  • I think it's hardcoded: https://github.com/Kitware/CMake/blob/master/Source/cmSourceFile.h#L126-L133 – Justin Jul 12 '18 at 16:26
  • As for what I could find in the docs: see the [`LANGUAGE`](https://cmake.org/cmake/help/latest/prop_sf/LANGUAGE.html) property on source files – Justin Jul 12 '18 at 16:29
  • Related: https://stackoverflow.com/q/24534384/1896169 – Justin Jul 12 '18 at 16:32
  • @Justin Thanks! But how come `.obj` is not in the regex? It is definitely handled though. This does not cover the whole story probably. – user209974 Jul 13 '18 at 11:02

1 Answers1

1

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

Florian
  • 39,996
  • 9
  • 133
  • 149
  • Yes, you can really specify an object as a source. I added an example in my question. – user209974 Jul 14 '18 at 20:15
  • @user209974 Ok, I've found the code part in CMake doing this and updated my answer. But that seems very undocumented or at least I couldn't find a hint for this in the official documentation. – Florian Jul 15 '18 at 19:25