Skip to content

Commit

Permalink
Add libVersions in HPP and some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-bc committed Apr 23, 2024
1 parent bf49c69 commit 8578ae0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Autocoders/Python/src/fprime_ac/utils/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get_project_version(fallback=FALLBACK_VERSION):
return get_version_str(working_dir=fprime_directory, fallback=fallback)


def get_library_versions(fallback=FALLBACK_VERSION):
def get_library_versions(fallback=FALLBACK_VERSION) -> dict:
"""Calculate the versions of each libraries.
Return a dictionary with the library name as key and the version as value
"""
Expand Down
13 changes: 3 additions & 10 deletions cmake/autocoder/fpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,16 @@ function(fpp_setup_autocode AC_INPUT_FILES)
# Add in dictionary generation
if (GENERATED_DICT)
set(FPRIME_CURRENT_DICTIONARY_FILE_JSON "${GENERATED_DICT}" CACHE INTERNAL "" FORCE)
set(LIBRARY_FLAG)
set(FPRIME_PROJECT_VERSION_FILE "${CMAKE_BINARY_DIR}/versions/version.project")
set(FPRIME_FRAMEWORK_VERSION_FILE "${CMAKE_BINARY_DIR}/versions/version.framework")
set(FPRIME_LIBRARIES_VERSION_FILE "${CMAKE_BINARY_DIR}/versions/version.libraries")
set(FPRIME_JSON_VERSION_FILE "${CMAKE_BINARY_DIR}/versions/version.json")
add_custom_command(
OUTPUT ${GENERATED_DICT}
COMMAND ${FPRIME_FPP_TO_DICT_WRAPPER}
"--executable" "${FPP_TO_DICT}"
"--cmake-bin-dir" "${CMAKE_CURRENT_BINARY_DIR}"
"--projectVersionFile" "${FPRIME_PROJECT_VERSION_FILE}"
"--frameworkVersionFile" "${FPRIME_FRAMEWORK_VERSION_FILE}"
"--libraryVersionFile" "${FPRIME_LIBRARIES_VERSION_FILE}"
"--jsonVersionFile" "${FPRIME_JSON_VERSION_FILE}"
${IMPORTS} ${AC_INPUT_FILES}
DEPENDS ${FILE_DEPENDENCIES} ${MODULE_DEPENDENCIES}
${FPRIME_PROJECT_VERSION_FILE}
${FPRIME_FRAMEWORK_VERSION_FILE}
${FPRIME_LIBRARIES_VERSION_FILE}
${FPRIME_JSON_VERSION_FILE}
version
)
endif()
Expand Down
35 changes: 13 additions & 22 deletions cmake/autocoder/scripts/fpp_to_dict_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import subprocess
import argparse
import json


def main():
Expand All @@ -28,19 +29,9 @@ def main():
help="Source files",
)
parser.add_argument(
"--projectVersionFile",
"--jsonVersionFile",
default=False,
help="Path to file containing Project version number",
)
parser.add_argument(
"--frameworkVersionFile",
default=False,
help="Path to file containing Framework version number",
)
parser.add_argument(
"--libraryVersionFile",
default=False,
help="Library version string",
help="Path to file containing versions in JSON format",
)
parser.add_argument(
"--cmake-bin-dir",
Expand All @@ -54,23 +45,23 @@ def main():
)
args = parser.parse_args()

# Read versions from files
with open(args.projectVersionFile, "r") as file:
project_version = file.read().strip()
with open(args.frameworkVersionFile, "r") as file:
framework_version = file.read().strip()
with open(args.libraryVersionFile, "r") as file:
libraries_version = file.read().strip()
# Read versions from file
with open(args.jsonVersionFile, "r") as file:
json_version = json.load(file)
# Parses library versions into a string to input to fpp-to-dict
libs_str = ",".join(
f"{lib}@{version}" for lib, version in json_version.get("libraries", {}).items()
)

