Allow CMake files for dependencies to be included multiple times (#639)
[dyninst.git] / CMakeLists.txt
1 cmake_minimum_required (VERSION 3.4.0)
2 project (Dyninst)
3
4 set (DYNINST_ROOT ${PROJECT_SOURCE_DIR})
5 set (CMAKE_SKIP_BUILD_RPATH FALSE)
6 set (CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
7 set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
8 set (CMAKE_EXPORT_COMPILE_COMMANDS ON)
9 LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES 
10           "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
11 IF("${isSystemDir}" STREQUAL "-1")
12                     set (CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
13 ENDIF()
14
15 set(RT_SOURCE_DIR ${DYNINST_ROOT}/dyninstAPI_RT)
16 set(RT_BINARY_DIR ${PROJECT_BINARY_DIR}/dyninstAPI_RT)
17
18 set (CMAKE_MODULE_PATH "${DYNINST_ROOT}/cmake" "${DYNINST_ROOT}/cmake/Modules" ${CMAKE_MODULE_PATH})
19
20 find_package(ThreadDB)
21 find_package(Threads)
22 include(Boost)
23 include(ThreadingBuildingBlocks)
24 include(ElfUtils)
25 include(LibIberty)
26 include (${DYNINST_ROOT}/cmake/shared.cmake)
27
28 configure_file(cmake/version.h.in common/h/dyninstversion.h)
29 include_directories(${PROJECT_BINARY_DIR})
30 include_directories(${PROJECT_BINARY_DIR}/common/h)
31 set (HEADER_DIRS common
32     dataflowAPI
33     dyninstAPI
34     instructionAPI
35     parseAPI
36     patchAPI
37     proccontrol
38     stackwalk
39     symtabAPI
40     )
41 if(NOT ${PLATFORM} MATCHES nt)
42   set (HEADER_DIRS ${HEADER_DIRS}
43     dwarf
44     elf
45     symlite
46     )
47 endif()
48
49
50
51 foreach (dir ${HEADER_DIRS})
52   include_directories ( ${DYNINST_ROOT}/${dir}/h )
53 endforeach()
54
55
56 include_directories (
57   ${DYNINST_ROOT}
58   ${DYNINST_ROOT}/external
59   ${CILKSCREEN_INCLUDES}
60       ${TBB_INCLUDE_DIRS}
61   )
62
63 # Component time
64 add_subdirectory (common)
65 if(NOT ${PLATFORM} MATCHES nt)
66   add_subdirectory (elf)
67   add_subdirectory (dwarf)
68   add_subdirectory (symlite)
69   add_subdirectory (parseThat)
70 endif()
71 add_subdirectory (instructionAPI)
72 add_subdirectory (symtabAPI)
73 add_subdirectory (parseAPI)
74 add_subdirectory (proccontrol)
75 add_subdirectory (stackwalk)
76 add_subdirectory (patchAPI)
77 add_subdirectory(examples)
78 if(${SYMREADER} MATCHES symtabAPI)
79   add_subdirectory (dyninstAPI)
80   add_subdirectory (dynC_API)
81 endif()
82
83 if(BUILD_RTLIB)
84   # Build the RT library as a separate project so we can change compilers
85   message(STATUS "Configuring DyninstAPI_RT in ${RT_BINARY_DIR}")
86   file(REMOVE_RECURSE ${RT_BINARY_DIR}/CMakeCache.txt ${RT_BINARY_DIR}/CMakeFiles ${RT_BINARY_DIR}/Makefile)
87   file(MAKE_DIRECTORY ${RT_BINARY_DIR})
88   if (PLATFORM MATCHES bgq)
89     set (RT_C_COMPILER mpicc CACHE STRING "Compiler for runtime library")
90   else()
91     set (RT_C_COMPILER ${CMAKE_C_COMPILER} CACHE STRING "Compiler for runtime library")
92   endif()
93   set (ENABLE_STATIC_LIBS NO CACHE STRING "Build static libraries as well?")
94   message(STATUS "Configuring RT library")
95
96   execute_process(WORKING_DIRECTORY ${RT_BINARY_DIR}
97     COMMAND ${CMAKE_COMMAND}
98     -DCMAKE_C_COMPILER=${RT_C_COMPILER}
99     -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
100     -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
101     -DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
102     -DINSTALL_LIB_DIR=${INSTALL_LIB_DIR}
103     -DINSTALL_INCLUDE_DIR=${INSTALL_INCLUDE_DIR}
104     -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
105     -DBUILD_RTLIB_32=${BUILD_RTLIB_32}
106     -DPLATFORM=${PLATFORM}
107     -G ${CMAKE_GENERATOR}
108     ${RT_SOURCE_DIR})
109   find_file(${RT_MAKEFILE} Makefile PATHS ${RT_BINARY_DIR} NO_DEFAULT_PATH)
110   message(STATUS "RTlib Makefile: ${RT_MAKEFILE}")
111   if(MSVC)
112       include_external_msproject(DyninstAPI_RT dyninstAPI_RT/dyninstAPI_RT.vcxproj)
113           include_external_msproject(DyninstAPI_RT_static dyninstAPI_RT/dyninstAPI_RT_static.vcxproj)
114   else()
115     add_custom_target(DyninstRT
116       ALL
117       $(MAKE)
118       WORKING_DIRECTORY ${RT_BINARY_DIR}
119       COMMENT "Building DyninstRT")
120     if(TARGET TBB)
121       add_dependencies(DyninstRT TBB)
122     endif()
123     add_dependencies(dyninstAPI DyninstRT)
124     if(TARGET dyninstAPI-static)
125       add_dependencies(dyninstAPI-static DyninstRT)
126     endif()
127     if(TARGET boost)
128       add_dependencies(DyninstRT boost)
129     endif()
130   endif()
131
132   install(SCRIPT "${RT_BINARY_DIR}/cmake_install.cmake")
133
134 else()
135   message(STATUS "Skipping DyninstAPI_RT. Be sure to build this library if you're using instrumentation.")
136 endif()
137 set (VERSION_STRING "${DYNINST_MAJOR_VERSION}.${DYNINST_MINOR_VERSION}.${DYNINST_PATCH_VERSION}")
138 set (DYNINST_NAME "DyninstAPI-${VERSION_STRING}")
139
140 if(BUILD_TARBALLS)
141   find_package(Git)
142   if(GIT_FOUND)
143     if(EXISTS "${DYNINST_ROOT}/.git/")
144       message(STATUS "Source tree is repository, building archive target")
145       add_custom_target(package ALL)
146       add_custom_command(TARGET package
147         COMMAND ${GIT_EXECUTABLE} archive --prefix="${DYNINST_NAME}/" --format=tar.gz -o "${CMAKE_BINARY_DIR}/${DYNINST_NAME}.tgz" HEAD
148         WORKING_DIRECTORY ${DYNINST_ROOT}
149         COMMENT "Packaging Dyninst")
150     endif()
151   endif()
152 endif()
153 if(BUILD_DOCS)
154   add_custom_target(doc)
155   set(LATEX_DOCS dynC_API dataflowAPI instructionAPI parseAPI patchAPI symtabAPI stackwalk)
156   set(WORD_DOCS proccontrol dyninstAPI)
157   add_custom_target(proccontrol-doc
158     DEPENDS ${CMAKE_SOURCE_DIR}/proccontrol/doc/proccontrol.pdf)
159   add_custom_target(dyninstAPI-doc
160     DEPENDS ${CMAKE_SOURCE_DIR}/dyninstAPI/doc/dyninstAPI.pdf)
161   foreach(COMPONENT ${WORD_DOCS})
162     add_dependencies(doc ${COMPONENT}-doc)
163     set_target_properties(${COMPONENT}-doc PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
164     install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${COMPONENT}/doc/${COMPONENT}.pdf
165       DESTINATION ${INSTALL_DOC_DIR}
166       RENAME ${COMPONENT}-${VERSION_STRING}.pdf
167       OPTIONAL
168       )
169   endforeach()
170
171   find_package(LATEX)
172   if(PDFLATEX_COMPILER)
173     file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/common/doc
174       DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/common)
175     foreach(COMPONENT ${LATEX_DOCS})
176       file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${COMPONENT}/doc
177         DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${COMPONENT})
178       set (DEPS "")
179       file(GLOB_RECURSE DEPS ${CMAKE_CURRENT_SOURCE_DIR}/${COMPONENT}/doc/*.tex ${CMAKE_CURRENT_SOURCE_DIR}/common/doc/*.tex)
180       add_custom_command(
181         OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${COMPONENT}/doc/${COMPONENT}.aux
182         DEPENDS ${DEPS}
183         COMMAND ${PDFLATEX_COMPILER}
184         ARGS -interaction=batchmode ${COMPONENT}.tex
185         WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${COMPONENT}/doc
186         COMMENT "Latex (first pass)"
187         )
188       add_custom_command(
189         OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${COMPONENT}/doc/${COMPONENT}.log
190         DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${COMPONENT}/doc/${COMPONENT}.aux
191         COMMAND ${PDFLATEX_COMPILER}
192         ARGS -interaction=batchmode ${COMPONENT}.tex
193         WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${COMPONENT}/doc
194         COMMENT "Latex (second pass)"
195         )
196       add_custom_target(${COMPONENT}-doc echo
197         DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${COMPONENT}/doc/${COMPONENT}.log
198         )
199       add_dependencies(doc ${COMPONENT}-doc)
200       set_target_properties(${COMPONENT}-doc PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
201       install(FILES  ${CMAKE_CURRENT_BINARY_DIR}/${COMPONENT}/doc/${COMPONENT}.pdf
202         DESTINATION ${INSTALL_DOC_DIR}
203         RENAME ${COMPONENT}-${VERSION_STRING}.pdf
204         OPTIONAL
205         )
206     endforeach()
207   else()
208     message(STATUS "LaTeX not found")
209   endif()
210 endif()
211
212 if(UNIX)
213   message(STATUS "Adding Unix-specific dependencies")
214   if(NOT USE_GNU_DEMANGLER)
215     add_dependencies(common libiberty_imp)
216   endif()
217 endif()
218
219 if(TARGET boost)
220     add_dependencies(common boost)
221 endif()
222
223 #add_subdirectory(testsuite)
224
225 # Copy out all of the cmake files so they can be used by the Testsuite
226 install(DIRECTORY ${DYNINST_ROOT}/cmake/ DESTINATION ${INSTALL_CMAKE_DIR})
227 install(FILES ${PROJECT_BINARY_DIR}/CMakeCache.txt DESTINATION ${INSTALL_CMAKE_DIR})