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