Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9d0f30be82096df4a7eacb5ae638391ec1d49675
[simgrid.git] / FindSimGrid.cmake
1 # CMake find module to search for the SimGrid library. 
2
3 # Copyright (c) 2016-2018. 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 # Once done, the following will be defined:
9 #
10 #  CMake >= 2.8.12:
11 #    Target SimGrid::Simgrid
12 #
13 #    Use as:
14 #      target_link_libraries(your-simulator SimGrid::SimGrid)
15 #
16 #  Older CMake (< 2.8.12)
17 #    SimGrid_INCLUDE_DIR - the SimGrid include directories
18 #    SimGrid_LIBRARY - link your simulator against it to use SimGrid
19 #
20 #    Use as:
21 #      include_directories("${SimGrid_INCLUDE_DIR}" SYSTEM)
22 #      target_link_libraries(your-simulator ${SimGrid_LIBRARY})
23 #
24 # This could be improved:
25 #  - Use automatic SimGridConfig.cmake creation via export/install(EXPORT in main CMakeLists.txt:
26 #    https://cliutils.gitlab.io/modern-cmake/chapters/exporting.html
27 #    https://cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file
28 #    https://github.com/boostcon/cppnow_presentations_2017/blob/master/05-19-2017_friday/effective_cmake__daniel_pfeifer__cppnow_05-19-2017.pdf
29
30 cmake_minimum_required(VERSION 2.8)
31
32 find_path(SimGrid_INCLUDE_DIR
33   NAMES simgrid_config.h
34   PATHS /opt/simgrid/include
35 )
36 find_library(SimGrid_LIBRARY
37   NAMES simgrid
38   PATHS /opt/simgrid/lib
39 )
40 mark_as_advanced(SimGrid_INCLUDE_DIR)
41 mark_as_advanced(SimGrid_LIBRARY)
42
43 if (SimGrid_INCLUDE_DIR)
44   set(SimGrid_VERSION_REGEX "^#define SIMGRID_VERSION_(MAJOR|MINOR|PATCH) ([0-9]+)$")
45   file(STRINGS "${SimGrid_INCLUDE_DIR}/simgrid_config.h" SimGrid_VERSION_STRING REGEX ${SimGrid_VERSION_REGEX})
46   set(SimGrid_VERSION "")
47
48   # Concat the matches to MAJOR.MINOR.PATCH assuming they appear in this order
49   foreach(match ${SimGrid_VERSION_STRING})
50     if(SimGrid_VERSION)
51       set(SimGrid_VERSION "${SimGrid_VERSION}.")
52     endif()
53     string(REGEX REPLACE ${SimGrid_VERSION_REGEX} "${SimGrid_VERSION}\\2" SimGrid_VERSION ${match})
54     set(SimGrid_VERSION_${CMAKE_MATCH_1} ${CMAKE_MATCH_2})
55   endforeach()
56   unset(SimGrid_VERSION_STRING)
57   unset(SimGrid_VERSION_REGEX)  
58 endif ()
59
60 include(FindPackageHandleStandardArgs)
61 find_package_handle_standard_args(SimGrid
62   FOUND_VAR SimGrid_FOUND
63   REQUIRED_VARS SimGrid_INCLUDE_DIR SimGrid_LIBRARY
64   VERSION_VAR SimGrid_VERSION
65 )
66
67 if (SimGrid_FOUND AND NOT CMAKE_VERSION VERSION_LESS 2.8.12)
68   add_library(SimGrid::SimGrid SHARED IMPORTED)
69   set_target_properties(SimGrid::SimGrid PROPERTIES
70     INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${SimGrid_INCLUDE_DIR}
71     INTERFACE_COMPILE_FEATURES cxx_alias_templates
72     IMPORTED_LOCATION ${SimGrid_LIBRARY}
73   )
74   # We need C++11, so check for it
75   if (NOT CMAKE_VERSION VERSION_LESS 3.8)
76     # 3.8+ allows us to simply require C++11 (or higher)
77     set_property(TARGET SimGrid::SimGrid PROPERTY INTERFACE_COMPILE_FEATURES cxx_std_11)
78   elseif (NOT CMAKE_VERSION VERSION_LESS 3.1)
79     # 3.1+ is similar but for certain features. We pick just one
80     set_property(TARGET SimGrid::SimGrid PROPERTY INTERFACE_COMPILE_FEATURES cxx_alias_templates)
81   else ()
82     # Old CMake can't do much. Just check the CXX_FLAGS and inform the user when a C++11 feature does not work
83     include(CheckCXXSourceCompiles)
84     set(CMAKE_REQUIRED_FLAGS "${CMAKE_CXX_FLAGS}")
85     check_cxx_source_compiles("using Foo = int; int main(){}" _SIMGRID_CXX11_ENABLED)
86     if (NOT _SIMGRID_CXX11_ENABLED)
87         message(WARNING "C++11 is required to use this library. Enable it with e.g. -std=c++11")
88     endif ()
89     unset(_SIMGRID_CXX11_ENABLED CACHE)
90   endif ()
91 endif ()
92