Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON dictionary generation to CMake system #2598

Merged
merged 27 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
27d605c
Adding dictionary generation
thomas-bc Mar 18, 2024
8cb6ad6
fix typo
thomas-bc Mar 18, 2024
d18d8da
add UT
thomas-bc Mar 18, 2024
104b97a
Refacto versions handling and add libraryVersion
thomas-bc Apr 17, 2024
8cc966e
Python format
thomas-bc Apr 17, 2024
21a117c
Pass in libraries only if not empty
thomas-bc Apr 17, 2024
01541fc
Fix logic
thomas-bc Apr 17, 2024
d40d410
debug stuff
thomas-bc Apr 17, 2024
8332abd
Add correct dependencies
thomas-bc Apr 18, 2024
a689516
More debugging
thomas-bc Apr 18, 2024
172d09f
Fix output files
thomas-bc Apr 18, 2024
bdccec2
oops
thomas-bc Apr 18, 2024
95e2ec9
Call synchronously
thomas-bc Apr 18, 2024
bf49c69
Explicitely add `version` target to dependency list
thomas-bc Apr 18, 2024
8578ae0
Add libVersions in HPP and some refactoring
thomas-bc Apr 23, 2024
9c38ec4
remove LIBRARY_VERSIONS if empty
thomas-bc Apr 23, 2024
d19355d
Add CMake test for version target
thomas-bc Apr 23, 2024
f51058a
formatting
thomas-bc Apr 23, 2024
772c727
Generate empty string when no libraries are present
thomas-bc Apr 24, 2024
59739e4
Fix lirary_versions key name mismatch
thomas-bc Apr 24, 2024
98016fc
Add more tests
thomas-bc Apr 24, 2024
936f97a
Add __attribute__((unused)) to appease CI
thomas-bc Apr 24, 2024
b45902e
Use LIBRARY_VERSIONS with TODO in SystemResources.cpp
thomas-bc Apr 24, 2024
24c2a4f
Use nullptr instead of "" when no library present
thomas-bc Apr 24, 2024
e67a59b
Use LIBRARY_VERSIONS in SystemResourcesTester
thomas-bc Apr 24, 2024
ed7ffe3
Make script variables local
thomas-bc Apr 25, 2024
5b15298
Switch to namespaced struct with constexpr
thomas-bc Apr 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Autocoders/Python/src/fprime_ac/utils/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" fprime version handling and reporting """
LeStarch marked this conversation as resolved.
Show resolved Hide resolved

import os
import subprocess

Expand Down Expand Up @@ -54,3 +55,20 @@ def get_project_version(fallback=FALLBACK_VERSION):
"""
fprime_directory = os.environ.get("FPRIME_PROJECT_ROOT", os.path.dirname(__file__))
return get_version_str(working_dir=fprime_directory, fallback=fallback)


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
"""
fprime_libraries = os.environ.get("FPRIME_LIBRARY_LOCATIONS", "")
if fprime_libraries == "":
return {}

lib_versions = {}
for library in fprime_libraries.split(":"):
library_name = os.path.basename(library)
lib_versions[library_name] = get_version_str(
working_dir=library, fallback=fallback
)
return lib_versions
10 changes: 5 additions & 5 deletions Svc/SystemResources/SystemResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include <cmath> //isnan()
#include <Svc/SystemResources/SystemResources.hpp>
#include <version.hpp>
#include <versions/version.hpp>
#include <FpConfig.hpp>

namespace Svc {
Expand Down Expand Up @@ -89,10 +89,10 @@
}

void SystemResources ::VERSION_cmdHandler(const FwOpcodeType opCode, const U32 cmdSeq) {
Fw::LogStringArg version_string(FRAMEWORK_VERSION);
Fw::LogStringArg version_string(Project::Version::FRAMEWORK_VERSION);
this->log_ACTIVITY_LO_FRAMEWORK_VERSION(version_string);

version_string = PROJECT_VERSION;
version_string = Project::Version::PROJECT_VERSION;
Dismissed Show dismissed Hide dismissed
this->log_ACTIVITY_LO_PROJECT_VERSION(version_string);
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
}
Expand Down Expand Up @@ -152,10 +152,10 @@
}

void SystemResources::Version() {
Fw::TlmString version_string(FRAMEWORK_VERSION);
Fw::TlmString version_string(Project::Version::FRAMEWORK_VERSION);
this->tlmWrite_FRAMEWORK_VERSION(version_string);

version_string= PROJECT_VERSION;
version_string= Project::Version::PROJECT_VERSION;
github-advanced-security[bot] marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
this->tlmWrite_PROJECT_VERSION(version_string);
}
} // end namespace Svc
10 changes: 5 additions & 5 deletions Svc/SystemResources/test/ut/SystemResourcesTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// ======================================================================

