1+ # Scan for all source files in the current or child directories
12file (GLOB_RECURSE sources "*.cpp" )
3+ # Scan for all header files in the current or child directories (necessary to install them later)
24file (GLOB_RECURSE headers "*.h" )
35
46# This step builds the API in the form of a statically linked library
@@ -7,13 +9,22 @@ add_library(cprover-api-cpp ${sources})
79# Being a library we should include them privately, but for now fair enough
810generic_includes(cprover-api-cpp)
911
12+ # Add all the current and the installed `include` directories as a PUBLIC header location
1013target_include_directories (cprover-api-cpp PUBLIC
1114 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >"
1215 "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR} >"
1316 "$<INSTALL_INTERFACE:include>" )
1417
18+ # To create a full static API library we need to archive together the content of all the other
19+ # module libraries we depend on. To do this we will use the `ar` command to unpack the other module
20+ # static libraries and append to the current library `.a` file.
21+
22+ # Get the dependency targets of the `solver` target (see `../solver/CMakeLists.txt`), so that they
23+ # are merged to the final library too. (such dependencies are not known statically as the selection
24+ # of the SAT backend is left to the building user.
1525get_target_property (LIBRARY_DEPENDENCIES solvers LINK_LIBRARIES )
1626
27+ # Get all the dependency targets we know statically.
1728list (APPEND
1829 LIBRARY_DEPENDENCIES
1930 "goto-programs"
@@ -36,30 +47,41 @@ list(APPEND
3647 "pointer-analysis"
3748 "cbmc-lib" )
3849
50+ # Remove possible duplicate library targets
3951list (REMOVE_DUPLICATES LIBRARY_DEPENDENCIES)
4052
53+ # Add all the dependency targets as dependencies of `cprover-api-cpp`
4154target_link_libraries (cprover-api-cpp
4255 PRIVATE
4356 ${LIBRARY_DEPENDENCIES} )
4457
58+ # To be able to invoke `ar` on the dependencies we need the paths of the libraries `.a` files.
59+ # Ths is done by using the cmake generator `$<TARGET_FILE:dependency>`, that in turn will be
60+ # substituted with the absolute path of the `dependency` output file (a `.a` file in this case).
61+ # Here we prepare a space-separated list of cmake generators that will resolved in absolute paths.
4562set (DEPENDENCY_TARGETS "" )
4663foreach (dep ${LIBRARY_DEPENDENCIES} )
4764 list (APPEND DEPENDENCY_TARGETS "$<TARGET_FILE:${dep} >" )
4865endforeach (dep LIBRARY_DEPENDENCIES)
49-
5066string (REPLACE ";" " " DEPENDENCY_TARGETS "${DEPENDENCY_TARGETS} " )
5167
68+ # To aggregate all the dependencies into a final `.a` file we add a custom pass after target
69+ # `cprover-api-cpp` has been built where the `aggregate_dependencies.sh` script is run with the `ar`
70+ # command, the destination library and the dependencies paths
5271add_custom_command (TARGET cprover-api-cpp POST_BUILD
5372 COMMAND "${CMAKE_CURRENT_SOURCE_DIR} /add_dependencies.sh" "${CMAKE_AR} " "$<TARGET_FILE:cprover-api-cpp>" "${DEPENDENCY_TARGETS} "
5473 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR} " )
5574
75+ # Set the properties of `cprover-api-cpp`. Mainly the output name `libcprover.<version>.a`, its
76+ # version (CBMC version) and the list of public headers to be installed
5677set_target_properties (cprover-api-cpp
5778 PROPERTIES
5879 OUTPUT_NAME "cprover.${CMAKE_PROJECT_VERSION} " # libcprover.<version>.a
5980 SOVERSION "${CMAKE_PROJECT_VERSION} "
6081 PUBLIC_HEADER "${headers} "
6182 )
6283
84+ # Install the target as usual in `lib` the library and in `include/cprover` the public headers.
6385install (TARGETS cprover-api-cpp
6486 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR} "
6587 COMPONENT lib
0 commit comments