Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e0357bf6e4e617b04c604de4e7e69520f97111eb
[simgrid.git] / tools / cmake / MakeLib.cmake
1 ### Make Libs
2
3 # On macOS, specify that rpath is useful to look for the dependencies
4 # See https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling and Java.cmake
5 # TODO: is it still useful now that Java is gone? For Python maybe?
6 set(CMAKE_MACOSX_RPATH TRUE)
7 if(APPLE)
8   SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # When installed, use system path
9   set(CMAKE_SKIP_BUILD_RPATH FALSE)         # When executing from build tree, take the lib from the build path if exists
10   set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) # When executing from build tree, take the lib from the system path if exists
11
12   # add the current location of libsimgrid-java.dynlib as a location for libsimgrid.dynlib
13   # (useful when unpacking the native libraries from the jarfile)
14   set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")
15 endif()
16
17 ###############################
18 # Declare the library content #
19 ###############################
20
21 # Actually declare our libraries
22 add_library(simgrid SHARED ${simgrid_sources})
23 set_target_properties(simgrid PROPERTIES VERSION ${libsimgrid_version})
24 # The library can obviously use the internal headers
25 set_property(TARGET simgrid
26              APPEND PROPERTY INCLUDE_DIRECTORIES "${INTERNAL_INCLUDES}")
27
28 add_dependencies(simgrid maintainer_files)
29
30 if("${CMAKE_SYSTEM}" MATCHES "Linux")
31   add_library(sthread SHARED ${STHREAD_SRC})
32   set_target_properties(sthread PROPERTIES VERSION ${libsimgrid_version})
33   set_property(TARGET sthread
34                 APPEND PROPERTY INCLUDE_DIRECTORIES "${INTERNAL_INCLUDES}")
35   target_link_libraries(sthread simgrid)
36 else()
37   set(EXTRA_DIST ${EXTRA_DIST} ${STHREAD_SRC})
38 endif()
39
40 if(HAVE_MMALLOC)
41   add_library(sgmalloc SHARED ${SGMALLOC_SRC})
42   set_property(TARGET sgmalloc
43                 APPEND PROPERTY INCLUDE_DIRECTORIES "${INTERNAL_INCLUDES}")
44 endif()
45
46 if(SIMGRID_HAVE_MC)
47   add_executable(simgrid-mc ${MC_SIMGRID_MC_SRC})
48   target_link_libraries(simgrid-mc simgrid)
49   set_target_properties(simgrid-mc
50                         PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
51   set_property(TARGET simgrid-mc
52                 APPEND PROPERTY INCLUDE_DIRECTORIES "${INTERNAL_INCLUDES}")
53   install(TARGETS simgrid-mc # install that binary without breaking the rpath on Mac
54     RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}/)
55   add_dependencies(tests-mc simgrid-mc)
56   if("${CMAKE_SYSTEM}" MATCHES "Linux")
57     add_dependencies(tests-mc sthread)
58   endif()
59 endif()
60
61 # Compute the dependencies of SimGrid
62 #####################################
63 # search for dlopen
64 if("${CMAKE_SYSTEM_NAME}" MATCHES "kFreeBSD|Linux|SunOS")
65   find_library(DL_LIBRARY dl)
66 endif()
67 mark_as_advanced(DL_LIBRARY)
68
69 if (HAVE_BOOST_CONTEXTS)
70   target_link_libraries(simgrid ${Boost_CONTEXT_LIBRARY})
71 endif()
72
73 if (HAVE_BOOST_STACKTRACE_BACKTRACE)
74   target_link_libraries(simgrid ${Boost_STACKTRACE_BACKTRACE_LIBRARY})
75 endif()
76
77 if (HAVE_BOOST_ADDR2LINE_BACKTRACE)
78   target_link_libraries(simgrid ${Boost_STACKTRACE_ADDR2LINE_LIBRARY})
79 endif()
80
81 if(CMAKE_USE_PTHREADS_INIT)
82   target_link_libraries(simgrid ${CMAKE_THREAD_LIBS_INIT})
83 endif()
84
85 if(HAVE_PAPI)
86   SET(SIMGRID_DEP "${SIMGRID_DEP} -lpapi")
87 endif()
88
89 if(HAVE_GRAPHVIZ)
90   if(HAVE_CGRAPH_LIB)
91     SET(SIMGRID_DEP "${SIMGRID_DEP} -lcgraph")
92   else()
93     if(HAVE_AGRAPH_LIB)
94       SET(SIMGRID_DEP "${SIMGRID_DEP} -lagraph -lcdt")
95     endif()
96   endif()
97 endif()
98
99 if(SIMGRID_HAVE_JSON)
100   target_link_libraries(simgrid  nlohmann_json::nlohmann_json)
101 endif()
102
103 if(NOT ${DL_LIBRARY} STREQUAL "")
104   SET(SIMGRID_DEP "${SIMGRID_DEP} ${DL_LIBRARY}")
105 endif()
106
107 if(HAVE_POSIX_GETTIME)
108   SET(SIMGRID_DEP "${SIMGRID_DEP} -lrt")
109 endif()
110
111 if("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
112   set(SIMGRID_DEP "${SIMGRID_DEP} -lprocstat")
113 endif()
114
115 # Compute the dependencies of SMPI
116 ##################################
117
118 if(enable_smpi)
119   add_executable(smpimain src/smpi/smpi_main.c)
120   target_link_libraries(smpimain simgrid)
121   set_target_properties(smpimain
122     PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/simgrid)
123   install(TARGETS smpimain # install that binary without breaking the rpath on Mac
124     RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}/simgrid)
125   add_dependencies(tests smpimain)
126
127   add_executable(smpireplaymain src/smpi/smpi_replay_main.cpp)
128   target_compile_options(smpireplaymain PRIVATE -fpic)
129   target_link_libraries(smpireplaymain simgrid -fpic -shared)
130   set_target_properties(smpireplaymain
131     PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/simgrid)
132   install(TARGETS smpireplaymain # install that binary without breaking the rpath on Mac
133     RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}/simgrid)
134   add_dependencies(tests smpireplaymain)
135
136   if(SMPI_FORTRAN)
137     if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
138       SET(SIMGRID_DEP "${SIMGRID_DEP} -lgfortran")
139     elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
140       SET(SIMGRID_DEP "${SIMGRID_DEP} -lifcore")
141     elseif(CMAKE_Fortran_COMPILER_ID MATCHES "PGI|Flang")
142       SET(SIMGRID_DEP "${SIMGRID_DEP} -lflang")
143       if("${CMAKE_SYSTEM}" MATCHES "FreeBSD")
144         set(SIMGRID_DEP "${SIMGRID_DEP} -lexecinfo")
145         if ("${CMAKE_SYSTEM_VERSION}" STRGREATER_EQUAL "12")
146             set(SIMGRID_DEP "${SIMGRID_DEP} -lpgmath")
147         endif()
148         if ("${CMAKE_SYSTEM_VERSION}" MATCHES "12\\.1")
149             set(SIMGRID_DEP "${SIMGRID_DEP} -lomp")
150         endif()
151       endif()
152     endif()
153   endif()
154
155 endif()
156
157 if(enable_smpi AND APPLE)
158   set(SIMGRID_DEP "${SIMGRID_DEP} -Wl,-U -Wl,_smpi_simulated_main")
159 endif()
160
161 # See https://github.com/HewlettPackard/foedus_code/blob/master/foedus-core/cmake/FindGccAtomic.cmake
162 FIND_LIBRARY(GCCLIBATOMIC_LIBRARY NAMES atomic atomic.so.1 libatomic.so.1
163   HINTS
164     $ENV{HOME}/local/lib64
165     $ENV{HOME}/local/lib
166     /usr/local/lib64
167     /usr/local/lib
168     /opt/local/lib64
169     /opt/local/lib
170     /usr/lib64
171     /usr/lib
172     /lib64
173     /lib
174 )
175
176 # Fix a FTBFS on armel, mips, mipsel and friends (Debian's #872881)
177 if(CMAKE_COMPILER_IS_GNUCC AND GCCLIBATOMIC_LIBRARY)
178     set(SIMGRID_DEP   "${SIMGRID_DEP}   -Wl,--as-needed -latomic -Wl,--no-as-needed")
179 endif()
180 mark_as_advanced(GCCLIBATOMIC_LIBRARY)
181
182 if(enable_model-checking AND (NOT LINKER_VERSION VERSION_LESS "2.30"))
183     set(SIMGRID_DEP   "${SIMGRID_DEP}   -Wl,-znorelro -Wl,-znoseparate-code")
184 endif()
185
186 target_link_libraries(simgrid   ${SIMGRID_DEP})
187
188 # Dependencies from maintainer mode
189 ###################################
190 if(enable_maintainer_mode)
191   add_dependencies(simgrid smpi_generated_headers_call_location_tracing)
192 endif()
193 if(enable_maintainer_mode AND BISON_EXE AND LEX_EXE)
194   add_dependencies(simgrid automaton_generated_src)
195 endif()