build: remove git dependency when using version.cache and add a test

This commit is contained in:
Gabriel Ferreira
2022-08-06 19:29:05 -03:00
parent 72ff5fc759
commit da221e41f6
3 changed files with 121 additions and 17 deletions

View File

@@ -118,6 +118,10 @@ macro(write_configtable)
string(APPEND out
"Build directory : ${CMAKE_OUTPUT_DIRECTORY}\n"
)
string(APPEND out "Build version embedding : ")
check_on_or_off("${NS3_ENABLE_BUILD_VERSION}" "${ENABLE_BUILD_VERSION}")
string(APPEND out "BRITE Integration : ")
check_on_or_off("ON" "${NS3_BRITE}")

View File

@@ -80,32 +80,23 @@ function(configure_embedded_version)
return()
endif()
set(HAS_NS3_TAGS False)
mark_as_advanced(GIT)
find_program(GIT git)
if("${GIT}" STREQUAL "GIT-NOTFOUND")
message(
STATUS "Git was not found. Version related targets won't be enabled"
STATUS
"Git was not found. Build version embedding won't be enabled if version.cache is not found."
)
return()
else()
if(EXISTS ${PROJECT_SOURCE_DIR}/.git)
# If the git history exists, check if ns-3 git tags were found
check_git_repo_has_ns3_tags(HAS_NS3_TAGS NS3_VERSION_TAG)
endif()
endif()
check_git_repo_has_ns3_tags(HAS_NS3_TAGS NS3_VERSION_TAG)
set(version_cache_file_template
${PROJECT_SOURCE_DIR}/build-support/version.cache.in
)
set(version_cache_file ${PROJECT_SOURCE_DIR}/src/core/model/version.cache)
# Check if ns-3 git tags were found or at least version.cache file exists in
# the src/core/model
if((NOT HAS_NS3_TAGS) AND (NOT (EXISTS ${version_cache_file})))
message(
FATAL_ERROR
"The ns-3 git commit history or the version.cache file are required to embed build version into libraries."
)
return()
endif()
# If git tags were found, extract the information
if(HAS_NS3_TAGS)
check_ns3_closest_tags(
@@ -150,9 +141,24 @@ function(configure_embedded_version)
endif()
set(NS3_VERSION_BUILD_PROFILE ${cmakeBuildType})
set(version_cache_file_template
${PROJECT_SOURCE_DIR}/build-support/version.cache.in
)
# Create version.cache file
configure_file(${version_cache_file_template} ${version_cache_file} @ONLY)
else()
# If we could not find the Git executable, or there were not ns-3 tags in
# the git history, we fallback to the version.cache file
if(EXISTS ${version_cache_file})
message(STATUS "The version.cache file was found.")
else()
message(
FATAL_ERROR
"The version.cache file was not found and is required to embed the build version."
)
endif()
# Consume version.cache created previously
file(STRINGS ${version_cache_file} version_cache_contents)
foreach(line ${version_cache_contents})

View File

@@ -1339,6 +1339,100 @@ class NS3ConfigureTestCase(NS3BaseTestCase):
self.assertIn("--profiling-format=google-trace --profiling-output=../cmake_performance_trace.log", stdout)
self.assertTrue(os.path.exists(os.path.join(ns3_path, "cmake_performance_trace.log")))
def test_18_CheckBuildVersionAndVersionCache(self):
"""!
Check if ENABLE_BUILD_VERSION and version.cache are working
as expected
@return None
"""
try:
from python_on_whales import docker
from python_on_whales.exceptions import DockerException
except ModuleNotFoundError:
self.skipTest("python-on-whales was not found")
# Import rootless docker settings from .bashrc
with open(os.path.expanduser("~/.bashrc"), "r") as f:
docker_settings = re.findall("(DOCKER_.*=.*)", f.read())
for setting in docker_settings:
key, value = setting.split("=")
os.environ[key] = value
del docker_settings, setting, key, value
# Create Docker client instance and start it
with docker.run("ubuntu:22.04", interactive=True, detach=True, tty=False, volumes=[(ns3_path, "/ns-3-dev")]) as container:
# Redefine the execute command of the container
def split_exec(self, cmd):
return self._execute(cmd.split(), workdir="/ns-3-dev")
container._execute = container.execute
container.execute = partial(split_exec, container)
# Install basic packages
container.execute("apt-get update")
container.execute("apt-get install -y python3 ninja-build cmake g++")
# Clean ns-3 artifacts
container.execute("./ns3 clean")
# Set path to version.cache file
version_cache_file = os.path.join(ns3_path, "src/core/model/version.cache")
# First case: try without a version cache or Git
if os.path.exists(version_cache_file):
os.remove(version_cache_file)
# We need to catch the exception since the command will fail
try:
container.execute("./ns3 configure -G Ninja --enable-build-version")
except DockerException:
pass
self.assertFalse(os.path.exists(os.path.join(ns3_path, "cmake-cache", "build.ninja")))
# Second case: try with a version cache file but without Git (it should succeed)
version_cache_contents = ("CLOSEST_TAG = '\"ns-3.0.0\"'\n"
"VERSION_COMMIT_HASH = '\"0000000000\"'\n"
"VERSION_DIRTY_FLAG = '0'\n"
"VERSION_MAJOR = '3'\n"
"VERSION_MINOR = '0'\n"
"VERSION_PATCH = '0'\n"
"VERSION_RELEASE_CANDIDATE = '\"\"'\n"
"VERSION_TAG = '\"ns-3.0.0\"'\n"
"VERSION_TAG_DISTANCE = '0'\n"
"VERSION_BUILD_PROFILE = 'debug'\n"
)
with open(version_cache_file, "w") as version:
version.write(version_cache_contents)
# Configuration should now succeed
container.execute("./ns3 clean")
container.execute("./ns3 configure -G Ninja --enable-build-version")
self.assertTrue(os.path.exists(os.path.join(ns3_path, "cmake-cache", "build.ninja")))
# And contents of version cache should be unchanged
with open(version_cache_file, "r") as version:
self.assertEqual(version.read(), version_cache_contents)
# Third case: we rename the .git directory temporarily and reconfigure
# to check if it gets configured successfully when Git is found but
# there is not .git history
os.rename(os.path.join(ns3_path, ".git"), os.path.join(ns3_path, "temp_git"))
try:
container.execute("apt-get install -y git")
container.execute("./ns3 clean")
container.execute("./ns3 configure -G Ninja --enable-build-version")
except Exception:
pass
os.rename(os.path.join(ns3_path, "temp_git"), os.path.join(ns3_path, ".git"))
self.assertTrue(os.path.exists(os.path.join(ns3_path, "cmake-cache", "build.ninja")))
# Fourth case: test with Git and git history. Now the version.cache should be replaced.
container.execute("./ns3 clean")
container.execute("./ns3 configure -G Ninja --enable-build-version")
self.assertTrue(os.path.exists(os.path.join(ns3_path, "cmake-cache", "build.ninja")))
with open(version_cache_file, "r") as version:
self.assertNotEqual(version.read(), version_cache_contents)
class NS3BuildBaseTestCase(NS3BaseTestCase):
"""!