#include "SystemResourcesTester.hpp"
#include "version.hpp"
#include "versions/version.hpp"
#define INSTANCE 0
#define MAX_HISTORY_SIZE 100

Expand Down Expand Up @@ -114,8 +114,8 @@ void SystemResourcesTester ::test_tlm(bool enabled) {
ASSERT_TLM_FRAMEWORK_VERSION_SIZE((enabled) ? 1 : 0);
ASSERT_TLM_PROJECT_VERSION_SIZE((enabled) ? 1 : 0);
if (enabled) {
ASSERT_TLM_FRAMEWORK_VERSION(0, FRAMEWORK_VERSION);
ASSERT_TLM_PROJECT_VERSION(0, PROJECT_VERSION);
ASSERT_TLM_FRAMEWORK_VERSION(0, Project::Version::FRAMEWORK_VERSION);
ASSERT_TLM_PROJECT_VERSION(0, Project::Version::PROJECT_VERSION);
}
ASSERT_TLM_SIZE((enabled) ? (count + 3) : 0); // CPU count channels + avg + 2 ver
break;
Expand All @@ -133,9 +133,9 @@ void SystemResourcesTester ::test_disable_enable() {
void SystemResourcesTester ::test_version_evr() {
this->sendCmd_VERSION(0, 0);
ASSERT_EVENTS_FRAMEWORK_VERSION_SIZE(1);
ASSERT_EVENTS_FRAMEWORK_VERSION(0, FRAMEWORK_VERSION);
ASSERT_EVENTS_FRAMEWORK_VERSION(0, Project::Version::FRAMEWORK_VERSION);
ASSERT_EVENTS_PROJECT_VERSION_SIZE(1);
ASSERT_EVENTS_PROJECT_VERSION(0, FRAMEWORK_VERSION);
ASSERT_EVENTS_PROJECT_VERSION(0, Project::Version::FRAMEWORK_VERSION);
}

// ----------------------------------------------------------------------
Expand Down
26 changes: 23 additions & 3 deletions cmake/autocoder/fpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
include_guard()
include(utilities)
include(autocoder/helpers)
set(FPRIME_FPP_TO_DICT_WRAPPER "${CMAKE_CURRENT_LIST_DIR}/scripts/fpp_to_dict_wrapper.py")

autocoder_setup_for_multiple_sources()
####
Expand All @@ -17,7 +18,7 @@ autocoder_setup_for_multiple_sources()
####
function(locate_fpp_tools)
# Loop through each tool, looking if it was found and check the version
foreach(TOOL FPP_DEPEND FPP_TO_XML FPP_TO_CPP FPP_LOCATE_DEFS)
foreach(TOOL FPP_DEPEND FPP_TO_XML FPP_TO_CPP FPP_LOCATE_DEFS FPP_TO_DICT)
# Skipped already defined tools
if (${TOOL})
continue()
Expand Down Expand Up @@ -207,8 +208,11 @@ function(fpp_setup_autocode AC_INPUT_FILES)
# Separate the source files into the CPP and XML steps
set(GENERATED_AI)
set(GENERATED_CPP)
set(GENERATED_DICT)
foreach(GENERATED IN LISTS GENERATED_FILES)
if (GENERATED MATCHES ".*\\.xml")
if (GENERATED MATCHES ".*TopologyDictionary\.json")
list(APPEND GENERATED_DICT "${GENERATED}")
elseif (GENERATED MATCHES ".*\\.xml")
list(APPEND GENERATED_AI "${GENERATED}")
else()
list(APPEND GENERATED_CPP "${GENERATED}")
Expand All @@ -234,7 +238,23 @@ function(fpp_setup_autocode AC_INPUT_FILES)
DEPENDS ${FILE_DEPENDENCIES} ${MODULE_DEPENDENCIES}
)
endif()
set(AUTOCODER_GENERATED ${GENERATED_AI} ${GENERATED_CPP})
# Add in dictionary generation
if (GENERATED_DICT)
set(FPRIME_CURRENT_DICTIONARY_FILE_JSON "${GENERATED_DICT}" CACHE INTERNAL "" FORCE)
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}"
"--jsonVersionFile" "${FPRIME_JSON_VERSION_FILE}"
${IMPORTS} ${AC_INPUT_FILES}
DEPENDS ${FILE_DEPENDENCIES} ${MODULE_DEPENDENCIES}
${FPRIME_JSON_VERSION_FILE}
version
)
endif()
set(AUTOCODER_GENERATED ${GENERATED_AI} ${GENERATED_CPP} ${GENERATED_DICT})
set(AUTOCODER_GENERATED "${AUTOCODER_GENERATED}" PARENT_SCOPE)
set(AUTOCODER_DEPENDENCIES "${MODULE_DEPENDENCIES}" PARENT_SCOPE)
set(AUTOCODER_INCLUDES "${FILE_DEPENDENCIES}" PARENT_SCOPE)
Expand Down
81 changes: 81 additions & 0 deletions cmake/autocoder/scripts/fpp_to_dict_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python3
"""
fpp_to_dict_wrapper.py:

Wrapper script to run fpp-to-dict within the build process.
This is required to be able to read the version files and pass them to the fpp-to-dict executable.

@author thomas-bc
"""

