... | ... | @@ -26,18 +26,18 @@ for convenient, flexible reference to outside executables and libraries. |
|
|
Consider the following example which creates and uses an IMPORTED
|
|
|
executable
|
|
|
target:
|
|
|
|
|
|
add_executable(generator IMPORTED) # 1
|
|
|
set_property(TARGET generator PROPERTY IMPORTED_LOCATION "/path/to/some_generator") # 2
|
|
|
|
|
|
set(GENERATED_SRC ${CMAKE_CURRENT_BINARY_DIR}/generated.c)
|
|
|
add_custom_command(
|
|
|
OUTPUT ${GENERATED_SRC}
|
|
|
COMMAND generator ${GENERATED_SRC} # 3
|
|
|
)
|
|
|
|
|
|
add_executable(myexe src1.c src2.c ${GENERATED_SRC})
|
|
|
|
|
|
```cmake
|
|
|
add_executable(generator IMPORTED) # 1
|
|
|
set_property(TARGET generator PROPERTY IMPORTED_LOCATION "/path/to/some_generator") # 2
|
|
|
|
|
|
set(GENERATED_SRC ${CMAKE_CURRENT_BINARY_DIR}/generated.c)
|
|
|
add_custom_command(
|
|
|
OUTPUT ${GENERATED_SRC}
|
|
|
COMMAND generator ${GENERATED_SRC} # 3
|
|
|
)
|
|
|
|
|
|
add_executable(myexe src1.c src2.c ${GENERATED_SRC})
|
|
|
```
|
|
|
Line \#1 creates a new CMake target called "`generator`". Line \#2 tells
|
|
|
CMake the location of the file on disk to import. Line \#3 references
|
|
|
the target in a custom command. The generated build system will contain
|
... | ... | @@ -49,10 +49,12 @@ in the rule to generate the source file. |
|
|
|
|
|
Libraries may also be used through imported targets:
|
|
|
|
|
|
add_library(foo STATIC IMPORTED)
|
|
|
set_property(TARGET foo PROPERTY IMPORTED_LOCATION /path/to/libfoo.a)
|
|
|
add_executable(myexe src1.c src2.c)
|
|
|
target_link_libraries(myexe foo)
|
|
|
```cmake
|
|
|
add_library(foo STATIC IMPORTED)
|
|
|
set_property(TARGET foo PROPERTY IMPORTED_LOCATION /path/to/libfoo.a)
|
|
|
add_executable(myexe src1.c src2.c)
|
|
|
target_link_libraries(myexe foo)
|
|
|
```
|
|
|
|
|
|
The generated build system will contain a command line such as
|
|
|
|
... | ... | @@ -61,11 +63,13 @@ The generated build system will contain a command line such as |
|
|
in the rule to link `myexe`. On Windows a .dll and its .lib import
|
|
|
library may be imported together:
|
|
|
|
|
|
add_library(bar SHARED IMPORTED)
|
|
|
set_property(TARGET bar PROPERTY IMPORTED_LOCATION c:/path/to/bar.dll)
|
|
|
set_property(TARGET bar PROPERTY IMPORTED_IMPLIB c:/path/to/bar.lib)
|
|
|
add_executable(myexe src1.c src2.c)
|
|
|
target_link_libraries(myexe bar)
|
|
|
```cmake
|
|
|
add_library(bar SHARED IMPORTED)
|
|
|
set_property(TARGET bar PROPERTY IMPORTED_LOCATION c:/path/to/bar.dll)
|
|
|
set_property(TARGET bar PROPERTY IMPORTED_IMPLIB c:/path/to/bar.lib)
|
|
|
add_executable(myexe src1.c src2.c)
|
|
|
target_link_libraries(myexe bar)
|
|
|
```
|
|
|
|
|
|
The generated build system will contain a command line such as
|
|
|
|
... | ... | @@ -74,11 +78,13 @@ The generated build system will contain a command line such as |
|
|
in the rule to link `myexe`. A library with multiple configurations may
|
|
|
be imported with a single target:
|
|
|
|
|
|
add_library(foo STATIC IMPORTED)
|
|
|
set_property(TARGET foo PROPERTY IMPORTED_LOCATION_RELEASE c:/path/to/foo.lib)
|
|
|
set_property(TARGET foo PROPERTY IMPORTED_LOCATION_DEBUG c:/path/to/foo_d.lib)
|
|
|
add_executable(myexe src1.c src2.c)
|
|
|
target_link_libraries(myexe foo)
|
|
|
```cmake
|
|
|
add_library(foo STATIC IMPORTED)
|
|
|
set_property(TARGET foo PROPERTY IMPORTED_LOCATION_RELEASE c:/path/to/foo.lib)
|
|
|
set_property(TARGET foo PROPERTY IMPORTED_LOCATION_DEBUG c:/path/to/foo_d.lib)
|
|
|
add_executable(myexe src1.c src2.c)
|
|
|
target_link_libraries(myexe foo)
|
|
|
```
|
|
|
|
|
|
The generated build system will link `myexe` to `foo.lib` when it is
|
|
|
built in the release configuration and `foo_d.lib` when built in the
|
... | ... | @@ -88,11 +94,13 @@ In some cases, such as when using the result of a `find_library` command |
|
|
to create an imported target, the type of the library (static or shared)
|
|
|
is not known. As of CMake 2.6.2 one may specify this explicitly:
|
|
|
|
|
|
find_library(FOO_LIBRARY NAMES foo)
|
|
|
add_library(foo UNKNOWN IMPORTED)
|
|
|
set_property(TARGET foo PROPERTY IMPORTED_LOCATION "${FOO_LIBRARY}")
|
|
|
add_executable(myexe src1.c src2.c)
|
|
|
target_link_libraries(myexe foo)
|
|
|
```cmake
|
|
|
find_library(FOO_LIBRARY NAMES foo)
|
|
|
add_library(foo UNKNOWN IMPORTED)
|
|
|
set_property(TARGET foo PROPERTY IMPORTED_LOCATION "${FOO_LIBRARY}")
|
|
|
add_executable(myexe src1.c src2.c)
|
|
|
target_link_libraries(myexe foo)
|
|
|
```
|
|
|
|
|
|
In this case CMake does not know the library type, so it just puts the
|
|
|
library on the link line as-is. Therefore on Windows there is no special
|
... | ... | @@ -112,9 +120,11 @@ the target files also provides a file to help import them. |
|
|
The `install(TARGETS)` and `install(EXPORT)` commands work together to
|
|
|
install a target and a file to help import it. For example, the code
|
|
|
|
|
|
add_executable(generator generator.c)
|
|
|
install(TARGETS generator DESTINATION lib/myproj/generators EXPORT myproj-targets)
|
|
|
install(EXPORT myproj-targets DESTINATION lib/myproj)
|
|
|
```cmake
|
|
|
add_executable(generator generator.c)
|
|
|
install(TARGETS generator DESTINATION lib/myproj/generators EXPORT myproj-targets)
|
|
|
install(EXPORT myproj-targets DESTINATION lib/myproj)
|
|
|
```
|
|
|
|
|
|
installs the files
|
|
|
|
... | ... | @@ -123,22 +133,26 @@ installs the files |
|
|
|
|
|
The `myproj-targets.cmake` file contains code such as
|
|
|
|
|
|
add_executable(generator IMPORTED)
|
|
|
set_property(
|
|
|
TARGET generator
|
|
|
PROPERTY IMPORTED_LOCATION ${PREFIX}/lib/myproj/generators/generator
|
|
|
)
|
|
|
```cmake
|
|
|
add_executable(generator IMPORTED)
|
|
|
set_property(
|
|
|
TARGET generator
|
|
|
PROPERTY IMPORTED_LOCATION ${PREFIX}/lib/myproj/generators/generator
|
|
|
)
|
|
|
```
|
|
|
|
|
|
(in practice the `${PREFIX}` is computed relative to the file location
|
|
|
automatically). An outside project may now contain code such as
|
|
|
|
|
|
include(${PREFIX}/lib/myproj/myproj-targets.cmake) # 1
|
|
|
set(GENERATED_SRC ${CMAKE_CURRENT_BINARY_DIR}/generated.c)
|
|
|
add_custom_command(
|
|
|
OUTPUT ${GENERATED_SRC}
|
|
|
COMMAND generator ${GENERATED_SRC} # 2
|
|
|
)
|
|
|
add_executable(myexe src1.c src2.c ${GENERATED_SRC})
|
|
|
```cmake
|
|
|
include(${PREFIX}/lib/myproj/myproj-targets.cmake) # 1
|
|
|
set(GENERATED_SRC ${CMAKE_CURRENT_BINARY_DIR}/generated.c)
|
|
|
add_custom_command(
|
|
|
OUTPUT ${GENERATED_SRC}
|
|
|
COMMAND generator ${GENERATED_SRC} # 2
|
|
|
)
|
|
|
add_executable(myexe src1.c src2.c ${GENERATED_SRC})
|
|
|
```
|
|
|
|
|
|
Line \#1 loads the target import script. The script may import any
|
|
|
number of targets. Their locations are computed relative to the script
|
... | ... | @@ -150,16 +164,20 @@ executable from its installed location. |
|
|
|
|
|
Libraries may also be exported and imported:
|
|
|
|
|
|
add_library(foo STATIC foo1.c)
|
|
|
install(TARGETS foo DESTINATION lib EXPORT myproj-targets)
|
|
|
install(EXPORT myproj-targets DESTINATION lib/myproj)
|
|
|
```cmake
|
|
|
add_library(foo STATIC foo1.c)
|
|
|
install(TARGETS foo DESTINATION lib EXPORT myproj-targets)
|
|
|
install(EXPORT myproj-targets DESTINATION lib/myproj)
|
|
|
```
|
|
|
|
|
|
This installs the library and an import file referencing it. Outside
|
|
|
projects may simply write
|
|
|
|
|
|
include(${PREFIX}/lib/myproj/myproj-targets.cmake)
|
|
|
add_executable(myexe src1.c)
|
|
|
target_link_libraries(myexe foo)
|
|
|
```cmake
|
|
|
include(${PREFIX}/lib/myproj/myproj-targets.cmake)
|
|
|
add_executable(myexe src1.c)
|
|
|
target_link_libraries(myexe foo)
|
|
|
```
|
|
|
|
|
|
and the executable will be linked to the library `foo` exported and
|
|
|
installed by the original project.
|
... | ... | @@ -171,18 +189,20 @@ command is needed to install an import file that references all targets. |
|
|
Both of the examples above may be combined into a single export, even if
|
|
|
they are in different subdirectories of the project:
|
|
|
|
|
|
# A/CMakeLists.txt
|
|
|
add_executable(generator generator.c)
|
|
|
install(TARGETS generator DESTINATION lib/myproj/generators EXPORT myproj-targets)
|
|
|
```cmake
|
|
|
# A/CMakeLists.txt
|
|
|
add_executable(generator generator.c)
|
|
|
install(TARGETS generator DESTINATION lib/myproj/generators EXPORT myproj-targets)
|
|
|
|
|
|
# B/CMakeLists.txt
|
|
|
add_library(foo STATIC foo1.c)
|
|
|
install(TARGETS foo DESTINATION lib EXPORT myproj-targets)
|
|
|
# B/CMakeLists.txt
|
|
|
add_library(foo STATIC foo1.c)
|
|
|
install(TARGETS foo DESTINATION lib EXPORT myproj-targets)
|
|
|
|
|
|
# Top CMakeLists.txt
|
|
|
add_subdirectory(A)
|
|
|
add_subdirectory(B)
|
|
|
install(EXPORT myproj-targets DESTINATION lib/myproj)
|
|
|
# Top CMakeLists.txt
|
|
|
add_subdirectory(A)
|
|
|
add_subdirectory(B)
|
|
|
install(EXPORT myproj-targets DESTINATION lib/myproj)
|
|
|
```
|
|
|
|
|
|
## Exporting from a Build Tree
|
|
|
|
... | ... | @@ -194,14 +214,18 @@ project that references the build tree with no installation involved. |
|
|
The `export` command is used to generate a file exporting targets from a
|
|
|
project build tree. For example, the code
|
|
|
|
|
|
add_executable(generator generator.c)
|
|
|
export(TARGETS generator FILE myproj-exports.cmake)
|
|
|
```cmake
|
|
|
add_executable(generator generator.c)
|
|
|
export(TARGETS generator FILE myproj-exports.cmake)
|
|
|
```
|
|
|
|
|
|
will create a file in the project build tree called
|
|
|
`myproj-exports.cmake` that contains code such as
|
|
|
|
|
|
add_executable(generator IMPORTED)
|
|
|
set_property(TARGET generator PROPERTY IMPORTED_LOCATION "/path/to/build/tree/generator")
|
|
|
```cmake
|
|
|
add_executable(generator IMPORTED)
|
|
|
set_property(TARGET generator PROPERTY IMPORTED_LOCATION "/path/to/build/tree/generator")
|
|
|
```
|
|
|
|
|
|
This file may be loaded by an outside project that is aware of the
|
|
|
project build tree in order to use the executable to generate a source
|
... | ... | |