File: FindNS3.cmake

package info (click to toggle)
simgrid 4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 38,980 kB
  • sloc: cpp: 123,583; ansic: 66,779; python: 8,358; java: 6,406; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,337; perl: 1,436; makefile: 105; lisp: 49; javascript: 7; sed: 6
file content (146 lines) | stat: -rw-r--r-- 5,750 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Try to find the ns-3 library.
#
# The user can hint a path using the NS3_HINT option.
#
# Once done, the following will be defined:
#
#  NS3_INCLUDE_DIRS - the ns-3 include directories
#  NS3_LIBRARY_PATH - path to the libs
#  NS3_LIBRARIES - link these to use ns-3 (full path to libs)
#
# This could be improved in many ways (patches welcome):
#  - No proper find_package() integration

set(SIMGRID_HAVE_NS3 0)
find_package(PkgConfig)

pkg_check_modules(NS3 ns3-core>=3.28 ns3-csma ns3-point-to-point ns3-internet ns3-network ns3-applications ns3-wifi)
if(NS3_FOUND) # Starting from 3.36, ns3 provides a working pkg-config file, making things much easier
  set(SIMGRID_HAVE_NS3 1)
  set(NS3_LIBRARY_PATH ${NS3_LIBRARY_DIRS})
  set(NS3_INCLUDE_DIR ${NS3_INCLUDE_DIRS})  
  set(NS3_LIBRARIES "")
  foreach(elm ${NS3_LDFLAGS})
    if (elm MATCHES "^-L" OR elm MATCHES "^-lns3")
      if ((NOT NS3_LIBRARIES MATCHES " ${elm} ") AND (NOT NS3_LIBRARIES MATCHES " ${elm}$")) 
        set(NS3_LIBRARIES "${NS3_LIBRARIES} ${elm}")
      endif()
    endif()
  endforeach()

  set(NS3_VERSION "${NS3_ns3-core_VERSION}")
  string(REGEX REPLACE "3\\.([-.0-9a-z]+)" "\\1" NS3_MINOR_VERSION "${NS3_VERSION}")
  if(NS3_MINOR_VERSION MATCHES "\\.")
    string(REGEX REPLACE "^[0-9]*\\.([0-9]+$)" "\\1" NS3_PATCH_VERSION "${NS3_MINOR_VERSION}")
    string(REGEX REPLACE "^([0-9]+)\\.[0-9]*$" "\\1" NS3_MINOR_VERSION "${NS3_MINOR_VERSION}")
  else()
    set(NS3_PATCH_VERSION "0")
  endif()

  
  # No pkg-config found. Try to go the old path
