Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4338dbdb5099ad99c6b4c1ca7134e9491f8ec0a4
[simgrid.git] / tools / cmake / Modules / FindNS3.cmake
1 # Try to find the ns-3 library.
2 #
3 # The user can hint a path using the NS3_HINT option.
4 #
5 # Once done, the following will be defined:
6 #
7 #  NS3_INCLUDE_DIRS - the ns-3 include directories
8 #  NS3_LIBRARY_PATH - path to the libs
9 #  NS3_LIBRARIES - link these to use ns-3 (full path to libs)
10 #
11 # This could be improved in many ways (patches welcome):
12 #  - No proper find_package() integration
13
14 set(SIMGRID_HAVE_NS3 0)
15 find_package(PkgConfig)
16
17 pkg_check_modules(NS3 ns3-core>=3.28 ns3-csma ns3-point-to-point ns3-internet ns3-network ns3-applications ns3-wifi)
18 if(NS3_FOUND) # Starting from 3.36, ns3 provides a working pkg-config file, making things much easier
19   set(SIMGRID_HAVE_NS3 1)
20   set(NS3_LIBRARY_PATH ${NS3_LIBRARY_DIRS})
21   set(NS3_INCLUDE_DIR ${NS3_INCLUDE_DIRS})  
22   set(NS3_LIBRARIES "")
23   foreach(elm ${NS3_LDFLAGS})
24     if (elm MATCHES "^-L" OR elm MATCHES "^-lns3")
25       if ((NOT NS3_LIBRARIES MATCHES " ${elm} ") AND (NOT NS3_LIBRARIES MATCHES " ${elm}$")) 
26         set(NS3_LIBRARIES "${NS3_LIBRARIES} ${elm}")
27       endif()
28     endif()
29   endforeach()
30
31   set(NS3_VERSION "${NS3_ns3-core_VERSION}")
32   string(REGEX REPLACE "3.([.0-9\-a-z]+)" "\\1" NS3_MINOR_VERSION "${NS3_VERSION}")
33   if(NS3_MINOR_VERSION MATCHES ".")
34     string(REGEX REPLACE "^[0-9]*\.([0-9]+$)" "\\1" NS3_PATCH_VERSION "${NS3_MINOR_VERSION}")
35     string(REGEX REPLACE "^([0-9]+)\.[0-9]*$" "\\1" NS3_MINOR_VERSION "${NS3_MINOR_VERSION}")
36   else()
37     set(NS3_PATCH_VERSION "0")
38   endif()
39
40   
41   # No pkg-config found. Try to go the old path
42 else()
43   set(NS3_HINT ${ns3_path} CACHE PATH "Path to search for NS3 lib and include")
44
45   set(NS3_KNOWN_VERSIONS "3.28" "3.29" "3.30" "3.31" "3.32" "3.33" "3.34" "3.35")
46
47   foreach (_ns3_ver ${NS3_KNOWN_VERSIONS})
48     list(APPEND _ns3_LIB_SEARCH_DIRS "ns${_ns3_ver}-core" "ns${_ns3_ver}-core-optimized" "ns${_ns3_ver}-core-debug" "ns${_ns3_ver}-core-default")
49     list(APPEND _ns3_INCLUDE_SEARCH_DIRS "include/ns${_ns3_ver}")
50   endforeach()
51
52   find_library(NS3_LIBRARIES
53     NAME ns3-core
54         ${_ns3_LIB_SEARCH_DIRS}
55     PATH_SUFFIXES lib64 lib ns3/lib
56     PATHS
57     ${NS3_HINT}
58     )
59
60   find_path(NS3_INCLUDE_DIR
61     NAME ns3/core-module.h
62     PATH_SUFFIXES include ns3/include
63                   ${_ns3_INCLUDE_SEARCH_DIRS}
64     PATHS
65     ${NS3_HINT}
66     )
67
68   if(NS3_INCLUDE_DIR)
69     message(STATUS "Looking for ns3/core-module.h - found")
70   else()
71     message(STATUS "Looking for ns3/core-module.h - not found")
72   endif()
73   mark_as_advanced(NS3_INCLUDE_DIR)
74
75   message(STATUS "Looking for lib ns3-core")
76   if(NS3_LIBRARIES)
77     message(STATUS "Looking for lib ns3-core - found")
78   else()
79     message(STATUS "Looking for lib ns3-core - not found")
80   endif()
81   mark_as_advanced(NS3_LIBRARIES)
82
83   if(NS3_INCLUDE_DIR)
84     if(NS3_LIBRARIES)
85       set(SIMGRID_HAVE_NS3 1)
86       if(NS3_LIBRARIES MATCHES "-optimized")
87         set (NS3_SUFFIX "-optimized")
88       elseif(NS3_LIBRARIES MATCHES "-debug")
89         set (NS3_SUFFIX "-debug")
90       elseif(NS3_LIBRARIES MATCHES "-default")
91         set (NS3_SUFFIX "-default")
92       else()
93         set (NS3_SUFFIX "")
94       endif()
95       message(STATUS "ns-3 found ${NS3_LIBRARIES}")
96       string(REGEX REPLACE ".*libns(.*)-core.*" "\\1" NS3_VERSION "${NS3_LIBRARIES}")
97       string(REGEX REPLACE "3.([.0-9\-a-z]+)" "\\1" NS3_MINOR_VERSION "${NS3_VERSION}")
98       if(NS3_MINOR_VERSION MATCHES "dev")
99         set(NS3_MINOR_VERSION "99")
100       endif()
101       if(NS3_MINOR_VERSION MATCHES ".")
102         string(REGEX REPLACE "^[0-9]*\.([0-9]+$)" "\\1" NS3_PATCH_VERSION "${NS3_MINOR_VERSION}")
103         string(REGEX REPLACE "^([0-9]+)\.[0-9]*$" "\\1" NS3_MINOR_VERSION "${NS3_MINOR_VERSION}")
104       else()
105         set(NS3_PATCH_VERSION "0")
106       endif()
107       get_filename_component(NS3_LIBRARY_PATH "${NS3_LIBRARIES}" PATH)
108
109       # Compute NS3_PATH
110       string(REGEX REPLACE "(.*)/lib" "\\1" NS3_PATH "${NS3_LIBRARY_PATH}")
111
112       if (NOT NS3_LIBRARY_PATH STREQUAL "/usr/lib")
113         string(REGEX MATCH "${NS3_LIBRARY_PATH}" MatchResult "$ENV{LD_LIBRARY_PATH}")
114         if(NOT MatchResult)
115           message(STATUS "Warning: NS3 not installed in system path, and not listed in LD_LIBRARY_PATH."
116                         "         You want to: export LD_LIBRARY_PATH=${NS3_LIBRARY_PATH}\${LD_LIBRARY_PATH:+:\$LD_LIBRARY_PATH}")
117         endif()
118       endif()
119     endif()
120   endif()
121   mark_as_advanced(NS3_LIBRARY_PATH)
122 endif()
123
124 if(SIMGRID_HAVE_NS3)
125   message(STATUS "ns-3 found (v${NS3_VERSION}; minor:${NS3_MINOR_VERSION}; patch:${NS3_PATCH_VERSION}; libpath: ${NS3_LIBRARY_PATH}).")
126   link_directories(${NS3_LIBRARY_PATH})
127   include_directories(${NS3_INCLUDE_DIR})
128 else()
129   message(STATUS "Warning: Please install ns-3 (version 3.22 or higher -- http://www.nsnam.org/releases/) or disable this feature.")
130 endif()