I just got into cmake because I started working on a bigger project. I need to add module tests. There are several embedded devices, each has it own application running on it.
Most of these applications share code. So the code is divided into modules. The problem is that some modules are used in most of the other modules. These modules are common, common-net and log.
A simplified project (app) for a device has following structure:
.
|-- CMakeLists.txt (for app)
|-- LICENSE
|-- app
| |-- inc
| | |-- appfile1.hpp
| | `-- appfile2.hpp
| `-- src
| |-- appfile1.cpp
| |-- appfile2.cpp
| `-- main.cpp
|-- comp
| |-- comp1
| | |-- CMakeLists.txt (for test)
| | |-- comp
| | | |-- CMakeLists.txt (for lib)
| | | |-- intf
| | | | |-- comp1file.hpp
| | | | `-- comp1file.hpp
| | | `-- src
| | | |-- comp1file.cpp
| | | `-- comp1file.cpp
| | `-- test
| | `-- src
| | `-- comp1testfile.cpp
| |-- comp2
| | |-- CMakeLists.txt (for test)
| | |-- comp
| | | |-- CMakeLists.txt (for lib)
| | | |-- inc
| | | | `-- comp2file1.hpp
| | | |-- intf
| | | | |-- comp2file2.hpp
| | | | |-- comp2file3.hpp
| | | | |-- comp2file4.hpp
| | | `-- src
| | | |-- comp2file1.cpp
| | | |-- comp2file2.cpp
| | | `-- comp2file3.cpp
| | `-- test
| | |-- inc
| | | `-- comp2testfile.hpp
| | `-- src
| | `-- comp2testfile.cpp
| |-- common
| | |-- CMakeLists.txt (for test)
| | `-- comp
| | |-- inc
| | | |-- commonfile1.hpp
| | | `-- commonfile2.hpp
| | `-- src
| | `-- commonfile1.cpp
| |-- common-net
| | |-- CMakeLists.txt (for test)
| | |-- comp
| | | |-- inc
| | | | |-- netfile1.hpp
| | | | |-- netfile2.hpp
| | | | |-- netfile3.hpp
| | | | |-- netfile4.hpp
| | | | |-- netfile5.hpp
| | | `-- src
| | | |-- netfile1.cpp
| | | |-- netfile2.cpp
| | | |-- netfile3.cpp
| | | |-- netfile4.cpp
| | | |-- netfile5.cpp
| | `-- test
| | |-- inc
| | | |-- nettestfile1.hpp
| | `-- src
| | |-- nettestfile1.cpp
| | |-- nettestfile2.cpp
| | |-- nettestfile3.cpp
| |-- comp3
| | |-- CMakeLists.txt (for test)
| | |-- comp
| | | |-- CMakeLists.txt (for lib)
| | | |-- intf
| | | | |-- comp3file1.hpp
| | | | `-- comp3file2.hpp
| | | `-- src
| | | `-- comp3file1.cpp
| | `-- test
| | `-- src
| | `-- comp3testfile1.cpp
| |-- log
| | `-- comp
| | |-- inc
| | | |-- logfile1.hpp
| | | |-- logfile2.hpp
| | |-- intf
| | | |-- logfile3.hpp
| | | |-- logfile4.hpp
| | `-- src
| | |-- logfile1.cpp
| | |-- logfile2.cpp
| | |-- logfile3.cpp
| | `-- logfile4.cpp
| |-- comp4
| | |-- CMakeLists.txt (for test)
| | |-- comp
| | | |-- CMakeLists.txt (for lib)
| | | |-- intf
| | | | |-- comp4file1.hpp
| | | | |-- comp4file2.hpp
| | | | |-- comp4file3.hpp
| | | `-- src
| | | |-- comp4file1.cpp
| | | |-- comp4file2.cpp
| | | `-- comp4file3.cpp
| | `-- test
| | |-- inc
| | | |-- comp4testfile1.hpp
| | | `-- comp4testfile2.hpp
| | `-- src
| | |-- comp4testfile1.cpp
| | |-- comp4testfile2.cpp
| | |-- comp4testfile3.cpp
| | |-- comp4testfile4.cpp
| | `-- comp4testfile5.cpp
|-- gcc-4.8.cmake
|-- gcc-4.9.cmake
`-- gcc-default.cmake
I know it has a lot of "comp" in it, but that naming was not up to me..
Each comp has 2 CMakefiles: The top level one is to create a unit test executable to test the module. The one in comp is to make a library. So that it can be used with add_subdirectory in the app CMakefile.
The problem is that a lot of modules are dependent of common, common-net and log. Currently those parts are not build as libs. I'm guessing cause add_subdirectory gave problems if used in multiple modules? Is this clean?
This leads to: Each dependent module includes the common headers in the comp CMakefile. At the top level (app CMakefile) the common sources are added to the executable. But for each module test I also have to add the common sources to the test executable.
This seems strange to me and I think something is wrong. But I dont know how to solve it cleanly?
Looking at the project tree and the need to test each module independently. Is the approach to add sources at the top level ok or is that not done?