else()
  set(NS3_HINT ${ns3_path} CACHE PATH "Path to search for NS3 lib and include")

  set(NS3_KNOWN_VERSIONS "3-dev" "3.28" "3.29" "3.30" "3.31" "3.32" "3.33" "3.34" "3.35") # subsequent versions use pkg-config

  foreach (_ns3_ver ${NS3_KNOWN_VERSIONS})
    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")
    list(APPEND _ns3_INCLUDE_SEARCH_DIRS "include/ns${_ns3_ver}")
  endforeach()

  find_library(NS3_LIBRARIES
    NAME ns3-core
        ${_ns3_LIB_SEARCH_DIRS}
    PATH_SUFFIXES lib64 lib ns3/lib
    PATHS
    ${NS3_HINT}
    )

  find_path(NS3_INCLUDE_DIR
    NAME ns3/core-module.h
    PATH_SUFFIXES include ns3/include
                  ${_ns3_INCLUDE_SEARCH_DIRS}
    PATHS
    ${NS3_HINT}
    )

  if(NS3_INCLUDE_DIR)
    message(STATUS "Looking for ns3/core-module.h - found")
  else()
    message(STATUS "Looking for ns3/core-module.h - not found")
  endif()
  mark_as_advanced(NS3_INCLUDE_DIR)

  message(STATUS "Looking for lib ns3-core")
  if(NS3_LIBRARIES)
    message(STATUS "Looking for lib ns3-core - found")
  else()
    message(STATUS "Looking for lib ns3-core - not found")
  endif()
  mark_as_advanced(NS3_LIBRARIES)

  if(NS3_INCLUDE_DIR)
    if(NS3_LIBRARIES)
      set(SIMGRID_HAVE_NS3 1)
      if(NS3_LIBRARIES MATCHES "-optimized")
        set (NS3_SUFFIX "-optimized")
      elseif(NS3_LIBRARIES MATCHES "-debug")
        set (NS3_SUFFIX "-debug")
      elseif(NS3_LIBRARIES MATCHES "-default")
        set (NS3_SUFFIX "-default")
      else()
        set (NS3_SUFFIX "")
      endif()
      message(STATUS "ns-3 found ${NS3_LIBRARIES}")
      string(REGEX REPLACE ".*libns(.*)-core.*" "\\1" NS3_VERSION "${NS3_LIBRARIES}")
      string(REGEX REPLACE "3\\.([-.0-9a-z]+)" "\\1" NS3_MINOR_VERSION "${NS3_VERSION}")
      if(NS3_MINOR_VERSION MATCHES "dev")
        set(NS3_MINOR_VERSION "99")
      endif()
      if(NS3_MINOR_VERSION MATCHES "\\.")
        string(REGEX REPLACE "^[0-9]*\\.([0-9]+$)" "\\1" NS3_PATCH_VERSION "${NS3_MINOR_VERSION}")
        string(REGEX REPLACE "^([0-9]+)\\.[0-9]*$" "\\1" NS3_MINOR_VERSION "${NS3_MINOR_VERSION}")
      else()
        set(NS3_PATCH_VERSION "0")
      endif()
      get_filename_component(NS3_LIBRARY_PATH "${NS3_LIBRARIES}" PATH)

      # Compute NS3_PATH
      string(REGEX REPLACE "(.*)/lib" "\\1" NS3_PATH "${NS3_LIBRARY_PATH}")

      if (NOT NS3_LIBRARY_PATH STREQUAL "/usr/lib")
        string(REGEX MATCH "${NS3_LIBRARY_PATH}" MatchResult "$ENV{LD_LIBRARY_PATH}")
        if(NOT MatchResult)
          message(STATUS "Warning: NS3 not installed in system path, and not listed in LD_LIBRARY_PATH."
                        "         You want to: export LD_LIBRARY_PATH=${NS3_LIBRARY_PATH}\${LD_LIBRARY_PATH:+:\$LD_LIBRARY_PATH}")
        endif()
      endif()
    endif()
  endif()
  mark_as_advanced(NS3_LIBRARY_PATH)

  set(NS3_LIBRARIES "")
  foreach(lib core csma point-to-point internet network applications wifi)
    set(NS3_LIBRARIES "${NS3_LIBRARIES} -lns${NS3_VERSION}-${lib}${NS3_SUFFIX}")
  endforeach()
endif()

set(SIMGRID_HAVE_NS3_GetNextEventTime FALSE)
if(SIMGRID_HAVE_NS3)
  try_compile(compile_ns3 ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_ns3.cpp
              CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${NS3_INCLUDE_DIR}" "-DLINK_DIRECTORIES=${NS3_LIBRARY_PATH}"
              LINK_LIBRARIES "${NS3_LIBRARIES}"
              OUTPUT_VARIABLE compile_ns3_output)
  if(NOT compile_ns3)
    message(STATUS "ns-3 does not have the ns3::Simulator::GetNextEventTime patch. The ns-3 model will not be idempotent. Compilation output:\n ${compile_ns3_output}")
  else()
    set(SIMGRID_HAVE_NS3_GetNextEventTime TRUE)
  endif()
  file(REMOVE ${CMAKE_BINARY_DIR}/prog_ns3)
  message(STATUS "ns-3 found (v${NS3_VERSION}; minor ver:${NS3_MINOR_VERSION}; patch ver:${NS3_PATCH_VERSION}; GetNextEventTime patch: ${SIMGRID_HAVE_NS3_GetNextEventTime}; libpath: ${NS3_LIBRARY_PATH}).")
  link_directories(${NS3_LIBRARY_PATH})
  include_directories(${NS3_INCLUDE_DIR})
else()
  message(STATUS "Warning: Please install ns-3 (version 3.22 or higher -- http://www.nsnam.org/releases/) or disable this feature.")
endif()