我有一个作为我的项目的一部分构建并链接到的库。我想提供设施,可以选择在系统范围内安装库(或设置 ${CMAKE_INSTALL_PREFIX} 的任何位置)。否则,默认情况下,项目的最终构建产品将静态链接到库,并安装前者,但库二进制文件保留在构建目录中。
换句话说:
$ make
$ make install
将构建和安装程序,但仅类似于
$ make install.foo
将库安装到 ${CMAKE_INSTALL_PREFIX},如果需要,首先构建它。
到目前为止,我有类似的东西(从实际脚本中简化,所以可能会有错误):
INCLUDE_DIRECTORIES( "${CMAKE_CURRENT_LIST_DIR}")
SET (FOO_LIBRARY "foo")
# Following builds library and makes it available to
# be linked other targets within project by:
# TARGET_LINK_LIBRARIES(${progname} ${FOO_LIBRARY})
ADD_LIBRARY(${FOO_LIBRARY}
foo/foo.cpp # and other sources ...
)
###########################################################
# Approach #1
# -----------
# Optionally allow users to install it by invoking:
#
# cmake .. -DINSTALL_FOO="yes"
#
# This works, but it means that users will have to run
# cmake again to switch back and forth between the libary
# installation and non-library installation.
#
OPTION(INSTALL_FOO "Install foo" OFF)
IF (INSTALL_FOO)
INSTALL(TARGETS ${FOO_LIBRARY} DESTINATION lib/foo)
SET(FOO_HEADERS foo/foo.h)
INSTALL(FILES ${FOO_HEADERS} DESTINATION include/foo)
UNSET(INSTALL_FOO CACHE)
ENDIF()
###########################################################
###########################################################
# Approach #2
# -----------
# Optionally allow users to install it by invoking:
#
# make install.foo
#
# Unfortunately, this gets installed by "make install",
# which I want to avoid
SET(FOO_INSTALL "install.foo")
ADD_CUSTOM_TARGET(${FOO_INSTALL}
COMMAND ${CMAKE_COMMAND}
-D COMPONENT=foo
-P cmake_install.cmake)
ADD_DEPENDENCIES(${FOO_INSTALL} ${FOO_LIBRARY})
INSTALL(TARGETS ${FOO_LIBRRARY}
DESTINATION lib/foo COMPONENT foo)
SET(FOO_HEADERS foo/foo.h)
INSTALL(FILES ${FOO_HEADERS}
DESTINATION include/foo COMPONENT foo)
###########################################################
可以看出,方法#1 的工作,但安装库所需的步骤是:
$ cmake .. -DINSTALL_FOO="yes"
$ make && make install
然后,要回到“正常”构建,用户必须记住在没有“-DINSTALL_FOO”选项的情况下再次运行 cmake,否则该库将在下一次“make install”时安装。
第二种方法在我运行“make install.foo”时有效,但如果我运行“make install”,它也会安装库。我想避免后者。
有人对如何实现这一目标有任何建议吗?