Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'doc_link' into 'master'
[simgrid.git] / FindSimGrid.cmake
1 # CMake find module to search for the SimGrid library. 
2
3 # Copyright (c) 2016-2021. The SimGrid Team.
4 #
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.
7
8 #
9 # USERS OF PROGRAMS USING SIMGRID
10 # -------------------------------
11 #
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 .
14 #
15 # If this file does not find SimGrid, define SimGrid_PATH:
16 #    SimGrid_PATH=/path/to/simgrid  cmake .
17
18 #
19 # DEVELOPERS OF PROGRAMS USING SIMGRID
20 # ------------------------------------
21 #
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.
25 #
26 #  2. Afterward, if you have CMake >= 2.8.12, this will define a
27 #     target called 'SimGrid::Simgrid'. Use it as:
28 #       target_link_libraries(your-simulator SimGrid::SimGrid)
29 #
30 #    With older CMake (< 2.8.12), it simply defines several variables:
31 #       SimGrid_INCLUDE_DIR - the SimGrid include directories
32 #       SimGrid_LIBRARY - link your simulator against it to use SimGrid
33 #    Use as:
34 #      include_directories("${SimGrid_INCLUDE_DIR}" SYSTEM)
35 #      target_link_libraries(your-simulator ${SimGrid_LIBRARY})
36 #
37 #  In both cases, it also define a SimGrid_VERSION macro, that you
38 #    can use to deal with API evolutions as follows:
39 #
40 #    #if SimGrid_VERSION < 31800
41 #      (code to use if the installed version is lower than v3.18)
42 #    #elif SimGrid_VERSION < 31900
43 #      (code to use if we are using SimGrid v3.18.x)
44 #    #else
45 #      (code to use with SimGrid v3.19+)
46 #    #endif
47 #
48 #  Since SimGrid header files require C++14, so we set CMAKE_CXX_STANDARD to 14.
49 #    Change this variable in your own file if you need a later standard.
50
51
52 # IMPROVING THIS FILE
53 # -------------------
54 #  - Use automatic SimGridConfig.cmake creation via export/install(EXPORT in main CMakeLists.txt:
55 #    https://cliutils.gitlab.io/modern-cmake/chapters/exporting.html
56 #    https://cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file
57 #    https://github.com/boostcon/cppnow_presentations_2017/blob/master/05-19-2017_friday/effective_cmake__daniel_pfeifer__cppnow_05-19-2017.pdf
58
59 cmake_minimum_required(VERSION 2.8)
60
61 set(CMAKE_CXX_STANDARD 14)
62 set(CMAKE_CXX_STANDARD_REQUIRED ON)
63
64 find_path(SimGrid_INCLUDE_DIR
65   NAMES simgrid/config.h
66   NAMES simgrid/version.h
67   PATHS ${SimGrid_PATH}/include /opt/simgrid/include
68 )
69 if (NOT SimGrid_INCLUDE_DIR)
70   # search under the old name
71   find_path(SimGrid_INCLUDE_DIR
72     NAMES simgrid_config.h
73     PATHS ${SimGrid_PATH}/include /opt/simgrid/include
74   )
75 endif()    
76 find_library(SimGrid_LIBRARY
77   NAMES simgrid
78   PATHS ${SimGrid_PATH}/lib /opt/simgrid/lib
79 )
80 mark_as_advanced(SimGrid_INCLUDE_DIR)
81 mark_as_advanced(SimGrid_LIBRARY)
82
83 if (SimGrid_INCLUDE_DIR)
84   set(SimGrid_VERSION_REGEX "^#define SIMGRID_VERSION_(MAJOR|MINOR|PATCH) ([0-9]+)$")
85   if (EXISTS "${SimGrid_INCLUDE_DIR}/simgrid/version.h")
86     file(STRINGS "${SimGrid_INCLUDE_DIR}/simgrid/version.h" SimGrid_VERSION_STRING REGEX ${SimGrid_VERSION_REGEX})
87   elseif (EXISTS "${SimGrid_INCLUDE_DIR}/simgrid/config.h")
88     file(STRINGS "${SimGrid_INCLUDE_DIR}/simgrid/config.h" SimGrid_VERSION_STRING REGEX ${SimGrid_VERSION_REGEX})
89   else()
90     file(STRINGS "${SimGrid_INCLUDE_DIR}/simgrid_config.h" SimGrid_VERSION_STRING REGEX ${SimGrid_VERSION_REGEX})
91   endif()
92   set(SimGrid_VERSION "")
93
94   # Concat the matches to MAJOR.MINOR.PATCH assuming they appear in this order
95   foreach(match ${SimGrid_VERSION_STRING})
96     if(SimGrid_VERSION)
97       set(SimGrid_VERSION "${SimGrid_VERSION}.")
98     endif()
99     string(REGEX REPLACE ${SimGrid_VERSION_REGEX} "${SimGrid_VERSION}\\2" SimGrid_VERSION ${match})
100     set(SimGrid_VERSION_${CMAKE_MATCH_1} ${CMAKE_MATCH_2})
101   endforeach()
102   unset(SimGrid_VERSION_STRING)
103   unset(SimGrid_VERSION_REGEX)  
104 endif ()
105
106 include(FindPackageHandleStandardArgs)
107 find_package_handle_standard_args(SimGrid
108   FOUND_VAR SimGrid_FOUND
109   REQUIRED_VARS SimGrid_INCLUDE_DIR SimGrid_LIBRARY
110   VERSION_VAR SimGrid_VERSION
111 )
112
113 if (SimGrid_FOUND AND NOT CMAKE_VERSION VERSION_LESS 2.8.12)
114   add_library(SimGrid::SimGrid SHARED IMPORTED)
115   set_target_properties(SimGrid::SimGrid PROPERTIES
116     INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${SimGrid_INCLUDE_DIR}
117     INTERFACE_COMPILE_FEATURES cxx_alias_templates
118     IMPORTED_LOCATION ${SimGrid_LIBRARY}
119   )
120   # We need C++14, so check for it just in case the user removed it since compiling SimGrid
121   if (NOT CMAKE_VERSION VERSION_LESS 3.8)
122     # 3.8+ allows us to simply require C++14 (or higher)
123     set_property(TARGET SimGrid::SimGrid PROPERTY INTERFACE_COMPILE_FEATURES cxx_std_14)
124   elseif (NOT CMAKE_VERSION VERSION_LESS 3.1)
125     # 3.1+ is similar but for certain features. We pick just one
126     set_property(TARGET SimGrid::SimGrid PROPERTY INTERFACE_COMPILE_FEATURES cxx_attribute_deprecated)
127   else ()
128     # Old CMake can't do much. Just check the CXX_FLAGS and inform the user when a C++14 feature does not work
129     include(CheckCXXSourceCompiles)
130     set(CMAKE_REQUIRED_FLAGS "${CMAKE_CXX_FLAGS}")
131     check_cxx_source_compiles("
132 #if __cplusplus < 201402L
133 #error
134 #else
135 int main(){}
136 #endif
137 " _SIMGRID_CXX14_ENABLED)
138     if (NOT _SIMGRID_CXX14_ENABLED)
139         message(WARNING "C++14 is required to use SimGrid. Enable it with e.g. -std=c++14")
140     endif ()
141     unset(_SIMGRID_CXX14_ENABLED CACHE)
142   endif ()
143 endif ()
144