Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adapt to the upcoming ns-3 v3.36.1
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 2 Jul 2022 11:39:11 +0000 (13:39 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 2 Jul 2022 20:41:59 +0000 (22:41 +0200)
CMakeLists.txt
tools/cmake/Modules/FindNS3.cmake

index ca5ab2d..b03c595 100644 (file)
@@ -228,12 +228,10 @@ if(enable_ns3)
   include(FindNS3)
   if (SIMGRID_HAVE_NS3)
     if (NOT NS3_VERSION EQUAL "3-dev" AND NS3_VERSION VERSION_LESS "3.28")
-      message(FATAL_ERROR "SimGrid needs at least ns-3.28. Please upgrade or disable that cmake option.")
+      message(FATAL_ERROR "SimGrid needs ns-3 in version 3.28 or higher. Please upgrade or disable that cmake option.")
     endif()
     set(SIMGRID_HAVE_NS3 1)
-    foreach(lib core csma point-to-point internet network applications wifi)
-      set(SIMGRID_DEP "${SIMGRID_DEP} -lns${NS3_VERSION}-${lib}${NS3_SUFFIX}")
-    endforeach()
+    set(SIMGRID_DEP "${SIMGRID_DEP} ${NS3_LIBRARIES}")
   else()
     message(FATAL_ERROR "Cannot find ns-3. Please install it (apt-get install ns3 libns3-dev) or disable that cmake option")
   endif()
index 89b7798..4338dbd 100644 (file)
 #  NS3_LIBRARIES - link these to use ns-3 (full path to libs)
 #
 # This could be improved in many ways (patches welcome):
-#  - No way to specify a minimal version (v3.26 is hardcoded).
 #  - No proper find_package() integration
 
-## ADDING A ns-3 VERSION.
-#   - Add "ns3.${version}-core ns3.${version}-core-debug ns3.${version}-core-optimized" to the NAME line of the find_library below
-#   - Add "include/ns3{version}" to the PATH_SUFFIXES line of the find_path below
-
 set(SIMGRID_HAVE_NS3 0)
-set(NS3_HINT ${ns3_path} CACHE PATH "Path to search for NS3 lib and include")
+find_package(PkgConfig)
 
-set(NS3_KNOWN_VERSIONS "3-dev" "3.22" "3.23" "3.24" "3.25" "3.26" "3.27" "3.28" "3.29" "3.30" "3.31" "3.32" "3.33" "3.34" "3.35" "3.36" "3.37" "3.38" "3.39" "3.40")
+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()
 
-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()
+  set(NS3_VERSION "${NS3_ns3-core_VERSION}")
+  string(REGEX REPLACE "3.([.0-9\-a-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()
 
-find_library(NS3_LIBRARIES
-  NAME ns3-core
-      ${_ns3_LIB_SEARCH_DIRS}
-  PATH_SUFFIXES lib64 lib ns3/lib
-  PATHS
-  ${NS3_HINT}
-  )
+  
+  # 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")
 
-find_path(NS3_INCLUDE_DIR
-  NAME ns3/core-module.h
-  PATH_SUFFIXES include ns3/include
-                ${_ns3_INCLUDE_SEARCH_DIRS}
-  PATHS
-  ${NS3_HINT}
-  )
+  set(NS3_KNOWN_VERSIONS "3.28" "3.29" "3.30" "3.31" "3.32" "3.33" "3.34" "3.35")
 
-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)
+  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()
 
-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)
+  find_library(NS3_LIBRARIES
+    NAME ns3-core
+        ${_ns3_LIB_SEARCH_DIRS}
+    PATH_SUFFIXES lib64 lib ns3/lib
+    PATHS
+    ${NS3_HINT}
+    )
 
-if(NS3_INCLUDE_DIR)
+  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)
-    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-9\-a-z]+)" "\\1" NS3_MINOR_VERSION "${NS3_VERSION}")
-    if(NS3_MINOR_VERSION MATCHES "dev")
-      set(NS3_MINOR_VERSION "99")
-    endif()
-    get_filename_component(NS3_LIBRARY_PATH "${NS3_LIBRARIES}" PATH)
+    message(STATUS "Looking for lib ns3-core - found")
+  else()
+    message(STATUS "Looking for lib ns3-core - not found")
+  endif()
+  mark_as_advanced(NS3_LIBRARIES)
 
-    # Compute NS3_PATH
-    string(REGEX REPLACE "(.*)/lib" "\\1" NS3_PATH "${NS3_LIBRARY_PATH}")
+  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-9\-a-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)
 
-    message(STATUS "ns-3 found (v${NS3_VERSION}; incl:${NS3_INCLUDE_DIR}; lib: ${NS3_LIBRARY_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}")
+      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)
 endif()
-mark_as_advanced(NS3_LIBRARY_PATH)
 
 if(SIMGRID_HAVE_NS3)
+  message(STATUS "ns-3 found (v${NS3_VERSION}; minor:${NS3_MINOR_VERSION}; patch:${NS3_PATCH_VERSION}; libpath: ${NS3_LIBRARY_PATH}).")
   link_directories(${NS3_LIBRARY_PATH})
   include_directories(${NS3_INCLUDE_DIR})
 else()