I know that CMake automatically somehow figures out the dependency tree. It will build things in the right order normally.
I decided to use options and that has caused it to guess poorly.
I have a variant of this SO question following:
/CMakeLists.txt
/src/CMakeLists.txt
/testpackage1/CMakeLists.txt
/testpackage2/CMakeLists.txt
In my case, mylib has two kinds -- a static and a dynamic.
OPTION(MYLIB_BUILD_STATIC "Build static library" TRUE)
IF(MYLIB_BUILD_STATIC)
ADD_LIBRARY(mylib_static STATIC ${MyLib_SRCS})
...
SET_TARGET_PROPERTIES(mylib_static PROPERTIES OUTPUT_NAME "mylib")
SET(MYLIB_LIB_TARGET mylib_static)
ENDIF(MYLIB_BUILD_STATIC)
OPTION(MYLIB_BUILD_SHARED "Build shared library" TRUE)
IF(MYLIB_BUILD_SHARED)
ADD_LIBRARY(mylib SHARED ${MyLib_SRCS})
...
SET(MYLIB_LIB_TARGET mylib)
ENDIF(MYLIB_BUILD_SHARED)
At this point ${MYLIB_LIB_TARGET}
has either mylib
or mylib_static
in it
In the unit test packages....
OPTION(MYLIB_BUILD_TEST "Build test program." TRUE)
IF(MYLIB_BUILD_TEST)
INCLUDE_DIRECTORIES(${MYLIB_SOURCE_DIR})
ADD_EXECUTABLE(test_mylib main.c)
IF(NOT MYLIB_LIB_TARGET)
MESSAGE(FATAL_ERROR "MYLIB_LIB_TARGET is not specified")
ENDIF()
TARGET_LINK_LIBRARIES(test_mylib ${MYLIB_LIB_TARGET})
ENDIF(MYLIB_BUILD_TEST)
In the parent:
ADD_SUBDIRECTORY("src")
ADD_SUBDIRECTORY("testpackage1")
ADD_SUBDIRECTORY("testpackage2")
What I get is: MYLIB_LIB_TARGET is not specified
from the unit test project
I think it's because the options are messing with the automatic dependency tree logic in CMake.