cmd_args = [
args.executable,
"--directory",
args.cmake_bin_dir,
"--projectVersion",
project_version,
json_version.get("project", "0.0.0"),
"--frameworkVersion",
framework_version,
*(["--libraryVersions", libraries_version] if libraries_version else []),
json_version.get("framework", "0.0.0"),
*(["--libraryVersions", libs_str] if libs_str else []), # "" unpacks nothing
"--imports",
args.i,
*args.sources,
Expand Down
7 changes: 4 additions & 3 deletions cmake/target/version.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
#
# A basic versioning target which will produce the version files.
####
set(FPRIME_VERSION_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/version/generate_version_header.py" CACHE PATH "Script used to generate version.hpp")
set(FPRIME_VERSION_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/version/generate_version_info.py" CACHE PATH "Script used to generate version files")

function(version_add_global_target TARGET)
set(OUTPUT_DIR "${CMAKE_BINARY_DIR}/versions")
set(OUTPUT_HPP "${OUTPUT_DIR}/version.hpp")
set(OUTPUT_FILES "${OUTPUT_HPP}" "${OUTPUT_DIR}/version.project" "${OUTPUT_DIR}/version.framework" "${OUTPUT_DIR}/version.libraries")
set(OUTPUT_JSON "${OUTPUT_DIR}/version.json")
file(MAKE_DIRECTORY ${OUTPUT_DIR})
# Add check argument when requested
set(OPTIONAL_CHECK_ARG)
string(REGEX REPLACE ";" ":" FPRIME_LIBRARY_LOCATIONS_CSV "${FPRIME_LIBRARY_LOCATIONS}")
if (FPRIME_CHECK_FRAMEWORK_VERSION)
set(OPTIONAL_CHECK_ARG "--check")
endif()
add_custom_target("${TARGET}" ALL BYPRODUCTS "${OUTPUT_FILES}"
add_custom_target("${TARGET}" ALL BYPRODUCTS "${OUTPUT_HPP}" "${OUTPUT_JSON}"
COMMAND "${CMAKE_COMMAND}"
-E env "PYTHONPATH=${PYTHONPATH}:${FPRIME_FRAMEWORK_PATH}/Autocoders/Python/src"
"FPRIME_PROJECT_ROOT=${FPRIME_PROJECT_ROOT}"
"FPRIME_FRAMEWORK_PATH=${FPRIME_FRAMEWORK_PATH}"
"FPRIME_LIBRARY_LOCATIONS=${FPRIME_LIBRARY_LOCATIONS_CSV}"
"${FPRIME_VERSION_SCRIPT}" "${OUTPUT_DIR}" "${OPTIONAL_CHECK_ARG}"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${OUTPUT_HPP}.tmp" "${OUTPUT_HPP}"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${OUTPUT_JSON}.tmp" "${OUTPUT_JSON}"
WORKING_DIRECTORY "${FPRIME_PROJECT_ROOT}"
)
endfunction()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env python3
"""
# ===============================================================================
# NAME: generate_version_header.py
# NAME: generate_version_info.py
#
# DESCRIPTION: Creates a version header file of specified name.
# It takes as input a filename and creates a header file
# with a constant string which is the git hash of the current version
# DESCRIPTION: Creates a version.hpp and version.json file with version informations
#
# USAGE: ./generate_version_header.py /path/tofile/version.hpp
# USAGE: ./generate_version_info.py /path/to/outdir/
#
# AUTHOR: sfregoso
# EMAIL: [email protected]
Expand All @@ -20,6 +18,7 @@
import argparse
import os
import sys
import json
from pathlib import Path

from fprime_ac.utils.version import (
Expand All @@ -30,7 +29,7 @@
)


def create_version_file(output_dir, framework_version, project_version):
def create_version_file_hpp(output_dir, framework_version, project_version):
"""
Create the version file using the provided name and path.
"""
Expand All @@ -54,29 +53,29 @@ def create_version_file(output_dir, framework_version, project_version):
fid.write(
'static const char* PROJECT_VERSION = "{}";\n'.format(project_version)
)
fid.write("static const char* LIBRARY_VERSIONS[] = {\n")
for lib, version in get_library_versions().items():
fid.write(f' "{lib}@{version}",\n')
fid.write("};\n")
fid.write("\n")
fid.write("#endif\n")
fid.write("\n")
# TODO: add library versions to this file


def create_version_files_raw_txt(
output_dir, framework_version, project_version, lib_versions
def create_version_file_json(
output_dir: str, framework_version: str, project_version: str, lib_versions: dict
):
"""
Create the version files using the provided name and path.
"""
framework_file = Path(output_dir) / "version.framework"
project_file = Path(output_dir) / "version.project"
lib_file = Path(output_dir) / "version.libraries"

with open(framework_file, "w") as file:
file.write(framework_version)
with open(project_file, "w") as file:
file.write(project_version)
with open(lib_file, "w") as file:
for lib, version in lib_versions.items():
file.write(f"{lib}@{version},")
json_file = Path(output_dir) / "version.json.tmp"
json_obj = {
"framework": framework_version,
"project": project_version,
"libraries": lib_versions,
}
with open(json_file, "w") as file:
json.dump(json_obj, file)


def main():
Expand All @@ -102,8 +101,8 @@ def main():
fprime_version = get_fprime_version()
project_version = get_project_version()
lib_versions = get_library_versions()
create_version_file(args.output_dir, fprime_version, project_version)
create_version_files_raw_txt(
create_version_file_hpp(args.output_dir, fprime_version, project_version)
create_version_file_json(
args.output_dir, fprime_version, project_version, lib_versions
)

Expand Down

0 comments on commit 8578ae0

Please sign in to comment.