1 # CMake find module to search for the SimGrid library.
3 # Copyright (c) 2016-2023. The SimGrid Team.
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the license (GNU LGPL) which comes with this package.
9 # USERS OF PROGRAMS USING SIMGRID
10 # -------------------------------
12 # If cmake does not find this file, add its path to CMAKE_PREFIX_PATH:
13 # CMAKE_PREFIX_PATH="/path/to/FindSimGrid.cmake:$CMAKE_PREFIX_PATH" cmake .
15 # If this file does not find SimGrid, define SimGrid_PATH:
16 # cmake -DSimGrid_PATH=/path/to/simgrid .
19 # DEVELOPERS OF PROGRAMS USING SIMGRID
20 # ------------------------------------
22 # 1. Include this file in your own CMakeLists.txt (before defining any target)
23 # Either by copying it in your tree, or (recommended) by using the
24 # version automatically installed by SimGrid.
26 # 2. This will define a target called 'SimGrid::Simgrid'. Use it as:
27 # target_link_libraries(your-simulator SimGrid::SimGrid)
29 # It also defines a SimGrid_VERSION macro, that you can use to deal with API
30 # evolutions as follows:
32 # #if SimGrid_VERSION < 31800
33 # (code to use if the installed version is lower than v3.18)
34 # #elif SimGrid_VERSION < 31900
35 # (code to use if we are using SimGrid v3.18.x)
37 # (code to use with SimGrid v3.19+)
40 # Since SimGrid header files require C++17, so we set CMAKE_CXX_STANDARD to 17.
41 # Change this variable in your own file if you need a later standard.
43 # DEVELOPPERS OF MPI PROGRAMS USING SIMGRID
44 # You should use smpi_c_target() on the targets that are intended to run in SMPI.
45 # ${SMPIRUN} is correctly set if it's installed.
48 # add_executable(roundtrip roundtrip.c)
49 # smpi_c_target(roundtrip)
52 # add_test(NAME Roundtrip
53 # COMMAND ${SMPIRUN} -platform ${CMAKE_SOURCE_DIR}/../cluster_backbone.xml -np 2 ./roundtrip)
58 # - Use automatic SimGridConfig.cmake creation via export/install(EXPORT in main CMakeLists.txt:
59 # https://cliutils.gitlab.io/modern-cmake/chapters/exporting.html
60 # https://cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file
61 # https://github.com/boostcon/cppnow_presentations_2017/blob/master/05-19-2017_friday/effective_cmake__daniel_pfeifer__cppnow_05-19-2017.pdf
63 cmake_minimum_required(VERSION 3.5)
65 set(CMAKE_CXX_STANDARD 17)
66 set(CMAKE_CXX_STANDARD_REQUIRED ON)
68 find_path(SimGrid_INCLUDE_DIR
69 NAMES simgrid/config.h
70 NAMES simgrid/version.h
71 PATHS ${SimGrid_PATH}/include /opt/simgrid/include
73 if (NOT SimGrid_INCLUDE_DIR)
74 # search under the old name
75 find_path(SimGrid_INCLUDE_DIR
76 NAMES simgrid_config.h
77 PATHS ${SimGrid_PATH}/include /opt/simgrid/include
80 find_library(SimGrid_LIBRARY
82 PATHS ${SimGrid_PATH}/lib /opt/simgrid/lib
84 mark_as_advanced(SimGrid_INCLUDE_DIR)
85 mark_as_advanced(SimGrid_LIBRARY)
87 if (SimGrid_INCLUDE_DIR)
88 set(SimGrid_VERSION_REGEX "^#define SIMGRID_VERSION_(MAJOR|MINOR|PATCH) ([0-9]+)$")
89 if (EXISTS "${SimGrid_INCLUDE_DIR}/simgrid/version.h")
90 file(STRINGS "${SimGrid_INCLUDE_DIR}/simgrid/version.h" SimGrid_VERSION_STRING REGEX ${SimGrid_VERSION_REGEX})
91 elseif (EXISTS "${SimGrid_INCLUDE_DIR}/simgrid/config.h")
92 file(STRINGS "${SimGrid_INCLUDE_DIR}/simgrid/config.h" SimGrid_VERSION_STRING REGEX ${SimGrid_VERSION_REGEX})
94 file(STRINGS "${SimGrid_INCLUDE_DIR}/simgrid_config.h" SimGrid_VERSION_STRING REGEX ${SimGrid_VERSION_REGEX})
96 set(SimGrid_VERSION "")
98 # Concat the matches to MAJOR.MINOR.PATCH assuming they appear in this order
99 foreach(match ${SimGrid_VERSION_STRING})
101 set(SimGrid_VERSION "${SimGrid_VERSION}.")
103 string(REGEX REPLACE ${SimGrid_VERSION_REGEX} "${SimGrid_VERSION}\\2" SimGrid_VERSION ${match})
104 set(SimGrid_VERSION_${CMAKE_MATCH_1} ${CMAKE_MATCH_2})
106 unset(SimGrid_VERSION_STRING)
107 unset(SimGrid_VERSION_REGEX)
110 include(FindPackageHandleStandardArgs)
111 find_package_handle_standard_args(SimGrid
112 FOUND_VAR SimGrid_FOUND
113 REQUIRED_VARS SimGrid_INCLUDE_DIR SimGrid_LIBRARY
114 VERSION_VAR SimGrid_VERSION
119 find_program(SMPIRUN smpirun
120 HINTS ${SimGrid_PATH}/bin /opt/simgrid/bin)
122 MACRO(smpi_c_target NAME)
123 target_compile_options(${NAME} PUBLIC "-include;smpi/smpi_helpers.h;-fPIC;-shared;-Wl,-z,defs")
124 target_link_options(${NAME} PUBLIC "-fPIC;-shared;-Wl,-z,defs;-lm")
125 target_link_libraries(${NAME} PUBLIC ${SimGrid_LIBRARY})
126 target_include_directories(${NAME} PUBLIC "${SimGrid_INCLUDE_DIR};${SimGrid_INCLUDE_DIR}/smpi")
129 add_library(SimGrid::SimGrid SHARED IMPORTED)
130 set_target_properties(SimGrid::SimGrid PROPERTIES
131 INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${SimGrid_INCLUDE_DIR}
132 INTERFACE_COMPILE_FEATURES cxx_alias_templates
133 IMPORTED_LOCATION ${SimGrid_LIBRARY}
135 # We need C++17, so check for it just in case the user removed it since compiling SimGrid
136 if (NOT CMAKE_VERSION VERSION_LESS 3.8)
137 # 3.8+ allows us to simply require C++17 (or higher)
138 set_property(TARGET SimGrid::SimGrid PROPERTY INTERFACE_COMPILE_FEATURES cxx_std_17)
140 # Old CMake can't do much. Just check the CXX_FLAGS and inform the user when a C++17 feature does not work
141 include(CheckCXXSourceCompiles)
142 set(CMAKE_REQUIRED_FLAGS "${CMAKE_CXX_FLAGS}")
143 check_cxx_source_compiles("
144 #if __cplusplus < 201703L
149 " _SIMGRID_CXX17_ENABLED)
150 if (NOT _SIMGRID_CXX17_ENABLED)
151 message(WARNING "C++17 is required to use SimGrid. Enable it with e.g. -std=c++17")
153 unset(_SIMGRID_CXX14_ENABLED CACHE)