28

I am wanting to create CMakeLists.txt files that are more specifically named such as "CMakeLists_nightly.txt", "CMakeLists_weekly.txt" and so forth. The reason I want to do this is to cut down on the folder hierarchy clutter of my project. I could easily put each of these files in their own folder with the postfix I showed above but I do not want to do this.

Can I tell cmake to take a CMakeLists.txt file by another name? I have seen this question asked before on another forum (http://www.cmake.org/pipermail/cmake/2007-August/016036.html) but it was back in 2007 and the answer was no. Does the current version of CMake provide this capability?

Caleb
  • 870
  • 1
  • 10
  • 21
  • 1
    Note that I needed this question/answer because I have several "txt" files in my project folder and I want GIT to ignore them, the final solution for me was Whitelist "CMakeLists.txt" or just don't use "txt" extension for other files and ignore files without extension: http://stackoverflow.com/questions/19023550/how-do-i-add-files-without-dots-in-them-all-extension-less-files-to-the-gitign – CoffeDeveloper Jul 12 '15 at 12:08

2 Answers2

21

Not really, but you can emulate this by putting CMakeLists.txt in separate directories, e.g. continous/CMakeLists.txt and nightly/CMakeLists.txt. Use INCLUDE to include the appropriate scripts for each of the build configs.

Consider if this really is the right approach - completely separating the nightly and continuous script is a really bad idea as that will lead to duplication and a very bug prone build setup.

larsmoa
  • 12,604
  • 8
  • 62
  • 85
  • 16
    What is the reason for CMake not having this capability, do you think? Just something they have not implemented? Make has the -f option that lets you specify a makefile by any name and it seems like CMake should have this as well... – Caleb Aug 31 '11 at 13:13
  • If you are going to use Include, you may as well call them CMakeLists_nightly.txt and _weekly.txt like Caleb asked... – André Sep 01 '11 at 07:33
  • 3
    @Caleb: I'm not sure but it's been annoying me to as I really hate the .txt extension (makes it a pain to syntax highlight the files in some editors). One reason could be that they don't want you to do it like that, rather it should be implemented using `OPTION` or by separating the builds in directories. – larsmoa Sep 02 '11 at 06:57
  • Then it might help what @Andre suggested: You have a CMakelLists.txt that more or less simply includes a toplevel.cmake in the same directory. There you have your toplevel cmake file with an extension for your highlighting, and you can call it whatever you like :) Note that there are some statements that must be in the CMakeLists.txt, such as project(), which is implied if you don't state it explicitly. – SvenS Apr 28 '15 at 07:51
4

Answer, which came into my mind, while I was reading an answer from larsmoa and thinking about it little bit longer:

(this is not exactly the answer to the question about different name for CMakeLists.txt, but rather, to "how to have two different CMake configuraiton files in the same directory")

You can avoid creating multiple directories and storing there CMakeLists.txt (it may also be problematic, if you want your script to be the parent of everything). My Idea is, that you can have two "include" cmake files with any names you like. And then in CMakeLists.txt you may have an set(CACHE), which controlls, which include-script should be actually included.

With this setup you can have two build directories: one configured with one value of the option, and another - with another. Depending on that, in which build-directory you do the build, corresponding build definition will be used.

It can look something like this:

CMakeLists.txt:

set(
    MY_BUILD_KIND BUILD_A CACHE STRING 
    "Select build kind: BUILD_A or BUILD_B"
)

if ( MY_BUILD_KIND strequal "BUILD_A" )
    include(build_a.cmake)
elseif (MY_BUILD_KIND strequal "BUILD_B")
    include(build_b.cmake)
else ()
    message ( FATAL_ERROR "Unknown build kind: ${MY_BUILD_KIND}" )
endif () 

Background (why do I need it?): My situation is kind of exotic, I guess. I have a C++ project, different parts of which use two different compilers. And there is a part of it, which needs to be built by each of them. So the directory structure is like this:

  • Projects
    • CompilerAProjects
    • CompilerBProjects
    • CommonProjects

Here "CommonProjects" are included as Part of "CompilerAProjects" and also as part of "CompilerBProjects". Now we try to integrate cmake and I was thinking, how can we keep the structure, but do the build with CMake. If I put CMakeLists.txt in the root directory, then I don't understand, how to differentiate between two compilers. And if I don't have the root project, then it is not clear, how to refer to "sibling" project. So I came to the idea, that I can included sub-directories basing on the current compiler. And then I decided, that actually it is not necessary, that compiler is the driving factor, we can use set(CACHE) instead. And we are not restricted to select, which sub-directory we select, but can also include ".cmake" files.

Dmitrii Semikin
  • 2,134
  • 2
  • 20
  • 25