[CMake] coding style for modules

Dave Milter davemilter at gmail.com
Wed Jan 3 21:09:54 EST 2018


Hello,

If there is some libfoo that I want to use in my project,
and this libfoo has it's own CMakeLists.txt I have 3 options to deal
with such external dependency:

a) Treat it like every other external dependency and write cmake code
  like FindLibFoo.cmake to find libfoo on file system.

b)Include this project as git submodule and use ExternalProject to
invoke `cmake` and `cmake --build`

c)Include this project as git submodule and use add_subdirectory(libfoo)


Variant "c" has advantages that potentially in this case it is
possible to have almost the same compiler flags for libfoo and main
project, which may give the most
robust solution in case of usage visual studio (release/debug/multi
thread/single thread CRT problems), plus it is simplify a lot cross
compilation.

But usually "libfoo" CMakeLists.txt looks like this:

```
cmake_minimum_required(VERSION 3.10)

#configure many things that important for standalone
#project, but not requires for project as part of another project
#for example in case of gcc we add some special flags like -Wall
-Wextra -pedantic and so on

add_library(foo SHARED sources)
```

and part between `cmake_minimum_required` and `add_library` is useless
and sometime prevent cross compilation and usage of the same compiler flags.

So question how should I organize CMakeListst.txt of libfoo help deals with "c"?

Should I move all specific for standalone project code to:

if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
# i am standalone
endif()

or may be I should use some other tricks?


More information about the CMake mailing list