Debian Codegraph @

cmake/tribits/examples/TribitsExampleProject/packages/wrap_external/CMakeLists.txt

raw
1 #
2 # Package: WrapExternal
3 #
4
5 # The purpose of this package is to demonstrate how to wrap an externally
6 # configured and built piece of software. The external software once built
7 # provides libraries that need to be cast as TriBITS CMake libraries. To
8 # make this even more interesting, the external software has dependencies on
9 # upstream TriBITS packages.
10
11 tribits_package(WrapExternal)
12
13 #
14 # A) Determine if any downstream changes require a rebuild and deal with it
15 #
16
17 include(TribitsFindMostRecentFileTimestamp)
18
19 advanced_set(${PACKAGE_NAME}_SHOW_MOST_RECENT_FILES FALSE
20 CACHE BOOL
21 "Set to TRUE to see most recent modified files.")
22 if (${PACKAGE_NAME}_SHOW_MOST_RECENT_FILES)
23 set(SHOW_MOST_RECENT_FILES_ARG SHOW_MOST_RECENT_FILES)
24 else()
25 set(SHOW_MOST_RECENT_FILES_ARG)
26 endif()
27
28 tribits_determine_if_current_package_needs_rebuilt(
29 ${SHOW_MOST_RECENT_FILES_ARG}
30 SHOW_OVERALL_MOST_RECENT_FILES
31 CURRENT_PACKAGE_OUT_OF_DATE_OUT ${PACKAGE_NAME}_BULID_IS_OUT_OF_DATE
32 )
33
34 # If there is anything out of date, then just blow away the external build
35 if (${PACKAGE_NAME}_BULID_IS_OUT_OF_DATE)
36 message(
37 "\nBlowing away ${PACKAGE_NAME} build dir external_func/ so it will build from scratch!"
38 )
39 execute_process(
40 COMMAND rm -rf "${PACKAGE_BINARY_DIR}/external_func/"
41 )
42 endif()
43 # NOTE: There may be other ways to address the out of date build other than to
44 # just blow the whole thing away but some external packages can be pretty
45 # complex and the only safe way to ensure that the rebuild is correct is to
46 # start from scratch.
47
48 #
49 # B) Get the arguments for the configure of external_func
50 #
51
52 string(TOUPPER "${CMAKE_BUILD_TYPE}" upperBuildType)
53
54 get_target_property(simpleCxx_IncludeDirs
55 SimpleCxx::simplecxx INTERFACE_INCLUDE_DIRECTORIES)
56
57 get_target_property(withSubpackagesA_IncludeDirs
58 WithSubpackagesA::pws_a INTERFACE_INCLUDE_DIRECTORIES)
59
60 set(includeDirsList ${withSubpackagesA_IncludeDirs} ${simpleCxx_IncludeDirs})
61
62 if (${PACKAGE_NAME}_ENABLE_MixedLang)
63 get_target_property(mixedLang_IncludeDirs
64 MixedLang::mixedlang INTERFACE_INCLUDE_DIRECTORIES)
65 list(PREPEND includeDirsList ${mixedLang_IncludeDirs})
66 endif()
67 list(JOIN includeDirsList " -I" includeDirsCompileOptions)
68 set(includeDirsCompileOptions "-I${includeDirsCompileOptions}")
69
70 #
71 # C) Do configuration of the external project as a build target
72 #
73
74 set(EXTERNAL_FUNC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external_func)
75 set(EXTERNAL_FUNC_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/external_func)
76 set(EXTERNAL_FUNC_LIB_FILE ${EXTERNAL_FUNC_BINARY_DIR}/libexternal_func.a)
77 set(EXTERNAL_FUNC_MAKEFILE ${EXTERNAL_FUNC_BINARY_DIR}/Makefile)
78
79 file(MAKE_DIRECTORY ${EXTERNAL_FUNC_BINARY_DIR})
80
81 add_custom_command(
82 OUTPUT ${EXTERNAL_FUNC_MAKEFILE}
83 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
84 ${EXTERNAL_FUNC_SOURCE_DIR}/configure.py
85 COMMAND ${Python3_EXECUTABLE} ${EXTERNAL_FUNC_SOURCE_DIR}/configure.py
86 "--cxx=${CMAKE_CXX_COMPILER}"
87 "--cxx-flags=${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${upperBuildType}}"
88 "--ar=${CMAKE_AR}"
89 "--include-dirs=${includeDirsCompileOptions}"
90 "--src-dir=${EXTERNAL_FUNC_SOURCE_DIR}"
91 "--build-dir=${EXTERNAL_FUNC_BINARY_DIR}"
92 )
93
94 # NOTE: Above, we have to run the configure.py script at build time after
95 # generation because ${includeDirsCompileOptions} contains generation
96 # expressions that are evaluated at generation time by the
97 # add_custom_command() call.
98
99 #
100 # D) Define a custom build rule and target to create external_func library
101 #
102
103 add_custom_command(
104 OUTPUT ${EXTERNAL_FUNC_LIB_FILE}
105 DEPENDS ${EXTERNAL_FUNC_SOURCE_DIR}/external_func.hpp
106 ${EXTERNAL_FUNC_SOURCE_DIR}/external_func.cpp
107 ${EXTERNAL_FUNC_MAKEFILE}
108 COMMAND make ${CTEST_BUILD_FLAGS}
109 WORKING_DIRECTORY ${EXTERNAL_FUNC_BINARY_DIR}
110 )
111 add_custom_target( build_external_func
112 DEPENDS ${EXTERNAL_FUNC_LIB_FILE} )
113 # NOTE: You have to create a custom target associated with the generated
114 # library. You can't just use the custom command.
115 # NOTE: Above we list the source and header files that we know that if changed
116 # should trigger a rebuild of the library. We would not likely know this with
117 # a more complex external project.
118
119 #
120 # E) Define the imported external library
121 #
122 # Below, does what tribits_add_imported_library() would do automatically!
123
124 # E.1) Create an imported library target and set up the dependencies to it
125 # custom target and command.
126 add_library(external_func STATIC IMPORTED GLOBAL)
127 set_property(TARGET external_func PROPERTY IMPORTED_LOCATION
128 ${EXTERNAL_FUNC_LIB_FILE})
129 add_dependencies(external_func build_external_func)
130 # NOTE: Above, you have to use the custom target associated with the
131 # generation command and add it as a dependency of the imported library
132 # target.
133
134 # E.2) Make sure before we build the external library, we first build the
135 # libraries.
136 add_dependencies(build_external_func WithSubpackagesA::pws_a)
137 # NOTE: You have to put the lib dependencies on build target, not the imported
138 # library target!
139
140 # E.3) Update the TriBITS variables
141 append_set(${PACKAGE_NAME}_LIB_TARGETS external_func)
142 global_set(${PACKAGE_NAME}_LIBRARIES external_func WithSubpackagesA::pws_a)
143 global_set(${PACKAGE_NAME}_HAS_NATIVE_LIBRARIES ON)
144 tribits_include_directories(${EXTERNAL_FUNC_SOURCE_DIR})
145 # NOTE: Above, you have to add the upstream dependent libraries to the current
146 # package's list of libraries because you can't link to an importing lib with
147 # link_target_libraries() :-(
148
149 #
150 # F) Add an executable and test to show that it works!
151 #
152
153 if (${PACKAGE_NAME}_ENABLE_TESTS)
154
155 tribits_add_executable_and_test(run_external_func
156 SOURCES run_external_func.cpp
157 PASS_REGULAR_EXPRESSION "external_func A ${EXPECTED_SIMPLECXX_AND_DEPS}"
158 NUM_MPI_PROCS 1
159 )
160
161 endif()
162
163 # Run this if you need to ensure that the external library is build before you
164 # build this object file!
165 #set_source_files_properties(
166 # run_external_func.cpp
167 # PROPERTIES OBJECT_DEPENDS ${EXTERNAL_FUNC_LIB_FILE}
168 # )
169
170 # Add a target to clean up the external function
171 add_custom_target( clean_external_func
172 COMMAND rm ${EXTERNAL_FUNC_LIB_FILE})
173 # NOTE: that we really only need to remove the final library. The external build
174 # command will automatically know if the object file needs to be updated as
175 # well.
176
177 tribits_package_postprocess()
178
179 # LocalWords: WrapExternal TriBITS CMake makefile TPLs