import subprocess
import argparse
import json


def main():
"""
Main program entry point
"""
parser = argparse.ArgumentParser(description="Create version header")
parser.add_argument(
"--executable",
required=True,
help="Path to the fpp-to-dict executable",
)
parser.add_argument(
"sources",
nargs="+",
help="Source files",
)
parser.add_argument(
"--jsonVersionFile",
default=False,
help="Path to file containing versions in JSON format",
)
parser.add_argument(
"--cmake-bin-dir",
default=False,
help="Library version string",
)
parser.add_argument(
"-i",
default=False,
help="Imports",
)
args = parser.parse_args()

with open(args.jsonVersionFile, "r") as file:
versions = 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 versions.get("library_versions", {}).items()
)
framework_version = versions.get("framework_version")
project_version = versions.get("project_version")
if framework_version is None or project_version is None:
raise ValueError(
f"{args.jsonVersionFile} is missing 'framework_version' or 'project_version'"
)

cmd_args = [
args.executable,
"--directory",
args.cmake_bin_dir,
"--projectVersion",
project_version,
"--frameworkVersion",
framework_version,
*(["--libraryVersions", libs_str] if libs_str else []), # "" unpacks nothing
"--imports",
args.i,
*args.sources,
]

return subprocess.call(args=cmd_args)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions cmake/required.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ message(STATUS "[python3] python3 found at: ${PYTHON}")
message(STATUS "[fpp-tools] fpp-depend found at: ${FPP_DEPEND}")
LeStarch marked this conversation as resolved.
Show resolved Hide resolved
message(STATUS "[fpp-tools] fpp-to-xml found at: ${FPP_TO_XML}")
message(STATUS "[fpp-tools] fpp-to-cpp found at: ${FPP_TO_CPP}")
message(STATUS "[fpp-tools] fpp-to-dict found at: ${FPP_TO_DICT}")
message(STATUS "[fpp-tools] fpp-locate-defs found at: ${FPP_LOCATE_DEFS}")
2 changes: 1 addition & 1 deletion cmake/target/install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function(install_add_deployment_target MODULE TARGET SOURCES DEPENDENCIES FULL_D
ARCHIVE DESTINATION ${TOOLCHAIN_NAME}/${MODULE}/lib/static
COMPONENT ${MODULE}
)
install(FILES ${FPRIME_CURRENT_DICTIONARY_FILE} DESTINATION ${TOOLCHAIN_NAME}/${MODULE}/dict COMPONENT ${MODULE})
install(FILES ${FPRIME_CURRENT_DICTIONARY_FILE} ${FPRIME_CURRENT_DICTIONARY_FILE_JSON} DESTINATION ${TOOLCHAIN_NAME}/${MODULE}/dict COMPONENT ${MODULE})
add_custom_command(TARGET "${MODULE}" POST_BUILD COMMAND "${CMAKE_COMMAND}"
-DCMAKE_INSTALL_COMPONENT=${MODULE} -P ${CMAKE_BINARY_DIR}/cmake_install.cmake)
endfunction()
Expand Down
23 changes: 16 additions & 7 deletions cmake/target/version.cmake
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
####
# cmake/target/version.cmake:
#
# A basic versioning target which will produce the version.hpp file.
# 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_INFO_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/version/generate_version_info.py")

function(version_add_global_target TARGET)
set(OUTPUT_FILE "${CMAKE_BINARY_DIR}/version.hpp")
set(OUTPUT_DIR "${CMAKE_BINARY_DIR}/versions")
set(OUTPUT_HPP "${OUTPUT_DIR}/version.hpp")
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_FILE}"
COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=${PYTHONPATH}:${FPRIME_FRAMEWORK_PATH}/Autocoders/Python/src"
"${FPRIME_VERSION_SCRIPT}" "${OUTPUT_FILE}.tmp" "${OPTIONAL_CHECK_ARG}"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${OUTPUT_FILE}.tmp" "${OUTPUT_FILE}"
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_INFO_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
93 changes: 0 additions & 93 deletions cmake/target/version/generate_version_header.py

This file was deleted.

Loading
Loading