Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can CMake recognize CMakeLists.txt with another name (CMakeLists_nightly.txt)?

Tags:

cmake

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?

like image 985
Caleb Avatar asked Aug 30 '11 20:08

Caleb


People also ask

How does CMakeLists TXT work?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.

Where do I put CMakeLists txt?

CMakeLists. txt is placed at the root of the source tree of any application, library it will work for. If there are multiple modules, and each module can be compiled and built separately, CMakeLists. txt can be inserted into the sub folder.

What is CMakeLists used for?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.


2 Answers

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.

like image 184
larsmoa Avatar answered Jan 01 '23 20:01

larsmoa


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.

like image 29
Dmitrii Semikin Avatar answered Jan 01 '23 20:01

Dmitrii Semikin