I do have a multi-component application, modeled with having one parent and two childs. The first one defines libraries, include files, etc. The second one wants to use that information. How can I do that, without coding those names manually?
parent/CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
set(MYNAME1 MyName1)
set(MYLINKNAME MyLinkName)
add_subdirectory(sub1)
message("Parent1: " ${MYNAME1} " " ${MYNAME2} " " ${MYNAME3} " " ${MYLINKNAME})
add_subdirectory(sub2)
message("Parent2: " ${MYNAME1} " " ${MYNAME2} " " ${MYNAME3} " " ${MYLINKNAME})
sub1/CMakeLists.txt
set(MYNAME1 MySub1 PARENT_SCOPE)
set(MYNAME2 MySub2 PARENT_SCOPE)
set(MYNAME3 MySub3)
set(MYLINKNAME IsLinked)
message("Sub1: " ${MYNAME1} " " ${MYNAME2} " " ${MYNAME3} " " ${MYLINKNAME})
sub1/CMakeLists.txt
message("Sub2: " ${MYNAME1} " " ${MYNAME2} " " ${MYNAME3} " " ${MYLINKNAME})
set(MYNAME2 OtherName)
The result is:
Sub1: MyName1 MySub3 IsLinked
Parent1: MySub1 MySub2 MyLinkName
Sub2: MySub1 MySub2 MyLinkName
Parent1: MySub1 MySub2 MyLinkName
Which means that I can transfer information using PARENT_SCOPE from sub1 to sub2. However, I would like to avoid patching packages for testing, logging, etc. Any suggestions?
Replying to the question in your comment:
Transferring the location of the include files from a standard package to your main program
You can find an example for this in the official
FindZLIB.cmakemodule:The find-module creates an imported library
ZLIB::ZLIBand set itsINTERFACE_INCLUDE_DIRECTORIESproperty to the location of thezlibinclude files.Later, in your main program you invoke the find-module:
add your main target:
and add ZLIB as dependency:
The location of the
zlibinclude files will be available inmain.cppbut you can also query it in yourCMakeLists.txt:The same method works also for
In
main/child1/CMakeLists.txtyou can write:In
main/child2/CMakeLists.txtyou can writeIn
child2.cppthePUBLICinclude directories will be added to the include path. You can also query it in yourmain/child2/CMakeLists.txt: